Skip to content

Commit

Permalink
add combined update endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed May 12, 2024
1 parent ab69d94 commit 884bb88
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func Init(mux *http.ServeMux) error {
mux.HandleFunc("POST /savedata/clear", handleSaveData)
mux.HandleFunc("GET /savedata/newclear", handleNewClear)

// new session
mux.HandleFunc("POST /savedata/update2", handleSaveData2)

// daily
mux.HandleFunc("GET /daily/seed", handleDailySeed)
mux.HandleFunc("GET /daily/rankings", handleDailyRankings)
Expand Down
74 changes: 74 additions & 0 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,80 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
}

type CombinedSaveData struct {
System defs.SystemSaveData `json:"system"`
Session defs.SessionSaveData `json:"session"`
SessionSlotId int `json:"sessionSlotId"`
}

func handleSaveData2(w http.ResponseWriter, r *http.Request) {
var token []byte
token, err := tokenFromRequest(r)
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
return
}

uuid, err := uuidFromRequest(r)
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
return
}

var data CombinedSaveData
err = json.NewDecoder(r.Body).Decode(&data)
if err != nil {
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
return
}

var active bool
active, err = db.IsActiveSession(token)
if err != nil {
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
return
}

if !active {
httpError(w, r, fmt.Errorf("session out of date"), http.StatusBadRequest)
return
}

trainerId := data.System.TrainerId
secretId := data.System.SecretId

storedTrainerId, storedSecretId, err := db.FetchTrainerIds(uuid)
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}

if storedTrainerId > 0 || storedSecretId > 0 {
if trainerId != storedTrainerId || secretId != storedSecretId {
httpError(w, r, fmt.Errorf("session out of date"), http.StatusBadRequest)
return
}
} else {
if err := db.UpdateTrainerIds(trainerId, secretId, uuid); err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}
}

err = savedata.Update(uuid, data.SessionSlotId, data.Session)
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}
err = savedata.Update(uuid, 0, data.System)
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
return

Check failure on line 402 in api/endpoints.go

View workflow job for this annotation

GitHub Actions / Build (linux)

S1023: redundant `return` statement (gosimple)

Check failure on line 402 in api/endpoints.go

View workflow job for this annotation

GitHub Actions / Build (windows)

S1023: redundant `return` statement (gosimple)
}

func handleNewClear(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r)
if err != nil {
Expand Down

0 comments on commit 884bb88

Please sign in to comment.