-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame_serializer.py
More file actions
295 lines (255 loc) · 9.28 KB
/
Copy pathgame_serializer.py
File metadata and controls
295 lines (255 loc) · 9.28 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""
Serialize game state to JSON-compatible dicts for the web frontend.
"""
import globe
from constants import owners, option
from constants2 import CardType
from frames.card_frame import card as CardBase
from frames.persona_frame import persona as PersonaBase
from frames.actions import special_action
CARDTYPE_NAMES = {
CardType.ANY: "any",
CardType.STARTER: "starter",
CardType.WEAKNESS: "weakness",
CardType.HERO: "hero",
CardType.VILLAIN: "villain",
CardType.SUPERPOWER: "superpower",
CardType.EQUIPMENT: "equipment",
CardType.LOCATION: "location",
CardType.SPECIALSV: "special_sv",
}
OPTION_LABELS = {
option.NO: "No",
option.OK: "OK",
option.DONE: "Done",
option.EVEN: "Even",
option.ODD: "Odd",
option.CANNOT: "Cannot",
}
def serialize_card(c):
if c is None:
return None
return {
"id": getattr(c, 'card_id', str(id(c))),
"name": c.name,
"cost": c.cost,
"vp": c.vp,
"type": CARDTYPE_NAMES.get(c.ctype, "unknown"),
"text": getattr(c, 'text', ""),
"attack_text": getattr(c, 'attack_text', ""),
"image": c.image,
"defense": getattr(c, 'defense', False),
"has_attack": getattr(c, 'attack', False),
"frozen": list(c.frozen),
"rotation": getattr(c, 'rotation', 0),
"has_stack_ongoing": getattr(c, 'has_stack_ongoing', False),
}
def serialize_persona(p):
if p is None:
return None
return {
"id": p.name.replace(" ", "_").replace("'", ""),
"name": p.name,
"text": getattr(p, 'text', ""),
"image": p.image,
"active": getattr(p, 'active', True),
"rotation": getattr(p, 'rotation', 0),
}
def serialize_option(opt):
"""Serialize a query option - could be a card, persona, or button constant."""
if isinstance(opt, CardBase) and hasattr(opt, 'cost'):
result = serialize_card(opt)
result["opt_type"] = "card"
return result
elif isinstance(opt, PersonaBase):
return {
"opt_type": "persona",
"id": opt.name.replace(" ", "_").replace("'", ""),
"name": opt.name,
"text": getattr(opt, 'text', ""),
"image": opt.image,
}
elif isinstance(opt, int) and opt in OPTION_LABELS:
return {
"opt_type": "button",
"action": opt,
"label": OPTION_LABELS[opt],
}
else:
return {
"opt_type": "unknown",
"value": str(opt),
}
def serialize_query(query):
if query is None:
return None
return {
"text": query.text,
"context_card": serialize_card(query.card) if query.card else None,
"options": [serialize_option(opt) for opt in query.options],
}
def serialize_player(p, human_pid=None):
if p.persona is None:
persona_data = None
else:
persona_data = serialize_persona(p.persona)
special_opts = [
{"id": opt.action_id, "text": opt.button_text}
for opt in p.played.special_options
]
discard_top = None
if p.discard.contents:
discard_top = serialize_card(p.discard.contents[-1])
is_human = _is_human_web(p)
return {
"pid": p.pid,
"is_human": is_human,
"persona": persona_data,
"power": p.played.power,
"vp": p.vp,
"score": p.score,
"deck_size": p.deck.size(),
"discard_size": p.discard.size(),
"discard_top": discard_top,
# Show full hand only for human player; AI hand shows count only
"hand": [serialize_card(c) for c in p.hand.contents] if is_human else [],
"hand_size": p.hand.size(),
# Full discard list for all players (used by discard popup, including opponent view)
"discard_cards": [serialize_card(c) for c in p.discard.contents],
"played": [serialize_card(c) for c in p.played.contents],
"played_this_turn": [serialize_card(c) for c in p.played.played_this_turn],
"ongoing": [serialize_card(c) for c in p.ongoing.contents],
"under_persona": [serialize_card(c) for c in p.under_superhero.contents],
"over_persona": [serialize_card(c) for c in p.over_superhero.contents],
"special_options": special_opts,
}
def _is_human_web(player):
from controlers import human_web
return isinstance(player.controler, human_web)
def find_card_by_id(card_id):
"""Search all piles for a card with the given UUID."""
if not globe.boss:
return None
boss = globe.boss
all_piles = [
boss.lineup,
boss.main_deck,
boss.kick_stack,
boss.weakness_stack,
boss.supervillain_stack,
boss.destroyed_stack,
]
for pile in all_piles:
for c in pile.contents:
if getattr(c, 'card_id', None) == card_id:
return c
for p in boss.players:
for pile in [p.hand, p.deck, p.discard, p.played,
p.ongoing, p.under_superhero, p.over_superhero]:
for c in pile.contents:
if getattr(c, 'card_id', None) == card_id:
return c
# Also search query options (persona objects don't have card_id)
if globe.bus and globe.bus.display:
for opt in globe.bus.display.options:
if isinstance(opt, CardBase) and getattr(opt, 'card_id', None) == card_id:
return opt
return None
def find_persona_by_id(persona_id):
"""Search persona list for a persona with the given ID (name-derived)."""
if not globe.boss:
return None
for p in globe.boss.persona_list:
pid = p.name.replace(" ", "_").replace("'", "")
if pid == persona_id:
return p
# Also check already-assigned personas
for player in globe.boss.players:
if player.persona:
pid = player.persona.name.replace(" ", "_").replace("'", "")
if pid == persona_id:
return player.persona
return None
def _card_positions(boss):
"""Flat dict: card_id_str → pile_name for every card in the game."""
positions = {}
def add(cards, name):
for c in cards:
cid = str(getattr(c, 'card_id', ''))
if cid:
positions[cid] = name
add(boss.lineup.contents, 'lineup')
add(boss.main_deck.contents, 'main_deck')
add(boss.kick_stack.contents, 'kick_stack')
add(boss.weakness_stack.contents, 'weakness_stack')
add(boss.supervillain_stack.contents, 'sv_stack')
add(boss.destroyed_stack.contents, 'destroyed')
for p in boss.players:
pid = p.pid
add(p.hand.contents, f'p{pid}_hand')
add(p.deck.contents, f'p{pid}_deck')
add(p.discard.contents, f'p{pid}_discard')
add(p.played.contents, f'p{pid}_played')
add(p.ongoing.contents, f'p{pid}_ongoing')
add(p.under_superhero.contents, f'p{pid}_under')
add(p.over_superhero.contents, f'p{pid}_over')
return positions
def serialize_state():
"""Serialize the complete current game state."""
if not globe.boss:
return {"phase": "lobby"}
boss = globe.boss
# Determine phase
if len(boss.player_score) > 0:
phase = "game_over"
elif boss.game_ongoing:
phase = "playing"
elif boss.turn_number == 0:
phase = "choosing_persona"
else:
phase = "playing"
# Lineup
lineup = [serialize_card(c) for c in boss.lineup.contents]
# SV stack
sv_top = None
if boss.supervillain_stack.contents:
sv_top = serialize_card(boss.supervillain_stack.contents[-1])
# Players
players = [serialize_player(p) for p in boss.players]
# Active query
query = None
if globe.bus and globe.bus.display:
query = serialize_query(globe.bus.display)
# Flush recent events for animation hints
events = globe.flush_events()
return {
"phase": phase,
"whose_turn": boss.whose_turn,
"turn_number": boss.turn_number,
"main_deck_size": boss.main_deck.size(),
"destroyed_count": boss.destroyed_stack.size(),
"destroyed_cards": [serialize_card(c) for c in boss.destroyed_stack.contents],
"card_positions": _card_positions(boss),
"lineup": lineup,
"sv_stack": {
"top": sv_top,
"count": boss.supervillain_stack.size(),
"current_sv_id": getattr(boss.supervillain_stack.current_sv, 'card_id', None)
if boss.supervillain_stack.current_sv else None,
},
"kick_stack": {
"count": boss.kick_stack.size(),
"top_id": getattr(boss.kick_stack.contents[-1], 'card_id', None) if boss.kick_stack.contents else None,
"top": serialize_card(boss.kick_stack.contents[-1]) if boss.kick_stack.contents else None,
},
"weakness_stack": {
"count": boss.weakness_stack.size(),
"top_id": getattr(boss.weakness_stack.contents[-1], 'card_id', None) if boss.weakness_stack.contents else None,
},
"players": players,
"query": query,
"game_ongoing": boss.game_ongoing,
"player_scores": boss.player_score,
"end_reason": getattr(boss, 'end_reason', 'regular'),
"events": events,
}