-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
270 lines (230 loc) · 7.42 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"math"
"net/http"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
func main() {
steamID := flag.String("id", "", "Steam ID")
matchCount := flag.Int("count", 1000, "Number of matches to retrieve")
flag.Parse()
if *steamID == "" {
fmt.Println("Please provide a steam ID")
return
}
api := AOE2NET{
MatchAPI: "https://aoe2.net/api/player/matches?game=aoe2de&steam_id=%s&count=%d&start=%d",
CivAPI: "https://aoe2.net/api/strings?game=aoe2de&language=en",
}
civilizations, err := api.GetCivilazations()
if err != nil {
panic(err)
}
matches, err := api.GetAllMatches(*steamID, *matchCount)
if err != nil {
fmt.Println("error getting matches but attempting to continue", err)
}
playstyle := CalculatePlaystyle(*steamID, civilizations, matches)
fmt.Printf("%+v\n", playstyle)
fmt.Printf("Found %d matches, %f", len(matches), playstyle.Versatility())
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
pc := widgets.NewPieChart()
pc.Title = "Race Playstyle"
pc.SetRect(5, 5, 70, 36)
pc.Data = []float64{
playstyle.Archers / playstyle.Versatility() * 100,
playstyle.Cavalry / playstyle.Versatility() * 100,
playstyle.CavalryArchers / playstyle.Versatility() * 100,
playstyle.Infantry / playstyle.Versatility() * 100,
playstyle.Siege / playstyle.Versatility() * 100,
playstyle.Monks / playstyle.Versatility() * 100,
playstyle.Water / playstyle.Versatility() * 100,
}
pc.Colors = []ui.Color{130, 5, 1, ui.ColorWhite, 8, 3, 12}
pc.AngleOffset = -.5 * math.Pi
pc.LabelFormatter = func(i int, v float64) string {
var label string
switch i {
case 0:
label = "Archers"
case 1:
label = "Cavalry"
case 2:
label = "Cavalry Archers"
case 3:
label = "Infantry"
case 4:
label = "Siege"
case 5:
label = "Monks"
case 6:
label = "Water"
}
return fmt.Sprintf("%s: %.2f%%", label, v)
}
ui.Render(pc)
uiEvents := ui.PollEvents()
for {
e := <-uiEvents
switch e.ID {
case "q", "<C-c>":
return
}
}
}
type Civilization struct {
ID int `json:"id"`
Name string `json:"string"`
Playstyle Playstyle
}
type Match struct {
MatchID string `json:"match_id"`
LobbyID interface{} `json:"lobby_id"`
MatchUUID string `json:"match_uuid"`
Version interface{} `json:"version"`
Name string `json:"name"`
NumPlayers int `json:"num_players"`
NumSlots int `json:"num_slots"`
AverageRating interface{} `json:"average_rating"`
Cheats bool `json:"cheats"`
FullTechTree bool `json:"full_tech_tree"`
EndingAge int `json:"ending_age"`
Expansion interface{} `json:"expansion"`
GameType int `json:"game_type"`
HasCustomContent interface{} `json:"has_custom_content"`
HasPassword interface{} `json:"has_password"`
LockSpeed bool `json:"lock_speed"`
LockTeams bool `json:"lock_teams"`
MapSize int `json:"map_size"`
MapType int `json:"map_type"`
Pop int `json:"pop"`
Ranked bool `json:"ranked"`
LeaderboardID int `json:"leaderboard_id"`
RatingType int `json:"rating_type"`
Resources int `json:"resources"`
Rms interface{} `json:"rms"`
Scenario interface{} `json:"scenario"`
Server interface{} `json:"server"`
SharedExploration bool `json:"shared_exploration"`
Speed int `json:"speed"`
StartingAge int `json:"starting_age"`
TeamTogether bool `json:"team_together"`
TeamPositions bool `json:"team_positions"`
TreatyLength int `json:"treaty_length"`
Turbo bool `json:"turbo"`
Victory int `json:"victory"`
VictoryTime int `json:"victory_time"`
Visibility int `json:"visibility"`
Opened int `json:"opened"`
Started int `json:"started"`
Finished int `json:"finished"`
Players []struct {
ProfileID int `json:"profile_id"`
SteamID string `json:"steam_id"`
Name string `json:"name"`
Clan interface{} `json:"clan"`
Country string `json:"country"`
Slot int `json:"slot"`
SlotType int `json:"slot_type"`
Rating int `json:"rating"`
RatingChange interface{} `json:"rating_change"`
Games interface{} `json:"games"`
Wins interface{} `json:"wins"`
Streak interface{} `json:"streak"`
Drops interface{} `json:"drops"`
Color int `json:"color"`
Team int `json:"team"`
Civ int `json:"civ"`
CivAlpha int `json:"civ_alpha"`
Won bool `json:"won"`
} `json:"players"`
}
type AOE2NET struct {
MatchAPI string
CivAPI string
}
func (a AOE2NET) GetCivilazations() ([]*Civilization, error) {
var civilizations []*Civilization
r, err := http.Get(a.CivAPI)
if err != nil {
return civilizations, err
}
response := struct {
Civilizations []*Civilization `json:"civ"`
}{}
err = json.NewDecoder(r.Body).Decode(&response)
if err != nil {
return civilizations, err
}
for _, civilization := range response.Civilizations {
playstyle, err := GetPlaystyle(civilization.Name)
if err != nil {
fmt.Println("failed to retrieve playstyle", err)
continue
}
civilization.Playstyle = playstyle
}
return response.Civilizations, nil
}
func (a AOE2NET) GetMatches(steamID string, count int, startIndex int) ([]Match, error) {
var matches []Match
matchAPI := "https://aoe2.net/api/player/matches?game=aoe2de&steam_id=%s&count=%d&start=%d"
r, err := http.Get(fmt.Sprintf(matchAPI, steamID, count, startIndex))
if err != nil {
return matches, err
}
err = json.NewDecoder(r.Body).Decode(&matches)
if err != nil {
return matches, err
}
return matches, nil
}
func (a AOE2NET) GetAllMatches(steamID string, amount int) ([]Match, error) {
var allMatches []Match
var matchesRetrieved int
for matchesRetrieved < amount {
batchAmount := amount - matchesRetrieved
if batchAmount > 1000 {
batchAmount = 1000
}
matches, err := a.GetMatches(steamID, batchAmount, matchesRetrieved)
if err != nil {
return allMatches, err
}
if len(matches) == 0 {
break
}
matchesRetrieved += len(matches)
allMatches = append(allMatches, matches...)
}
return allMatches, nil
}
func CalculatePlaystyle(steamID string, civilizations []*Civilization, matches []Match) Playstyle {
playerPlaystyle := Playstyle{}
for _, match := range matches {
for _, player := range match.Players {
if player.SteamID == steamID {
for _, civilization := range civilizations {
if player.Civ == civilization.ID {
playerPlaystyle.Archers += civilization.Playstyle.Archers
playerPlaystyle.Cavalry += civilization.Playstyle.Cavalry
playerPlaystyle.CavalryArchers += civilization.Playstyle.CavalryArchers
playerPlaystyle.Infantry += civilization.Playstyle.Infantry
playerPlaystyle.Siege += civilization.Playstyle.Siege
playerPlaystyle.Monks += civilization.Playstyle.Monks
playerPlaystyle.Water += civilization.Playstyle.Water
}
}
}
}
}
return playerPlaystyle
}