fix: pause game during save load to prevent stale events

The game loop could generate events with the old world's timestamp while
loading a save, causing 100年1月 events to appear after loading a 106年
save. Now we pause the game before loading and keep it paused after,
giving the frontend time to refresh state.
This commit is contained in:
Zihao Xu
2026-01-07 23:30:26 -08:00
committed by bridge
parent 6b99552f08
commit 624f697bee

View File

@@ -1307,6 +1307,9 @@ def api_load_game(req: LoadGameRequest):
if not target_path.exists(): if not target_path.exists():
raise HTTPException(status_code=404, detail="File not found") raise HTTPException(status_code=404, detail="File not found")
# 暂停游戏,防止 game_loop 在加载过程中使用旧 world 生成事件。
game_instance["is_paused"] = True
# 关闭旧 World 的 EventManager释放 SQLite 连接。 # 关闭旧 World 的 EventManager释放 SQLite 连接。
old_world = game_instance.get("world") old_world = game_instance.get("world")
if old_world and hasattr(old_world, "event_manager"): if old_world and hasattr(old_world, "event_manager"):
@@ -1323,6 +1326,9 @@ def api_load_game(req: LoadGameRequest):
game_instance["sim"] = new_sim game_instance["sim"] = new_sim
game_instance["current_save_path"] = target_path game_instance["current_save_path"] = target_path
# 加载完成后保持暂停状态,让用户决定何时恢复。
# 这也给前端时间来刷新状态。
return {"status": "ok", "message": "Game loaded"} return {"status": "ok", "message": "Game loaded"}
except Exception as e: except Exception as e:
import traceback import traceback