-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.go
575 lines (500 loc) · 16.2 KB
/
types.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package main
import (
"fmt"
"github.com/charmbracelet/log"
mapping "github.com/dofusdude/dodumap"
"github.com/hashicorp/go-memdb"
)
type ApiImageUrls struct {
Icon string `json:"icon"`
Sd string `json:"sd,omitempty"`
Hq string `json:"hq,omitempty"`
Hd string `json:"hd,omitempty"`
}
func RenderImageUrls(urls []string) ApiImageUrls {
if len(urls) == 0 {
return ApiImageUrls{}
}
var res ApiImageUrls
res.Icon = urls[0]
if len(urls) > 1 {
res.Sd = urls[1]
}
if len(urls) > 2 {
res.Hq = urls[2]
}
if len(urls) > 3 {
res.Hd = urls[3]
}
return res
}
type ApiAllSearchItem struct {
Type *ApiType `json:"type,omitempty"`
Level *int `json:"level,omitempty"`
ImageUrls *ApiImageUrls `json:"image_urls,omitempty"`
}
type ApiAllSearchResultType struct {
NameId string `json:"name_id"`
}
type ApiAllSearchResult struct {
Name string `json:"name"`
Id int `json:"ankama_id"`
Type ApiAllSearchResultType `json:"type"`
ItemFields *ApiAllSearchItem `json:"item_fields,omitempty"`
}
type ApiAllSearchResultScore struct {
Result ApiAllSearchResult `json:"result"`
Score float64 `json:"score"`
}
type ApiEffect struct {
MinInt int `json:"int_minimum"`
MaxInt int `json:"int_maximum"`
Type ApiEffectType `json:"type"`
IgnoreMinInt bool `json:"ignore_int_min"`
IgnoreMaxInt bool `json:"ignore_int_max"`
Formatted string `json:"formatted"`
}
func RenderEffects(effects *[]mapping.MappedMultilangEffect, lang string) []ApiEffect {
var retEffects []ApiEffect
for _, effect := range *effects {
retEffects = append(retEffects, ApiEffect{
MinInt: effect.Min,
MaxInt: effect.Max,
IgnoreMinInt: effect.IsMeta || effect.MinMaxIrrelevant == -2,
IgnoreMaxInt: effect.IsMeta || effect.MinMaxIrrelevant <= -1,
Type: ApiEffectType{
Name: effect.Type[lang],
Id: effect.ElementId,
IsMeta: effect.IsMeta,
IsActive: effect.Active,
},
Formatted: effect.Templated[lang],
})
}
if len(retEffects) > 0 {
return retEffects
}
return nil
}
type ApiCondition struct {
Operator string `json:"operator"`
IntValue int `json:"int_value"`
Element ApiConditionType `json:"element"`
}
type ApiConditionNode struct {
Condition *ApiCondition `json:"condition,omitempty"`
IsOperand bool `json:"is_operand"`
Relation *string `json:"relation,omitempty"` // "and" or "or"
Children []*ApiConditionNode `json:"children,omitempty"`
}
type APIResource struct {
Id int `json:"ankama_id"`
Name string `json:"name"`
Description string `json:"description"`
Type ApiType `json:"type"`
Level int `json:"level"`
Pods int `json:"pods"`
ImageUrls ApiImageUrls `json:"image_urls,omitempty"`
Effects []ApiEffect `json:"effects,omitempty"`
Conditions *ApiConditionNode `json:"conditions,omitempty"`
Recipe []APIRecipe `json:"recipe,omitempty"`
}
func RenderResource(item *mapping.MappedMultilangItemUnity, lang string) APIResource {
resource := APIResource{
Id: item.AnkamaId,
Name: item.Name[lang],
Type: ApiType{
Name: item.Type.Name[lang],
Id: item.Type.ItemTypeId,
},
Description: item.Description[lang],
Level: item.Level,
Pods: item.Pods,
ImageUrls: RenderImageUrls(ImageUrls(item.IconId, "item", ItemImgResolutions)),
Recipe: nil,
}
resource.Conditions = RenderConditionTree(item.Conditions, lang)
effects := RenderEffects(&item.Effects, lang)
if len(effects) == 0 {
resource.Effects = nil
} else {
resource.Effects = effects
}
return resource
}
type APIEquipment struct {
Id int `json:"ankama_id"`
Name string `json:"name"`
Description string `json:"description"`
Type ApiType `json:"type"`
IsWeapon bool `json:"is_weapon"`
Level int `json:"level"`
Pods int `json:"pods"`
ImageUrls ApiImageUrls `json:"image_urls,omitempty"`
Effects []ApiEffect `json:"effects,omitempty"`
Conditions *ApiConditionNode `json:"conditions,omitempty"`
Recipe []APIRecipe `json:"recipe,omitempty"`
ParentSet *APISetReverseLink `json:"parent_set,omitempty"`
}
func RenderEquipment(item *mapping.MappedMultilangItemUnity, lang string) APIEquipment {
var setLink *APISetReverseLink = nil
if item.HasParentSet {
setLink = &APISetReverseLink{
Id: item.ParentSet.Id,
Name: item.ParentSet.Name[lang],
}
}
equip := APIEquipment{
Id: item.AnkamaId,
Name: item.Name[lang],
Type: ApiType{
Name: item.Type.Name[lang],
Id: item.Type.ItemTypeId,
},
Description: item.Description[lang],
Level: item.Level,
Pods: item.Pods,
ImageUrls: RenderImageUrls(ImageUrls(item.IconId, "item", ItemImgResolutions)),
IsWeapon: false,
Recipe: nil,
ParentSet: setLink,
}
equip.Conditions = RenderConditionTree(item.Conditions, lang)
effects := RenderEffects(&item.Effects, lang)
if len(effects) == 0 {
equip.Effects = nil
} else {
equip.Effects = effects
}
return equip
}
type APIRange struct {
Min int `json:"min"`
Max int `json:"max"`
}
type APISetReverseLink struct {
Id int `json:"id"`
Name string `json:"name"`
}
type APIWeapon struct {
Id int `json:"ankama_id"`
Name string `json:"name"`
Description string `json:"description"`
Type ApiType `json:"type"`
IsWeapon bool `json:"is_weapon"`
Level int `json:"level"`
Pods int `json:"pods"`
ImageUrls ApiImageUrls `json:"image_urls,omitempty"`
Effects []ApiEffect `json:"effects,omitempty"`
Conditions *ApiConditionNode `json:"conditions,omitempty"`
CriticalHitProbability int `json:"critical_hit_probability"`
CriticalHitBonus int `json:"critical_hit_bonus"`
MaxCastPerTurn int `json:"max_cast_per_turn"`
ApCost int `json:"ap_cost"`
Range APIRange `json:"range"`
Recipe []APIRecipe `json:"recipe,omitempty"`
ParentSet *APISetReverseLink `json:"parent_set,omitempty"`
}
func RenderWeapon(item *mapping.MappedMultilangItemUnity, lang string) APIWeapon {
var setLink *APISetReverseLink = nil
if item.HasParentSet {
setLink = &APISetReverseLink{
Id: item.ParentSet.Id,
Name: item.ParentSet.Name[lang],
}
}
weapon := APIWeapon{
Id: item.AnkamaId,
Name: item.Name[lang],
Type: ApiType{
Name: item.Type.Name[lang],
Id: item.Type.ItemTypeId,
},
Description: item.Description[lang],
Level: item.Level,
Pods: item.Pods,
ImageUrls: RenderImageUrls(ImageUrls(item.IconId, "item", ItemImgResolutions)),
Recipe: nil,
CriticalHitBonus: item.CriticalHitBonus,
CriticalHitProbability: item.CriticalHitProbability,
MaxCastPerTurn: item.MaxCastPerTurn,
ApCost: item.ApCost,
Range: APIRange{
Min: item.MinRange,
Max: item.Range,
},
IsWeapon: true,
ParentSet: setLink,
}
weapon.Conditions = RenderConditionTree(item.Conditions, lang)
effects := RenderEffects(&item.Effects, lang)
if len(effects) == 0 {
weapon.Effects = nil
} else {
weapon.Effects = effects
}
return weapon
}
type MappedMultilangCondition struct {
Element string `json:"element"`
Operator string `json:"operator"`
Value int `json:"value"`
Templated map[string]string `json:"templated"`
}
func buildAPIConditionTree(out **ApiConditionNode, root *mapping.ConditionTreeNodeMapped, lang string) {
if root == nil {
return
}
if *out == nil {
*out = new(ApiConditionNode)
}
(*out).IsOperand = root.IsOperand
if root.IsOperand {
(*out).Condition = &ApiCondition{
Operator: root.Value.Operator,
IntValue: root.Value.Value,
Element: ApiConditionType{
Name: root.Value.Templated[lang],
Id: root.Value.ElementId,
},
}
return
} else {
(*out).Relation = root.Relation
(*out).Children = make([]*ApiConditionNode, len(root.Children))
for i, child := range root.Children {
childOut := new(*ApiConditionNode)
buildAPIConditionTree(childOut, child, lang)
(*out).Children[i] = *childOut
}
return
}
}
func RenderConditionTree(conditions *mapping.ConditionTreeNodeMapped, lang string) *ApiConditionNode {
retConditionTree := new(*ApiConditionNode)
buildAPIConditionTree(retConditionTree, conditions, lang)
return *retConditionTree
}
func RenderConditions(conditions *[]mapping.MappedMultilangCondition, lang string) []ApiCondition {
var retConditions []ApiCondition
for _, condition := range *conditions {
retConditions = append(retConditions, ApiCondition{
Operator: condition.Operator,
IntValue: condition.Value,
Element: ApiConditionType{
Name: condition.Templated[lang],
Id: condition.ElementId,
},
})
}
if len(retConditions) > 0 {
return retConditions
}
return nil
}
type ApiType struct {
Name string `json:"name"`
Id int `json:"id"`
}
type ApiConditionType struct {
Name string `json:"name"`
Id int `json:"id"`
}
type ApiEffectType struct {
Name string `json:"name"`
Id int `json:"id"`
IsMeta bool `json:"is_meta"`
IsActive bool `json:"is_active"`
}
type APIListItem struct {
Id int `json:"ankama_id"`
Name string `json:"name"`
Type ApiType `json:"type"`
Level int `json:"level"`
ImageUrls ApiImageUrls `json:"image_urls,omitempty"`
// extra fields
Description *string `json:"description,omitempty"`
Recipe []APIRecipe `json:"recipe,omitempty"`
Conditions *ApiConditionNode `json:"conditions,omitempty"`
Effects []ApiEffect `json:"effects,omitempty"`
// extra equipment
IsWeapon *bool `json:"is_weapon,omitempty"`
Pods *int `json:"pods,omitempty"`
ParentSet *APISetReverseLink `json:"parent_set,omitempty"`
// extra weapon
CriticalHitProbability *int `json:"critical_hit_probability,omitempty"`
CriticalHitBonus *int `json:"critical_hit_bonus,omitempty"`
MaxCastPerTurn *int `json:"max_cast_per_turn,omitempty"`
ApCost *int `json:"ap_cost,omitempty"`
Range *APIRange `json:"range,omitempty"`
}
func RenderItemListEntry(item *mapping.MappedMultilangItemUnity, lang string) APIListItem {
return APIListItem{
Id: item.AnkamaId,
Name: item.Name[lang],
Type: ApiType{
Name: item.Type.Name[lang],
Id: item.Type.ItemTypeId,
},
Level: item.Level,
ImageUrls: RenderImageUrls(ImageUrls(item.IconId, "item", ItemImgResolutions)),
}
}
type APIListItemType struct {
Id int `json:"ankama_id"`
NameId string `json:"name_id"` // not translated
}
type APIListTypedItem struct {
Id int `json:"ankama_id"`
Name string `json:"name"`
Type ApiType `json:"type"`
ItemSubtype APIListItemType `json:"item_subtype"`
Level int `json:"level"`
ImageUrls ApiImageUrls `json:"image_urls,omitempty"`
}
func RenderTypedItemListEntry(item *mapping.MappedMultilangItemUnity, lang string) APIListTypedItem {
return APIListTypedItem{
Id: item.AnkamaId,
Name: item.Name[lang],
Type: ApiType{
Name: item.Type.Name[lang],
Id: item.Type.ItemTypeId,
},
ItemSubtype: APIListItemType{
Id: item.Type.CategoryId,
NameId: CategoryIdApiMapping(item.Type.CategoryId),
},
Level: item.Level,
ImageUrls: RenderImageUrls(ImageUrls(item.IconId, "item", ItemImgResolutions)),
}
}
func RenderMountListEntry(mount *mapping.MappedMultilangMount, lang string) APIMount {
return APIMount{
Id: mount.AnkamaId,
Name: mount.Name[lang],
ImageUrls: RenderImageUrls(ImageUrls(mount.AnkamaId, "mount", MountImgResolutions)),
Family: APIMountFamily{
Id: mount.FamilyId,
Name: mount.FamilyName[lang],
},
}
}
type APIRecipe struct {
AnkamaId int `json:"item_ankama_id"`
ItemType string `json:"item_subtype"`
Quantity int `json:"quantity"`
}
func RenderRecipe(recipe mapping.MappedMultilangRecipe, db *memdb.MemDB) []APIRecipe {
if len(recipe.Entries) == 0 {
return nil
}
txn := db.Txn(false)
defer txn.Abort()
var apiRecipes []APIRecipe
for _, entry := range recipe.Entries {
raw, err := txn.First(fmt.Sprintf("%s-%s", CurrentRedBlueVersionStr(Version.MemDb), "all_items"), "id", entry.ItemId)
if err != nil {
log.Error(err)
return nil
}
item := raw.(*mapping.MappedMultilangItemUnity)
apiRecipes = append(apiRecipes, APIRecipe{
AnkamaId: entry.ItemId,
Quantity: entry.Quantity,
ItemType: CategoryIdApiMapping(item.Type.CategoryId),
})
}
return apiRecipes
}
type APIPageItem struct {
Links PaginationLinks `json:"_links,omitempty"`
Items []APIListItem `json:"items"`
}
type APIPageMount struct {
Links PaginationLinks `json:"_links,omitempty"`
Items []APIMount `json:"mounts"`
}
type APIPageSet struct {
Links PaginationLinks `json:"_links,omitempty"`
Items []APIListSet `json:"sets"`
}
type APIMountFamily struct {
Id int `json:"ankama_id"`
Name string `json:"name"`
}
type APIMount struct {
Id int `json:"ankama_id"`
Name string `json:"name"`
Family APIMountFamily `json:"family"`
ImageUrls ApiImageUrls `json:"image_urls,omitempty"`
Effects []ApiEffect `json:"effects,omitempty"`
}
func RenderMount(mount *mapping.MappedMultilangMount, lang string) APIMount {
resMount := APIMount{
Id: mount.AnkamaId,
Name: mount.Name[lang],
Family: APIMountFamily{
Id: mount.FamilyId,
Name: mount.FamilyName[lang],
},
ImageUrls: RenderImageUrls(ImageUrls(mount.AnkamaId, "mount", MountImgResolutions)),
}
effects := RenderEffects(&mount.Effects, lang)
if len(effects) == 0 {
resMount.Effects = nil
} else {
resMount.Effects = effects
}
return resMount
}
type APIListSet struct {
Id int `json:"ankama_id"`
Name string `json:"name"`
Items int `json:"items"`
Level int `json:"level"`
ContainsCosmetics bool `json:"contains_cosmetics"`
ContainsCosmeticsOnly bool `json:"contains_cosmetics_only"`
// extra fields
Effects map[int][]ApiEffect `json:"effects,omitempty"`
ItemIds []int `json:"equipment_ids,omitempty"`
}
func RenderSetListEntry(set *mapping.MappedMultilangSetUnity, lang string) APIListSet {
return APIListSet{
Id: set.AnkamaId,
Name: set.Name[lang],
Items: len(set.ItemIds),
Level: set.Level,
ContainsCosmetics: set.ContainsCosmetics,
ContainsCosmeticsOnly: set.ContainsCosmeticsOnly,
}
}
type APISet struct {
AnkamaId int `json:"ankama_id"`
Name string `json:"name"`
ItemIds []int `json:"equipment_ids"`
Effects map[int][]ApiEffect `json:"effects,omitempty"`
Level int `json:"highest_equipment_level"`
ContainsCosmetics bool `json:"contains_cosmetics"`
ContainsCosmeticsOnly bool `json:"contains_cosmetics_only"`
}
func RenderSet(set *mapping.MappedMultilangSetUnity, lang string) APISet {
effects := make(map[int][]ApiEffect)
for itemCombination, effect := range set.Effects {
effects[itemCombination] = RenderEffects(&effect, lang)
}
resSet := APISet{
AnkamaId: set.AnkamaId,
Name: set.Name[lang],
ItemIds: set.ItemIds,
Effects: effects,
Level: set.Level,
ContainsCosmetics: set.ContainsCosmetics,
ContainsCosmeticsOnly: set.ContainsCosmeticsOnly,
}
if len(effects) == 0 {
resSet.Effects = nil
} else {
resSet.Effects = effects
}
return resSet
}