-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGwentUtils.py
382 lines (306 loc) · 12.8 KB
/
GwentUtils.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/python3
import json
import re
import os
import xml.etree.ElementTree as xml
from pprint import pprint
IMAGE_SIZES = ['original', 'high', 'medium', 'low', 'thumbnail']
"""
Constants from Gwent Client.
"""
COMMON = 1
RARE = 2
EPIC = 4
LEGENDARY = 8
LEADER = 1
BRONZE = 2
SILVER = 4
GOLD = 8
NEUTRAL = 1
MONSTER = 2
NILFGAARD = 4
NORTHERN_REALMS = 8
SCOIATAEL = 16
SKELLIGE = 32
SYNDICATE = 64
TYPE_LEADER = 1
TYPE_SPELL = 2
TYPE_UNIT = 4
TYPE_ARTIFACT = 8
TYPE_STRATEGEM = 16
"""
Gwent Client ID -> Gwent Data ID mapping.
"""
RARITIES = { COMMON: "Common", RARE: "Rare", EPIC: "Epic", LEGENDARY: "Legendary"}
TIERS = { LEADER: "Leader", BRONZE: "Bronze", SILVER: "Silver", GOLD: "Gold"}
FACTIONS = { NEUTRAL: "Neutral", MONSTER: "Monster", NILFGAARD: "Nilfgaard",
NORTHERN_REALMS: "Northern Realms", SCOIATAEL: "Scoiatael", SKELLIGE: "Skellige", SYNDICATE: "Syndicate"}
TYPES = { TYPE_LEADER: "Leader", TYPE_SPELL: "Spell", TYPE_UNIT: "Unit", TYPE_ARTIFACT: "Artifact", TYPE_STRATEGEM: "Strategem"}
# Gaunter's 'Higher than 5' and 'Lower than 5' are not actually cards.
INVALID_TOKENS = ['200175', '200176']
LOCALES = ["en-US", "de-DE", "es-ES", "es-MX", "fr-FR", "it-IT", "ja-JP", "ko-KR", "pl-PL", "pt-BR", "ru-RU", "zh-CN", "zh-TW"]
LOCALISATION_FILE_NAMES = {
"en-US": "Localization/en-us.csv",
"de-DE": "Localization/de-de.csv",
"es-ES": "Localization/es-es.csv",
"es-MX": "Localization/es-mx.csv",
"fr-FR": "Localization/fr-fr.csv",
"it-IT": "Localization/it-it.csv",
"ja-JP": "Localization/ja-jp.csv",
"ko-KR": "Localization/ko-kr.csv",
"pl-PL": "Localization/pl-pl.csv",
"pt-BR": "Localization/pt-br.csv",
"ru-RU": "Localization/ru-ru.csv",
"zh-CN": "Localization/zh-cn.csv",
"zh-TW": "Localization/zh-tw.csv"
}
def save_json(filepath, data):
print("Saved JSON to: %s" % filepath)
with open(filepath, "w", encoding="utf-8", newline="\n") as f:
json.dump(data, f, sort_keys=True, indent=2, separators=(',', ': '))
def clean_html(raw_html):
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', raw_html)
return cleantext
def _is_token_valid(token, tooltips):
if token is not None and token.find('Tooltip') is not None:
valid = True
for locale in LOCALES:
tooltip = tooltips[locale].get(token.find('Tooltip').attrib['key'])
if tooltip is None or tooltip == '':
valid = False
return valid
else:
return False
def _get_evaluated_tooltips(raw_tooltips, card_names, card_abilities, card_templates):
# Generate complete tooltips from the raw_tooltips and accompanying data.
tooltips = {}
for card_id in raw_tooltips:
# Some cards don't have info.
if raw_tooltips.get(card_id) is None or raw_tooltips.get(card_id) == "":
tooltips[card_id] = ""
continue
# Set tooltip to be the raw tooltip string.
tooltips[card_id] = raw_tooltips[card_id]
template = card_templates[card_id]
# First replace the MaxRange placeholder
result = re.findall(r'.*?(\{Card\.MaxRange\}).*?', tooltips[card_id])
for key in result:
value = template.find('MaxRange').text
tooltips[card_id] = tooltips[card_id].replace(key, value)
# Replace provision cost placeholder
result = re.findall(r'.*?(\{Template\.Provision\}).*?', tooltips[card_id])
for key in result:
value = template.find('Provision').text
tooltips[card_id] = tooltips[card_id].replace(key, value)
# https://github.com/GwentCommunityDevelopers/gwent-data/issues/38
tooltips[card_id] = tooltips[card_id].replace("-B.P.BB_Hoard", "")
tooltips[card_id] = tooltips[card_id].replace("Tribute-B.P.BB_Tribute", "Tribute")
# Now replace all the other card abilities.
# Regex. Get all strings that lie between a '{' and '}'.
result = re.findall(r'.*?\{(.*?)\}.*?', tooltips[card_id])
for key in result:
ability_value = _get_card_ability_value(card_abilities, card_id, key)
if ability_value is not None:
tooltips[card_id] = tooltips[card_id].replace("{" + key + "}", ability_value)
return tooltips
def _get_card_ability_value(card_abilities, card_id, key):
ability = card_abilities.get(card_id)
if ability is None:
return None
lower_case_key = key.lower()
ability_data = ability.find('PersistentVariables')
if ability_data is not None:
for value in ability_data:
if value.attrib['Name'].lower() == lower_case_key:
return value.attrib['V']
ability_data = ability.find('TemporaryVariables')
if ability_data is not None:
for value in ability_data:
if value.attrib['Name'].lower() == lower_case_key:
return value.attrib['V']
def _get_tokens(card_templates, card_abilities):
tokens = {}
for card_id in card_templates:
tokens[card_id] = []
ability = card_abilities.get(card_id)
if ability is None:
continue
ability_data = ability.find('TemporaryVariables')
if ability_data is not None:
for value in ability_data.iter("V"):
if value.attrib.get('Type') == "CardDefinition":
token_id = value.attrib['TemplateId']
if token_id not in tokens[card_id]:
tokens[card_id].append(token_id)
for child in value:
if child.attrib.get('Type') == "CardDefinition":
token_id = child.attrib['TemplateId']
if token_id not in tokens[card_id]:
tokens[card_id].append(token_id)
return tokens
def _get_keywords(tooltips):
keywords_by_tooltip_id = {}
for tooltip_id in tooltips:
tooltip = tooltips[tooltip_id]
keywords = []
# Find all keywords in info string. E.g. find 'spawn' in '<keyword=spawn>'
# Can just use en-US here. It doesn't matter, all regions will return the same result.
result = re.findall(r'<keyword=([^>]+)>', tooltip)
for key in result:
if not key in keywords:
keywords.append(key)
keywords_by_tooltip_id[tooltip_id] = keywords
return keywords_by_tooltip_id
class GwentDataHelper:
def __init__(self, raw_folder):
self._folder = raw_folder
self.card_templates = self.get_card_templates()
raw_tooltips = {}
self.card_names = {}
self.flavor_strings = {}
self.categories = {}
for locale in LOCALES:
raw_tooltips[locale] = self.get_card_tooltips(locale)
self.card_names[locale] = self.get_card_names(locale)
self.flavor_strings[locale] = self.get_flavor_strings(locale)
self.categories[locale] = self.get_categories(locale)
card_abilities = self.get_card_abilities()
self.tooltips = {}
for locale in LOCALES:
self.tooltips[locale] = _get_evaluated_tooltips(raw_tooltips[locale], self.card_names[locale], card_abilities, self.card_templates)
# Can use any locale here, all locales will return the same result.
self.keywords = _get_keywords(self.tooltips[LOCALES[0]])
self.tokens = _get_tokens(self.card_templates, card_abilities)
self.artists = self.get_artists()
self.armor = self.get_card_armor()
def get_tooltips_file(self, locale):
path = self._folder + LOCALISATION_FILE_NAMES[locale]
if not os.path.isfile(path):
print("Couldn't find " + locale + " tooltips at " + path)
exit()
return path
def get_card_tooltips(self, locale):
tooltips_file = open(self.get_tooltips_file(locale), "r", encoding="utf-8")
tooltips = {}
for line in tooltips_file:
split = line.split(";", 1)
if "tooltip" not in split[0]:
continue
tooltip_id = split[0].replace("_tooltip", "").replace("\"", "").lstrip("0")
# Remove any weird tooltip ids e.g. 64_tooltip_lt
if "_lt" in tooltip_id or "_sa" in tooltip_id or "_b" in tooltip_id or "card_in_maintenance" in tooltip_id:
continue
# Remove any quotation marks and new lines.
tooltips[tooltip_id] = split[1].replace("\"\n", "").replace("\\n", "\n")
tooltips_file.close()
return tooltips
def get_keyword_tooltips(self, locale):
tooltips_file = open(self.get_tooltips_file(locale), "r", encoding="utf-8")
keywords = {}
for tooltip in tooltips_file:
split = tooltip.split(";", 1)
if "keyword" not in split[0]:
continue
keyword_id = split[0].replace("keyword_", "").replace("\"", "")
keywords[keyword_id] = {}
# Remove any quotation marks and new lines.
keywords[keyword_id] = split[1].replace("\"", "").replace("\n", "")
tooltips_file.close()
return keywords
def get_categories(self, locale):
tooltips_file = open(self.get_tooltips_file(locale), "r", encoding="utf-8")
categories = {}
for line in tooltips_file:
split = line.split(";", 1)
if "category" not in split[0]:
continue
category_id = split[0]
categories[category_id] = {}
# Remove any quotation marks and new lines.
categories[category_id] = split[1].replace("\"", "").replace("\n", "")
tooltips_file.close()
return categories
def get_card_templates(self):
path = self._folder + "Templates.xml"
if not os.path.isfile(path):
print("Couldn't find templates.xml at " + path)
exit()
card_templates = {}
tree = xml.parse(path)
root = tree.getroot()
for template in root.iter('Template'):
card_templates[template.attrib['Id']] = template
return card_templates
def get_artists(self):
path = self._folder + "ArtDefinitions.xml"
if not os.path.isfile(path):
print("Couldn't find ArtDefinitions.xml at " + path)
exit()
artists = {}
tree = xml.parse(path)
root = tree.getroot()
for art in root.iter('ArtDefinition'):
art_id = art.attrib['ArtId']
artist = art.get('ArtistName')
if artist != None:
artists[art_id] = artist
return artists
def get_card_abilities(self):
path = self._folder + "Abilities.xml"
if not os.path.isfile(path):
print("Couldn't find abilities.xml at " + path)
exit()
abilities = {}
tree = xml.parse(path)
root = tree.getroot()
for ability in root.iter('Ability'):
if ability.attrib['Type'] == "CardAbility":
card_id = ability.attrib['Template']
abilities[card_id] = ability
return abilities
def get_card_armor(self):
path = self._folder + "Templates.xml"
if not os.path.isfile(path):
print("Couldn't find templates.xml at " + path)
exit()
armor = {}
tree = xml.parse(path)
root = tree.getroot()
for template in root.iter('Template'):
armor_element = template.find('Armor')
if armor_element != None:
armor[template.attrib['Id']] = armor_element.text
return armor
def get_card_names(self, locale):
card_name_file = open(self.get_card_names_file(locale), "r", encoding="utf8")
card_names = {}
for line in card_name_file:
split = line.split(";", 1)
if len(split) < 2:
continue
if "_name" in split[0]:
name_id = split[0].replace("_name", "").replace("\"", "")
# Remove any quotation marks and new lines.
card_names[name_id] = split[1].replace("\"", "").replace("\n", "")
card_name_file.close()
return card_names
def get_flavor_strings(self, locale):
card_name_file = open(self.get_card_names_file(locale), "r", encoding="utf8")
flavor_strings = {}
for line in card_name_file:
split = line.split(";", 1)
if len(split) < 2:
continue
if "_fluff" in split[0]:
flavor_id = split[0].replace("_fluff", "").replace("\"", "")
# Remove any quotation marks and new lines.
flavor_strings[flavor_id] = split[1].replace("\"", "").replace("\n", "")
card_name_file.close()
return flavor_strings
def get_card_names_file(self, locale):
path = self._folder + LOCALISATION_FILE_NAMES[locale]
if not os.path.isfile(path):
print("Couldn't find " + locale + " card file at " + path)
exit()
return path