-
Notifications
You must be signed in to change notification settings - Fork 2
/
heroesinfo.py
458 lines (400 loc) · 17.4 KB
/
heroesinfo.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
__author__ = 'Cristian Orellana <crorella@gmail.com>'
import xml.etree.ElementTree as ET
from os import path
import json
import psycopg2ct as pg
import re
BASE_FILES = {"gamestrings": "mods/heroesdata.stormmod/enus.stormdata/LocalizedData/GameStrings.txt",
"buttondata": "mods/heroesdata.stormmod/base.stormdata/GameData/ButtonData.xml",
"herodata": "mods/heroesdata.stormmod/base.stormdata/GameData/HeroData.xml",
"talentdata": "mods/heroesdata.stormmod/base.stormdata/GameData/TalentData.xml",
"patchdata": "mods/core.stormmod/base.stormdata/DataBuildId.txt"}
HERO_NAMES_FILE = "heroesNames.txt"
PATH = "/Users/crorella/Documents/heroesjson/out/"
HERO_FILES_BASE_PATH = "mods/heroesdata.stormmod/base.stormdata/GameData/Heroes/"
HERO_FILES_ALTERNATE_PATH = "mods/heromods/"
STRING_FILES_ALTERNATE_PATH = "mods/heromods/"
HERO_SPECIFIC_FILES = [
"ThrallData",
"AnubarakData",
"WizardData",
"WitchDoctorData",
"UtherData",
"TinkerData",
"SylvanasData",
"StitchesData",
"RexxarData",
"NecromancerData",
"MurkyData",
"MonkData",
"MedicData",
"LostVikingsData",
"LeoricData",
"KaelthasData",
"JainaData",
"GennData",
"DryadData",
"DemonHunterData",
"CrusaderData",
"ChenData",
"ButcherData",
"AzmodanData",
"ArtanisData",
"TassadarData",
"DiabloData",
"ZeratulData",
"ZagaraData",
"SgtHammerData"
]
ALTERNATE_HERO_SPECIFIC_FILES = [
"thefirelords",
"zuljin",
"zarya",
"varian",
"valeera",
"tracer",
"samuro",
"probius",
"medivh",
"lucio",
"guldan",
"dva",
"dehaka",
"chromie",
"chogall",
"auriel",
"amazon",
"alarak",
"genji",
"malthael",
"kelthuzad",
"ana"
]
data = ET.parse(path.join(PATH, BASE_FILES["herodata"]))
icons = ET.parse(path.join(PATH, BASE_FILES["buttondata"]))
names = open(path.join(PATH, BASE_FILES["gamestrings"]))
talent_names = {}
talents = {}
hero_data = {}
patch = None
hero_names = {}
# Get hero names
# with file(path.join(PATH, HERO_NAMES_FILE)) as hero_names_file:
# build_regex = '([a-zA-Z]+)=([\w\W\'\.\-]+)'
# for line in hero_names_file:
# build = re.search(build_regex, line)
# if build:
# id = build.group(1)
# name = build.group(2)
# hero_names[id] = name
# #print "%s - %s" % (id, name)
# Get build
with file(path.join(PATH, BASE_FILES['patchdata'])) as build_data:
build_regex = '[a-zA-Z]+(\d+)'
for line in build_data:
build = re.search(build_regex, line)
patch = build.group(1)
print "Processing patch: %s" % patch
def get_button_names():
for name in names:
if name.startswith('Button/Name/'):
talent_names[name.split('/')[2].split('=')[0].strip()] = name.split('=')[1].strip()
def get_button_names_heroes(hero):
with file(path.join(PATH, STRING_FILES_ALTERNATE_PATH, hero + ".stormmod/enus.stormdata/LocalizedData/GameStrings.txt")) as strings:
for name in strings:
if name.startswith('Button/Name/'):
talent_names[name.split('/')[2].split('=')[0].strip()] = name.split('=')[1].strip()
def find_talent_id_by_face(face, hero=None):
found_talents = []
for t in talents:
if face in talents[t]['face']:
found_talents.append(t)
return found_talents
def get_talents(hero_root, hero=None):
heroes = hero_root.findall('CHero')
# Get talents for those heroes with specific data files
for h in heroes:
if h is not None:
for talent_tree in h.findall('TalentTreeArray'):
if talent_tree.attrib.get('Talent') not in talents:
hero_name = h.attrib.get('Id', None) or h.attrib.get('id', None)
talents[talent_tree.attrib.get('Talent')] = {}
talents[talent_tree.attrib.get('Talent')][hero_name] = {}
if h.find('HyperlinkId') is not None:
hero_name = h.find('HyperlinkId').attrib.get('value', None)
else:
hero_name = h.attrib.get('Id', None) or h.attrib.get('id', None)
#print "Assigning %s to %s" % (hero_name, talent_tree.attrib.get('Talent'))
if hero_name not in talents[talent_tree.attrib.get('Talent')].keys():
if len([k for k in talents[talent_tree.attrib.get('Talent')].keys() if k not in ['face', 'icon', 'name']]) == 0:
talents[talent_tree.attrib.get('Talent')] = {}
talents[talent_tree.attrib.get('Talent')][hero_name] = {}
talents[talent_tree.attrib.get('Talent')][hero_name]['tier'] = talent_tree.attrib.get('Tier')
talents[talent_tree.attrib.get('Talent')][hero_name]['column'] = talent_tree.attrib.get('Column')
talents[talent_tree.attrib.get('Talent')]['face'] = talents[talent_tree.attrib.get('Talent')].get('face', '')
if hero == 'Alarak':
for talent_tree in h.findall('TalentTreeArray'):
if talent_tree.attrib.get('Talent') not in talents or talents[talent_tree.attrib.get('Talent')].get('tier', None) is None:
talents[talent_tree.attrib.get('Talent')]['Alarak']['tier'] = talent_tree.attrib.get('Tier')
talents[talent_tree.attrib.get('Talent')]['Alarak']['column'] = talent_tree.attrib.get('Column')
def get_talents_faces(hero_root):
"""
This function retrieves the 'faces' used by the button in the talent selection UI.
The face is then used to lookup the icon file displayed in the button
:param hero_root:
:return:
"""
for talent in hero_root.findall('CTalent'):
if talent.attrib.get('id') is not None:
id = talent.attrib.get('id')
if id not in talents.keys():
talents[id] = {}
if talent.find('Face') is not None:
talents[id]['face'] = talent.find('Face').attrib.get('value', 'NA')
def get_talent_icons(hero_root):
"""
This function looks for the Buttons associated with the hero and retrieves the icons for those buttons
used to display talent selection
:param hero_root:
:return:
"""
# Some buttons are stored directly in the heroData.xml file, while others in the ButtonData.xml file
# This for loop is for the buttons stored in the same heroData file
for button in hero_root.findall('CButton'):
if button.attrib.get('id') is not None:
face = button.attrib.get('id')
if button.find('Icon') is not None:
found_talents = find_talent_id_by_face(face)
for t in found_talents:
icon = button.find('Icon').attrib.get('value', 'NA').split('\\')[-1].split('.')[0].lower()
talents[t]['icon'] = icon + '.png'
# This for loop is for the buttons stored in the buttonData file
for icon in icons.findall('CButton'):
if icon.attrib.get('id') is not None:
face = icon.attrib.get('id')
if icon.find('Icon') is not None:
found_talents = find_talent_id_by_face(face)
for t in found_talents:
icon_image = icon.find('Icon').attrib.get('value', 'NA').split('\\')[-1].split('.')[0].lower()
talents[t]['icon'] = icon_image + '.png'
def get_hero_info(hero_root):
"""
This function retrieves specific hero data, like universe, gender, difficulty, names, and so on.
:param hero_root:
:return:
"""
for hero_info in hero_root.findall('CHero'):
friendly_name = None
hero_data[hero_info.attrib.get('id', 'Missing')] = {}
for attribute in ['Role', 'Universe', 'Alignment', 'Rarity', 'SelectScreenButtonImage', 'Gender',
'Difficulty', 'AttributeId']:
if hero_info.find(attribute) is not None:
hero_data[hero_info.attrib.get('id', 'Missing')][attribute] = hero_info.find(attribute).attrib.get(
'value', 'Missing')
if hero_info.find('Ratings') is not None:
# print "\tRatings:"
for rating in ['Damage', 'Utility', 'Complexity', 'Survivability']:
if hero_info.find('Ratings').attrib.get(rating):
hero_data[hero_info.attrib.get('id', 'Missing')][rating] = hero_info.find('Ratings').attrib.get(
rating, 'NA')
elif hero_info.find('Ratings').find(rating) is not None:
hero_data[hero_info.attrib.get('id', 'Missing')][rating] = hero_info.find('Ratings').find(
rating).attrib.get('value', 'NA')
if hero_info.find('HyperlinkId') is not None:
friendly_name = hero_info.find('HyperlinkId').attrib.get('value', None)
hero_data[hero_info.attrib.get('id', 'Missing')]['friendly_name'] = friendly_name or hero_info.attrib.get(
'id', 'Missing')
def get_talent_names():
for name in talent_names:
found_talents = find_talent_id_by_face(name)
for t in found_talents:
talents[t]['name'] = talent_names[name]
def clean_unassigned_talents():
for talent in talents:
for hero in [k for k in talents[talent] if k not in ['face', 'icon', 'name']]:
if 'tier' not in talents[talent][hero].keys():
del talents[talent][hero]
#print talents[t]
def validate_talents():
incomplete_talents = []
for talent in talents:
if (talents[talent].get('icon', None) is None \
and talents[talent].get('hero', None) is None \
and talents[talent].get('column', None) is None \
and talents[talent].get('tier', None) is None) \
or talents[talent].get('face', None) is None \
or talents[talent].get('name', None) is None:
incomplete_talents.append(talents[talent])
try:
assert len(incomplete_talents) == 0
except AssertionError, e:
print "Error, there are %s incomplete talents:" % len(incomplete_talents)
for talent in incomplete_talents:
print "\t%s" % talent
# Get hero data to retrieve talent names and icons
def get_hero_data():
# get hero info for the "Classic" heroes
hero_root = data.getroot()
# Get talents for those heroes with specific data files
get_talents(hero_root)
# Get talent faces for heroes with specific data files
get_talents_faces(hero_root)
# Get talent icons for heroes with specific data files
get_talent_icons(hero_root)
get_hero_info(hero_root)
# First of all, get all the talents in the talentdata.xml file
heroData = ET.parse(path.join(PATH, BASE_FILES['talentdata']))
hero_root = heroData.getroot()
get_talents_faces(hero_root)
get_talent_icons(hero_root)
for hero in [h for h in ALTERNATE_HERO_SPECIFIC_FILES if h == 'alarak']:
print("Processing {0}".format(hero))
# alarak.stormmod / base.stormdata / GameData / AlarakData.xml
heroData = ET.parse(path.join(PATH, HERO_FILES_ALTERNATE_PATH, hero + ".stormmod", "base.stormdata/GameData/HeroData.xml"))
hero_root = heroData.getroot()
# Get talents for those heroes with specific data files
get_talents(hero_root, 'Alarak')
# Get talent faces for heroes with specific data files
get_talents_faces(hero_root)
# Get talent icons for heroes with specific data files
get_talent_icons(hero_root)
get_hero_info(hero_root)
# then get info for those heroes with a special heroData.xml file
for hero in HERO_SPECIFIC_FILES:
print("Processing {0}".format(hero))
heroData = ET.parse(path.join(PATH, HERO_FILES_BASE_PATH, hero, hero + ".xml"))
hero_root = heroData.getroot()
h = hero_root.find('CHero')
# Get talents for those heroes with specific data files5
get_talents(hero_root)
# Get talent faces for heroes with specific data files
get_talents_faces(hero_root)
# Get talent icons for heroes with specific data files
get_talent_icons(hero_root)
# Get hero info
get_hero_info(hero_root)
# Then, get info for heroes in the new heromod folder structure
for hero in ALTERNATE_HERO_SPECIFIC_FILES:
print("Processing {0}".format(hero))
# populate the talent names
get_button_names_heroes(hero)
heroData = ET.parse(path.join(PATH, HERO_FILES_ALTERNATE_PATH, hero + ".stormmod", "base.stormdata/GameData/", hero + "Data.xml"))
hero_root = heroData.getroot()
h = hero_root.find('CHero')
# Get talents for those heroes with specific data files
get_talents(hero_root)
# Get talent faces for heroes with specific data files
get_talents_faces(hero_root)
# Get talent icons for heroes with specific data files
get_talent_icons(hero_root)
get_hero_info(hero_root)
# Do a final pass to retrieve icons for those heroes where the talent data is
# distributed between HeroData.xml and TalentData.xml
for icon in icons.findall('CButton'):
if icon.attrib.get('id') is not None:
face = icon.attrib.get('id')
if icon.find('Icon') is not None:
found_talents = find_talent_id_by_face(face)
for t in found_talents:
icon_image = icon.find('Icon').attrib.get('value', 'NA').split('\\')[-1].split('.')[0].lower()
talents[t]['icon'] = icon_image + '.png'
def save_files():
with file("heroes.json", "w") as heroes_file:
json.dump(hero_data, heroes_file)
with file("talents.json", "w") as talents_file:
json.dump(talents, talents_file)
# move the data to the database
def save_to_db():
try:
rConn = pg.connect(database="hotstats",
user="hotstats",
password="YOUR_PASS",
host="YOUR_SERVER",
port="5432")
cursor = rConn.cursor()
rConn.autocommit = True
except Exception, e:
print "Error while trying to establish connection with database %s" % e
insert_hero = """
INSERT INTO heroes (
id,
patch,
name,
role,
universe,
alignment,
rarity,
portrait_icon,
gender,
difficulty,
damage,
utility,
complexity,
survivability,
attrib_name
)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON CONFLICT ON CONSTRAINT hero_pk DO UPDATE SET
role = excluded.role,
universe = excluded.universe,
alignment = excluded.alignment,
rarity = excluded.rarity,
portrait_icon = excluded.portrait_icon,
gender = excluded.gender,
difficulty = excluded.difficulty,
damage = excluded.damage,
utility = excluded.utility,
complexity = excluded.complexity,
survivability = excluded.survivability,
attrib_name = excluded.attrib_name;
"""
insert_talent = """
INSERT INTO talents (
id,
patch,
name,
hero,
tier,
position,
icon
)
VALUES (%s, %s, %s, %s, %s, %s, %s)
ON CONFLICT ON CONSTRAINT talents_pk DO UPDATE SET
name = excluded.name,
tier = excluded.tier,
position = excluded.position,
icon = excluded.icon
"""
for hero in hero_data:
cursor.execute(insert_hero, [hero,
patch,
hero_data[hero]['friendly_name'],
hero_data[hero].get('Role'),
hero_data[hero].get('Universe'),
hero_data[hero].get('Alignment'),
hero_data[hero].get('Rarity'),
hero_data[hero].get('SelectScreenButtonImage'),
hero_data[hero].get('Gender'),
hero_data[hero].get('Difficulty'),
hero_data[hero].get('Damage', 0),
hero_data[hero].get('Utility', 0),
hero_data[hero].get('Complexity', 0),
hero_data[hero].get('Survivability', 0),
hero_data[hero].get('AttributeId', '').lower()])
insert_talent_statements = []
for talent in talents:
for hero in [k for k in talents[talent] if k not in ['face', 'icon', 'name']]:
cursor.execute(insert_talent, [talent,
patch,
talents[talent].get('name'),
hero,
talents[talent][hero]['tier'],
talents[talent][hero]['column'],
talents[talent].get('icon')])
if __name__ == "__main__":
get_button_names()
get_hero_data()
get_talent_names()
clean_unassigned_talents()
#validate_talents()
save_to_db()