aboutsummaryrefslogtreecommitdiff
path: root/rankings
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2025-07-24 14:40:22 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2025-07-24 14:40:22 +0300
commitb0d199936b546c75d4b19d99591237f0bf97fe55 (patch)
treee9391880e7db2bd1ea8ff25d91aeea8dd98f186e /rankings
parentfix/frontend: fixed sidebar title size, removed unnecessary imports (diff)
parentfeat/backend: add newrelic integration (#274) (diff)
downloadlphub-b0d199936b546c75d4b19d99591237f0bf97fe55.tar.gz
lphub-b0d199936b546c75d4b19d99591237f0bf97fe55.tar.bz2
lphub-b0d199936b546c75d4b19d99591237f0bf97fe55.zip
Merge branch 'main' into css-overhaulcss-overhaul
Diffstat (limited to 'rankings')
-rw-r--r--rankings/fetch.go50
-rw-r--r--rankings/input/records.json4
-rw-r--r--rankings/main.go5
3 files changed, 41 insertions, 18 deletions
diff --git a/rankings/fetch.go b/rankings/fetch.go
index 7e63427..7e9a449 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,7 +13,12 @@ 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
@@ -21,7 +27,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us
21 end := 5000 27 end := 5000
22 28
23 for fetchAnotherPage { 29 for fetchAnotherPage {
24 portalGunEntries := fetchRecordsFromMap(47459, 0, 5000, useCache) 30 portalGunEntries, err := fetchRecordsFromMap(47459, 0, 5000, useCache)
31 if err != nil {
32 return nil, err
33 }
25 fetchAnotherPage = portalGunEntries.needsAnotherPage(&records[0]) 34 fetchAnotherPage = portalGunEntries.needsAnotherPage(&records[0])
26 if fetchAnotherPage { 35 if fetchAnotherPage {
27 start = end + 1 36 start = end + 1
@@ -50,7 +59,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us
50 end = 5000 59 end = 5000
51 60
52 for fetchAnotherPage { 61 for fetchAnotherPage {
53 doorsEntries := fetchRecordsFromMap(47740, start, end, useCache) 62 doorsEntries, err := fetchRecordsFromMap(47740, start, end, useCache)
63 if err != nil {
64 return nil, err
65 }
54 fetchAnotherPage = doorsEntries.needsAnotherPage(&records[51]) 66 fetchAnotherPage = doorsEntries.needsAnotherPage(&records[51])
55 if fetchAnotherPage { 67 if fetchAnotherPage {
56 start = end + 1 68 start = end + 1
@@ -94,7 +106,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us
94 end := 5000 106 end := 5000
95 107
96 for fetchAnotherPage { 108 for fetchAnotherPage {
97 entries := fetchRecordsFromMap(record.MapID, start, end, useCache) 109 entries, err := fetchRecordsFromMap(record.MapID, start, end, useCache)
110 if err != nil {
111 return nil, err
112 }
98 fetchAnotherPage = entries.needsAnotherPage(&record) 113 fetchAnotherPage = entries.needsAnotherPage(&record)
99 if fetchAnotherPage { 114 if fetchAnotherPage {
100 start = end + 1 115 start = end + 1
@@ -137,10 +152,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us
137 } 152 }
138 153
139 } 154 }
140 return players 155 return players, nil
141} 156}
142 157
143func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) *Leaderboard { 158func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) (*Leaderboard, error) {
144 var filename string 159 var filename string
145 if useCache { 160 if useCache {
146 filename := fmt.Sprintf("./cache/lb_%d_%d_%d.xml", mapID, start, end) 161 filename := fmt.Sprintf("./cache/lb_%d_%d_%d.xml", mapID, start, end)
@@ -152,25 +167,27 @@ func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) *Leaderbo
152 if err != nil { 167 if err != nil {
153 log.Fatalln("failed to unmarshal cache.", err.Error()) 168 log.Fatalln("failed to unmarshal cache.", err.Error())
154 } 169 }
155 return &leaderboard 170 return &leaderboard, nil
156 } 171 }
157 } 172 }
158 173
159 url := fmt.Sprintf("https://steamcommunity.com/stats/Portal2/leaderboards/%d?xml=1&start=%d&end=%d", mapID, start, end) 174 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) 175 resp, err := http.Get(url)
161 log.Println("fetched", url, ":", resp.StatusCode)
162 if err != nil { 176 if err != nil {
163 log.Fatalln("failed to fetch leaderboard.", err.Error()) 177 log.Println("failed to fetch leaderboard.", err.Error())
178 return nil, errLb
164 } 179 }
165 respBytes, err := io.ReadAll(resp.Body) 180 respBytes, err := io.ReadAll(resp.Body)
166 if err != nil { 181 if err != nil {
167 log.Fatalln("failed to read leadeboard body.", err.Error()) 182 log.Println("failed to read leadeboard body.", err.Error())
183 return nil, errLb
168 } 184 }
169 leaderboard := Leaderboard{} 185 leaderboard := Leaderboard{}
170 err = xml.Unmarshal(respBytes, &leaderboard) 186 err = xml.Unmarshal(respBytes, &leaderboard)
171 if err != nil { 187 if err != nil {
172 log.Println(string(respBytes)) 188 log.Println(string(respBytes))
173 log.Fatalln("failed to unmarshal leaderboard.", err.Error()) 189 log.Println("failed to unmarshal leaderboard.", err.Error())
190 return nil, errLb
174 } 191 }
175 192
176 if useCache { 193 if useCache {
@@ -179,10 +196,10 @@ func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) *Leaderbo
179 } 196 }
180 } 197 }
181 198
182 return &leaderboard 199 return &leaderboard, nil
183} 200}
184 201
185func fetchPlayerInfo(players []*Player) { 202func fetchPlayerInfo(players []*Player) error {
186 log.Println("fetching info for", len(players), "players") 203 log.Println("fetching info for", len(players), "players")
187 204
188 ids := make([]string, len(players)) 205 ids := make([]string, len(players))
@@ -193,11 +210,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, ",")) 210 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) 211 resp, err := http.Get(url)
195 if err != nil { 212 if err != nil {
196 log.Fatalln(err.Error()) 213 log.Println(err.Error())
214 return errPi
197 } 215 }
198 body, err := io.ReadAll(resp.Body) 216 body, err := io.ReadAll(resp.Body)
199 if err != nil { 217 if err != nil {
200 log.Fatalln(err.Error()) 218 log.Println(err.Error())
219 return errPi
201 } 220 }
202 type PlayerSummary struct { 221 type PlayerSummary struct {
203 SteamID SteamID `json:"steamid"` 222 SteamID SteamID `json:"steamid"`
@@ -223,4 +242,5 @@ func fetchPlayerInfo(players []*Player) {
223 } 242 }
224 } 243 }
225 } 244 }
245 return nil
226} 246}
diff --git a/rankings/input/records.json b/rankings/input/records.json
index 884fd49..2b1fb21 100644
--- a/rankings/input/records.json
+++ b/rankings/input/records.json
@@ -298,7 +298,7 @@
298 "id": 47822, 298 "id": 47822,
299 "name": "Finale 2", 299 "name": "Finale 2",
300 "mode": 1, 300 "mode": 1,
301 "wr": 2 301 "wr": 0
302 }, 302 },
303 { 303 {
304 "id": 47823, 304 "id": 47823,
@@ -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{}