feat: auto clear death avatar after 20 years
This commit is contained in:
@@ -97,6 +97,38 @@ class AvatarManager:
|
||||
"""辅助方法:遍历所有角色(活人+死者)"""
|
||||
return itertools.chain(self.avatars.values(), self.dead_avatars.values())
|
||||
|
||||
def cleanup_long_dead_avatars(self, current_time: "MonthStamp", threshold_years: int = 20) -> int:
|
||||
"""
|
||||
清理长期已故的角色。
|
||||
|
||||
Args:
|
||||
current_time: 当前时间戳
|
||||
threshold_years: 死亡超过多少年则清理 (默认20年)
|
||||
|
||||
Returns:
|
||||
清理的角色数量
|
||||
"""
|
||||
if not self.dead_avatars:
|
||||
return 0
|
||||
|
||||
to_remove = []
|
||||
for aid, avatar in self.dead_avatars.items():
|
||||
if avatar.death_info:
|
||||
death_time = avatar.death_info.get("time") # int 类型的时间戳
|
||||
if death_time is not None:
|
||||
# 计算时间差 (MonthStamp 本质是 int, 表示总月数)
|
||||
elapsed_months = int(current_time) - death_time
|
||||
elapsed_years = elapsed_months // 12
|
||||
|
||||
if elapsed_years >= threshold_years:
|
||||
to_remove.append(aid)
|
||||
|
||||
# 批量删除
|
||||
if to_remove:
|
||||
self.remove_avatars(to_remove)
|
||||
|
||||
return len(to_remove)
|
||||
|
||||
def remove_avatar(self, avatar_id: str) -> None:
|
||||
"""
|
||||
从管理器中彻底删除一个 avatar(无论是死是活),并清理所有与其相关的双向关系。
|
||||
|
||||
@@ -479,6 +479,14 @@ class Simulator:
|
||||
|
||||
# 15. (每年1月) 更新计算关系 (二阶关系)
|
||||
self._phase_update_calculated_relations()
|
||||
|
||||
# 15.5 (每年1月) 清理由于时间久远而被遗忘的死者
|
||||
if self.world.month_stamp.get_month() == Month.JANUARY:
|
||||
# 20年写死或者做成 CONFIG.game.dead_cleanup_years
|
||||
cleaned_count = self.world.avatar_manager.cleanup_long_dead_avatars(self.world.month_stamp, 20)
|
||||
if cleaned_count > 0:
|
||||
# 记录日志,但不产生游戏内事件
|
||||
get_logger().logger.info(f"Cleaned up {cleaned_count} long-dead avatars.")
|
||||
|
||||
# 16. 归档与时间推进
|
||||
return self._finalize_step(events)
|
||||
|
||||
Reference in New Issue
Block a user