forked from jamiechapman/hearthstonejson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·266 lines (218 loc) · 6.34 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
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
#!/usr/bin/env python
import os
import json
import sys
from argparse import ArgumentParser
from enum import IntEnum
from hearthstone.cardxml import load
from hearthstone.enums import CardType, Faction, GameTag, Locale
NBSP = "\u00A0"
MECHANICS_TAGS = [
GameTag.ADJACENT_BUFF,
GameTag.AI_MUST_PLAY,
GameTag.APPEAR_FUNCTIONALLY_DEAD,
GameTag.ADAPT,
GameTag.AURA,
GameTag.BATTLECRY,
GameTag.CHARGE,
GameTag.CHOOSE_ONE,
GameTag.COMBO,
GameTag.COUNTER,
GameTag.DEATHRATTLE,
GameTag.DISCOVER,
GameTag.DIVINE_SHIELD,
GameTag.ENRAGED,
GameTag.EVIL_GLOW,
GameTag.FORGETFUL,
GameTag.FREEZE,
GameTag.INSPIRE,
GameTag.IMMUNE,
GameTag.JADE_GOLEM,
GameTag.MORPH,
GameTag.POISONOUS,
GameTag.QUEST,
GameTag.RECEIVES_DOUBLE_SPELLDAMAGE_BONUS,
GameTag.RITUAL,
GameTag.SECRET,
GameTag.SILENCE,
GameTag.STEALTH,
GameTag.TAG_ONE_TURN_EFFECT,
GameTag.TAUNT,
GameTag.TOPDECK,
GameTag.UNTOUCHABLE,
GameTag.WINDFURY,
GameTag.ImmuneToSpellpower,
GameTag.InvisibleDeathrattle,
GameTag.CANT_ATTACK,
GameTag.CANT_BE_TARGETED_BY_ABILITIES,
GameTag.CANT_BE_TARGETED_BY_HERO_POWERS,
]
def json_dump(obj, filename, pretty=False):
print("Writing to %r" % (filename))
kwargs = {
"ensure_ascii": False,
"separators": (",", ":"),
"sort_keys": True,
}
if pretty:
kwargs["indent"] = "\t"
kwargs["separators"] = (",", ": ")
with open(filename, "w", encoding="utf8") as f:
json.dump(obj, f, **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_tags(card):
tags, referenced_tags = [], []
for gametag in MECHANICS_TAGS:
tag = card.tags.get(gametag, 0)
if tag:
tags.append(gametag.name)
referenced_tag = card.referenced_tags.get(gametag, 0)
if referenced_tag:
referenced_tags.append(gametag.name)
return tags, referenced_tags
def clean_card_description(text):
text = text.replace("_", NBSP)
if "@" in text:
text, collection_text = text.split("@")
text = text.strip()
collection_text = collection_text.strip()
else:
collection_text = ""
return text, collection_text
def serialize_card(card):
text, collection_text = clean_card_description(card.description)
ret = {
"id": card.id,
"dbfId": card.dbf_id,
"name": card.name,
"flavor": card.flavortext,
"text": text,
"collectionText": collection_text,
"howToEarn": card.how_to_earn,
"howToEarnGolden": card.how_to_earn_golden,
"targetingArrowText": card.targeting_arrow_text,
"artist": card.artist,
"faction": card.faction,
"hideStats": card.hide_stats,
"playerClass": card.card_class, # Deprecated 2017-03-06
"cardClass": card.card_class,
"race": card.race,
"rarity": card.rarity,
"set": card.card_set,
"type": card.type,
"collectible": card.collectible,
"elite": card.elite,
"attack": card.atk,
"cost": card.cost,
"durability": card.durability,
"health": card.health,
"overload": card.overload,
"spellDamage": card.spell_damage,
}
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
tags, referenced_tags = get_tags(card)
if tags:
ret["mechanics"] = tags
if referenced_tags:
ret["referencedTags"] = referenced_tags
if card.entourage:
ret["entourage"] = card.entourage
if card.multiple_classes:
ret["classes"] = [c.name for c in card.classes]
if card.multi_class_group:
ret["multiClassGroup"] = card.multi_class_group.name
if card.requirements:
ret["playRequirements"] = {k.name: v for k, v in card.requirements.items()}
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):
tag_names = {
GameTag.CARDNAME: "name",
GameTag.FLAVORTEXT: "flavor",
GameTag.CARDTEXT_INHAND: "text",
GameTag.HOW_TO_EARN: "howToEarn",
GameTag.HOW_TO_EARN_GOLDEN: "howToEarnGolden",
GameTag.TARGETING_ARROW_TEXT: "targetingArrowText",
}
ret = []
for card in cards:
obj = serialize_card(card)
obj["collectionText"] = {}
for tag, key in tag_names.items():
value = card.strings.get(tag, {})
if key == "text":
for locale, localized_value in value.items():
text, collection_text = clean_card_description(localized_value)
value[locale] = text
if collection_text:
obj["collectionText"][locale] = collection_text
if value:
obj[key] = value
if not obj["collectionText"]:
del obj["collectionText"]
ret.append(obj)
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,
required=True,
dest="input_dir",
help="Input hsdata directory"
)
parser.add_argument("--locale", type=str, nargs="*", help="Only generate one locale")
args = parser.parse_args(sys.argv[1:])
db, xml = load(os.path.join(args.input_dir, "CardDefs.xml"))
cards = db.values()
collectible_cards = [card for card in cards if card.collectible]
filter_locales = [loc.lower() for loc in args.locale or []]
for locale in Locale:
if locale.unused:
continue
if filter_locales and locale.name.lower() not in filter_locales:
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)
# Generate merged locales
if "all" in filter_locales or not filter_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()