forked from ronoaldo/swgoh
/
datacollections.go
325 lines (311 loc) · 8.79 KB
/
datacollections.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
package swgohhelp
import (
"bytes"
"encoding/json"
"log"
)
// DataPlayerTitle is the data library information about player titles.
type DataPlayerTitle struct {
ID string `json:"id,omitempty"`
Name string `json:"nameKey,omitempty"`
Desc string `json:"descKey,omitempty"`
Details string `json:"shortDescKey,omitempty"`
}
// DataPlayerTitles retrieves the data collection for player titles.
func (c *Client) DataPlayerTitles() (result map[string]DataPlayerTitle, err error) {
cacheKey := "data.playerTitles"
if ok := c.gameData.Get(cacheKey, &result); ok {
return result, err
}
// Prepare data collection call
payload, err := json.Marshal(map[string]interface{}{
"collection": "playerTitleList",
"language": "eng_us",
"match": map[string]interface{}{
"hidden": false,
"obtainable": true,
},
})
if err != nil {
return nil, err
}
// Parse result
resp, err := c.call("POST", "/swgoh/data", "application/json", bytes.NewReader(payload))
if err != nil {
return
}
defer resp.Body.Close()
values := make([]DataPlayerTitle, 0)
err = json.NewDecoder(resp.Body).Decode(&values)
if err != nil {
return
}
// Prepare response as map for easier usage
result = make(map[string]DataPlayerTitle)
for i := range values {
result[values[i].ID] = values[i]
}
// Cache prepared response map
c.gameData.Put(cacheKey, &result)
log.Printf("swgohhelp: saving cache for updated titles")
return
}
// DataUnitAbility is the ability display name and icon
type DataUnitAbility struct {
ID string `json:"id,omitempty"`
Name string `json:"nameKey,omitempty"`
Icon string `json:"icon,omitempty"`
}
// DataUnitAbilities returns a map of ability IDs to their descriptions.
func (c *Client) DataUnitAbilities() (result map[string]DataUnitAbility, err error) {
cacheKey := "data.unitAbilities"
if ok := c.gameData.Get(cacheKey, &result); ok {
return result, err
}
// Prepare data collection call
payload, err := json.Marshal(map[string]interface{}{
"collection": "abilityList",
"language": "eng_us",
"project": map[string]int{
"id": 1,
"nameKey": 1,
"coolDown": 1,
"icon": 1,
"descriptiveTagList": 1,
"aiParams": 1,
"abilityReference": 1,
"skillType": 1,
"isZeta": 1,
},
})
if err != nil {
return nil, err
}
// Parse result
resp, err := c.call("POST", "/swgoh/data", "application/json", bytes.NewReader(payload))
if err != nil {
return
}
defer resp.Body.Close()
values := make([]DataUnitAbility, 0)
err = json.NewDecoder(resp.Body).Decode(&values)
if err != nil {
return
}
// Prepare response as map for easier usage
result = make(map[string]DataUnitAbility)
for i := range values {
result[values[i].ID] = values[i]
}
log.Printf("swgohhelp: saving cache for updated abilities")
c.gameData.Put(cacheKey, &result)
return
}
// DataUnitSkill is the map for units and their abilities
type DataUnitSkill struct {
ID string `json:"id,omitempty"`
AbilityID string `json:"abilityReference,omitempty"`
Type int `json:"skillType,omitempty"`
IsZeta bool `json:"isZeta,omitempty"`
}
// DataUnitSkills returns a map of skill IDs to their ability IDs.
func (c *Client) DataUnitSkills() (result map[string]DataUnitSkill, err error) {
cacheKey := "data.unitSkills"
if ok := c.gameData.Get(cacheKey, &result); ok {
return result, nil
}
// Prepare data collection call
payload, err := json.Marshal(map[string]interface{}{
"collection": "skillList",
"language": "eng_us",
"project": map[string]int{
"id": 1,
"abilityReference": 1,
"isZeta": 1,
},
})
if err != nil {
return nil, err
}
// Parse result
resp, err := c.call("POST", "/swgoh/data", "application/json", bytes.NewReader(payload))
if err != nil {
return
}
defer resp.Body.Close()
values := make([]DataUnitSkill, 0)
err = json.NewDecoder(resp.Body).Decode(&values)
if err != nil {
return
}
// Prepare response as map for easier usage
result = make(map[string]DataUnitSkill)
for i := range values {
result[values[i].ID] = values[i]
}
log.Printf("swgohhelp: saving cache for updated skills")
c.gameData.Put(cacheKey, &result)
return
}
// DataUnitCategory is the category "tags" labels for characters and ships.
type DataUnitCategory struct {
ID string `json:"id,omitempty"`
Name string `json:"descKey,omitempty"`
Visible bool `json:"visible"`
}
// DataUnitCategories returns a map of category IDs to their descriptions.
func (c *Client) DataUnitCategories() (result map[string]DataUnitCategory, err error) {
cacheKey := "data.unitCategories"
if ok := c.gameData.Get(cacheKey, &result); ok {
return result, nil
}
// Prepare data collection call
payload, err := json.Marshal(map[string]interface{}{
"collection": "categoryList",
"language": "eng_us",
"match": map[string]interface{}{
"visible": true,
},
"project": map[string]int{
"id": 1,
"descKey": 1,
"visible": 1,
},
})
if err != nil {
return nil, err
}
// Parse result
resp, err := c.call("POST", "/swgoh/data", "application/json", bytes.NewReader(payload))
if err != nil {
return
}
defer resp.Body.Close()
values := make([]DataUnitCategory, 0)
err = json.NewDecoder(resp.Body).Decode(&values)
if err != nil {
return
}
// Prepare response as map for easier usage
result = make(map[string]DataUnitCategory)
for i := range values {
result[values[i].ID] = values[i]
}
log.Printf("swgohhelp: saving cache for updated categoryList")
c.gameData.Put(cacheKey, &result)
return
}
// DataUnit is the unit basic data info.
type DataUnit struct {
ID string `json:"baseId,omitempty"`
Name string `json:"nameKey,omitempty"`
MaxRarity int `json:"maxRarity,omitempty"`
ForceAlignment int `json:"forceAlignment,omitempty"`
CombatType int `json:"combatType,omitempty"`
CombatTypeName string `json:"combatTypeName,omitempty"`
CategoryRefs []string `json:"categoryIdList,omitempty"`
Categories []string `json:"categoryList,omitempty"`
SkillRefs []DataUnitSkillList `json:"skillReferenceList,omitempty"`
Skills []UnitSkill `json:"skillList,omitempty"`
}
// DataUnitSkillList is an unit skill identifier and requirements.
type DataUnitSkillList struct {
ID string `json:"skillId,omitempty"`
RequiredTier int `json:"requiredTier,omitempty"`
RequiredRarity int `json:"requiredRarity,omitempty"`
}
// DataUnits returns a map of unit IDs to their details in game.
func (c *Client) DataUnits() (result map[string]DataUnit, err error) {
cacheKey := "data.units"
if ok := c.gameData.Get(cacheKey, &result); ok {
return result, err
}
// Prepare data collection call
payload, err := json.Marshal(map[string]interface{}{
"collection": "unitsList",
"language": "eng_us",
"match": map[string]interface{}{
"rarity": 7,
"obtainable": true,
},
"project": map[string]int{
"id": 1,
"thumbnailName": 1,
"nameKey": 1,
"maxRarity": 1,
"categoryIdList": 1,
"forceAlignment": 1,
"skillReferenceList": 1,
"baseId": 1,
"combatType": 1,
},
})
if err != nil {
return nil, err
}
// Parse result
resp, err := c.call("POST", "/swgoh/data", "application/json", bytes.NewReader(payload))
if err != nil {
return
}
defer resp.Body.Close()
values := make([]DataUnit, 0)
err = json.NewDecoder(resp.Body).Decode(&values)
if err != nil {
return
}
// Prepare response as map for easier usage
categories, err := c.DataUnitCategories()
if err != nil {
return
}
skills, err := c.DataUnitSkills()
if err != nil {
return
}
abilities, err := c.DataUnitAbilities()
if err != nil {
return
}
result = make(map[string]DataUnit)
for i := range values {
switch values[i].CombatType {
case 1:
values[i].CombatTypeName = "Character"
case 2:
values[i].CombatTypeName = "Ship"
}
switch values[i].ForceAlignment {
case 2:
values[i].Categories = append(values[i].Categories, "Light Side")
case 3:
values[i].Categories = append(values[i].Categories, "Dark Side")
}
for j := range values[i].CategoryRefs {
ref := values[i].CategoryRefs[j]
if category, ok := categories[ref]; ok {
if category.Name == "Placeholder" {
continue
}
values[i].Categories = append(values[i].Categories, category.Name)
}
}
for j := range values[i].SkillRefs {
ref := values[i].SkillRefs[j]
if skill, ok := skills[ref.ID]; ok {
if ability, ok := abilities[skill.AbilityID]; ok {
unitSkill := UnitSkill{
ID: skill.ID,
Name: ability.Name,
IsZeta: skill.IsZeta,
}
values[i].Skills = append(values[i].Skills, unitSkill)
}
}
}
result[values[i].ID] = values[i]
}
log.Printf("swgohhelp: saving cache for updated unitList")
c.gameData.Put(cacheKey, &result)
return
}