This commit is contained in:
bridge
2025-11-08 02:59:54 +08:00
parent fbd9a60415
commit 76e86eebb5
3 changed files with 15 additions and 45 deletions

View File

@@ -4,8 +4,6 @@ from src.classes.action import InstantAction
from src.classes.event import Event
from src.classes.battle import decide_battle, get_effective_strength_pair
from src.classes.story_teller import StoryTeller
from src.classes.action.event_helper import EventHelper
from src.utils.asyncio_utils import schedule_background
class Battle(InstantAction):
@@ -73,23 +71,12 @@ class Battle(InstantAction):
pass
result_event = Event(self.world.month_stamp, result_text, related_avatars=rel_ids)
# 异步生成战斗小故事并在完成后推送事件,避免阻塞事件循环
# 生成战斗小故事(同步调用,与其他动作保持一致)
target = self._get_target(avatar_name)
start_text = getattr(self, "_start_event_content", "") or result_event.content
month_at_finish = self.world.month_stamp
story = StoryTeller.tell_from_actors(start_text, result_event.content, self.avatar, target, prompt=self.STORY_PROMPT)
story_event = Event(self.world.month_stamp, story, related_avatars=rel_ids)
async def _gen_and_push_story():
story = await StoryTeller.tell_from_actors_async(start_text, result_event.content, self.avatar, target, prompt=self.STORY_PROMPT)
story_event = Event(month_at_finish, story, related_avatars=rel_ids)
EventHelper.push_pair(story_event, initiator=self.avatar, target=target, to_sidebar_once=True)
def _fallback_sync():
story = StoryTeller.tell_from_actors(start_text, result_event.content, self.avatar, target, prompt=self.STORY_PROMPT)
story_event = Event(month_at_finish, story, related_avatars=rel_ids)
EventHelper.push_pair(story_event, initiator=self.avatar, target=target, to_sidebar_once=True)
schedule_background(_gen_and_push_story(), fallback=_fallback_sync)
return [result_event]
return [result_event, story_event]

View File

@@ -9,8 +9,6 @@ from src.utils.config import CONFIG
from src.classes.avatar import Avatar
from src.classes.event import Event
from src.classes.story_teller import StoryTeller
from src.classes.action.event_helper import EventHelper
from src.utils.asyncio_utils import schedule_background
from src.classes.technique import (
TechniqueGrade,
get_random_upper_technique_for_avatar,
@@ -237,7 +235,7 @@ def _get_fortune_technique_for_avatar(avatar: Avatar) -> Optional[Technique]:
return random.choices(candidates, weights=weights, k=1)[0]
def try_trigger_fortune(avatar: Avatar) -> list[Event]:
async def try_trigger_fortune(avatar: Avatar) -> list[Event]:
"""
在月度结算阶段尝试触发奇遇。
规则:
@@ -300,35 +298,19 @@ def try_trigger_fortune(avatar: Avatar) -> list[Event]:
related_avatars.append(master.id)
actors_for_story = [avatar, master] # 拜师奇遇需要两个人的信息
# 生成故事(异步避免阻塞
# 生成故事(异步,等待完成
event_text = f"遭遇奇遇({theme}{res_text}"
story_prompt = "请据此写100~150字小故事。"
month_at_finish = avatar.world.month_stamp
base_event = Event(month_at_finish, event_text, related_avatars=related_avatars)
async def _gen_and_push_story():
# 拜师奇遇传入两个角色,其他奇遇传入一个角色
story = await StoryTeller.tell_from_actors_async(event_text, res_text, *actors_for_story, prompt=story_prompt)
story_event = Event(month_at_finish, story, related_avatars=related_avatars)
# 根据涉及角色数量推送事件
if len(actors_for_story) == 1:
EventHelper.push_self(story_event, avatar, to_sidebar=True)
else:
# 拜师奇遇涉及两个角色
EventHelper.push_pair(story_event, initiator=avatar, target=actors_for_story[1], to_sidebar_once=True)
# 生成故事事件
story = await StoryTeller.tell_from_actors_async(event_text, res_text, *actors_for_story, prompt=story_prompt)
story_event = Event(month_at_finish, story, related_avatars=related_avatars)
def _fallback_sync():
story = StoryTeller.tell_from_actors(event_text, res_text, *actors_for_story, prompt=story_prompt)
story_event = Event(month_at_finish, story, related_avatars=related_avatars)
if len(actors_for_story) == 1:
EventHelper.push_self(story_event, avatar, to_sidebar=True)
else:
EventHelper.push_pair(story_event, initiator=avatar, target=actors_for_story[1], to_sidebar_once=True)
schedule_background(_gen_and_push_story(), fallback=_fallback_sync)
return [base_event]
# 返回基础事件和故事事件
return [base_event, story_event]
__all__ = [

View File

@@ -107,7 +107,7 @@ class Simulator:
events.append(event)
return events
def _phase_passive_effects(self):
async def _phase_passive_effects(self):
"""
被动结算阶段:
- 更新时间效果如HP回复
@@ -117,7 +117,8 @@ class Simulator:
for avatar in self.world.avatar_manager.avatars.values():
avatar.update_time_effect()
for avatar in list(self.world.avatar_manager.avatars.values()):
events.extend(try_trigger_fortune(avatar))
fortune_events = await try_trigger_fortune(avatar)
events.extend(fortune_events)
return events
def _phase_log_events(self, events):
@@ -155,7 +156,7 @@ class Simulator:
events.extend(self._phase_update_age_and_birth())
# 6. 被动结算(时间效果+奇遇)
events.extend(self._phase_passive_effects())
events.extend(await self._phase_passive_effects())
# 7. 日志
# 统一写入事件管理器