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)
// savedata
mux.HandleFunc("POST /savedata/update", legacyHandleSaveData) // DEPRECATED: use PUT method
mux.HandleFunc("GET /savedata/delete", legacyHandleSaveData) // DEPRECATED: use DELETE method
mux.HandleFunc("POST /savedata/clear", legacyHandleSaveData) // TODO: use clearSessionData
mux.HandleFunc("POST /savedata/update", legacyHandleSaveData)
mux.HandleFunc("GET /savedata/delete", legacyHandleSaveData)
mux.HandleFunc("POST /savedata/clear", legacyHandleSaveData)
mux.HandleFunc("GET /savedata/newclear", legacyHandleNewClear)
mux.HandleFunc("/savedata/session/{action}", handleSession)
mux.HandleFunc("/savedata/system/{action}", handleSystem)
// new session
mux.HandleFunc("POST /savedata/updateall", handleUpdateAll) // DEPRECATED: use PUT method
mux.HandleFunc("PUT /savedata/updateall", handleUpdateAll)
mux.HandleFunc("POST /savedata/updateall", handleUpdateAll)
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("PUT /savedata/session", handleSession)
mux.HandleFunc("DELETE /savedata/session", handleSession)
// daily
mux.HandleFunc("GET /daily/seed", handleDailySeed)

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

Loading…
Cancel
Save