66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
import os
|
|||
|
|
import random
|
||
|
|
from nonebot import logger
|
||
|
|
from .equipment_renderer import render_equipment_card
|
||
|
|
from .equipment_store import list_stratagems
|
||
|
|
|
||
|
|
|
||
|
|
basic_path = os.path.dirname(__file__)
|
||
|
|
save_path = os.path.join(basic_path, "temp")
|
||
|
|
img_path = os.path.join(basic_path, "img")
|
||
|
|
data_path = os.path.join(basic_path, "data")
|
||
|
|
|
||
|
|
|
||
|
|
async def get_random_equipment(count):
|
||
|
|
data = list_stratagems()
|
||
|
|
indices = select_random_equipment(len(data), count)
|
||
|
|
selected_equipment = [data[i] for i in indices]
|
||
|
|
|
||
|
|
return await render_equipment_card(selected_equipment)
|
||
|
|
|
||
|
|
|
||
|
|
async def get_equipment_by_combination(type_combination):
|
||
|
|
data = list_stratagems()
|
||
|
|
|
||
|
|
equipment_by_type = categorize_equipment_by_type(data)
|
||
|
|
selected_equipment = select_equipment_by_type(equipment_by_type, type_combination)
|
||
|
|
|
||
|
|
logger.debug(f"根据组合选择的装备: {selected_equipment}")
|
||
|
|
return await render_equipment_card(selected_equipment)
|
||
|
|
|
||
|
|
|
||
|
|
def categorize_equipment_by_type(data):
|
||
|
|
equipment_by_type = {}
|
||
|
|
for item in data:
|
||
|
|
equip_type = item['type']
|
||
|
|
if equip_type not in equipment_by_type:
|
||
|
|
equipment_by_type[equip_type] = []
|
||
|
|
equipment_by_type[equip_type].append(item)
|
||
|
|
return equipment_by_type
|
||
|
|
|
||
|
|
|
||
|
|
def select_equipment_by_type(equipment_by_type, type_combination):
|
||
|
|
selected_equipment = []
|
||
|
|
backpack_count = 0
|
||
|
|
|
||
|
|
for equip_type, count in type_combination.items():
|
||
|
|
if equip_type in equipment_by_type:
|
||
|
|
available_items = equipment_by_type[equip_type]
|
||
|
|
random.shuffle(available_items)
|
||
|
|
selected = 0
|
||
|
|
for item in available_items:
|
||
|
|
if selected < count:
|
||
|
|
if item['backpack'] == 1 and backpack_count >= 1:
|
||
|
|
continue
|
||
|
|
selected_equipment.append(item)
|
||
|
|
selected += 1
|
||
|
|
if item['backpack'] == 1:
|
||
|
|
backpack_count += 1
|
||
|
|
else:
|
||
|
|
break
|
||
|
|
|
||
|
|
return selected_equipment
|
||
|
|
|
||
|
|
|
||
|
|
def select_random_equipment(max_equipment, count):
|
||
|
|
return random.sample(range(max_equipment), count)
|