aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--rankings/fetch.go51
-rw-r--r--rankings/input/records.json2
-rw-r--r--rankings/main.go5
3 files changed, 41 insertions, 17 deletions
diff --git a/rankings/fetch.go b/rankings/fetch.go
index 7e63427..76a3526 100644
--- a/rankings/fetch.go
+++ b/rankings/fetch.go
@@ -3,6 +3,7 @@ package main
3import ( 3import (
4 "encoding/json" 4 "encoding/json"
5 "encoding/xml" 5 "encoding/xml"
6 "errors"
6 "fmt" 7 "fmt"
7 "io" 8 "io"
8 "log" 9 "log"
@@ -12,16 +13,25 @@ import (
12 "strings" 13 "strings"
13) 14)
14 15
15func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, useCache bool) map[SteamID]*Player { 16var (
17 errLb error = errors.New("leaderboards error")
18 errPi error = errors.New("playerinfo error")
19)
20
21func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, useCache bool) (map[SteamID]*Player, error) {
16 log.Println("fetching leaderboard") 22 log.Println("fetching leaderboard")
17 players := map[SteamID]*Player{} 23 players := map[SteamID]*Player{}
18 // first init players map with records from portal gun and doors 24 // first init players map with records from portal gun and doors
25 return nil, errLb
19 fetchAnotherPage := true 26 fetchAnotherPage := true
20 start := 0 27 start := 0
21 end := 5000 28 end := 5000
22 29
23 for fetchAnotherPage { 30 for fetchAnotherPage {
24 portalGunEntries := fetchRecordsFromMap(47459, 0, 5000, useCache) 31 portalGunEntries, err := fetchRecordsFromMap(47459, 0, 5000, useCache)
32 if err != nil {
33 return nil, err
34 }
25 fetchAnotherPage = portalGunEntries.needsAnotherPage(&records[0]) 35 fetchAnotherPage = portalGunEntries.needsAnotherPage(&records[0])
26 if fetchAnotherPage { 36 if fetchAnotherPage {
27 start = end + 1 37 start = end + 1
@@ -50,7 +60,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us
50 end = 5000 60 end = 5000
51 61
52 for fetchAnotherPage { 62 for fetchAnotherPage {
53 doorsEntries := fetchRecordsFromMap(47740, start, end, useCache) 63 doorsEntries, err := fetchRecordsFromMap(47740, start, end, useCache)
64 if err != nil {
65 return nil, err
66 }
54 fetchAnotherPage = doorsEntries.needsAnotherPage(&records[51]) 67 fetchAnotherPage = doorsEntries.needsAnotherPage(&records[51])
55 if fetchAnotherPage { 68 if fetchAnotherPage {
56 start = end + 1 69 start = end + 1
@@ -94,7 +107,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us
94 end := 5000 107 end := 5000
95 108
96 for fetchAnotherPage { 109 for fetchAnotherPage {
97 entries := fetchRecordsFromMap(record.MapID, start, end, useCache) 110 entries, err := fetchRecordsFromMap(record.MapID, start, end, useCache)
111 if err != nil {
112 return nil, err
113 }
98 fetchAnotherPage = entries.needsAnotherPage(&record) 114 fetchAnotherPage = entries.needsAnotherPage(&record)
99 if fetchAnotherPage { 115 if fetchAnotherPage {
100 start = end + 1 116 start = end + 1
@@ -137,10 +153,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us
137 } 153 }
138 154
139 } 155 }
140 return players 156 return players, nil
141} 157}
142 158
143func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) *Leaderboard { 159func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) (*Leaderboard, error) {
144 var filename string 160 var filename string
145 if useCache { 161 if useCache {
146 filename := fmt.Sprintf("./cache/lb_%d_%d_%d.xml", mapID, start, end) 162 filename := fmt.Sprintf("./cache/lb_%d_%d_%d.xml", mapID, start, end)
@@ -152,25 +168,27 @@ func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) *Leaderbo
152 if err != nil { 168 if err != nil {
153 log.Fatalln("failed to unmarshal cache.", err.Error()) 169 log.Fatalln("failed to unmarshal cache.", err.Error())
154 } 170 }
155 return &leaderboard 171 return &leaderboard, nil
156 } 172 }
157 } 173 }
158 174
159 url := fmt.Sprintf("https://steamcommunity.com/stats/Portal2/leaderboards/%d?xml=1&start=%d&end=%d", mapID, start, end) 175 url := fmt.Sprintf("https://steamcommunity.com/stats/Portal2/leaderboards/%d?xml=1&start=%d&end=%d", mapID, start, end)
160 resp, err := http.Get(url) 176 resp, err := http.Get(url)
161 log.Println("fetched", url, ":", resp.StatusCode)
162 if err != nil { 177 if err != nil {
163 log.Fatalln("failed to fetch leaderboard.", err.Error()) 178 log.Println("failed to fetch leaderboard.", err.Error())
179 return nil, errLb
164 } 180 }
165 respBytes, err := io.ReadAll(resp.Body) 181 respBytes, err := io.ReadAll(resp.Body)
166 if err != nil { 182 if err != nil {
167 log.Fatalln("failed to read leadeboard body.", err.Error()) 183 log.Println("failed to read leadeboard body.", err.Error())
184 return nil, errLb
168 } 185 }
169 leaderboard := Leaderboard{} 186 leaderboard := Leaderboard{}
170 err = xml.Unmarshal(respBytes, &leaderboard) 187 err = xml.Unmarshal(respBytes, &leaderboard)
171 if err != nil { 188 if err != nil {
172 log.Println(string(respBytes)) 189 log.Println(string(respBytes))
173 log.Fatalln("failed to unmarshal leaderboard.", err.Error()) 190 log.Println("failed to unmarshal leaderboard.", err.Error())
191 return nil, errLb
174 } 192 }
175 193
176 if useCache { 194 if useCache {
@@ -179,10 +197,10 @@ func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) *Leaderbo
179 } 197 }
180 } 198 }
181 199
182 return &leaderboard 200 return &leaderboard, nil
183} 201}
184 202
185func fetchPlayerInfo(players []*Player) { 203func fetchPlayerInfo(players []*Player) error {
186 log.Println("fetching info for", len(players), "players") 204 log.Println("fetching info for", len(players), "players")
187 205
188 ids := make([]string, len(players)) 206 ids := make([]string, len(players))
@@ -193,11 +211,13 @@ func fetchPlayerInfo(players []*Player) {
193 url := fmt.Sprintf("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=%s&steamids=%s", os.Getenv("API_KEY"), strings.Join(ids, ",")) 211 url := fmt.Sprintf("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=%s&steamids=%s", os.Getenv("API_KEY"), strings.Join(ids, ","))
194 resp, err := http.Get(url) 212 resp, err := http.Get(url)
195 if err != nil { 213 if err != nil {
196 log.Fatalln(err.Error()) 214 log.Println(err.Error())
215 return errPi
197 } 216 }
198 body, err := io.ReadAll(resp.Body) 217 body, err := io.ReadAll(resp.Body)
199 if err != nil { 218 if err != nil {
200 log.Fatalln(err.Error()) 219 log.Println(err.Error())
220 return errPi
201 } 221 }
202 type PlayerSummary struct { 222 type PlayerSummary struct {
203 SteamID SteamID `json:"steamid"` 223 SteamID SteamID `json:"steamid"`
@@ -223,4 +243,5 @@ func fetchPlayerInfo(players []*Player) {
223 } 243 }
224 } 244 }
225 } 245 }
246 return nil
226} 247}
diff --git a/rankings/input/records.json b/rankings/input/records.json
index d960004..2b1fb21 100644
--- a/rankings/input/records.json
+++ b/rankings/input/records.json
@@ -377,7 +377,7 @@
377 "id": 47839, 377 "id": 47839,
378 "name": "Catapults", 378 "name": "Catapults",
379 "mode": 2, 379 "mode": 2,
380 "wr": 4 380 "wr": 3
381 }, 381 },
382 { 382 {
383 "id": 47842, 383 "id": 47842,
diff --git a/rankings/main.go b/rankings/main.go
index 552f058..928d3be 100644
--- a/rankings/main.go
+++ b/rankings/main.go
@@ -58,7 +58,10 @@ func run() {
58 overrides := readOverrides() 58 overrides := readOverrides()
59 log.Println("loaded", len(overrides), "player overrides") 59 log.Println("loaded", len(overrides), "player overrides")
60 60
61 players := fetchLeaderboard(records, overrides, useCache) 61 players, err := fetchLeaderboard(records, overrides, useCache)
62 if err != nil {
63 return
64 }
62 65
63 spRankings := []*Player{} 66 spRankings := []*Player{}
64 mpRankings := []*Player{} 67 mpRankings := []*Player{}