feat: AI Town MVP - 完整三层架构实现
- Python FastAPI 引擎:世界状态模拟、全局能量条、世界事件系统 - Node.js WebSocket 服务器:实时通信、事件队列批处理 - 前端仪表盘:世界状态可视化、行动日志、事件展示 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
35
engine-python/app/state.py
Normal file
35
engine-python/app/state.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from .models import WorldState, AgentState, GlobalMeter
|
||||
|
||||
STATE_FILE = Path(__file__).parent.parent / "state.json"
|
||||
|
||||
|
||||
def get_default_state() -> WorldState:
|
||||
"""创建默认的世界状态"""
|
||||
return WorldState(
|
||||
tick=0,
|
||||
weather="sunny",
|
||||
town_mood=0,
|
||||
agents={
|
||||
"alice": AgentState(emotion="calm", goal="探索小镇", memory=[]),
|
||||
"bob": AgentState(emotion="calm", goal="与人交流", memory=[]),
|
||||
},
|
||||
events=[],
|
||||
global_meter=GlobalMeter(value=0, threshold=100, cooldown=0),
|
||||
)
|
||||
|
||||
|
||||
def load_state() -> WorldState:
|
||||
"""从文件加载状态,如果不存在则创建默认状态"""
|
||||
if STATE_FILE.exists():
|
||||
with open(STATE_FILE, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
return WorldState(**data)
|
||||
return get_default_state()
|
||||
|
||||
|
||||
def save_state(state: WorldState) -> None:
|
||||
"""保存状态到文件"""
|
||||
with open(STATE_FILE, "w", encoding="utf-8") as f:
|
||||
json.dump(state.model_dump(), f, ensure_ascii=False, indent=2)
|
||||
Reference in New Issue
Block a user