trioskosmos commited on
Commit
886cd06
·
verified ·
1 Parent(s): b7dcbff

Upload ai/research/compile_numba_db.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. ai/research/compile_numba_db.py +149 -0
ai/research/compile_numba_db.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+
5
+ # Ensure project root is in path
6
+ sys.path.append(os.getcwd())
7
+
8
+ from engine.models.ability import (
9
+ Ability,
10
+ AbilityCostType,
11
+ Condition,
12
+ ConditionType,
13
+ Cost,
14
+ Effect,
15
+ EffectType,
16
+ TargetType,
17
+ TriggerType,
18
+ )
19
+
20
+
21
+ def load_json(path):
22
+ with open(path, "r", encoding="utf-8") as f:
23
+ return json.load(f)
24
+
25
+
26
+ def reconstruct_ability(data):
27
+ """Reconstruct Ability object from JSON dict."""
28
+ effects = []
29
+ for eff_data in data.get("effects", []):
30
+ effects.append(
31
+ Effect(
32
+ effect_type=EffectType(eff_data["effect_type"]),
33
+ value=eff_data.get("value", 0),
34
+ target=TargetType(eff_data.get("target", 0)),
35
+ params=eff_data.get("params", {}),
36
+ is_optional=eff_data.get("is_optional", False),
37
+ )
38
+ )
39
+
40
+ conditions = []
41
+ for cond_data in data.get("conditions", []):
42
+ conditions.append(
43
+ Condition(
44
+ type=ConditionType(cond_data["type"]),
45
+ params=cond_data.get("params", {}),
46
+ is_negated=cond_data.get("is_negated", False),
47
+ )
48
+ )
49
+
50
+ costs = []
51
+ for cost_data in data.get("costs", []):
52
+ costs.append(
53
+ Cost(
54
+ type=AbilityCostType(cost_data["type"]),
55
+ value=cost_data.get("value", 0),
56
+ params=cost_data.get("params", {}),
57
+ is_optional=cost_data.get("is_optional", False),
58
+ )
59
+ )
60
+
61
+ return Ability(
62
+ raw_text=data.get("raw_text", ""),
63
+ trigger=TriggerType(data.get("trigger", 0)),
64
+ effects=effects,
65
+ conditions=conditions,
66
+ costs=costs,
67
+ is_once_per_turn=data.get("is_once_per_turn", False),
68
+ )
69
+
70
+
71
+ def main():
72
+ print("--- Compiling Cards to Numba Bytecode ---")
73
+
74
+ # Load Data
75
+ compiled_data = load_json("data/cards_compiled.json")
76
+ verified_pool = load_json("data/verified_card_pool.json")
77
+
78
+ # Target Lists
79
+ target_ids = []
80
+
81
+ # Handle list-format or dict-format verified_card_pool.json
82
+ if isinstance(verified_pool, dict):
83
+ verified_abilities = verified_pool.get("verified_abilities", [])
84
+ else:
85
+ verified_abilities = verified_pool
86
+
87
+ # Pre-search map
88
+ cid_map = {}
89
+ for cid, card in compiled_data.get("member_db", {}).items():
90
+ cid_map[card["card_no"]] = cid
91
+ for cid, card in compiled_data.get("live_db", {}).items():
92
+ cid_map[card["card_no"]] = cid
93
+
94
+ for card_no in verified_abilities:
95
+ # Find ID by card_no
96
+ if card_no in cid_map:
97
+ target_ids.append(int(cid_map[card_no]))
98
+ else:
99
+ print(f"Warning: Verified card {card_no} not found in DB.")
100
+
101
+ bytecode_map = {}
102
+ stats = {"compiled": 0, "failed": 0, "skipped": 0}
103
+
104
+ # Iterate all cards (or just verified?)
105
+ # Let's do ALL cards types found in DB to be safe, but focus reporting on verified.
106
+ # Iterate all cards from both DBs
107
+ all_cards = {}
108
+ all_cards.update(compiled_data.get("member_db", {}))
109
+ all_cards.update(compiled_data.get("live_db", {}))
110
+
111
+ for cid, card_data in all_cards.items():
112
+ cid = int(cid)
113
+ # Assuming only 1 main ability for now or handling the first checking trigger?
114
+ # The VM typically applies an action by ID.
115
+ # We need a schema: ActionID -> Bytecode.
116
+ # For now, let's map: CardID_AbilityIndex -> Bytecode
117
+
118
+ abilities = card_data.get("abilities", [])
119
+ for i, ab_data in enumerate(abilities):
120
+ try:
121
+ # Reconstruct and Compile
122
+ ability = reconstruct_ability(ab_data)
123
+ bc = ability.compile()
124
+
125
+ # Check if non-trivial
126
+ if len(bc) > 4: # generic empty return is length 4
127
+ key = f"{cid}_{i}"
128
+ bytecode_map[key] = bc
129
+ stats["compiled"] += 1
130
+ else:
131
+ stats["skipped"] += 1
132
+ except Exception:
133
+ # print(f"Failed to compile card {cid} ability {i}: {e}")
134
+ stats["failed"] += 1
135
+
136
+ print("Compilation Complete.")
137
+ print(f"Compiled: {stats['compiled']}")
138
+ print(f"Skipped (Empty/NoOp): {stats['skipped']}")
139
+ print(f"Failed: {stats['failed']}")
140
+
141
+ # Save
142
+ out_path = "data/cards_numba.json"
143
+ with open(out_path, "w", encoding="utf-8") as f:
144
+ json.dump(bytecode_map, f, indent=0)
145
+ print(f"Saved to {out_path}")
146
+
147
+
148
+ if __name__ == "__main__":
149
+ main()