trioskosmos commited on
Commit
aa9d06f
·
verified ·
1 Parent(s): ae8dcb4

Upload ai/utils/record_game.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. ai/utils/record_game.py +134 -0
ai/utils/record_game.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import json
3
+ import os
4
+ import random
5
+ import sys
6
+
7
+ # Add project root to path
8
+ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
+
10
+ import engine_rust
11
+ from ai.benchmark_decks import parse_deck
12
+
13
+
14
+ def get_action_name(action_id, game, db_json):
15
+ """Decodes Action ID into a human-readable name."""
16
+ if action_id == 0:
17
+ return "Pass/End Phase"
18
+
19
+ phase = game.phase
20
+ p_idx = game.current_player
21
+ p_state = game.get_player(p_idx)
22
+
23
+ if phase in [-1, 0]: # Mulligan
24
+ indices = [i for i in range(len(p_state.hand)) if (action_id >> i) & 1]
25
+ return f"Mulligan: Discard indices {indices}"
26
+
27
+ if phase == 4: # Main
28
+ if 1 <= action_id <= 180:
29
+ adj = action_id - 1
30
+ hand_idx = adj // 3
31
+ slot_idx = adj % 3
32
+ if hand_idx < len(p_state.hand):
33
+ cid = p_state.hand[hand_idx]
34
+ card = db_json["member_db"].get(str(cid), {"name": f"Unknown Member {cid}"})
35
+ return f"Play {card['name']} to Slot {slot_idx}"
36
+ elif 200 <= action_id < 400:
37
+ adj = action_id - 200
38
+ slot_idx = adj // 10
39
+ ab_idx = adj % 10
40
+ return f"Activate Ability {ab_idx} in Slot {slot_idx}"
41
+
42
+ if phase == 5: # LiveSet
43
+ if 400 <= action_id < 500:
44
+ hand_idx = action_id - 400
45
+ if hand_idx < len(p_state.hand):
46
+ cid = p_state.hand[hand_idx]
47
+ card = db_json["live_db"].get(str(cid), {"name": f"Unknown Live {cid}"})
48
+ return f"Set Live: {card['name']}"
49
+
50
+ return f"Unknown Action ({action_id})"
51
+
52
+
53
+ def record_game(output_file, sims=200):
54
+ with open("data/cards_compiled.json", "r", encoding="utf-8") as f:
55
+ db_content = f.read()
56
+ db_json = json.loads(db_content)
57
+ db = engine_rust.PyCardDatabase(db_content)
58
+ game = engine_rust.PyGameState(db)
59
+
60
+ muse_deck_path = "ai/decks/muse_cup.txt"
61
+ p_deck, p_lives, p_energy = parse_deck(
62
+ muse_deck_path, db_json["member_db"], db_json["live_db"], db_json.get("energy_db", {})
63
+ )
64
+
65
+ game.initialize_game(p_deck, p_deck, p_energy, p_energy, p_lives, p_lives)
66
+
67
+ with open(output_file, "w", encoding="utf-8") as f:
68
+ f.write(f"--- GAME RECORD: MCTS ({sims} sims) vs RANDOM ---\n")
69
+ f.write(f"Deck: {muse_deck_path}\n\n")
70
+
71
+ step = 0
72
+ while not game.is_terminal() and step < 1000:
73
+ cp = game.current_player
74
+ phase = game.phase
75
+ p_state = game.get_player(cp)
76
+
77
+ phase_names = {
78
+ -1: "MulliganP1",
79
+ 0: "MulliganP2",
80
+ 1: "Active",
81
+ 2: "Energy",
82
+ 3: "Draw",
83
+ 4: "Main",
84
+ 5: "LiveSet",
85
+ 6: "PerfP1",
86
+ 7: "PerfP2",
87
+ 8: "LiveResult",
88
+ }
89
+ p_name = phase_names.get(phase, f"Phase {phase}")
90
+
91
+ f.write(f"[Step {step} | Turn {game.turn} | Player {cp} ({'MCTS' if cp == 0 else 'RANDOM'}) | {p_name}]\n")
92
+
93
+ is_interactive = phase in [-1, 0, 4, 5]
94
+
95
+ if is_interactive:
96
+ tapped = sum(1 for t in p_state.tapped_energy if t)
97
+ total_en = len(p_state.energy_zone)
98
+ f.write(f" Energy: {total_en - tapped}/{total_en}\n")
99
+ f.write(f" Hand: {p_state.hand}\n")
100
+ f.write(f" Stage: {p_state.stage}\n")
101
+ f.write(f" Live Zone: {p_state.live_zone} (Revealed: {p_state.live_zone_revealed})\n")
102
+ f.write(f" Score: {p_state.score}\n")
103
+
104
+ if cp == 0:
105
+ suggestions = game.get_mcts_suggestions(sims, engine_rust.SearchHorizon.TurnEnd)
106
+ action = suggestions[0][0] if suggestions else 0
107
+ else:
108
+ legal = game.get_legal_action_ids()
109
+ action = random.choice(legal) if legal else 0
110
+
111
+ f.write(f" Action: {action} ({get_action_name(action, game, db_json)})\n")
112
+ game.step(action)
113
+ else:
114
+ game.step(0)
115
+
116
+ f.write("\n")
117
+ step += 1
118
+
119
+ f.write("\n--- GAME COMPLETE ---\n")
120
+ p0 = game.get_player(0)
121
+ p1 = game.get_player(1)
122
+ f.write(f"Final Outcome: winner={game.get_winner()}\n")
123
+ f.write(f"P0 (MCTS): Score {p0.score}, Success Lives: {len(p0.success_lives)}\n")
124
+ f.write(f"P1 (RANDOM): Score {p1.score}, Success Lives: {len(p1.success_lives)}\n")
125
+
126
+ print(f"Game record saved to {output_file}")
127
+
128
+
129
+ if __name__ == "__main__":
130
+ parser = argparse.ArgumentParser()
131
+ parser.add_argument("--output", default="game_record.txt")
132
+ parser.add_argument("--sims", type=int, default=200)
133
+ args = parser.parse_args()
134
+ record_game(args.output, sims=args.sims)