From 672b32e566c10aec55f6f01f0a5dcc0dd32c67ed Mon Sep 17 00:00:00 2001 From: bridge Date: Wed, 24 Sep 2025 22:47:49 +0800 Subject: [PATCH] root to csv --- src/classes/root.py | 99 +++++++++++++++++++++++++----------- static/game_configs/root.csv | 13 +++++ 2 files changed, 81 insertions(+), 31 deletions(-) create mode 100644 static/game_configs/root.csv diff --git a/src/classes/root.py b/src/classes/root.py index af282c4..46780d3 100644 --- a/src/classes/root.py +++ b/src/classes/root.py @@ -1,14 +1,16 @@ """ 灵根 五行元素与灵根组合: -- RootElement:金、木、水、火、土 -- Root:由一个或多个 RootElement 组成(单灵根、双灵根、天灵根) +- RootElement:金、木、水、火、土(恒定不变) +- Root:从 CSV 配置加载(单/双/天灵根等),每个成员包含(中文名, 元素列表) """ from enum import Enum -from typing import List, Tuple +from typing import List, Tuple, Dict from collections import defaultdict from src.classes.essence import EssenceType +from src.utils.df import game_configs +from src.utils.config import CONFIG class RootElement(Enum): @@ -22,31 +24,11 @@ class RootElement(Enum): return self.value -class Root(Enum): +class _RootMixin: """ - 复合灵根定义:每个成员包含(中文名, 元素列表) + Root 的基础实现:成员值为 (中文名, 元素元组)。 + 通过功能式 API 动态创建真正的 Root 枚举。 """ - GOLD = ("金灵根", (RootElement.GOLD,)) - WOOD = ("木灵根", (RootElement.WOOD,)) - WATER = ("水灵根", (RootElement.WATER,)) - FIRE = ("火灵根", (RootElement.FIRE,)) - EARTH = ("土灵根", (RootElement.EARTH,)) - - THUNDER = ("雷灵根", (RootElement.WATER, RootElement.EARTH)) # 水+土 - ICE = ("冰灵根", (RootElement.GOLD, RootElement.WATER)) # 金+水 - WIND = ("风灵根", (RootElement.WOOD, RootElement.WATER)) # 木+水 - DARK = ("暗灵根", (RootElement.FIRE, RootElement.EARTH)) # 火+土 - - HEAVEN = ( - "天灵根", - ( - RootElement.GOLD, - RootElement.WOOD, - RootElement.WATER, - RootElement.FIRE, - RootElement.EARTH, - ), - ) def __new__(cls, cn_name: str, elements: Tuple[RootElement, ...]): obj = object.__new__(cls) @@ -61,6 +43,35 @@ class Root(Enum): def __str__(self) -> str: return f"{self.value}({', '.join(str(e) for e in self.elements)})" + +def _build_root_members_from_csv() -> Dict[str, tuple]: + """ + 从 CSV 读取 Root 定义,返回用于创建枚举的 members 映射。 + CSV 列:id,key,name,element_list,其中 element_list 用分号分隔中文名(如:金;木)。 + """ + df = game_configs.get("root") + sep = CONFIG.df.ids_separator + members: Dict[str, tuple] = {} + for _, row in df.iterrows(): + key = str(row["key"]).strip() + cn_name = str(row["name"]).strip() + elements_field = str(row["element_list"]).strip() + element_names = [s.strip() for s in elements_field.split(sep) if str(s).strip()] + element_members: List[RootElement] = [] + for en in element_names: + try: + # RootElement 的值即为中文名,因此可直接用中文构造 + element_members.append(RootElement(en)) + except Exception as e: + raise ValueError(f"root.csv 中未知的元素名称: {en}") from e + members[key] = (cn_name, tuple(element_members)) + return members + + +# 动态创建 Root 枚举(使用 mixin 作为 type,使 __new__ 生效) +Root = Enum("Root", _build_root_members_from_csv(), type=_RootMixin) + + # 元素到灵气类型的一一对应 _essence_by_element = { RootElement.GOLD: EssenceType.GOLD, @@ -71,13 +82,39 @@ _essence_by_element = { } -def get_essence_types_for_root(root: Root) -> List[EssenceType]: +def get_essence_types_for_root(root: "Root") -> List[EssenceType]: """ 获取与该灵根匹配的灵气类型列表(任一元素匹配即视为可用)。 """ return [_essence_by_element[e] for e in root.elements] -extra_breakthrough_success_rate = { - Root.HEAVEN: 0.1, -} -extra_breakthrough_success_rate = defaultdict(lambda: 0, extra_breakthrough_success_rate) \ No newline at end of file + +def _load_extra_breakthrough_success_rate_from_csv() -> Dict["Root", float]: + """ + 从 root.csv 载入各灵根的额外突破成功率,默认0。 + 列名:extra_breakthrough_success_rate + """ + df = game_configs.get("root") + if df is None: + return {} + bonuses: Dict["Root", float] = {} + for _, row in df.iterrows(): + key = str(row["key"]).strip() + try: + root_member = getattr(Root, key) + except AttributeError: + # CSV 中的 key 未在 Root 中注册(理论不可能),跳过 + continue + try: + bonus = float(row.get("extra_breakthrough_success_rate", 0) or 0) + except Exception: + bonus = 0.0 + bonuses[root_member] = bonus + return bonuses + + +# 从配置构造带默认值的加成表 +extra_breakthrough_success_rate = defaultdict( + lambda: 0.0, + _load_extra_breakthrough_success_rate_from_csv(), +) \ No newline at end of file diff --git a/static/game_configs/root.csv b/static/game_configs/root.csv new file mode 100644 index 0000000..41eea85 --- /dev/null +++ b/static/game_configs/root.csv @@ -0,0 +1,13 @@ +id,key,name,element_list,desc,extra_breakthrough_success_rate +,,元素列表以分号分隔(示例:金;木),描述不会在代码中使用,额外突破成功率 +1,GOLD,金灵根,金,刚强果断;攻击力强,适合剑修,0 +2,WOOD,木灵根,木,仁厚坚韧;擅恢复、毒术与木属性法术,0 +3,WATER,水灵根,水,智慧圆融;身法轻盈,善辅助与隐匿,0 +4,FIRE,火灵根,火,激情暴烈;高爆发攻术,火系功法,0 +5,EARTH,土灵根,土,稳重踏实;防御高,体修或傀儡术,0 +6,THUNDER,雷灵根,水;土,爆发性极强;战斗系顶尖,0 +7,ICE,冰灵根,金;水,控制力强;持久性好,0 +8,WIND,风灵根,木;水,身法卓绝;难以捉摸,0 +9,DARK,暗灵根,火;土,隐匿系;擅幻术与诡道,0 +10,HEAVEN,天灵根,金;木;水;火;土,资质通天;五行皆通,修行全面,0.1 +