forked from 0x263b/Porygon2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dota.go
229 lines (217 loc) · 7.62 KB
/
dota.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
package dota
import (
"fmt"
"github.com/Seventy-Two/Cara"
"github.com/Seventy-Two/Cara/web"
"strings"
"time"
)
const (
dotaLeagueURL = "http://api.steampowered.com/IDOTA2Match_570/GetLiveLeagueGames/v1/?key=%s"
dotaListingURL = "http://api.steampowered.com/IDOTA2Match_570/GetLeagueListing/v1/?key=%s"
dotaMatchURL = "http://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/v1/?key=%s"
dotaHeroesURL = "http://api.steampowered.com/IEconDOTA2_570/GetHeroes/v1/?language=en_gb&key=%s"
Timer = "15:04:05"
)
func dotamatches(command *bot.Cmd, matches []string) (msg []string, err error) {
data := &LeagueGames{}
listing := &LeagueListing{}
getHeroes := &GetHeroes{}
var radiantNet int
var direNet int
var worth int
heroes := false
showScore := false
showTowers := false
if strings.Contains(matches[0], "h") {
heroes = true
}
if strings.Contains(matches[0], "s") {
showScore = true
}
if strings.Contains(matches[0], "t") {
showTowers = true
}
err = web.GetJSON(fmt.Sprintf(dotaListingURL, bot.Config.API.Dota), listing)
if err != nil {
msg = append(msg, fmt.Sprintf("Could not retrieve league listings."))
return msg, nil
}
err = web.GetJSON(fmt.Sprintf(dotaLeagueURL, bot.Config.API.Dota), data)
if err != nil {
msg = append(msg, fmt.Sprintf("Could not retrieve matches."))
return msg, nil
}
web.GetJSON(fmt.Sprintf(dotaHeroesURL, bot.Config.API.Dota), getHeroes)
if err != nil {
msg = append(msg, fmt.Sprintf("Could not retrieve heroes."))
return msg, nil
}
var str []string
for i := 0; i < len(data.Result.Games) ; i++ {
worth = 0
radiantNet = 0
direNet = 0
if (data.Result.Games[i].LeagueTier == 2 && data.Result.Games[i].Spectators >= 1000) || (data.Result.Games[i].LeagueTier == 3 && data.Result.Games[i].Spectators >= 200) {
var leaguename string
herostr := ""
radTower := ""
direTower := ""
radHeroes := []string{""}
direHeroes := []string{""}
radiant := data.Result.Games[i].RadiantTeam.TeamName
dire := data.Result.Games[i].DireTeam.TeamName
radiantScore := data.Result.Games[i].Scoreboard.Radiant.Score
direScore := data.Result.Games[i].Scoreboard.Dire.Score
game := data.Result.Games[i].RadiantSeriesWins + data.Result.Games[i].DireSeriesWins + 1
viewers := data.Result.Games[i].Spectators
for k :=0; k < len(listing.Result.Leagues); k++ {
if data.Result.Games[i].LeagueID == listing.Result.Leagues[k].Leagueid {
leaguename = listing.Result.Leagues[k].Name
leaguename = strings.Replace(leaguename, "#DOTA_Item", "", -1)
leaguename = strings.Replace(leaguename, "_", " ", -1)
leaguename = strings.TrimSpace(leaguename)
}
}
duration := int(data.Result.Games[i].Scoreboard.Duration)
t := fmt.Sprintf((time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC).Add(time.Duration(duration) * time.Second)).Format(Timer))
for j := 0; j < len(data.Result.Games[i].Scoreboard.Radiant.Players); j++ {
radiantNet += data.Result.Games[i].Scoreboard.Radiant.Players[j].NetWorth
}
if heroes {
for k := 0; k < len(data.Result.Games[i].Scoreboard.Radiant.Picks); k++ {
radHeroes = append(radHeroes, getHerofromID(data.Result.Games[i].Scoreboard.Radiant.Picks[k].HeroID, getHeroes))
}
}
for j := 0; j < len(data.Result.Games[i].Scoreboard.Dire.Players); j++ {
direNet += data.Result.Games[i].Scoreboard.Dire.Players[j].NetWorth
}
if heroes {
for k := 0; k < len(data.Result.Games[i].Scoreboard.Dire.Picks); k++ {
direHeroes = append(direHeroes, getHerofromID(data.Result.Games[i].Scoreboard.Dire.Picks[k].HeroID, getHeroes))
}
}
worth = radiantNet - direNet
if showTowers {
radTower, direTower = towerToString(data.Result.Games[i].Scoreboard.Radiant.TowerState, data.Result.Games[i].Scoreboard.Dire.TowerState)
}
if heroes {
herostr = fmt.Sprintf("\x0303%s\x03\x0304%s\x03|", strings.Join(radHeroes[:], "\x03|\x0303"), strings.Join(direHeroes[:], "\x03|\x0304"))
}
if dire == "" {
dire = "Dire"
}
if radiant == "" {
radiant = "Radiant"
}
if showScore && worth != 0 {
if worth > 0 {
str = append(str, fmt.Sprintf("\x0307net+%d \x0303%s\x03 %s %d-%d %s \x0304%s\x03 [%s Game %d, %d viewers, League: %s %s]" ,
worth,
radiant,
radTower,
radiantScore,
direScore,
direTower,
dire,
t,
game,
viewers,
leaguename,
herostr))
} else {
worth = -worth
str = append(str, fmt.Sprintf("\x0303%s\x03 %s %d-%d %s \x0304%s \x0307net+%d\x03 [%s Game %d, %d viewers, League: %s %s]",
radiant,
radTower,
radiantScore,
direScore,
direTower,
dire,
worth,
t,
game,
viewers,
leaguename,
herostr))
}
} else {
str = append(str, fmt.Sprintf("\x0303%s\x03 - \x0304%s\x03 [Game %d, %d viewers, League: %s %s]",
radiant,
dire,
game,
viewers,
leaguename,
herostr))
}
}
}
if len(str) == 0 {
msg = append(msg, fmt.Sprintf("No games found."))
return msg, nil
}
return str, nil
}
func towerToString(rad int, dire int) (radTower string, direTower string) {
towerUp := "♜"
towerDown := "♖"
ancient := "♚"
radstr := ancient + fmt.Sprintf("%011b", int64(rad))
radstr = organise(radstr)
radstr = strings.Replace(radstr, "0", towerDown, -1)
radstr = strings.Replace(radstr, "1", towerUp, -1)
direstr := ancient + fmt.Sprintf("%011b", int64(dire))
direstr = organise(direstr)
var tempstr string
for _,v := range direstr {
tempstr = string(v) + tempstr // because allow runes and unicode
}
direstr = tempstr
direstr = strings.Replace(direstr, "0", towerDown, -1)
direstr = strings.Replace(direstr, "1", towerUp, -1)
return radstr, direstr
}
func organise(in string) (out string) {
// volvo returns towers grouped by top/mid/bot, when we want towers grouped by tier
tempin := []rune(in)
var tempout []rune
tempout = append(tempout, tempin[0])
tempout = append(tempout, ' ')
tempout = append(tempout, tempin[1])
tempout = append(tempout, tempin[2])
tempout = append(tempout, ' ')
tempout = append(tempout, tempin[3])
tempout = append(tempout, tempin[6])
tempout = append(tempout, tempin[9])
tempout = append(tempout, ' ')
tempout = append(tempout, tempin[4])
tempout = append(tempout, tempin[7])
tempout = append(tempout, tempin[10])
tempout = append(tempout, ' ')
tempout = append(tempout, tempin[5])
tempout = append(tempout, tempin[8])
tempout = append(tempout, tempin[11])
out = string(tempout)
return out
}
func getHerofromID(id int, heroes *GetHeroes) (out string) {
if id == 0 {
return "PICK"
}
out = getShortHero(id)
if out != "" {
return out
}
for m := 0; m < len(heroes.Result.Heroes); m++ {
if heroes.Result.Heroes[m].ID == id {
out = heroes.Result.Heroes[m].LocalizedName
return out
}
}
return ""
}
func init() {
bot.RegisterMultiCommand(
"^(d2)\\s?(h(?:ero)?)?\\s?(s(?:core)?)?\\s?(t(?:ower)?)?$",
dotamatches)
}