Rework savedata API

pull/42/head
Pancakes 5 months ago
parent 656a935a6c
commit f9cce330f6
No known key found for this signature in database
GPG Key ID: 5792877BFA27DC8F

@ -52,23 +52,19 @@ func Init(mux *http.ServeMux) error {
mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount) mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount)
// savedata // savedata
mux.HandleFunc("POST /savedata/update", legacyHandleSaveData) // DEPRECATED: use PUT method mux.HandleFunc("POST /savedata/update", legacyHandleSaveData)
mux.HandleFunc("GET /savedata/delete", legacyHandleSaveData) // DEPRECATED: use DELETE method mux.HandleFunc("GET /savedata/delete", legacyHandleSaveData)
mux.HandleFunc("POST /savedata/clear", legacyHandleSaveData) // TODO: use clearSessionData mux.HandleFunc("POST /savedata/clear", legacyHandleSaveData)
mux.HandleFunc("GET /savedata/newclear", legacyHandleNewClear) mux.HandleFunc("GET /savedata/newclear", legacyHandleNewClear)
mux.HandleFunc("/savedata/session/{action}", handleSession)
mux.HandleFunc("/savedata/system/{action}", handleSystem)
// new session // new session
mux.HandleFunc("POST /savedata/updateall", handleUpdateAll) // DEPRECATED: use PUT method mux.HandleFunc("POST /savedata/updateall", handleUpdateAll)
mux.HandleFunc("PUT /savedata/updateall", handleUpdateAll)
mux.HandleFunc("GET /savedata/system", handleSystem) mux.HandleFunc("GET /savedata/system", handleSystem)
mux.HandleFunc("PUT /savedata/system", handleSystem)
mux.HandleFunc("DELETE /savedata/system", handleSystem)
mux.HandleFunc("POST /savedata/system/verify", handleSystemVerify)
mux.HandleFunc("GET /savedata/session", handleSession) mux.HandleFunc("GET /savedata/session", handleSession)
mux.HandleFunc("PUT /savedata/session", handleSession)
mux.HandleFunc("DELETE /savedata/session", handleSession)
// daily // daily
mux.HandleFunc("GET /daily/seed", handleDailySeed) mux.HandleFunc("GET /daily/seed", handleDailySeed)

@ -176,8 +176,10 @@ func handleSession(w http.ResponseWriter, r *http.Request) {
return return
} }
switch r.Method { switch r.PathValue("action") {
case "GET": default:
fallthrough
case "get":
save, err := savedata.GetSession(uuid, slot) save, err := savedata.GetSession(uuid, slot)
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
http.Error(w, err.Error(), http.StatusNotFound) http.Error(w, err.Error(), http.StatusNotFound)
@ -190,7 +192,7 @@ func handleSession(w http.ResponseWriter, r *http.Request) {
} }
writeJSON(w, r, save) writeJSON(w, r, save)
case "PUT": case "update":
var session defs.SessionSaveData var session defs.SessionSaveData
err = json.NewDecoder(r.Body).Decode(&session) err = json.NewDecoder(r.Body).Decode(&session)
if err != nil { if err != nil {
@ -204,108 +206,48 @@ func handleSession(w http.ResponseWriter, r *http.Request) {
return return
} }
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusOK)
case "DELETE": case "clear":
err := savedata.DeleteSession(uuid, slot) var session defs.SessionSaveData
err = json.NewDecoder(r.Body).Decode(&session)
if err != nil { if err != nil {
httpError(w, r, err, http.StatusInternalServerError) httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
return return
} }
w.WriteHeader(http.StatusNoContent) seed, err := db.GetDailyRunSeed()
}
}
const legacyClientSessionId = "LEGACY_CLIENT"
// FIXME UNFINISHED!!!
/*func clearSessionData(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r)
if err != nil {
httpError(w, r, err, http.StatusUnauthorized)
return
}
var slot int
if r.URL.Query().Has("slot") {
slot, err = strconv.Atoi(r.URL.Query().Get("slot"))
if err != nil { if err != nil {
httpError(w, r, err, http.StatusBadRequest) httpError(w, r, err, http.StatusInternalServerError)
return return
} }
}
var save any
var session defs.SessionSaveData
err = json.NewDecoder(r.Body).Decode(&session)
if err != nil {
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
return
}
save = session resp, err := savedata.Clear(uuid, slot, seed, session)
var active bool
active, err = db.IsActiveSession(uuid, legacyClientSessionId) // TODO: unfinished, read token from query
if err != nil {
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
return
}
var trainerId, secretId int
if r.URL.Query().Has("trainerId") && r.URL.Query().Has("secretId") {
trainerId, err = strconv.Atoi(r.URL.Query().Get("trainerId"))
if err != nil { if err != nil {
httpError(w, r, err, http.StatusBadRequest) httpError(w, r, err, http.StatusInternalServerError)
return return
} }
secretId, err = strconv.Atoi(r.URL.Query().Get("secretId")) writeJSON(w, r, resp)
case "newclear":
resp, err := savedata.NewClear(uuid, slot)
if err != nil { if err != nil {
httpError(w, r, err, http.StatusBadRequest) httpError(w, r, fmt.Errorf("failed to read new clear: %s", err), http.StatusInternalServerError)
return return
} }
}
storedTrainerId, storedSecretId, err := db.FetchTrainerIds(uuid)
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}
if storedTrainerId > 0 || storedSecretId > 0 { writeJSON(w, r, resp)
if trainerId != storedTrainerId || secretId != storedSecretId { case "delete":
httpError(w, r, fmt.Errorf("session out of date: stored trainer or secret ID does not match"), http.StatusBadRequest) err := savedata.DeleteSession(uuid, slot)
return
}
} else {
err = db.UpdateTrainerIds(trainerId, secretId, uuid)
if err != nil { if err != nil {
httpError(w, r, fmt.Errorf("unable to update trainer ID: %s", err), http.StatusInternalServerError) httpError(w, r, err, http.StatusInternalServerError)
return return
} }
}
if !active {
save = savedata.ClearResponse{Error: "session out of date: not active"}
}
var seed string
seed, err = db.GetDailyRunSeed()
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}
response, err := savedata.Clear(uuid, slot, seed, save.(defs.SessionSaveData)) w.WriteHeader(http.StatusOK)
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
} }
jsonResponse(w, r, response)
} }
*/
const legacyClientSessionId = "LEGACY_CLIENT"
func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) { func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r) uuid, err := uuidFromRequest(r)
@ -526,60 +468,8 @@ type SystemVerifyRequest struct {
} }
type SystemVerifyResponse struct { type SystemVerifyResponse struct {
Valid bool `json:"valid"` Valid bool `json:"valid"`
SystemData *defs.SystemSaveData `json:"systemData"` SystemData defs.SystemSaveData `json:"systemData"`
}
func handleSystemVerify(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r)
if err != nil {
httpError(w, r, err, http.StatusUnauthorized)
return
}
var input SystemVerifyRequest
err = json.NewDecoder(r.Body).Decode(&input)
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(uuid, input.ClientSessionId)
if err != nil {
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
return
}
response := SystemVerifyResponse{
Valid: active,
}
// not valid, send server state
if !active {
err = db.UpdateActiveSession(uuid, input.ClientSessionId)
if err != nil {
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
return
}
var storedSaveData defs.SystemSaveData
storedSaveData, err = db.ReadSystemSaveData(uuid)
if err != nil {
httpError(w, r, fmt.Errorf("failed to read session save data: %s", err), http.StatusInternalServerError)
return
}
response.SystemData = &storedSaveData
}
err = db.UpdateAccountLastActivity(uuid)
if err != nil {
httpError(w, r, fmt.Errorf("failed to update account last activity: %s", err), http.StatusInternalServerError)
return
}
writeJSON(w, r, response)
} }
func handleSystem(w http.ResponseWriter, r *http.Request) { func handleSystem(w http.ResponseWriter, r *http.Request) {
@ -594,14 +484,24 @@ func handleSystem(w http.ResponseWriter, r *http.Request) {
return return
} }
err = db.UpdateActiveSession(uuid, r.URL.Query().Get("clientSessionId")) active, err := db.IsActiveSession(uuid, r.URL.Query().Get("clientSessionId"))
if err != nil { if err != nil {
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
return return
} }
switch r.Method { switch r.PathValue("action") {
case "GET": default:
fallthrough
case "get":
if !active {
err = db.UpdateActiveSession(uuid, r.URL.Query().Get("clientSessionId"))
if err != nil {
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
return
}
}
save, err := savedata.GetSystem(uuid) save, err := savedata.GetSystem(uuid)
if err != nil { if err != nil {
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
@ -613,10 +513,13 @@ func handleSystem(w http.ResponseWriter, r *http.Request) {
return return
} }
// TODO: apply vouchers
writeJSON(w, r, save) writeJSON(w, r, save)
case "PUT": case "update":
if !active {
httpError(w, r, fmt.Errorf("session out of date: not active"), http.StatusBadRequest)
return
}
var system defs.SystemSaveData var system defs.SystemSaveData
err = json.NewDecoder(r.Body).Decode(&system) err = json.NewDecoder(r.Body).Decode(&system)
if err != nil { if err != nil {
@ -631,14 +534,45 @@ func handleSystem(w http.ResponseWriter, r *http.Request) {
} }
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
case "DELETE": case "verify":
var input SystemVerifyRequest
err = json.NewDecoder(r.Body).Decode(&input)
if err != nil {
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
return
}
response := SystemVerifyResponse{
Valid: active,
}
// not valid, send server state
if !active {
err = db.UpdateActiveSession(uuid, input.ClientSessionId)
if err != nil {
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
return
}
var storedSaveData defs.SystemSaveData
storedSaveData, err = db.ReadSystemSaveData(uuid)
if err != nil {
httpError(w, r, fmt.Errorf("failed to read session save data: %s", err), http.StatusInternalServerError)
return
}
response.SystemData = storedSaveData
}
writeJSON(w, r, response)
case "delete":
err := savedata.DeleteSystem(uuid) err := savedata.DeleteSystem(uuid)
if err != nil { if err != nil {
httpError(w, r, err, http.StatusInternalServerError) httpError(w, r, err, http.StatusInternalServerError)
return return
} }
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusOK)
} }
} }

Loading…
Cancel
Save