forked from jamiechapman/hearthstonejson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·231 lines (190 loc) · 5.57 KB
/
generate.py
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
#!/usr/bin/env python
import os
import json
import sys
from argparse import ArgumentParser
from enum import IntEnum
from hearthstone.dbf import Dbf
from hearthstone.cardxml import load
from hearthstone.enums import CardType, Faction, GameTag, Locale, LOCALIZED_TAGS
MECHANICS_TAGS = [
GameTag.ADJACENT_BUFF,
GameTag.AURA,
GameTag.BATTLECRY,
GameTag.CHARGE,
GameTag.COMBO,
GameTag.DEATHRATTLE,
GameTag.DIVINE_SHIELD,
GameTag.ENRAGED,
GameTag.FORGETFUL,
GameTag.FREEZE,
GameTag.INSPIRE,
GameTag.MORPH,
GameTag.OVERLOAD,
GameTag.POISONOUS,
GameTag.SECRET,
GameTag.SILENCE,
GameTag.STEALTH,
GameTag.SPELLPOWER,
GameTag.TAG_ONE_TURN_EFFECT,
GameTag.TAUNT,
GameTag.TREASURE,
GameTag.WINDFURY,
GameTag.ImmuneToSpellpower,
GameTag.InvisibleDeathrattle,
]
def json_dump(obj, filename, pretty=False):
print("Writing to %r" % (filename))
if pretty:
kwargs = {"sort_keys": True, "indent": "\t", "separators": (",", ": ")}
else:
kwargs = {"separators": (",", ":")}
with open(filename, "w", encoding="utf8") as f:
json.dump(obj, f, ensure_ascii=False, **kwargs)
def show_field(card, k, v):
if k == "cost" and card.type not in (CardType.ENCHANTMENT, CardType.HERO):
return True
if k == "faction" and v == Faction.NEUTRAL:
return False
if k == "attack" and card.type in (CardType.MINION, CardType.WEAPON):
return True
if k == "health" and card.type in (CardType.MINION, CardType.HERO):
return True
if k == "durability" and card.type == CardType.WEAPON:
return True
return bool(v)
def get_mechanics(card):
ret = []
for tag in MECHANICS_TAGS:
value = card.tags.get(tag, 0)
if value:
ret.append(tag.name)
return ret
TAG_NAMES = {
GameTag.CARDNAME: "name",
GameTag.FLAVORTEXT: "flavortext",
GameTag.CARDTEXT_INHAND: "text",
GameTag.CardTextInPlay: "textInPlay",
GameTag.HOW_TO_EARN: "howToEarn",
GameTag.HOW_TO_EARN_GOLDEN: "howToEarnGolden",
GameTag.TARGETING_ARROW_TEXT: "targetingArrowText",
}
def serialize_card(card):
ret = {
"id": card.id,
"name": card.name,
"flavor": card.flavortext,
"text": card.description,
"textInPlay": card.playtext,
"howToEarn": card.how_to_earn,
"howToEarnGolden": card.how_to_earn_golden,
"targetingArrowText": card.targeting_arrow_text,
"artist": card.artist,
"faction": card.faction,
"playerClass": card.card_class,
"race": card.race,
"rarity": card.rarity,
"set": card.card_set,
"type": card.type,
"collectible": card.collectible,
"attack": card.atk,
"cost": card.cost,
"durability": card.durability,
"health": card.health,
}
ret = {k: v for k, v in ret.items() if show_field(card, k, v)}
for k, v in ret.items():
if isinstance(v, IntEnum):
ret[k] = v.name
mechanics = get_mechanics(card)
if mechanics:
ret["mechanics"] = mechanics
if card.entourage:
ret["entourage"] = card.entourage
if card.requirements:
ret["playRequirements"] = {k.name: v for k, v in card.requirements.items()}
if card.craftable:
ret["dust"] = card.crafting_costs + card.disenchant_costs
# if card.choose_cards:
# ret["chooseCards"] = card.choose_cards
return ret
def export_cards_to_file(cards, filename, locale):
ret = []
for card in cards:
card.locale = locale
ret.append(serialize_card(card))
json_dump(ret, filename)
def export_all_locales_cards_to_file(cards, filename):
ret = []
for card in cards:
obj = serialize_card(card)
for tag in LOCALIZED_TAGS:
if tag in TAG_NAMES:
value = card._localized_tags[tag]
if value:
obj[TAG_NAMES[tag]] = value
ret.append(obj)
json_dump(ret, filename)
def write_cardbacks(dbf, filename, locale):
ret = []
for record in dbf.records:
ret.append({
"id": record["ID"],
"note_desc": record["NOTE_DESC"],
"source": record["SOURCE"],
"enabled": record["ENABLED"],
"name": record.get("NAME", {}).get(locale.name, ""),
"prefab_name": record.get("PREFAB_NAME", ""),
"description": record.get("DESCRIPTION", {}).get(locale.name, ""),
"source_description": record.get("SOURCE_DESCRIPTION", {}).get(locale.name, ""),
})
json_dump(ret, filename)
def main():
parser = ArgumentParser()
parser.add_argument(
"-o", "--output-dir",
type=str,
dest="output_dir",
default="out",
help="Output directory"
)
parser.add_argument(
"-i", "--input-dir",
type=str,
dest="input_dir",
default="hs-data",
help="Input hs-data directory"
)
args = parser.parse_args(sys.argv[1:])
db, xml = load(os.path.join(args.input_dir, "CardDefs.xml"))
dbf_path = os.path.join(args.input_dir, "DBF", "CARD_BACK.xml")
if not os.path.exists(dbf_path):
print("Skipping card back generation (%s does not exist)" % (dbf_path))
dbf = None
else:
dbf = Dbf.load(dbf_path)
cards = db.values()
collectible_cards = [card for card in cards if card.collectible]
for locale in Locale:
if locale.unused:
continue
basedir = os.path.join(args.output_dir, locale.name)
if not os.path.exists(basedir):
os.makedirs(basedir)
filename = os.path.join(basedir, "cards.json")
export_cards_to_file(cards, filename, locale.name)
filename = os.path.join(basedir, "cards.collectible.json")
export_cards_to_file(collectible_cards, filename, locale.name)
if dbf is not None:
filename = os.path.join(basedir, "cardbacks.json")
write_cardbacks(dbf, filename, locale)
# Generate merged locales
basedir = os.path.join(args.output_dir, "all")
if not os.path.exists(basedir):
os.makedirs(basedir)
filename = os.path.join(basedir, "cards.json")
export_all_locales_cards_to_file(cards, filename)
filename = os.path.join(basedir, "cards.collectible.json")
export_all_locales_cards_to_file(collectible_cards, filename)
if __name__ == "__main__":
main()