Remove /savedata/get

pull/34/head
Pancakes 6 months ago
parent ddf1e8d9e5
commit 023b8aaed4
No known key found for this signature in database
GPG Key ID: 5792877BFA27DC8F

@ -52,7 +52,6 @@ func Init(mux *http.ServeMux) error {
mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount) mux.HandleFunc("GET /game/classicsessioncount", handleGameClassicSessionCount)
// savedata // savedata
mux.HandleFunc("GET /savedata/get", legacyHandleGetSaveData)
mux.HandleFunc("POST /savedata/update", legacyHandleSaveData) mux.HandleFunc("POST /savedata/update", legacyHandleSaveData)
mux.HandleFunc("GET /savedata/delete", legacyHandleSaveData) // TODO use deleteSystemSave mux.HandleFunc("GET /savedata/delete", legacyHandleSaveData) // TODO use deleteSystemSave
mux.HandleFunc("POST /savedata/clear", legacyHandleSaveData) // TODO use clearSessionData mux.HandleFunc("POST /savedata/clear", legacyHandleSaveData) // TODO use clearSessionData

@ -173,8 +173,7 @@ func handleGetSessionData(w http.ResponseWriter, r *http.Request) {
return return
} }
var save any save, err := savedata.Session(uuid, slot)
save, err = savedata.Get(uuid, 1, 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)
return return
@ -190,54 +189,6 @@ func handleGetSessionData(w http.ResponseWriter, r *http.Request) {
const legacyClientSessionId = "LEGACY_CLIENT" const legacyClientSessionId = "LEGACY_CLIENT"
func legacyHandleGetSaveData(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r)
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
return
}
datatype := -1
if r.URL.Query().Has("datatype") {
datatype, err = strconv.Atoi(r.URL.Query().Get("datatype"))
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
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
if datatype == 0 {
err = db.UpdateActiveSession(uuid, legacyClientSessionId) // we dont have a client id
if err != nil {
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
return
}
}
save, err = savedata.Get(uuid, datatype, slot)
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}
writeJSON(w, r, save)
}
// FIXME UNFINISHED!!! // FIXME UNFINISHED!!!
/*func clearSessionData(w http.ResponseWriter, r *http.Request) { /*func clearSessionData(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r) uuid, err := uuidFromRequest(r)
@ -438,8 +389,8 @@ func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) {
} }
var save any var save any
// /savedata/get and /savedata/delete specify datatype, but don't expect data in body // /savedata/delete specify datatype, but don't expect data in body
if r.URL.Path != "/savedata/get" && r.URL.Path != "/savedata/delete" { if r.URL.Path != "/savedata/delete" {
if datatype == 0 { if datatype == 0 {
var system defs.SystemSaveData var system defs.SystemSaveData
err = json.NewDecoder(r.Body).Decode(&system) err = json.NewDecoder(r.Body).Decode(&system)
@ -463,15 +414,6 @@ func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) {
} }
var active bool var active bool
if r.URL.Path == "/savedata/get" {
if datatype == 0 {
err = db.UpdateActiveSession(uuid, clientSessionId)
if err != nil {
httpError(w, r, fmt.Errorf("failed to update active session: %s", err), http.StatusBadRequest)
return
}
}
} else {
active, err = db.IsActiveSession(uuid, clientSessionId) active, err = db.IsActiveSession(uuid, 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)
@ -522,15 +464,8 @@ func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) {
return return
} }
} }
}
switch r.URL.Path { switch r.URL.Path {
case "/savedata/get":
save, err = savedata.Get(uuid, datatype, slot)
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
case "/savedata/update": case "/savedata/update":
err = savedata.Update(uuid, slot, save) err = savedata.Update(uuid, slot, save)
case "/savedata/delete": case "/savedata/delete":
@ -716,8 +651,7 @@ func handleGetSystemData(w http.ResponseWriter, r *http.Request) {
return return
} }
var save any //TODO this is always system save data save, err := savedata.System(uuid)
save, err = savedata.Get(uuid, 0, 0)
if err != nil { if err != nil {
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)
@ -727,6 +661,7 @@ func handleGetSystemData(w http.ResponseWriter, r *http.Request) {
return return
} }
//TODO apply vouchers //TODO apply vouchers
writeJSON(w, r, save) writeJSON(w, r, save)

@ -1,82 +0,0 @@
/*
Copyright (C) 2024 Pagefault Games
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package savedata
import (
"fmt"
"strconv"
"github.com/pagefaultgames/rogueserver/db"
"github.com/pagefaultgames/rogueserver/defs"
)
// /savedata/get - get save data
func Get(uuid []byte, datatype, slot int) (any, error) {
switch datatype {
case 0: // System
system, err := db.ReadSystemSaveData(uuid)
if err != nil {
return nil, err
}
// TODO this should be a transaction
compensations, err := db.FetchAndClaimAccountCompensations(uuid)
if err != nil {
return nil, fmt.Errorf("failed to fetch compensations: %s", err)
}
var needsUpdate bool
for compensationType, amount := range compensations {
system.VoucherCounts[strconv.Itoa(compensationType)] += amount
if amount > 0 {
needsUpdate = true
}
}
if needsUpdate {
err = db.StoreSystemSaveData(uuid, system)
if err != nil {
return nil, fmt.Errorf("failed to update system save data: %s", err)
}
err = db.DeleteClaimedAccountCompensations(uuid)
if err != nil {
return nil, fmt.Errorf("failed to delete claimed compensations: %s", err)
}
err = db.UpdateAccountStats(uuid, system.GameStats, system.VoucherCounts)
if err != nil {
return nil, fmt.Errorf("failed to update account stats: %s", err)
}
}
return system, nil
case 1: // Session
if slot < 0 || slot >= defs.SessionSlotCount {
return nil, fmt.Errorf("slot id %d out of range", slot)
}
session, err := db.ReadSessionSaveData(uuid, slot)
if err != nil {
return nil, err
}
return session, nil
default:
return nil, fmt.Errorf("invalid data type")
}
}

@ -0,0 +1,24 @@
package savedata
import (
"fmt"
"github.com/pagefaultgames/rogueserver/db"
"github.com/pagefaultgames/rogueserver/defs"
)
func Session(uuid []byte, slot int) (defs.SessionSaveData, error) {
var session defs.SessionSaveData
if slot < 0 || slot >= defs.SessionSlotCount {
return session, fmt.Errorf("slot id %d out of range", slot)
}
var err error
session, err = db.ReadSessionSaveData(uuid, slot)
if err != nil {
return session, err
}
return session, nil
}

@ -0,0 +1,48 @@
package savedata
import (
"fmt"
"strconv"
"github.com/pagefaultgames/rogueserver/db"
"github.com/pagefaultgames/rogueserver/defs"
)
func System(uuid []byte) (defs.SystemSaveData, error) {
system, err := db.ReadSystemSaveData(uuid)
if err != nil {
return system, err
}
// TODO this should be a transaction
compensations, err := db.FetchAndClaimAccountCompensations(uuid)
if err != nil {
return system, fmt.Errorf("failed to fetch compensations: %s", err)
}
var needsUpdate bool
for compensationType, amount := range compensations {
system.VoucherCounts[strconv.Itoa(compensationType)] += amount
if amount > 0 {
needsUpdate = true
}
}
if needsUpdate {
err = db.StoreSystemSaveData(uuid, system)
if err != nil {
return system, fmt.Errorf("failed to update system save data: %s", err)
}
err = db.DeleteClaimedAccountCompensations(uuid)
if err != nil {
return system, fmt.Errorf("failed to delete claimed compensations: %s", err)
}
err = db.UpdateAccountStats(uuid, system.GameStats, system.VoucherCounts)
if err != nil {
return system, fmt.Errorf("failed to update account stats: %s", err)
}
}
return system, nil
}
Loading…
Cancel
Save