aboutsummaryrefslogtreecommitdiff
path: root/rankings/fetch.go
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2024-09-12 00:25:15 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2024-09-12 00:25:15 +0300
commitdf6f6cb5ff8957be8f01d58d60857da2c094a3d9 (patch)
tree5ec5a8a95633d7fa6cce62654a9bc6fc6204f788 /rankings/fetch.go
parentrefactor: fix module ver (diff)
downloadlphub-df6f6cb5ff8957be8f01d58d60857da2c094a3d9.tar.gz
lphub-df6f6cb5ff8957be8f01d58d60857da2c094a3d9.tar.bz2
lphub-df6f6cb5ff8957be8f01d58d60857da2c094a3d9.zip
refactor: unofficial rankings implementation
Diffstat (limited to 'rankings/fetch.go')
-rw-r--r--rankings/fetch.go185
1 files changed, 185 insertions, 0 deletions
diff --git a/rankings/fetch.go b/rankings/fetch.go
new file mode 100644
index 0000000..ee5d5bb
--- /dev/null
+++ b/rankings/fetch.go
@@ -0,0 +1,185 @@
1package main
2
3import (
4 "encoding/json"
5 "encoding/xml"
6 "fmt"
7 "io"
8 "log"
9 "net/http"
10 "os"
11 "strconv"
12)
13
14func fetchLeaderboard(records *[]Record, overrides *map[string]map[string]int) *map[string]*Player {
15 log.Println("fetching leaderboard")
16 players := map[string]*Player{}
17 // first init players map with records from portal gun and doors
18 fetchAnotherPage := true
19 start := 0
20 end := 5000
21
22 for fetchAnotherPage {
23 portalGunEntries := fetchRecordsFromMap(47459, 0, 5000)
24 fetchAnotherPage = portalGunEntries.needsAnotherPage(&(*records)[0])
25 if fetchAnotherPage {
26 start = end + 1
27 end = start + 5000
28 }
29 for _, entry := range portalGunEntries.Entries.Entry {
30 if entry.Score < 0 {
31 continue // ban
32 }
33 players[entry.SteamID] = &Player{
34 SteamID: entry.SteamID,
35 Entries: []PlayerEntry{
36 {
37 MapID: 47459,
38 MapScore: entry.Score,
39 },
40 },
41 SpScoreCount: entry.Score,
42 SpIterations: 1,
43 }
44 }
45 }
46
47 fetchAnotherPage = true
48 start = 0
49 end = 5000
50
51 for fetchAnotherPage {
52 doorsEntries := fetchRecordsFromMap(47740, start, end)
53 fetchAnotherPage = doorsEntries.needsAnotherPage(&(*records)[51])
54 if fetchAnotherPage {
55 start = end + 1
56 end = start + 5000
57 }
58 for _, entry := range doorsEntries.Entries.Entry {
59 if entry.Score < 0 {
60 continue // ban
61 }
62 player, ok := players[entry.SteamID]
63 if !ok {
64 players[entry.SteamID] = &Player{
65 SteamID: entry.SteamID,
66 Entries: []PlayerEntry{
67 {
68 MapID: 47740,
69 MapScore: entry.Score,
70 },
71 },
72 MpScoreCount: entry.Score,
73 MpIterations: 1,
74 }
75 } else {
76 player.Entries = append(player.Entries, PlayerEntry{
77 MapID: 47740,
78 MapScore: entry.Score,
79 })
80 player.MpScoreCount = entry.Score
81 player.MpIterations++
82 }
83 }
84 }
85
86 for _, record := range *records {
87 if record.MapID == 47459 || record.MapID == 47740 {
88 continue
89 }
90
91 fetchAnotherPage := true
92 start := 0
93 end := 5000
94
95 for fetchAnotherPage {
96 entries := fetchRecordsFromMap(record.MapID, start, end)
97 fetchAnotherPage = entries.needsAnotherPage(&record)
98 if fetchAnotherPage {
99 start = end + 1
100 end = start + 5000
101 }
102 for _, entry := range (*entries).Entries.Entry {
103 player, ok := players[entry.SteamID]
104 if !ok {
105 continue
106 }
107 score := entry.Score
108 if entry.Score < record.MapWR {
109 _, ok := (*overrides)[entry.SteamID]
110 if ok {
111 _, ok := (*overrides)[entry.SteamID][strconv.Itoa(record.MapID)]
112 if ok {
113 score = (*overrides)[entry.SteamID][strconv.Itoa(record.MapID)]
114 } else {
115 continue // ban
116 }
117 } else {
118 continue // ban
119 }
120 }
121 if record.MapLimit != nil && score > *record.MapLimit {
122 continue // ignore above limit
123 }
124 player.Entries = append(player.Entries, PlayerEntry{
125 MapID: record.MapID,
126 MapScore: score,
127 })
128 if record.MapMode == 1 {
129 player.SpScoreCount += score
130 player.SpIterations++
131 } else if record.MapMode == 2 {
132 player.MpScoreCount += score
133 player.MpIterations++
134 }
135 }
136 }
137
138 }
139 return &players
140}
141
142func fetchRecordsFromMap(mapID int, start int, end int) *Leaderboard {
143 resp, err := http.Get(fmt.Sprintf("https://steamcommunity.com/stats/Portal2/leaderboards/%d?xml=1&start=%d&end=%d", mapID, start, end))
144 if err != nil {
145 log.Fatalln(err.Error())
146 }
147 respBytes, err := io.ReadAll(resp.Body)
148 if err != nil {
149 log.Fatalln(err.Error())
150 }
151 leaderboard := Leaderboard{}
152 err = xml.Unmarshal(respBytes, &leaderboard)
153 if err != nil {
154 log.Fatalln(err.Error())
155 }
156 return &leaderboard
157}
158
159func fetchPlayerInfo(player *Player) {
160 url := fmt.Sprintf("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=%s&steamids=%s", os.Getenv("API_KEY"), player.SteamID)
161 resp, err := http.Get(url)
162 if err != nil {
163 log.Fatalln(err.Error())
164 }
165 body, err := io.ReadAll(resp.Body)
166 if err != nil {
167 log.Fatalln(err.Error())
168 }
169 type PlayerSummary struct {
170 PersonaName string `json:"personaname"`
171 AvatarFull string `json:"avatarfull"`
172 }
173
174 type Result struct {
175 Response struct {
176 Players []PlayerSummary `json:"players"`
177 } `json:"response"`
178 }
179 var data Result
180 if err := json.Unmarshal(body, &data); err != nil {
181 log.Fatalln(err.Error())
182 }
183 player.AvatarLink = data.Response.Players[0].AvatarFull
184 player.Username = data.Response.Players[0].PersonaName
185}