-
Notifications
You must be signed in to change notification settings - Fork 4
/
epic.go
440 lines (379 loc) · 14.4 KB
/
epic.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package fornitego
import (
"bytes"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
)
// Epic API endpoints
const (
oauthTokenURL = "https://account-public-service-prod03.ol.epicgames.com/account/api/oauth/token"
oauthExchangeURL = "https://account-public-service-prod03.ol.epicgames.com/account/api/oauth/exchange"
accountLookupURL = "https://persona-public-service-prod06.ol.epicgames.com/persona/api/public/account"
accountInfoURL = "https://account-public-service-prod03.ol.epicgames.com/account/api/public/account"
killSessionURL = "https://account-public-service-prod03.ol.epicgames.com/account/api/oauth/sessions/kill"
serverStatusURL = "https://lightswitch-public-service-prod06.ol.epicgames.com/lightswitch/api/service/bulk/status?serviceId=Fortnite"
accountStatsURL = "https://fortnite-public-service-prod11.ol.epicgames.com/fortnite/api/stats/accountId"
winsLeaderboardURL = "https://fortnite-public-service-prod11.ol.epicgames.com/fortnite/api/leaderboards/type/global/stat/br_placetop1_%v_m0%v/window/weekly"
)
// Platform types
const (
PC = "pc"
Xbox = "xb1"
PS4 = "ps4"
)
// tokenResponse defines the response collected by a request to the OAUTH token endpoint.
type tokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
ExpiresAt string `json:"expires_at"`
RefreshToken string `json:"refresh_token"`
RefreshExpires int `json:"refresh_expires"`
RefreshExpiresAt string `json:"refresh_expires_at"`
AccountID string `json:"account_id"`
ClientID string `json:"client_id"`
}
// tokenResponse defines the response collected by a request to the OAUTH exchange endpoint.
type exchangeResponse struct {
ExpiresInSeconds int `json:"expiresInSeconds"`
Code string `json:"code"`
CreatingClientID string `json:"creatingClientId"`
}
// lookupResponse defines the response collected by a request to the persona lookup endpoint.
type lookupResponse struct {
ID string `json:"id"`
DisplayName string `json:"displayName"`
}
// statsResponse defines the response collected by a request to the battle royal stats endpoint.
type statsResponse []statsRecord
// statsRecord defines a single entry in a statsResponse.
type statsRecord struct {
Name string `json:"name"`
Value int `json:"value"`
Window string `json:"window"`
OwnerType int `json:"ownerType"`
}
// Player is the hierarchical struct used to contain information regarding a player's account info and stats.
type Player struct {
AccountInfo AccountInfo
Stats Stats
}
// AccountInfo contains basic information about the user.
type AccountInfo struct {
AccountID string
Username string
Platform string
}
// Stats is the structure which holds the player's stats for the 3 different game modes offered in Battle Royal.
type Stats struct {
Solo statDetails
Duo statDetails
Squad statDetails
}
// statDetails is the specific statistics for any given group mode.
type statDetails struct {
Wins int
Top3 int `json:",omitempty"` // Squad-only
Top5 int `json:",omitempty"` // Duo-only
Top6 int `json:",omitempty"` // Squad-only
Top10 int `json:",omitempty"` // Solo-only
Top12 int `json:",omitempty"` // Duo-only
Top25 int `json:",omitempty"` // Solo-only
KillDeathRatio string
WinPercentage string
Matches int
Kills int
MinutesPlayed int
KillsPerMatch string
KillsPerMinute string
Score int
}
// GlobalWinsLeaderboard contains an array of the top X players by wins on a specific platform and party mode.
type GlobalWinsLeaderboard []leaderboardEntry
// leaderboardEntry defines a single entry in a GlobalWinsLeaderboard object.
type leaderboardEntry struct {
DisplayName string
Rank int
Wins int
}
// QueryPlayer looks up a player by their username and platform, and returns information about that player, namely, the
// statistics for the 3 different party modes.
func (s *Session) QueryPlayer(name string, accountId string, platform string) (*Player, error) {
if name == "" && accountId == "" {
return nil, errors.New("no player name or id provided")
}
switch platform {
case PC, Xbox, PS4:
default:
return nil, errors.New("invalid platform specified")
}
if name != "" && accountId == "" {
userInfo, err := s.findUserInfo(name)
if err != nil {
return nil, err
}
accountId = userInfo.ID
}
sr, err := s.QueryPlayerById(accountId)
if err != nil {
return nil, err
}
acctInfoMap, err := s.getAccountNames([]string{accountId})
if err != nil {
return nil, err
}
cleanAcctID := strings.Replace(accountId, "-", "", -1)
return &Player{
AccountInfo: AccountInfo{
AccountID: accountId,
Username: acctInfoMap[cleanAcctID],
Platform: platform,
},
Stats: s.mapStats(sr, platform),
}, nil
}
func (s *Session) QueryPlayerById(accountId string) (*statsResponse, error) {
u := fmt.Sprintf("%v/%v/%v/%v/%v", accountStatsURL, accountId, "bulk", "window", "alltime")
req, err := s.client.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
// Set authorization to use access token.
req.Header.Set("Authorization", fmt.Sprintf("%v %v", AuthBearer, s.AccessToken))
sr := &statsResponse{}
resp, err := s.client.Do(req, sr)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if len(*sr) == 0 {
return nil, errors.New("no statistics found for player " + accountId)
}
return sr, nil
}
// findUserInfo requests additional account information by a username.
func (s *Session) findUserInfo(username string) (*lookupResponse, error) {
req, err := s.client.NewRequest(http.MethodGet, accountLookupURL+"/lookup?q="+url.QueryEscape(username), nil)
if err != nil {
return nil, err
}
// Set authorization to use access token.
req.Header.Set("Authorization", fmt.Sprintf("%v %v", AuthBearer, s.AccessToken))
ret := &lookupResponse{}
resp, err := s.client.Do(req, ret)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if ret.ID == "" {
return nil, errors.New("player not found")
}
return ret, nil
}
// Name identifiers for group type. Used in parsing URLs and responses.
const (
Solo = "_p2"
Duo = "_p10"
Squad = "_p9"
)
// getStatType is a simple helper function to return the party type if present in a given string.
func getStatType(seed string) string {
switch {
case strings.Contains(seed, Solo):
return Solo
case strings.Contains(seed, Duo):
return Duo
default: // _p9
return Squad
}
}
// mapStats takes a statsResponse object and converts it into a Stats object. It parses the JSON returned from Epic
// regarding a player's stats, and maps it accordingly based on party type, as well as calculates several useful ratios.
func (s *Session) mapStats(records *statsResponse, platform string) Stats {
// Initialize new map with stat details objects based on group type.
groups := make(map[string]*statDetails)
groups[Solo] = &statDetails{}
groups[Duo] = &statDetails{}
groups[Squad] = &statDetails{}
// Loop through the stats for a specific user properly sorting and organizing by group type into their own objects.
for _, record := range *records {
switch {
case strings.Contains(record.Name, "placetop1_"+platform):
groups[getStatType(record.Name)].Wins = record.Value
case strings.Contains(record.Name, "placetop3_"+platform):
groups[getStatType(record.Name)].Top3 = record.Value
case strings.Contains(record.Name, "placetop5_"+platform):
groups[getStatType(record.Name)].Top5 = record.Value
case strings.Contains(record.Name, "placetop6_"+platform):
groups[getStatType(record.Name)].Top6 = record.Value
case strings.Contains(record.Name, "placetop10_"+platform):
groups[getStatType(record.Name)].Top10 = record.Value
case strings.Contains(record.Name, "placetop12_"+platform):
groups[getStatType(record.Name)].Top12 = record.Value
case strings.Contains(record.Name, "placetop25_"+platform):
groups[getStatType(record.Name)].Top25 = record.Value
case strings.Contains(record.Name, "matchesplayed_"+platform):
groups[getStatType(record.Name)].Matches = record.Value
case strings.Contains(record.Name, "kills_"+platform):
groups[getStatType(record.Name)].Kills = record.Value
case strings.Contains(record.Name, "score_"+platform):
groups[getStatType(record.Name)].Score = record.Value
case strings.Contains(record.Name, "minutesplayed_"+platform):
groups[getStatType(record.Name)].MinutesPlayed = record.Value
}
}
// Build new return object using the prepared map data.
ret := Stats{
Solo: *groups[Solo],
Duo: *groups[Duo],
Squad: *groups[Squad],
}
// Calculate additional information such as kill/death ratios, win percentages, etc.
calculateStatsRatios(&ret.Solo)
calculateStatsRatios(&ret.Duo)
calculateStatsRatios(&ret.Squad)
// Return built Stats object.
return ret
}
// calculateStatsRatios takes a party-specific statDetails object and performs ratio calculations on specific data to
// provide kill death ratio, win percentage, and kills per minute/match.
func calculateStatsRatios(s *statDetails) {
s.KillDeathRatio = strconv.FormatFloat(ratio(s.Kills, s.Matches-s.Wins), 'f', 2, 64)
s.WinPercentage = strconv.FormatFloat(ratio(s.Wins, s.Matches)*100, 'f', 2, 64)
s.KillsPerMinute = strconv.FormatFloat(ratio(s.Kills, s.MinutesPlayed), 'f', 2, 64)
s.KillsPerMatch = strconv.FormatFloat(ratio(s.Kills, s.Matches), 'f', 2, 64)
}
// ratio is a helper function to perform float division without causing a division by 0 panic.
func ratio(a, b int) float64 {
if b == 0 {
return 0
}
return float64(a) / float64(b)
}
type leaderboardResponse struct {
StatName string `json:"statName"`
StatWindow string `json:"statWindow"`
Entries []struct {
AccountID string `json:"accountId"`
Value int `json:"value"`
Rank int `json:"rank"`
} `json:"entries"`
}
// GetWinsLeaderboard returns the top 50 players and their rank position based on global wins for a specific platform,
// and party/group type.
func (s *Session) GetWinsLeaderboard(platform, groupType string) (*GlobalWinsLeaderboard, error) {
qp := url.Values{}
qp.Add("ownertype", "1") // unknown
qp.Add("pageNumber", "0") // not implemented in-game?
qp.Add("itemsPerPage", "50") // definable up to how many?
// Prepare new request to obtain leaderboard information. Epic literally expects an empty JSON array as input in
// order for the request to be valid, hence sending a buffer of an empty array.
u := fmt.Sprintf(winsLeaderboardURL, platform, groupType) + "?" + qp.Encode()
req, err := s.client.NewRequest(http.MethodPost, u, bytes.NewBufferString("[]"))
if err != nil {
return nil, err
}
// Use access token and set content type to JSON since we're ending an empty array with the request.
req.Header.Set("Authorization", fmt.Sprintf("%v %v", AuthBearer, s.AccessToken))
req.Header.Set("Content-Type", "application/json")
// Perform request and collect response data into leaderboardResponse object.
lr := &leaderboardResponse{}
resp, err := s.client.Do(req, lr)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Loop through entries received building an array of account IDs.
var accountIDs []string
for _, item := range lr.Entries {
accountIDs = append(accountIDs, item.AccountID)
}
// Send account IDs off to be queried so we can collect their human-readable display name (Epic Username).
acctInfoMap, err := s.getAccountNames(accountIDs)
if err != nil {
return nil, err
}
// Initialize return object, and look through entries once more mapping their username as display name obtained
// just before.
ret := GlobalWinsLeaderboard{}
for _, b := range lr.Entries {
cleanAcctID := strings.Replace(b.AccountID, "-", "", -1)
ret = append(ret, leaderboardEntry{
DisplayName: acctInfoMap[cleanAcctID],
Rank: b.Rank,
Wins: b.Value,
})
}
// Return new leaderboard object.
return &ret, nil
}
// getAccountNames is a helper to query a bulk amount of account IDs to get additional information on them, in
// particular, their username.
func (s *Session) getAccountNames(ids []string) (map[string]string, error) {
// Build query parameter string based on account IDs supplied.
var p string
for _, id := range ids {
// Note: Epic strips the hyphens '-' in the request.
p += "accountId=" + strings.Replace(id, "-", "", -1) + "&"
}
p = p[:len(p)-1] // Strip trailing '&'.
// Prepare new request to the persona server for information about these accounts.
req, err := s.client.NewRequest(http.MethodGet, accountInfoURL+"?"+p, nil)
if err != nil {
return nil, err
}
// Set authorization header to use our access token.
req.Header.Set("Authorization", fmt.Sprintf("%v %v", AuthBearer, s.AccessToken))
// Perform query and collect response into an array of lookupResponse objects.
var data []lookupResponse
resp, err := s.client.Do(req, &data)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Prepare return map where we will map the account ID to the newly-collected username (DisplayName).
ret := make(map[string]string)
for _, a := range data {
ret[a.ID] = a.DisplayName
}
return ret, nil
}
// statusResponse is the expected response from a server status check.
type statusResponse []struct {
Status string `json:"status"`
Message string `json:"message"`
MaintenanceURI interface{} `json:"maintenanceUri"`
}
// CheckStatus checks the status of the Fortnite game service. Will return false with error containing the status
// message from Epic.
func (s *Session) CheckStatus() (bool, error) {
// Prepare new request.
req, err := s.client.NewRequest(http.MethodGet, serverStatusURL, nil)
if err != nil {
return false, err
}
// Set authorization header to use access token.
req.Header.Set("Authorization", fmt.Sprintf("%v %v", AuthBearer, s.AccessToken))
// Perform request and decode response into a statusResponse object.
var sr statusResponse
resp, err := s.client.Do(req, &sr)
if err != nil {
return false, err
}
defer resp.Body.Close()
// Ensure at least one value of the array has been provided to prevent panic.
if len(sr) == 0 {
return false, errors.New("no status response received")
}
// Switch between the status string to determine whether the service is up or down.
switch sr[0].Status {
case "UP":
// Never return the message here since it doesn't seem to be removed when the server resume online status.
return true, nil
default:
return false, errors.New("service is down: " + sr[0].Message)
}
}