Rework savedata API

pull/42/head
Pancakes 6 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,38 +206,8 @@ 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)
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
}
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 {
httpError(w, r, err, http.StatusBadRequest)
return
}
}
var save any
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 {
@ -243,69 +215,39 @@ const legacyClientSessionId = "LEGACY_CLIENT"
return return
} }
save = session seed, err := db.GetDailyRunSeed()
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 {
httpError(w, r, err, http.StatusBadRequest)
return
}
secretId, err = strconv.Atoi(r.URL.Query().Get("secretId"))
if err != nil { if err != nil {
httpError(w, r, err, http.StatusBadRequest) httpError(w, r, err, http.StatusInternalServerError)
return return
} }
}
storedTrainerId, storedSecretId, err := db.FetchTrainerIds(uuid) resp, err := savedata.Clear(uuid, slot, seed, session)
if err != nil { if err != nil {
httpError(w, r, err, http.StatusInternalServerError) httpError(w, r, err, http.StatusInternalServerError)
return return
} }
if storedTrainerId > 0 || storedSecretId > 0 { writeJSON(w, r, resp)
if trainerId != storedTrainerId || secretId != storedSecretId { case "newclear":
httpError(w, r, fmt.Errorf("session out of date: stored trainer or secret ID does not match"), http.StatusBadRequest) resp, err := savedata.NewClear(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, fmt.Errorf("failed to read new clear: %s", err), http.StatusInternalServerError)
return return
} }
}
if !active {
save = savedata.ClearResponse{Error: "session out of date: not active"}
}
var seed string writeJSON(w, r, resp)
seed, err = db.GetDailyRunSeed() case "delete":
err := savedata.DeleteSession(uuid, slot)
if err != nil { if err != nil {
httpError(w, r, err, http.StatusInternalServerError) httpError(w, r, err, http.StatusInternalServerError)
return 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)
@ -527,118 +469,110 @@ 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) { func handleSystem(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r) uuid, err := uuidFromRequest(r)
if err != nil { if err != nil {
httpError(w, r, err, http.StatusUnauthorized) httpError(w, r, err, http.StatusUnauthorized)
return return
} }
var input SystemVerifyRequest if !r.URL.Query().Has("clientSessionId") {
err = json.NewDecoder(r.Body).Decode(&input) httpError(w, r, fmt.Errorf("missing clientSessionId"), http.StatusBadRequest)
if err != nil {
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
return return
} }
var active bool active, err := db.IsActiveSession(uuid, r.URL.Query().Get("clientSessionId"))
active, err = db.IsActiveSession(uuid, input.ClientSessionId)
if err != nil { if err != nil {
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
return return
} }
response := SystemVerifyResponse{ switch r.PathValue("action") {
Valid: active, default:
} fallthrough
case "get":
// not valid, send server state
if !active { if !active {
err = db.UpdateActiveSession(uuid, input.ClientSessionId) err = db.UpdateActiveSession(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 update active session: %s", err), http.StatusBadRequest)
return 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 save, err := savedata.GetSystem(uuid)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
httpError(w, r, err, http.StatusInternalServerError)
} }
err = db.UpdateAccountLastActivity(uuid)
if err != nil {
httpError(w, r, fmt.Errorf("failed to update account last activity: %s", err), http.StatusInternalServerError)
return return
} }
writeJSON(w, r, response) writeJSON(w, r, save)
case "update":
if !active {
httpError(w, r, fmt.Errorf("session out of date: not active"), http.StatusBadRequest)
return
} }
func handleSystem(w http.ResponseWriter, r *http.Request) { var system defs.SystemSaveData
uuid, err := uuidFromRequest(r) err = json.NewDecoder(r.Body).Decode(&system)
if err != nil { if err != nil {
httpError(w, r, err, http.StatusUnauthorized) httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
return return
} }
if !r.URL.Query().Has("clientSessionId") { err = savedata.PutSystem(uuid, system)
httpError(w, r, fmt.Errorf("missing clientSessionId"), http.StatusBadRequest) if err != nil {
httpError(w, r, fmt.Errorf("failed to put system data: %s", err), http.StatusInternalServerError)
return return
} }
err = db.UpdateActiveSession(uuid, r.URL.Query().Get("clientSessionId")) w.WriteHeader(http.StatusNoContent)
case "verify":
var input SystemVerifyRequest
err = json.NewDecoder(r.Body).Decode(&input)
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 decode request body: %s", err), http.StatusBadRequest)
return return
} }
switch r.Method { response := SystemVerifyResponse{
case "GET": Valid: active,
save, err := savedata.GetSystem(uuid)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
httpError(w, r, err, http.StatusInternalServerError)
} }
// 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 return
} }
// TODO: apply vouchers var storedSaveData defs.SystemSaveData
storedSaveData, err = db.ReadSystemSaveData(uuid)
writeJSON(w, r, save)
case "PUT":
var system defs.SystemSaveData
err = json.NewDecoder(r.Body).Decode(&system)
if err != nil { if err != nil {
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest) httpError(w, r, fmt.Errorf("failed to read session save data: %s", err), http.StatusInternalServerError)
return return
} }
err = savedata.PutSystem(uuid, system) response.SystemData = storedSaveData
if err != nil {
httpError(w, r, fmt.Errorf("failed to put system data: %s", err), http.StatusInternalServerError)
return
} }
w.WriteHeader(http.StatusNoContent) writeJSON(w, r, response)
case "DELETE": 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