From 1685d6e13b8a09b0fb4f2ae94fb2e66a437f9155 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Sat, 25 Jan 2025 00:38:49 +0300 Subject: wr: finale 2 - 0 portals (#258) --- rankings/input/records.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rankings/input') diff --git a/rankings/input/records.json b/rankings/input/records.json index 884fd49..d960004 100644 --- a/rankings/input/records.json +++ b/rankings/input/records.json @@ -298,7 +298,7 @@ "id": 47822, "name": "Finale 2", "mode": 1, - "wr": 2 + "wr": 0 }, { "id": 47823, -- cgit v1.2.3 From e329da571b7cc5178ea210dd109f37b3ebf83ee2 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Wed, 19 Mar 2025 15:02:21 +0000 Subject: feat/rankings: add catapults 3 wr, handle err (#265) --- rankings/fetch.go | 51 ++++++++++++++++++++++++++++++++------------- rankings/input/records.json | 2 +- rankings/main.go | 5 ++++- 3 files changed, 41 insertions(+), 17 deletions(-) (limited to 'rankings/input') 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 import ( "encoding/json" "encoding/xml" + "errors" "fmt" "io" "log" @@ -12,16 +13,25 @@ import ( "strings" ) -func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, useCache bool) map[SteamID]*Player { +var ( + errLb error = errors.New("leaderboards error") + errPi error = errors.New("playerinfo error") +) + +func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, useCache bool) (map[SteamID]*Player, error) { log.Println("fetching leaderboard") players := map[SteamID]*Player{} // first init players map with records from portal gun and doors + return nil, errLb fetchAnotherPage := true start := 0 end := 5000 for fetchAnotherPage { - portalGunEntries := fetchRecordsFromMap(47459, 0, 5000, useCache) + portalGunEntries, err := fetchRecordsFromMap(47459, 0, 5000, useCache) + if err != nil { + return nil, err + } fetchAnotherPage = portalGunEntries.needsAnotherPage(&records[0]) if fetchAnotherPage { start = end + 1 @@ -50,7 +60,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us end = 5000 for fetchAnotherPage { - doorsEntries := fetchRecordsFromMap(47740, start, end, useCache) + doorsEntries, err := fetchRecordsFromMap(47740, start, end, useCache) + if err != nil { + return nil, err + } fetchAnotherPage = doorsEntries.needsAnotherPage(&records[51]) if fetchAnotherPage { start = end + 1 @@ -94,7 +107,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us end := 5000 for fetchAnotherPage { - entries := fetchRecordsFromMap(record.MapID, start, end, useCache) + entries, err := fetchRecordsFromMap(record.MapID, start, end, useCache) + if err != nil { + return nil, err + } fetchAnotherPage = entries.needsAnotherPage(&record) if fetchAnotherPage { start = end + 1 @@ -137,10 +153,10 @@ func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, us } } - return players + return players, nil } -func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) *Leaderboard { +func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) (*Leaderboard, error) { var filename string if useCache { 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 if err != nil { log.Fatalln("failed to unmarshal cache.", err.Error()) } - return &leaderboard + return &leaderboard, nil } } url := fmt.Sprintf("https://steamcommunity.com/stats/Portal2/leaderboards/%d?xml=1&start=%d&end=%d", mapID, start, end) resp, err := http.Get(url) - log.Println("fetched", url, ":", resp.StatusCode) if err != nil { - log.Fatalln("failed to fetch leaderboard.", err.Error()) + log.Println("failed to fetch leaderboard.", err.Error()) + return nil, errLb } respBytes, err := io.ReadAll(resp.Body) if err != nil { - log.Fatalln("failed to read leadeboard body.", err.Error()) + log.Println("failed to read leadeboard body.", err.Error()) + return nil, errLb } leaderboard := Leaderboard{} err = xml.Unmarshal(respBytes, &leaderboard) if err != nil { log.Println(string(respBytes)) - log.Fatalln("failed to unmarshal leaderboard.", err.Error()) + log.Println("failed to unmarshal leaderboard.", err.Error()) + return nil, errLb } if useCache { @@ -179,10 +197,10 @@ func fetchRecordsFromMap(mapID int, start int, end int, useCache bool) *Leaderbo } } - return &leaderboard + return &leaderboard, nil } -func fetchPlayerInfo(players []*Player) { +func fetchPlayerInfo(players []*Player) error { log.Println("fetching info for", len(players), "players") ids := make([]string, len(players)) @@ -193,11 +211,13 @@ func fetchPlayerInfo(players []*Player) { url := fmt.Sprintf("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=%s&steamids=%s", os.Getenv("API_KEY"), strings.Join(ids, ",")) resp, err := http.Get(url) if err != nil { - log.Fatalln(err.Error()) + log.Println(err.Error()) + return errPi } body, err := io.ReadAll(resp.Body) if err != nil { - log.Fatalln(err.Error()) + log.Println(err.Error()) + return errPi } type PlayerSummary struct { SteamID SteamID `json:"steamid"` @@ -223,4 +243,5 @@ func fetchPlayerInfo(players []*Player) { } } } + return nil } 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 @@ "id": 47839, "name": "Catapults", "mode": 2, - "wr": 4 + "wr": 3 }, { "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() { overrides := readOverrides() log.Println("loaded", len(overrides), "player overrides") - players := fetchLeaderboard(records, overrides, useCache) + players, err := fetchLeaderboard(records, overrides, useCache) + if err != nil { + return + } spRankings := []*Player{} mpRankings := []*Player{} -- cgit v1.2.3