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
|
package api0
import (
"crypto/sha256"
"encoding/hex"
"net/http"
"strconv"
"time"
"github.com/pg9182/atlas/pkg/pdata"
"github.com/rs/zerolog/hlog"
)
func pdataFilterInfo(path ...string) bool {
switch path[0] {
case "gen", "xp", "activeCallingCardIndex", "activeCallsignIconIndex", "activeCallsignIconStyleIndex", "netWorth":
return true
default:
return false
}
}
func pdataFilterStats(path ...string) bool {
switch path[0] {
case "gen", "xp", "credits", "netWorth", "factionXP", "titanXP", "fdTitanXP", "gameStats", "mapStats", "timeStats",
"distanceStats", "weaponStats", "weaponKillStats", "killStats", "deathStats", "miscStats", "fdStats", "titanStats",
"kdratio_lifetime", "kdratio_lifetime_pvp", "winStreak", "highestWinStreakEver":
return true
default:
return false
}
}
func pdataFilterLoadout(path ...string) bool {
switch path[0] {
case "factionChoice", "activePilotLoadout", "activeTitanLoadout", "pilotLoadouts", "titanLoadouts":
return true
default:
return false
}
}
func (h *Handler) handlePlayer(w http.ResponseWriter, r *http.Request) {
var pdataFilter func(...string) bool
switch r.URL.Path {
case "/player/pdata":
pdataFilter = nil
case "/player/info":
pdataFilter = pdataFilterInfo
case "/player/stats":
pdataFilter = pdataFilterStats
case "/player/loadout":
pdataFilter = pdataFilterLoadout
default:
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
if r.Method != http.MethodOptions && r.Method != http.MethodGet && r.Method != http.MethodHead {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
// - cache publicly, allow reusing responses for multiple users
// - allow reusing responses if server is down
// - cache for up to 30s
// - check for updates after 15s
w.Header().Set("Cache-Control", "public, max-age=15, stale-while-revalidate=15")
w.Header().Set("Expires", time.Now().UTC().Add(time.Second*30).Format(http.TimeFormat))
// - allow CORS requests from all origins
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, HEAD")
w.Header().Set("Access-Control-Max-Age", "86400")
if r.Method == http.MethodOptions {
w.Header().Set("Allow", "OPTIONS, GET, HEAD")
w.WriteHeader(http.StatusNoContent)
return
}
uidQ := r.URL.Query().Get("id")
if uidQ == "" {
respJSON(w, r, http.StatusBadRequest, map[string]any{
"success": false,
"error": ErrorCode_BAD_REQUEST,
"msg": ErrorCode_BAD_REQUEST.Messagef("id param is required"),
})
return
}
uid, err := strconv.ParseUint(uidQ, 10, 64)
if err != nil {
respJSON(w, r, http.StatusNotFound, map[string]any{
"success": false,
"error": ErrorCode_PLAYER_NOT_FOUND,
})
return
}
// if it's a HEAD request, we just need the hash to set the etag
if r.Method == http.MethodHead {
hash, exists, err := h.PdataStorage.GetPdataHash(uid)
if err != nil {
hlog.FromRequest(r).Error().
Err(err).
Uint64("uid", uid).
Msgf("failed to read pdata hash from storage")
w.WriteHeader(http.StatusInternalServerError)
return
}
if !exists {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("ETag", `W/"`+hex.EncodeToString(hash[:])+`"`)
w.WriteHeader(http.StatusOK)
return
}
buf, exists, err := h.PdataStorage.GetPdataCached(uid, [sha256.Size]byte{})
if err != nil {
hlog.FromRequest(r).Error().
Err(err).
Uint64("uid", uid).
Msgf("failed to read pdata from storage")
respJSON(w, r, http.StatusInternalServerError, map[string]any{
"success": false,
"error": ErrorCode_INTERNAL_SERVER_ERROR,
"msg": ErrorCode_INTERNAL_SERVER_ERROR.Message(),
})
return
}
if !exists {
respJSON(w, r, http.StatusNotFound, map[string]any{
"success": false,
"error": ErrorCode_PLAYER_NOT_FOUND,
})
return
}
hash := sha256.Sum256(buf)
w.Header().Set("ETag", `W/"`+hex.EncodeToString(hash[:])+`"`)
var pd pdata.Pdata
if err := pd.UnmarshalBinary(buf); err != nil {
hlog.FromRequest(r).Warn().
Err(err).
Uint64("uid", uid).
Str("pdata_sha256", hex.EncodeToString(hash[:])).
Msgf("failed to parse pdata from storage")
respJSON(w, r, http.StatusInternalServerError, map[string]any{
"success": false,
"error": ErrorCode_INTERNAL_SERVER_ERROR,
"msg": ErrorCode_INTERNAL_SERVER_ERROR.Messagef("failed to parse pdata from storage"),
})
return
}
jbuf, err := pd.MarshalJSONFilter(pdataFilter)
if err != nil {
hlog.FromRequest(r).Error().
Err(err).
Uint64("uid", uid).
Str("pdata_sha256", hex.EncodeToString(hash[:])).
Msgf("failed to encode pdata as json")
respJSON(w, r, http.StatusInternalServerError, map[string]any{
"success": false,
"error": ErrorCode_INTERNAL_SERVER_ERROR,
"msg": ErrorCode_INTERNAL_SERVER_ERROR.Messagef("failed to encode pdata as json"),
})
return
}
jbuf = append(jbuf, '\n')
w.Header().Set("Content-Type", "application/json; charset=utf-8")
respMaybeCompress(w, r, http.StatusOK, jbuf)
}
|