forked from credondocr/dota2api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleagues.go
433 lines (384 loc) · 11.2 KB
/
leagues.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
package dota2api
import (
"encoding/json"
"fmt"
"time"
)
func (api Dota2) getLiveGamesUrl() string {
return fmt.Sprintf("%s/%s/%s/", api.dota2MatchUrl, "GetLiveLeagueGames", api.steamApiVersion)
}
type liveGamesJSON struct {
Result struct {
Games []liveGameJSON `json:"games" bson:"games"`
Status int `json:"status" bson:"status"`
} `json:"result" bson:"result"`
}
type liveGameJSON struct {
Players liveGamePlayersJSON `json:"players" bson:"players"`
LobbyID int64 `json:"lobby_id" bson:"lobby_id"`
MatchID int64 `json:"match_id" bson:"match_id"`
Spectators int `json:"spectators" bson:"spectators"`
LeagueID int `json:"league_id" bson:"league_id"`
LeagueNodeId int `json:"league_node_id" bson:"league_node_id"`
StreamDelayS int `json:"stream_delay_s" bson:"steam_delay_s"`
RadiantSeriesWins int `json:"radiant_series_wins" bson:"radiant_series_wins"`
DireSeriesWins int `json:"dire_series_wins" bson:"dire_series_win"`
SeriesType int `json:"series_type" bson:"series_type"`
Scoreboard scoreboardJSON `json:"scoreboard" bson:"scoreboard"`
DireTeam playersTeamJSON `json:"dire_team,omitempty" bson:"dire_team"`
RadiantTeam playersTeamJSON `json:"radiant_team,omitempty" bson:"radiant_team"`
}
type liveGamePlayersJSON []liveGamePlayerJSON
type liveGamePlayerJSON struct {
AccountID int64 `json:"account_id" bson:"account_id"`
Name string `json:"name" bson:"name"`
HeroID int `json:"hero_id" bson:"hero_id"`
Team int `json:"team" bson:"team"`
}
type playersTeamJSON struct {
TeamName string `json:"team_name" bson:"team_name"`
TeamID int64 `json:"team_id" bson:"team_id"`
TeamLogo int64 `json:"team_logo" bson:"team_logo"`
Complete bool `json:"complete" bson:"complete"`
}
type scoreboardJSON struct {
Duration float64 `json:"duration" bson:"duration"`
RoshanRespawnTimer int `json:"roshan_respawn_timer" bson:"roshan_respawn"`
Radiant sideJSON `json:"radiant" bson:"radiant"`
Dire sideJSON `json:"dire" bson:"dire"`
}
type sideJSON struct {
Score int `json:"score" bson:"score"`
TowerState uint16 `json:"tower_state" bson:"tower_state"`
BarracksState uint8 `json:"barracks_state" bson:"barracks_state"`
Picks picksOrBansJSON `json:"picks" bson:"picks"`
Bans picksOrBansJSON `json:"bans" bson:"bans"`
Players livePlayersJSON `json:"players" bson:"players"`
Abilities LiveAbilities `json:"abilities" bson:"abilities"`
}
type picksOrBansJSON []struct {
HeroID int `json:"hero_id" bson:"hero_id"`
}
type livePlayersJSON []livePlayerJSON
type livePlayerJSON struct {
PlayerSlot int `json:"player_slot" bson:"player_slot"`
AccountID int64 `json:"account_id" bson:"account_id"`
HeroID int `json:"hero_id" bson:"hero_id"`
Kills int `json:"kills" bson:"kills"`
Deaths int `json:"death" bson:"death"`
Assists int `json:"assists" bson:"assists"`
LastHits int `json:"last_hits" bson:"last_hits"`
Denies int `json:"denies" bson:"denies"`
Gold int `json:"gold" bson:"gold"`
Level int `json:"Level" bson:"Level"`
GoldPerMin int `json:"gold_per_min" bson:"gold_per_min"`
XpPerMin int `json:"xp_per_min" bson:"xp_per_min"`
UltimateState int `json:"ultimate_state" bson:"ultimate_state"`
UltimateCooldown int `json:"ultimate_cooldown" bson:"ultimate_cooldown"`
Item0 int `json:"item0" bson:"item0"`
Item1 int `json:"item1" bson:"item1"`
Item2 int `json:"item2" bson:"item2"`
Item3 int `json:"item3" bson:"item3"`
Item4 int `json:"item4" bson:"item4"`
Item5 int `json:"item5" bson:"item5"`
RespawnTimer int `json:"respawn_timer" bson:"respawn"`
PositionX float64 `json:"position_x" bson:"position_x"`
PositionY float64 `json:"position_y" bson:"position_y"`
NetWorth int `json:"net_worth" bson:"net_worth"`
}
type LiveGame struct {
Players LiveGamePlayers
LobbyId int64
MatchId int64
Spectators int
League League
StreamDelay time.Duration
Series Series
Teams LiveTeams
ScoreBoard ScoreBoard
}
type LiveGames []LiveGame
func (l LiveGames) Count() int {
return len(l)
}
type LiveGamePlayer struct {
AccountId int64
Name string
Hero Hero
Team int
}
type LiveGamePlayers []LiveGamePlayer
type League struct {
LeagueId int
LeagueNodeId int
}
type Series struct {
Radiant int
Dire int
}
type LiveTeam struct {
TeamName string
TeamId int64
TeamLogo int64
Complete bool
}
type ScoreBoard struct {
Duration time.Duration
RoshanRespawnTimer time.Duration
Sides Sides
}
type Sides struct {
Radiant SideLive
Dire SideLive
}
type SideLive struct {
Score int
BuildingsState TeamBuildingsState
Picks []Hero
Bans []Hero
Players PlayersLive
Abilities LiveAbilities
}
type LiveTeams struct {
Radiant LiveTeam
Dire LiveTeam
}
type PlayerLive struct {
PlayerSlot int
AccountID int64
Hero Hero
KDA KDA
Stats PlayerStatsLive
Items LivePlayerItems
RespawnTimer time.Duration
UltimateState UltimateState
Position Position
Gold PlayerGold
}
type PlayersLive []PlayerLive
type PlayerStatsLive struct {
LastHits int
Denies int
GoldPerMinute int
XpPerMinute int
Level int
}
type UltimateState struct {
UltimateState int
UltimateCooldown time.Duration
}
type LivePlayerItems struct {
Item0 Item
Item1 Item
Item2 Item
Item3 Item
Item4 Item
Item5 Item
}
type Position struct {
X, Y float64
}
type LiveAbility struct {
AbilityID int `json:"ability_id" bson:"ability_id"`
AbilityLevel int `json:"ability_level" bson:"ability_level"`
}
type LiveAbilities []LiveAbility
func (l liveGamesJSON) toLiveGames(api Dota2) (LiveGames, error) {
ret := make(LiveGames, len(l.Result.Games))
for i, g := range l.Result.Games {
var err error
if ret[i], err = g.toLiveGame(api); err != nil {
return nil, err
}
}
return ret, nil
}
func (g liveGameJSON) toLiveGame(api Dota2) (LiveGame, error) {
ret := LiveGame{
LobbyId: g.LobbyID,
MatchId: g.MatchID,
Spectators: g.Spectators,
League: League{
LeagueId: g.LeagueID,
LeagueNodeId: g.LeagueNodeId,
},
StreamDelay: time.Duration(g.StreamDelayS) * time.Second,
Series: Series{
Radiant: g.RadiantSeriesWins,
Dire: g.DireSeriesWins,
},
Teams: LiveTeams{
Radiant: LiveTeam{
TeamName: g.RadiantTeam.TeamName,
TeamId: g.RadiantTeam.TeamID,
TeamLogo: g.RadiantTeam.TeamLogo,
Complete: g.RadiantTeam.Complete,
},
Dire: LiveTeam{
TeamName: g.DireTeam.TeamName,
TeamId: g.DireTeam.TeamID,
TeamLogo: g.DireTeam.TeamLogo,
Complete: g.DireTeam.Complete,
},
},
}
var err error
if ret.Players, err = g.Players.toPlayers(api); err != nil {
return ret, err
}
ret.ScoreBoard, err = g.Scoreboard.toScoreBoard(api)
return ret, err
}
func (p liveGamePlayersJSON) toPlayers(api Dota2) (LiveGamePlayers, error) {
l := make(LiveGamePlayers, len(p))
heroes, err := api.GetHeroes()
if err != nil {
return l, err
}
for i, player := range p {
var err error
if l[i], err = player.toPlayer(heroes); err != nil {
return nil, err
}
}
return l, nil
}
func (p liveGamePlayerJSON) toPlayer(heroes Heroes) (lGP LiveGamePlayer, err error) {
lGP = LiveGamePlayer{
AccountId: p.AccountID,
Name: p.Name,
Team: p.Team,
}
if p.HeroID != 0 {
if lGP.Hero, err = heroes.GetById(p.HeroID); err != nil {
return
}
}
return
}
func (s scoreboardJSON) toScoreBoard(api Dota2) (ScoreBoard, error) {
ret := ScoreBoard{
Duration: time.Duration(s.Duration * float64(time.Second)),
RoshanRespawnTimer: time.Duration(s.RoshanRespawnTimer) * time.Second,
Sides: Sides{},
}
var err error
if ret.Sides.Radiant, err = s.Radiant.toSideLive(api); err != nil {
return ret, err
}
ret.Sides.Dire, err = s.Dire.toSideLive(api)
return ret, err
}
func (s sideJSON) toSideLive(api Dota2) (SideLive, error) {
ret := SideLive{
Score: s.Score,
BuildingsState: TeamBuildingsState{}.from(s.TowerState, s.BarracksState),
Abilities: s.Abilities,
}
var err error
heroes, err := api.GetHeroes()
if err != nil {
return ret, err
}
items, err := api.GetItems()
if err != nil {
return ret, err
}
if ret.Picks, err = s.Picks.toHeroSlice(heroes); err != nil {
return ret, err
}
if ret.Bans, err = s.Bans.toHeroSlice(heroes); err != nil {
return ret, err
}
ret.Players, err = s.Players.toLivePlayers(heroes, items)
return ret, err
}
func (l livePlayersJSON) toLivePlayers(heroes Heroes, items Items) (PlayersLive, error) {
ret := make(PlayersLive, len(l))
for i, player := range l {
var err error
if ret[i], err = player.toLivePlayer(heroes, items); err != nil {
return nil, err
}
}
return ret, nil
}
func (l livePlayerJSON) toLivePlayer(heroes Heroes, items Items) (pL PlayerLive, err error) {
pL = PlayerLive{
PlayerSlot: l.PlayerSlot,
AccountID: l.AccountID,
KDA: KDA{
Kills: l.Kills,
Deaths: l.Deaths,
Assists: l.Assists,
},
Stats: PlayerStatsLive{
LastHits: l.LastHits,
Denies: l.Denies,
GoldPerMinute: l.GoldPerMin,
XpPerMinute: l.XpPerMin,
Level: l.Level,
},
RespawnTimer: time.Duration(l.RespawnTimer) * time.Second,
UltimateState: UltimateState{
UltimateState: l.UltimateState,
UltimateCooldown: time.Duration(l.UltimateCooldown) * time.Second,
},
Position: Position{
X: l.PositionX,
Y: l.PositionY,
},
Gold: PlayerGold{
current: l.Gold,
spent: l.NetWorth - l.Gold,
},
}
if l.HeroID != 0 {
if pL.Hero, err = heroes.GetById(l.HeroID); err != nil {
return
}
}
fields := []*Item{&pL.Items.Item0, &pL.Items.Item1, &pL.Items.Item2, &pL.Items.Item3, &pL.Items.Item4, &pL.Items.Item5}
src := []int{l.Item0, l.Item1, l.Item2, l.Item3, l.Item4, l.Item5}
for i, fi := range fields {
if src[i] != 0 {
if *fi, err = items.GetById(src[i]); err != nil {
return
}
}
}
return
}
func (p picksOrBansJSON) toHeroSlice(heroes Heroes) (ret []Hero, err error) {
ret = make([]Hero, len(p))
for i, choice := range p {
if ret[i], err = heroes.GetById(choice.HeroID); err != nil {
return
}
}
return
}
func (api Dota2) GetLiveLeagueGames(params ...Parameter) (LiveGames, error) {
var liveGames liveGamesJSON
param, err := getParameterMap(nil, nil, params)
if err != nil {
return LiveGames{}, err
}
param["key"] = api.steamApiKey
url, err := parseUrl(api.getLiveGamesUrl(), param)
if err != nil {
return LiveGames{}, err
}
resp, err := api.Get(url)
if err != nil {
return LiveGames{}, err
}
err = json.Unmarshal(resp, &liveGames)
if err != nil {
return LiveGames{}, err
}
if liveGames.Result.Status != 200 {
return LiveGames{}, statusCodeError(liveGames.Result.Status, 200)
}
return liveGames.toLiveGames(api)
}