forked from credondocr/dota2api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheroes.go
222 lines (192 loc) · 4.56 KB
/
heroes.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
package dota2api
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"image"
"image/jpeg"
"image/png"
"sort"
"sync"
"sync/atomic"
)
var sizes = []string{"lg", "sb", "full", "vert"}
const (
heroPrefix = "npc_dota_hero_"
)
type HeroImageSize int
const (
SizeLg HeroImageSize = iota
SizeSb
SizeFull
SizeVert
)
func (api Dota2) getHeroesUrl() string {
return fmt.Sprintf("%s/%s/%s/", api.dota2EconUrl, "GetHeroes", api.dota2ApiVersion)
}
type heroesJSON struct {
Result struct {
Count int `json:"count" bson:"count"`
Heroes []heroJSON `json:"heroes" bson:"heroes"`
Status int `json:"status" bson:"status"`
} `json:"result" bson:"result"`
}
type Heroes struct {
heroes []Hero
}
// Returns the hero which has the given id
// If no matching hero is found, found = false, otherwise, found = true
//
// First tries with the index [id-1] which sometimes works, and is very fast to test
// If it doesn't work, it then run a dichotomy search.
func (h Heroes) GetById(id int) (Hero, error) {
if id < len(h.heroes) && id > 0 {
if h.heroes[id-1].Id == id {
return h.heroes[id-1], nil
}
}
beg, end := 0, len(h.heroes)-1
for beg <= end {
curr := (beg + end) / 2
if h.heroes[curr].Id == id {
return h.heroes[curr], nil
}
if id > h.heroes[curr].Id {
beg = curr + 1
} else {
end = curr - 1
}
}
return Hero{}, errors.New(fmt.Sprintf("hero with id %d not found", id))
}
// Returns the hero which has the given name
// If no matching hero is found, found = false, otherwise, found = true
//
// Runs a linear search
func (h Heroes) GetByName(name string) (Hero, error) {
for _, currentHero := range h.heroes {
if currentHero.Name.full == name {
return currentHero, nil
}
}
return Hero{}, errors.New(fmt.Sprintf("item with name %s not found", name))
}
type heroName struct {
name string
full string
}
func (hN heroName) GetName() string {
return hN.name
}
func (hN heroName) GetFullName() string {
return hN.full
}
func (hN heroName) GetPrefix() string {
return heroPrefix
}
func heroNameFromFullName(name string) heroName {
return heroName{
name: name[len(heroPrefix):],
full: name,
}
}
type Hero struct {
Id int
Name heroName
}
type heroJSON struct {
ID int
Name string
}
type getHeroesCache struct {
heroes Heroes
fromCache uint32
mutex sync.Mutex
}
// This function calls the API to get the list of the heroes
// Once a call has succeeded, the result is stored, and no further API call is made
// Instead, it returns a copy of the cached result
func (api *Dota2) GetHeroes() (Heroes, error) {
var err error
if atomic.LoadUint32(&api.heroesCache.fromCache) == 0 {
err = api.fillHeroesCache()
}
return api.heroesCache.heroes, err
}
func (h heroesJSON) toHeroes() Heroes {
var heroes Heroes
heroes.heroes = make([]Hero, len(h.Result.Heroes))
for i, hero := range h.Result.Heroes {
heroes.heroes[i] = hero.toHero()
}
sort.Slice(heroes.heroes, func(i, j int) bool {
return heroes.heroes[i].Id < heroes.heroes[j].Id
})
return heroes
}
func (h heroJSON) toHero() Hero {
return Hero{
Id: h.ID,
Name: heroName{
name: h.Name[len(heroPrefix):],
full: h.Name,
},
}
}
func (api Dota2) fillHeroesCache() error {
api.heroesCache.mutex.Lock()
defer api.heroesCache.mutex.Unlock()
if api.heroesCache.fromCache == 0 {
var heroesListJson heroesJSON
param := map[string]interface{}{
"key": api.steamApiKey,
}
url, err := parseUrl(api.getHeroesUrl(), param)
if err != nil {
return err
}
resp, err := api.Get(url)
if err != nil {
return err
}
if err = json.Unmarshal(resp, &heroesListJson); err != nil {
return err
}
if heroesListJson.Result.Status != 200 {
return statusCodeError(heroesListJson.Result.Status, 200)
}
api.heroesCache.heroes = heroesListJson.toHeroes()
atomic.StoreUint32(&api.heroesCache.fromCache, 1)
return nil
}
return nil
}
func (h Heroes) Count() int {
return len(h.heroes)
}
func getHeroImageUrl(d Dota2, name heroName, size HeroImageSize) string {
ext := "png"
if size == SizeVert {
ext = "jpg"
}
return fmt.Sprintf("%s/heroes/%s_%s.%s", d.dota2CDN, name.name, sizes[size], ext)
}
func (api Dota2) GetHeroImage(hero Hero, size HeroImageSize) (image.Image, error) {
url := getHeroImageUrl(api, hero.Name, size)
res, err := api.Get(url)
if err != nil {
return nil, err
}
var img image.Image
if size == SizeVert {
img, err = jpeg.Decode(bytes.NewReader(res))
}
if size != SizeVert || err != nil {
img, err = png.Decode(bytes.NewReader(res))
}
if err != nil && size != SizeVert {
img, err = jpeg.Decode(bytes.NewReader(res))
}
return img, err
}