diff options
| author | NeKz <NeKzor@users.noreply.github.com> | 2024-11-18 08:54:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-18 10:54:57 +0300 |
| commit | 780bcf4e02704553f0e3c4879c60230040bfa6f4 (patch) | |
| tree | 36fafa2b007561df9c627b5ad6fce5e064e0bdc8 /rankings/models.go | |
| parent | feat/rankings: make sorting stable (#235) (diff) | |
| download | lphub-780bcf4e02704553f0e3c4879c60230040bfa6f4.tar.gz lphub-780bcf4e02704553f0e3c4879c60230040bfa6f4.tar.bz2 lphub-780bcf4e02704553f0e3c4879c60230040bfa6f4.zip | |
feat/rankings: optimize Steam ID comparison (#236)
Diffstat (limited to 'rankings/models.go')
| -rw-r--r-- | rankings/models.go | 31 |
1 files changed, 28 insertions, 3 deletions
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"` |