better sharecode api handling, renamed endpoint to /player/id/track

This commit is contained in:
2021-10-17 12:27:35 +02:00
parent 9973951d4b
commit d39cdf8ed6
3 changed files with 114 additions and 15 deletions

101
main.go
View File

@@ -90,8 +90,22 @@ func housekeeping() {
for _, tPlayer := range tPlayerNeedShareCodeUpdate {
shareCodes, err := utils.GetNewShareCodesForPlayer(tPlayer, db.Lock, conf.Steam.APIKey, rL)
if err != nil {
log.Errorf("[HK] Error while request sharecodes: %v", err)
continue
switch err.(type) {
case utils.AuthcodeUnauthorizedError:
db.Lock.Lock()
err = tPlayer.Update().ClearAuthCode().ClearSharecodeUpdated().Exec(context.Background())
db.Lock.Unlock()
if err != nil {
log.Warningf("[HK] Unable to clear authcode for player %d: %v", tPlayer.ID, err)
}
continue
case utils.SharecodeNoMatchError:
log.Warningf("[HK] last shareCode for player %d does not match player", tPlayer.ID)
continue
default:
log.Errorf("[HK] Error while request sharecodes: %v", err)
continue
}
}
for _, code := range shareCodes {
@@ -256,11 +270,62 @@ func getPlayer(w http.ResponseWriter, r *http.Request) {
}
}
func postPlayerTrackMe(w http.ResponseWriter, r *http.Request) {
func deletePlayerTrack(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", conf.Httpd.CORSAllowDomains)
err := r.ParseForm()
if err != nil {
log.Infof("[PPTM] Unable to parse form data: %v", err)
log.Infof("[DPT] Unable to parse form data: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
id := r.Form.Get("id")
authCode := r.Form.Get("authcode")
if id == "" || authCode == "" || !utils.AuthCodeRegEx.MatchString(authCode) {
log.Infof("[PPTM] invalid arguments: %+v, %+v", id, authCode)
w.WriteHeader(http.StatusBadRequest)
return
}
tPlayer, err := utils.GetPlayer(db, id, conf.Steam.APIKey, rL)
if err != nil {
log.Infof("[PPT] player not found: %+v", err)
w.WriteHeader(http.StatusNotFound)
return
}
_, err = utils.IsAuthCodeValid(tPlayer, db.Lock, conf.Steam.APIKey, "", authCode, rL)
if err != nil {
switch e := err.(type) {
case utils.AuthcodeUnauthorizedError:
log.Infof("[DPT] authCode provided for player %s is invalid: %v", id, e)
w.WriteHeader(http.StatusUnauthorized)
return
default:
log.Infof("[DPT] Temporary Steam-API problem: %v", e)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
}
db.Lock.Lock()
err = tPlayer.Update().ClearAuthCode().ClearSharecodeUpdated().Exec(context.Background())
db.Lock.Unlock()
if err != nil {
log.Warningf("[PPT] update player failed: %+v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func postPlayerTrack(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", conf.Httpd.CORSAllowDomains)
err := r.ParseForm()
if err != nil {
log.Infof("[PPT] Unable to parse form data: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
@@ -270,30 +335,41 @@ func postPlayerTrackMe(w http.ResponseWriter, r *http.Request) {
shareCode := r.Form.Get("sharecode")
if id == "" || authCode == "" || !utils.AuthCodeRegEx.MatchString(authCode) {
log.Infof("[PPTM] invalid arguments: %+v, %+v, %+v", id, authCode, shareCode)
log.Infof("[PPT] invalid arguments: %+v, %+v, %+v", id, authCode, shareCode)
w.WriteHeader(http.StatusBadRequest)
return
}
tPlayer, err := utils.GetPlayer(db, id, conf.Steam.APIKey, rL)
if err != nil {
log.Infof("[PPTM] player not found: %+v", err)
log.Infof("[PPT] player not found: %+v", err)
w.WriteHeader(http.StatusNotFound)
return
}
_, err = utils.IsAuthCodeValid(tPlayer, db.Lock, conf.Steam.APIKey, shareCode, authCode, rL)
if err != nil {
log.Infof("[PPTM] authCode provided for player %s is invalid: %v", id, err)
w.WriteHeader(http.StatusUnauthorized)
return
switch e := err.(type) {
case utils.AuthcodeUnauthorizedError:
log.Infof("[PPT] authCode provided for player %s is invalid: %v", id, e)
w.WriteHeader(http.StatusUnauthorized)
return
case utils.SharecodeNoMatchError:
log.Infof("[PPT] shareCode provided for player %s is invalid: %v", id, e)
w.WriteHeader(http.StatusPreconditionFailed)
return
default:
log.Infof("[PPT] Temporary Steam-API problem: %v", e)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
}
db.Lock.Lock()
err = tPlayer.Update().SetAuthCode(authCode).Exec(context.Background())
db.Lock.Unlock()
if err != nil {
log.Warningf("[PPTM] update player failed: %+v", err)
log.Warningf("[PPT] update player failed: %+v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
@@ -301,7 +377,7 @@ func postPlayerTrackMe(w http.ResponseWriter, r *http.Request) {
if shareCode != "" && utils.ShareCodeRegEx.MatchString(shareCode) {
err := demoLoader.LoadDemo(&csgo.Demo{ShareCode: shareCode})
if err != nil {
log.Warningf("[PPTM] unable to queue match: %v", err)
log.Warningf("[PPT] unable to queue match: %v", err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
@@ -611,7 +687,8 @@ func main() {
// Define routes
router = mux.NewRouter().StrictSlash(true)
router.HandleFunc("/player/{id}", getPlayer).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/player/trackme", postPlayerTrackMe).Methods(http.MethodPost, http.MethodOptions)
router.HandleFunc("/player/{id}/track", postPlayerTrack).Methods(http.MethodPost, http.MethodOptions)
router.HandleFunc("/player/{id}/track", deletePlayerTrack).Methods(http.MethodOptions, http.MethodDelete)
router.HandleFunc("/match/parse/{sharecode}", getMatchParse).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/match/{id:[0-9]{19}}", getMatch).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/match/{id:[0-9]{19}}/weapons", getMatchWeapons).Methods(http.MethodGet, http.MethodOptions)