From c41285603b2d3c03e1006bcb8355dd35f260bbc1 Mon Sep 17 00:00:00 2001 From: bridge Date: Sat, 22 Nov 2025 17:50:12 +0800 Subject: [PATCH] add celestrial phenon to front --- src/server/main.py | 32 +++++- static/config.yml | 4 +- web/src/components/layout/StatusBar.vue | 124 +++++++++++++++++++++++- web/src/stores/world.ts | 13 ++- web/src/types/api.ts | 5 +- web/src/types/core.ts | 12 ++- 6 files changed, 181 insertions(+), 9 deletions(-) diff --git a/src/server/main.py b/src/server/main.py index 9e3e7f1..96f99bd 100644 --- a/src/server/main.py +++ b/src/server/main.py @@ -118,6 +118,34 @@ def serialize_events_for_client(events: List[Event]) -> List[dict]: }) return serialized +def serialize_phenomenon(phenomenon) -> Optional[dict]: + """序列化天地灵机对象""" + if not phenomenon: + return None + + # 安全地获取 rarity.name + rarity_str = "N" + if hasattr(phenomenon, "rarity") and phenomenon.rarity: + # 检查 rarity 是否是 Enum (RarityLevel) + if hasattr(phenomenon.rarity, "name"): + rarity_str = phenomenon.rarity.name + # 检查 rarity 是否是 Rarity dataclass (包含 level 字段) + elif hasattr(phenomenon.rarity, "level") and hasattr(phenomenon.rarity.level, "name"): + rarity_str = phenomenon.rarity.level.name + + # 生成效果描述 + from src.utils.effect_desc import format_effects_to_text + effect_desc = format_effects_to_text(phenomenon.effects) if hasattr(phenomenon, "effects") else "" + + return { + "id": phenomenon.id, + "name": phenomenon.name, + "desc": phenomenon.desc, + "rarity": rarity_str, + "duration_years": phenomenon.duration_years, + "effect_desc": effect_desc + } + def init_game(): """初始化游戏世界,逻辑复用自 src/run/run.py""" print("正在初始化游戏世界...") @@ -215,7 +243,8 @@ async def game_loop(): "year": int(world.month_stamp.get_year()), "month": world.month_stamp.get_month().value, "events": serialize_events_for_client(events), - "avatars": avatar_updates + "avatars": avatar_updates, + "phenomenon": serialize_phenomenon(world.current_phenomenon) } await manager.broadcast(state) except Exception as e: @@ -394,6 +423,7 @@ def get_state(): "avatar_count": len(world.avatar_manager.avatars), "avatars": av_list, "events": recent_events, + "phenomenon": serialize_phenomenon(world.current_phenomenon), "is_paused": game_instance.get("is_paused", False) } diff --git a/static/config.yml b/static/config.yml index a03698f..2a814d3 100644 --- a/static/config.yml +++ b/static/config.yml @@ -18,8 +18,8 @@ ai: max_parse_retries: 3 game: - init_npc_num: 12 - sect_num: 3 # init_npc_num大于sect_num时,会随机选择sect_num个宗门 + init_npc_num: 6 + sect_num: 2 # init_npc_num大于sect_num时,会随机选择sect_num个宗门 npc_birth_rate_per_month: 0.01 fortune_probability: 0.005 diff --git a/web/src/components/layout/StatusBar.vue b/web/src/components/layout/StatusBar.vue index 2ee0c7c..d57442f 100644 --- a/web/src/components/layout/StatusBar.vue +++ b/web/src/components/layout/StatusBar.vue @@ -1,7 +1,8 @@