diff options
| -rw-r--r-- | rankings/fetch.go | 12 | ||||
| -rw-r--r-- | rankings/filter.go | 4 | ||||
| -rw-r--r-- | rankings/models.go | 31 | ||||
| -rw-r--r-- | rankings/prefetch.go | 4 |
4 files changed, 38 insertions, 13 deletions
diff --git a/rankings/fetch.go b/rankings/fetch.go index cf04e81..7e63427 100644 --- a/rankings/fetch.go +++ b/rankings/fetch.go | |||
| @@ -12,9 +12,9 @@ import ( | |||
| 12 | "strings" | 12 | "strings" |
| 13 | ) | 13 | ) |
| 14 | 14 | ||
| 15 | func fetchLeaderboard(records []Record, overrides map[string]map[string]int, useCache bool) map[string]*Player { | 15 | func fetchLeaderboard(records []Record, overrides map[SteamID]map[string]int, useCache bool) map[SteamID]*Player { |
| 16 | log.Println("fetching leaderboard") | 16 | log.Println("fetching leaderboard") |
| 17 | players := map[string]*Player{} | 17 | players := map[SteamID]*Player{} |
| 18 | // first init players map with records from portal gun and doors | 18 | // first init players map with records from portal gun and doors |
| 19 | fetchAnotherPage := true | 19 | fetchAnotherPage := true |
| 20 | start := 0 | 20 | start := 0 |
| @@ -187,7 +187,7 @@ func fetchPlayerInfo(players []*Player) { | |||
| 187 | 187 | ||
| 188 | ids := make([]string, len(players)) | 188 | ids := make([]string, len(players)) |
| 189 | for _, player := range players { | 189 | for _, player := range players { |
| 190 | ids = append(ids, player.SteamID) | 190 | ids = append(ids, strconv.FormatInt(int64(player.SteamID), 10)) |
| 191 | } | 191 | } |
| 192 | 192 | ||
| 193 | url := fmt.Sprintf("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=%s&steamids=%s", os.Getenv("API_KEY"), strings.Join(ids, ",")) | 193 | url := fmt.Sprintf("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=%s&steamids=%s", os.Getenv("API_KEY"), strings.Join(ids, ",")) |
| @@ -200,9 +200,9 @@ func fetchPlayerInfo(players []*Player) { | |||
| 200 | log.Fatalln(err.Error()) | 200 | log.Fatalln(err.Error()) |
| 201 | } | 201 | } |
| 202 | type PlayerSummary struct { | 202 | type PlayerSummary struct { |
| 203 | SteamID string `json:"steamid"` | 203 | SteamID SteamID `json:"steamid"` |
| 204 | PersonaName string `json:"personaname"` | 204 | PersonaName string `json:"personaname"` |
| 205 | AvatarFull string `json:"avatarfull"` | 205 | AvatarFull string `json:"avatarfull"` |
| 206 | } | 206 | } |
| 207 | 207 | ||
| 208 | type Result struct { | 208 | type Result struct { |
diff --git a/rankings/filter.go b/rankings/filter.go index f90321f..0bfde1e 100644 --- a/rankings/filter.go +++ b/rankings/filter.go | |||
| @@ -6,7 +6,7 @@ import ( | |||
| 6 | "sort" | 6 | "sort" |
| 7 | ) | 7 | ) |
| 8 | 8 | ||
| 9 | func filterRankings(spRankings, mpRankings, overallRankings *[]*Player, players map[string]*Player) { | 9 | func filterRankings(spRankings, mpRankings, overallRankings *[]*Player, players map[SteamID]*Player) { |
| 10 | for k, p := range players { | 10 | for k, p := range players { |
| 11 | if p.SpIterations == 51 { | 11 | if p.SpIterations == 51 { |
| 12 | *spRankings = append(*spRankings, p) | 12 | *spRankings = append(*spRankings, p) |
| @@ -109,7 +109,7 @@ func filterRankings(spRankings, mpRankings, overallRankings *[]*Player, players | |||
| 109 | } | 109 | } |
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | func chunkMap[T any](m map[string]*T, chunkSize int) [][]*T { | 112 | func chunkMap[T any, K comparable](m map[K]*T, chunkSize int) [][]*T { |
| 113 | chunks := make([][]*T, 0, int(math.Ceil(float64(len(m))/float64(chunkSize)))) | 113 | chunks := make([][]*T, 0, int(math.Ceil(float64(len(m))/float64(chunkSize)))) |
| 114 | chunk := make([]*T, 0, chunkSize) | 114 | chunk := make([]*T, 0, chunkSize) |
| 115 | 115 | ||
diff --git a/rankings/models.go b/rankings/models.go index 1b349b0..0e15d34 100644 --- a/rankings/models.go +++ b/rankings/models.go | |||
| @@ -1,5 +1,11 @@ | |||
| 1 | package main | 1 | package main |
| 2 | 2 | ||
| 3 | import ( | ||
| 4 | "encoding/json" | ||
| 5 | "fmt" | ||
| 6 | "strconv" | ||
| 7 | ) | ||
| 8 | |||
| 3 | type Record struct { | 9 | type Record struct { |
| 4 | MapID int `json:"id"` | 10 | MapID int `json:"id"` |
| 5 | MapName string `json:"name"` | 11 | MapName string `json:"name"` |
| @@ -25,15 +31,34 @@ type LeaderboardEntries struct { | |||
| 25 | Entry []LeaderboardEntry `xml:"entry"` | 31 | Entry []LeaderboardEntry `xml:"entry"` |
| 26 | } | 32 | } |
| 27 | 33 | ||
| 34 | type SteamID int64 | ||
| 35 | |||
| 36 | func (m SteamID) MarshalJSON() ([]byte, error) { | ||
| 37 | return json.Marshal(strconv.FormatInt(int64(m), 10)) | ||
| 38 | } | ||
| 39 | |||
| 40 | func (id *SteamID) UnmarshalJSON(data []byte) error { | ||
| 41 | var s string | ||
| 42 | if err := json.Unmarshal(data, &s); err == nil { | ||
| 43 | n, err := strconv.ParseInt(s, 10, 64) | ||
| 44 | if err != nil { | ||
| 45 | return err | ||
| 46 | } | ||
| 47 | *id = SteamID(n) | ||
| 48 | return nil | ||
| 49 | } | ||
| 50 | return fmt.Errorf("invalid type for SteamID: %s", data) | ||
| 51 | } | ||
| 52 | |||
| 28 | type LeaderboardEntry struct { | 53 | type LeaderboardEntry struct { |
| 29 | SteamID string `xml:"steamid"` | 54 | SteamID SteamID `xml:"steamid"` |
| 30 | Score int `xml:"score"` | 55 | Score int `xml:"score"` |
| 31 | } | 56 | } |
| 32 | 57 | ||
| 33 | type Player struct { | 58 | type Player struct { |
| 34 | Username string `json:"user_name"` | 59 | Username string `json:"user_name"` |
| 35 | AvatarLink string `json:"avatar_link"` | 60 | AvatarLink string `json:"avatar_link"` |
| 36 | SteamID string `json:"steam_id"` | 61 | SteamID SteamID `json:"steam_id"` |
| 37 | Entries []PlayerEntry `json:"-"` | 62 | Entries []PlayerEntry `json:"-"` |
| 38 | SpScoreCount int `json:"sp_score"` | 63 | SpScoreCount int `json:"sp_score"` |
| 39 | MpScoreCount int `json:"mp_score"` | 64 | MpScoreCount int `json:"mp_score"` |
diff --git a/rankings/prefetch.go b/rankings/prefetch.go index a559b26..82bbb9c 100644 --- a/rankings/prefetch.go +++ b/rankings/prefetch.go | |||
| @@ -25,7 +25,7 @@ func readRecords() []Record { | |||
| 25 | return records | 25 | return records |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | func readOverrides() map[string]map[string]int { | 28 | func readOverrides() map[SteamID]map[string]int { |
| 29 | overridesFile, err := os.Open("./input/overrides.json") | 29 | overridesFile, err := os.Open("./input/overrides.json") |
| 30 | if err != nil { | 30 | if err != nil { |
| 31 | log.Fatalln(err.Error()) | 31 | log.Fatalln(err.Error()) |
| @@ -35,7 +35,7 @@ func readOverrides() map[string]map[string]int { | |||
| 35 | if err != nil { | 35 | if err != nil { |
| 36 | log.Fatalln(err.Error()) | 36 | log.Fatalln(err.Error()) |
| 37 | } | 37 | } |
| 38 | overrides := map[string]map[string]int{} | 38 | overrides := map[SteamID]map[string]int{} |
| 39 | err = json.Unmarshal(overridesFileBytes, &overrides) | 39 | err = json.Unmarshal(overridesFileBytes, &overrides) |
| 40 | if err != nil { | 40 | if err != nil { |
| 41 | log.Fatalln(err.Error()) | 41 | log.Fatalln(err.Error()) |