-
Notifications
You must be signed in to change notification settings - Fork 0
/
battlebit.go
178 lines (153 loc) · 4.58 KB
/
battlebit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
const (
BaseURL = "https://publicapi.battlebit.cloud/"
LeaderboardGetURL = BaseURL + "Leaderboard/Get"
)
type Category string
const (
CategoryAll Category = "all"
CategoryClans Category = "clans"
CategoryXP Category = "xp"
CategoryHeals Category = "heals"
CategoryRevives Category = "revives"
CategoryVehiclesDestroyed Category = "vehiclesDestroyed"
CategoryVehicleRepairs Category = "vehicleRepairs"
CategoryRoadkills Category = "roadkills"
CategoryLongestKill Category = "longestKill"
CategoryObjectives Category = "objectives"
CategoryKills Category = "kills"
)
var AllCategories = []Category{
CategoryClans,
CategoryXP,
CategoryHeals,
CategoryRevives,
CategoryVehiclesDestroyed,
CategoryVehicleRepairs,
CategoryRoadkills,
CategoryLongestKill,
CategoryObjectives,
CategoryKills,
}
type PlayerEntry struct {
Name string `json:"Name" bson:"Name"`
Value string `json:"Value" bson:"Value"`
}
type ClanEntry struct {
Clan string `json:"Clan" bson:"Clan"`
Tag string `json:"Tag" bson:"Tag"`
XP string `json:"XP" bson:"XP"`
MaxPlayers string `json:"MaxPlayers" bson:"MaxPlayers"`
}
type Leaderboard struct {
Id *primitive.ObjectID `bson:"_id,omitempty"`
TopClans *[]*ClanEntry `json:"TopClans,omitempty" bson:"TopClans,omitempty"`
MostXP *[]*PlayerEntry `json:"MostXP,omitempty" bson:"MostXP,omitempty"`
MostHeals *[]*PlayerEntry `json:"MostHeals,omitempty" bson:"MostHeals,omitempty"`
MostRevives *[]*PlayerEntry `json:"MostRevives,omitempty" bson:"MostRevives,omitempty"`
MostVehiclesDestroyed *[]*PlayerEntry `json:"MostVehiclesDestroyed,omitempty" bson:"MostVehiclesDestroyed,omitempty"`
MostVehicleRepairs *[]*PlayerEntry `json:"MostVehicleRepairs,omitempty" bson:"MostVehicleRepairs,omitempty"`
MostRoadkills *[]*PlayerEntry `json:"MostRoadkills,omitempty" bson:"MostRoadkills,omitempty"`
MostKills *[]*PlayerEntry `json:"MostKills,omitempty" bson:"MostKills,omitempty"`
}
func StringToCategory(s string) Category {
switch s {
case "clans":
return CategoryClans
case "xp":
return CategoryXP
case "heals":
return CategoryHeals
case "revives":
return CategoryRevives
case "vehiclesDestroyed":
return CategoryVehiclesDestroyed
case "vehicleRepairs":
return CategoryVehicleRepairs
case "roadkills":
return CategoryRoadkills
case "longestKill":
return CategoryLongestKill
case "objectives":
return CategoryObjectives
case "kills":
return CategoryKills
default:
return CategoryAll
}
}
func (c Category) ToMongoField() string {
switch c {
case CategoryClans:
return "TopClans"
case CategoryXP:
return "MostXP"
case CategoryHeals:
return "MostHeals"
case CategoryRevives:
return "MostRevives"
case CategoryVehiclesDestroyed:
return "MostVehiclesDestroyed"
case CategoryVehicleRepairs:
return "MostVehicleRepairs"
case CategoryRoadkills:
return "MostRoadkills"
case CategoryLongestKill:
return "MostLongestKill"
case CategoryObjectives:
return "MostObjectives"
case CategoryKills:
return "MostKills"
default:
return ""
}
}
func FetchLeaderboard() (*Leaderboard, error) {
resp, err := http.Get(LeaderboardGetURL)
if err != nil {
return nil, fmt.Errorf("error fetching leaderboard: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http request for leaderboard failed with status code: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %s", err)
}
// the fact i have to do this shit is so stupid, oki please let someone else write your api code
body = bytes.TrimPrefix(body, []byte("\xef\xbb\xbf")) // remove BOM
var j []json.RawMessage
if err := json.Unmarshal(body, &j); err != nil {
return nil, fmt.Errorf("error decoding leaderboard: %s", err)
}
var lb Leaderboard
for _, v := range j {
if err := json.Unmarshal(v, &lb); err != nil {
return nil, fmt.Errorf("error decoding leaderboard: %s", err)
}
}
return &lb, err
}
func (api *API) UpdateLeaderboardJob() {
for {
leaderboard, err := FetchLeaderboard()
if err != nil {
log.Printf("non fatal error fetching leaderboard: %s", err)
time.Sleep(10 * time.Second)
continue
}
api.db.AddLeaderboard(leaderboard)
time.Sleep(1 * time.Minute)
}
}