-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
104 lines (96 loc) · 3.4 KB
/
logger.py
File metadata and controls
104 lines (96 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
from typing import List, Dict, Any
from player_agent import LLMPlayerAgent
class GameLogger:
"""
游戏日志记录类,结构化记录游戏过程。
"""
def __init__(self):
self.logs: List[Dict[str, Any]] = []
self.roles: Dict[int, str] = {}
self.result: str = ""
self.detailed_prompts: List[Dict[str, Any]] = [] # 新增详细prompt日志
def log_roles(self, players: List[LLMPlayerAgent]):
"""
记录身份分配。
"""
self.roles = {p.player_id: p.role.value for p in players}
print(f"[LOG] Roles assigned: {self.roles}")
print(json.dumps({"roles": self.roles}, indent=2, ensure_ascii=False))
def log_night(self, round_num: int, wolves: List[LLMPlayerAgent], killed_id: int, log: dict = None):
"""
记录夜晚事件。
"""
if log is None:
log = {
"round": round_num,
"phase": "night",
"wolves": [w.player_id for w in wolves],
"killed": killed_id
}
self.logs.append(log)
print(f"[LOG] Night {round_num}: Wolves killed player {killed_id}")
print(json.dumps(log, indent=2, ensure_ascii=False))
def log_speeches(self, round_num: int, speeches: List[Dict]):
"""
记录白天发言。
"""
log = {
"round": round_num,
"phase": "day_speech",
"speeches": speeches
}
self.logs.append(log)
print(f"[LOG] Day {round_num}: Speeches recorded.")
print(json.dumps(log, indent=2, ensure_ascii=False))
def log_votes(self, round_num: int, votes: Dict[int, int], eliminated: int):
"""
记录投票结果。
"""
log = {
"round": round_num,
"phase": "day_vote",
"votes": votes,
"eliminated": eliminated
}
self.logs.append(log)
print(f"[LOG] Day {round_num}: Player {eliminated} eliminated by vote.")
print(json.dumps(log, indent=2, ensure_ascii=False))
def log_result(self, result: str):
"""
记录游戏胜负结果。
"""
self.result = result
log = {
"phase": "result",
"result": result
}
self.logs.append(log)
print(f"[LOG] Game result: {result}")
print(json.dumps(log, indent=2, ensure_ascii=False))
def log_prompt(self, player_id: int, round_num: int, phase: str, prompt: str, response: str):
"""
记录每个玩家每轮的prompt和LLM回复。
"""
entry = {
"player_id": player_id,
"round": round_num,
"phase": phase,
"prompt": prompt,
"response": response
}
self.detailed_prompts.append(entry)
print(f"[PROMPT LOG] Player {player_id} Round {round_num} Phase {phase}\nPrompt: {prompt}\nResponse: {response}\n")
def save(self, filename: str = "game_log.json"):
"""
保存日志到文件。
"""
data = {
"roles": self.roles,
"logs": self.logs,
"result": self.result,
"detailed_prompts": self.detailed_prompts # 保存详细prompt日志
}
with open(filename, "w") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
print(f"[LOG] Game log saved to {filename}")