-
Notifications
You must be signed in to change notification settings - Fork 2
/
staticimporter.go
507 lines (456 loc) · 12.9 KB
/
staticimporter.go
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package main
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"sync"
)
const pvpFeatsOfStrengthCategory int = 15270
var achievementIDs = []int{
// 2v2
399, 400, 401, 1159,
// 3v3
402, 403, 405, 1160, 5266, 5267, 2091,
// RBG achievements
5341, 5355, 5343, 5356, 6942, 6941,
}
var realmRegions = []string{"EU", "US", "KR", "TW"}
func importStaticData() {
logger.Println("Beginning import of static data")
for _, r := range realmRegions {
importRealms(r)
}
importRaces()
importClasses()
importSpecs()
importTalents()
importPvPTalents()
importAchievements()
logger.Println("Static data import complete")
}
func parseRealms(data *[]byte) []realm {
type Realms struct {
Realms []realm
}
var realms Realms
err := safeUnmarshal(data, &realms)
if err != nil {
logger.Printf("%s parsing realms failed: %s", warnPrefix, err)
return make([]realm, 0)
}
return realms.Realms
}
func importRealms(region string) {
var realmJSON *[]byte = getDynamic(region, "realm/index")
var realms []realm = parseRealms(realmJSON)
logger.Printf("Found %d %s realms", len(realms), region)
addRealms(&realms, region)
}
func parseRaces(data *[]byte) []race {
type Races struct {
Races []race
}
var races Races
err := safeUnmarshal(data, &races)
if err != nil {
logger.Printf("%s parsing races failed: %s", warnPrefix, err)
return make([]race, 0)
}
return races.Races
}
func importRaces() {
var racesJSON *[]byte = getStatic(region, "playable-race/index")
var races []race = parseRaces(racesJSON)
logger.Printf("Found %d races", len(races))
addRaces(&races)
}
func parseClasses(data *[]byte) []class {
type Classes struct {
Classes []class
}
var classes Classes
err := safeUnmarshal(data, &classes)
if err != nil {
logger.Printf("%s parsing classes failed: %s", warnPrefix, err)
return make([]class, 0)
}
return classes.Classes
}
func importClasses() {
var classesJSON *[]byte = getStatic(region, "playable-class/index")
var classes []class = parseClasses(classesJSON)
logger.Printf("Found %d classes", len(classes))
addClasses(&classes)
}
func importSpecs() {
var specsJSON *[]byte = getStatic(region, "playable-specialization/index")
var specs []spec = parseSpecs(specsJSON)
logger.Printf("Found %d specializations", len(specs))
addSpecs(&specs)
}
func parseSpecs(data *[]byte) []spec {
type CharacterSpecializationJSON struct {
ID int
}
type SpecsJSON struct {
CharacterSpecializations []CharacterSpecializationJSON `json:"character_specializations"`
}
var specsJSON SpecsJSON
err := safeUnmarshal(data, &specsJSON)
if err != nil {
logger.Printf("%s parsing specs failed: %s", warnPrefix, err)
return make([]spec, 0)
}
var specIDs []int = make([]int, 0)
for _, cs := range specsJSON.CharacterSpecializations {
specIDs = append(specIDs, cs.ID)
}
return getFullSpecInfo(specIDs)
}
func getFullSpecInfo(specIDs []int) []spec {
var specs []spec = make([]spec, 0)
var ch chan spec = make(chan spec, len(specIDs))
for _, i := range specIDs {
go getSpec(ch, i)
}
for range specIDs {
specs = append(specs, <-ch)
}
return specs
}
func getSpec(ch chan spec, specID int) {
type RoleJSON struct {
Role string `json:"type"`
}
type SpecJSON struct {
ID int
PlayableClass keyedValue `json:"playable_class"`
Name string
Media keyedValue
Role RoleJSON
PvpTalents []interface{} `json:"pvp_talents"`
TalentTree keyedValue `json:"spec_talent_tree"`
}
var path string = fmt.Sprintf("playable-specialization/%d", specID)
var icon = getIcon(region, path)
var specJSON *[]byte = getStatic(region, path)
var s SpecJSON
safeUnmarshal(specJSON, &s)
ch <- spec{
s.ID,
s.PlayableClass.ID,
s.Name,
s.Role.Role,
icon}
}
func importTalents() {
var paths = getTalentTreePaths()
talentMap := make(map[int]talent)
for _, path := range paths {
treeTalents := getTalentsFromTree(path)
for _, talent := range treeTalents {
talentMap[talent.ID] = talent
}
}
talents := make([]talent, len(talentMap))
var waitGroup sync.WaitGroup
i := 0
for _, t := range talentMap {
waitGroup.Add(1)
go func(i int, tal talent) {
defer waitGroup.Done()
icon := getIcon(region, fmt.Sprintf("spell/%d", tal.SpellID))
talentWithIcon := talent{
tal.ID,
tal.SpellID,
tal.ClassID,
tal.SpecID,
tal.Name,
icon,
tal.NodeID,
tal.Row,
tal.Col,
tal.Cat,
tal.HeroSpecs}
talents[i] = talentWithIcon
}(i, t)
i++
}
waitGroup.Wait()
addTalents(&talents)
}
func getTalentTreePaths() []string {
paths := make(map[string]string)
type TalentTreeJSON struct {
Key key
Name string
}
type TalentTreesJSON struct {
SpecTalentTrees []TalentTreeJSON `json:"spec_talent_trees"`
ClassTalentTrees []TalentTreeJSON `json:"class_talent_trees"`
HeroTalentTrees []TalentTreeJSON `json:"hero_talent_trees"`
}
var talentTreesJSON *[]byte = getStatic(region, "talent-tree/index")
var talentTreePaths TalentTreesJSON
err := safeUnmarshal(talentTreesJSON, &talentTreePaths)
if err != nil {
logger.Printf("%s parsing talent trees failed: %s", warnPrefix, err)
return []string{}
}
for _, talentTree := range talentTreePaths.SpecTalentTrees {
path := parseSpecTalentTreePath(talentTree.Key.Href)
if path != "" {
paths[path] = talentTree.Name
}
}
return getBestPaths(paths)
}
func getBestPaths(paths map[string]string) []string {
sortedPaths := make([]string, 0, len(paths))
for k := range paths {
sortedPaths = append(sortedPaths, k)
}
sort.Strings(sortedPaths)
pathsBySpec := make(map[int]string)
for _, path := range sortedPaths {
pathParts := strings.Split(path, "/")
spec, err := strconv.Atoi(pathParts[3])
if err != nil {
continue
}
pathVersion, _ := strconv.Atoi(pathParts[1])
if _, exists := pathsBySpec[spec]; exists {
existingParts := strings.Split(pathsBySpec[spec], "/")
existingVersion, _ := strconv.Atoi(existingParts[1])
if pathVersion > existingVersion {
pathsBySpec[spec] = path
}
} else {
pathsBySpec[spec] = path
}
}
// Priest spec tree returned by Blizzard's API is
// bugged above v795 so we'll need to hardcode that
pathsBySpec[258] = "talent-tree/795/playable-specialization/258"
bestPaths := make([]string, 0, len(pathsBySpec))
for _, path := range pathsBySpec {
bestPaths = append(bestPaths, path)
}
sort.Strings(bestPaths)
return bestPaths
}
func parseSpecTalentTreePath(href string) string {
return parseTalentTreePath(href, `\btalent-tree/[0-9]+/playable-specialization/[0-9]+`)
}
func parseTalentTreePath(href string, regexpPattern string) string {
pattern := regexp.MustCompile(regexpPattern)
match := pattern.Find([]byte(href))
if match == nil {
logger.Printf("%s Talent tree path not found in %s", warnPrefix, href)
return ""
}
return string(match)
}
func getTalentsFromTree(path string) []talent {
type TalentTreeJSON struct {
Class keyedValue `json:"playable_class"`
Spec keyedValue `json:"playable_specialization"`
ClassTalents []TalentNodeJSON `json:"class_talent_nodes"`
SpecTalents []TalentNodeJSON `json:"spec_talent_nodes"`
HeroTrees []HeroTreeJSON `json:"hero_talent_trees"`
}
var talentTreeJSON *[]byte = getStatic(region, path)
var talentTree TalentTreeJSON
err := safeUnmarshal(talentTreeJSON, &talentTree)
if err != nil {
logger.Printf("%s parsing talents failed: %s", warnPrefix, err)
return []talent{}
}
var talents []talent = make([]talent, 0)
if len(talentTree.SpecTalents) == 0 {
return talents
}
classTalents := parseTalents(talentTree.Class.ID, 0, "CLASS", []int{}, talentTree.ClassTalents)
talents = append(talents, classTalents...)
specTalents := parseTalents(talentTree.Class.ID, talentTree.Spec.ID, "SPEC", []int{}, talentTree.SpecTalents)
talents = append(talents, specTalents...)
for _, tree := range talentTree.HeroTrees {
spec_ids := []int{}
for _, spec := range tree.Specs {
spec_ids = append(spec_ids, spec.ID)
}
heroTalents := parseTalents(talentTree.Class.ID, 0, "HERO", spec_ids, tree.HeroTalentNodes)
talents = append(talents, heroTalents...)
}
return talents
}
func parseTalents(
classID int,
specID int,
category string,
heroSpecs []int,
talentNodes []TalentNodeJSON) []talent {
var talents []talent = make([]talent, 0)
for _, node := range talentNodes {
tooltips := extractTooltips(node.Ranks)
for _, tooltip := range tooltips {
spellID := tooltip.SpellTooltip.Spell.ID
if spellID == 0 {
continue
}
talent := talent{
tooltip.Talent.ID,
spellID,
classID,
specID,
tooltip.SpellTooltip.Spell.Name,
"",
node.ID,
node.Row,
node.Col,
category,
heroSpecs}
talents = append(talents, talent)
}
}
return talents
}
func extractTooltips(ranks []RankJSON) []TooltipJSON {
var tooltips []TooltipJSON = make([]TooltipJSON, 0)
if len(ranks) == 0 {
return tooltips
}
head := ranks[0]
if head.Tooltip.Talent.ID != 0 {
tooltips = append(tooltips, head.Tooltip)
} else if len(head.Choice) > 0 {
return head.Choice
}
return tooltips
}
func importPvPTalents() {
var talentsJSON *[]byte = getStatic(region, "pvp-talent/index")
var pvpTalents []pvpTalent = parsePvPTalents(talentsJSON)
logger.Printf("Found %d PvP Talents", len(pvpTalents))
addPvPTalents(&pvpTalents)
}
func parsePvPTalents(data *[]byte) []pvpTalent {
type PvPTalentsJSON struct {
PvPTalents []keyedValue `json:"pvp_talents"`
}
var pvpTalentsJSON PvPTalentsJSON
err := safeUnmarshal(data, &pvpTalentsJSON)
if err != nil {
logger.Printf("%s parsing pvp talents failed: %s", warnPrefix, err)
return make([]pvpTalent, 0)
}
var pvpTalents []pvpTalent = make([]pvpTalent, 0)
var ch chan pvpTalent = make(chan pvpTalent, len(pvpTalentsJSON.PvPTalents))
for _, keyedValue := range pvpTalentsJSON.PvPTalents {
go getPvPTalent(ch, keyedValue.ID)
}
for range pvpTalentsJSON.PvPTalents {
pvpTalents = append(pvpTalents, <-ch)
}
return pvpTalents
}
func getPvPTalent(ch chan pvpTalent, id int) {
type PvPTalentJSON struct {
Spell keyedValue
PlayableSpecialization keyedValue `json:"playable_specialization"`
}
var pvpTalentJSON *[]byte = getStatic(region, fmt.Sprintf("pvp-talent/%d", id))
var talentDetails PvPTalentJSON
safeUnmarshal(pvpTalentJSON, &talentDetails)
icon := getIcon(region, fmt.Sprintf("spell/%d", talentDetails.Spell.ID))
ch <- pvpTalent{
id,
talentDetails.Spell.Name,
talentDetails.Spell.ID,
talentDetails.PlayableSpecialization.ID,
icon}
}
func parseAchievements(data *[]byte) []achievement {
type Achievements struct {
ID int
Name string
Achievements []keyedValue
}
var achievements Achievements
err := safeUnmarshal(data, &achievements)
var pvpAchievements []achievement = make([]achievement, 0)
if err != nil {
logger.Printf("%s parsing achievements failed: %s", warnPrefix, err)
return pvpAchievements
}
var achievementIDs []int = make([]int, 0)
for _, ac := range achievements.Achievements {
if strings.Contains(ac.Name, "Season") {
achievementIDs = append(achievementIDs, ac.ID)
}
}
var ch chan achievement = make(chan achievement, len(achievementIDs))
for _, id := range achievementIDs {
go getPvPAchievement(ch, id)
}
for range achievementIDs {
pvpAchievements = append(pvpAchievements, <-ch)
}
return pvpAchievements
}
func getPvPAchievement(ch chan achievement, id int) {
ch <- getAchievement(id)
}
func getAchievement(id int) achievement {
type PvPAchievementJSON struct {
ID int
Name string
Description string
}
var pvpAchievementJSON *[]byte = getStatic(region, fmt.Sprintf("achievement/%d", id))
var pvpAchievementJSONDetails PvPAchievementJSON
safeUnmarshal(pvpAchievementJSON, &pvpAchievementJSONDetails)
icon := getIcon(region, fmt.Sprintf("achievement/%d", id))
return achievement{
id,
pvpAchievementJSONDetails.Name,
pvpAchievementJSONDetails.Description,
icon}
}
func importAchievements() {
var achievementsJSON *[]byte = getStatic(region, fmt.Sprintf("achievement-category/%d", pvpFeatsOfStrengthCategory))
var achievements []achievement = parseAchievements(achievementsJSON)
var seasonalCount int = len(achievements)
logger.Printf("Found %d seasonal achievements", seasonalCount)
for _, id := range achievementIDs {
achievement := getAchievement(id)
achievements = append(achievements, achievement)
}
logger.Printf("Found %d non-seasonal achievements", len(achievements)-seasonalCount)
addAchievements(&achievements)
}
type SpellTooltipJSON struct {
Spell keyedValue
}
type TooltipJSON struct {
Talent keyedValue
SpellTooltip SpellTooltipJSON `json:"spell_tooltip"`
}
type RankJSON struct {
Rank int
Tooltip TooltipJSON
Choice []TooltipJSON `json:"choice_of_tooltips"`
}
type TalentNodeJSON struct {
ID int
Row int `json:"display_row"`
Col int `json:"display_col"`
Ranks []RankJSON
}
type HeroTreeJSON struct {
ID int
HeroTalentNodes []TalentNodeJSON `json:"hero_talent_nodes"`
Specs []keyedValue `json:"playable_specializations"`
}