finish animal and plants
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Optional
|
||||
|
||||
from src.utils.df import game_configs
|
||||
from src.utils.config import CONFIG
|
||||
from src.classes.item import Item, items_by_id
|
||||
from src.classes.cultivation import Realm
|
||||
|
||||
@dataclass
|
||||
class Animal:
|
||||
@@ -10,13 +14,35 @@ class Animal:
|
||||
id: int
|
||||
name: str
|
||||
desc: str
|
||||
grade: int
|
||||
realm: Realm
|
||||
item_ids: list[int] = field(default_factory=list) # 该动物对应的物品IDs
|
||||
|
||||
# 这些字段将在__post_init__中设置
|
||||
items: list[Item] = field(init=False, default_factory=list) # 该动物对应的物品实例
|
||||
|
||||
def __post_init__(self):
|
||||
"""初始化物品实例"""
|
||||
for item_id in self.item_ids:
|
||||
if item_id in items_by_id:
|
||||
self.items.append(items_by_id[item_id])
|
||||
|
||||
def __hash__(self) -> int:
|
||||
return hash(self.id)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
def get_info(self) -> str:
|
||||
"""
|
||||
获取动物的详细信息,包括名字、描述、境界和材料
|
||||
"""
|
||||
info_parts = [f"【{self.name}】({self.realm.value})", self.desc]
|
||||
|
||||
if self.items:
|
||||
item_names = [item.name for item in self.items]
|
||||
info_parts.append(f"可获得材料:{', '.join(item_names)}")
|
||||
|
||||
return " - ".join(info_parts)
|
||||
|
||||
def _load_animals() -> tuple[dict[int, Animal], dict[str, Animal]]:
|
||||
"""从配表加载animal数据"""
|
||||
@@ -25,11 +51,19 @@ def _load_animals() -> tuple[dict[int, Animal], dict[str, Animal]]:
|
||||
|
||||
animal_df = game_configs["animal"]
|
||||
for _, row in animal_df.iterrows():
|
||||
# 处理item_ids
|
||||
item_ids_list = []
|
||||
item_ids = row.get("item_ids")
|
||||
if item_ids is not None and str(item_ids).strip() and str(item_ids) != 'nan':
|
||||
for item_id_str in str(item_ids).split(CONFIG.df.ids_separator):
|
||||
item_ids_list.append(int(float(item_id_str.strip())))
|
||||
|
||||
animal = Animal(
|
||||
id=int(row["id"]),
|
||||
name=str(row["name"]),
|
||||
desc=str(row["desc"]),
|
||||
grade=int(row["grade"])
|
||||
realm=Realm.from_id(int(row["stage_id"])),
|
||||
item_ids=item_ids_list
|
||||
)
|
||||
animals_by_id[animal.id] = animal
|
||||
animals_by_name[animal.name] = animal
|
||||
|
||||
Reference in New Issue
Block a user