Compare commits

...

6 Commits

Author SHA1 Message Date
Up
a063b1740c
add missing foreign key declarations 2024-05-15 04:12:20 +02:00
Up
e4713e6ea3
fall back to legacy save ID 2024-05-15 04:08:42 +02:00
Up
2aab022ce3
properly use client session ID 2024-05-15 04:07:47 +02:00
Up
e7cff35d69
delete vouchers when claiming them 2024-05-15 00:50:50 +02:00
Up
174b962f19
update verify endpoint 2024-05-15 00:00:38 +02:00
Up
e2efcd550c
fix faulty sql query 2024-05-14 23:17:14 +02:00
6 changed files with 24 additions and 30 deletions

View File

@ -56,7 +56,7 @@ func Init(mux *http.ServeMux) error {
// new session
mux.HandleFunc("POST /savedata/updateall", handleUpdateAll)
mux.HandleFunc("POST /savedata/verify", handleSessionVerify)
mux.HandleFunc("POST /savedata/system/verify", handleSystemVerify)
mux.HandleFunc("GET /savedata/system", handleGetSystemData)
mux.HandleFunc("GET /savedata/session", handleGetSessionData)

View File

@ -585,23 +585,18 @@ func handleUpdateAll(w http.ResponseWriter, r *http.Request) {
return
}
var clientSessionId string
if r.URL.Query().Has("clientSessionId") {
clientSessionId = r.URL.Query().Get("clientSessionId")
}
if clientSessionId == "" {
clientSessionId = legacyClientSessionId
}
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
}
if data.ClientSessionId == "" {
data.ClientSessionId = legacyClientSessionId
}
var active bool
active, err = db.IsActiveSession(uuid, clientSessionId)
active, err = db.IsActiveSession(uuid, data.ClientSessionId)
if err != nil {
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
return
@ -627,7 +622,7 @@ func handleUpdateAll(w http.ResponseWriter, r *http.Request) {
return
}
} else {
if err := db.UpdateTrainerIds(trainerId, secretId, uuid); err != nil {
if err = db.UpdateTrainerIds(trainerId, secretId, uuid); err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}
@ -646,24 +641,23 @@ func handleUpdateAll(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
type SessionVerifyResponse struct {
Valid bool `json:"valid"`
SessionData *defs.SessionSaveData `json:"sessionData"`
type SystemVerifyResponse struct {
Valid bool `json:"valid"`
SystemData *defs.SystemSaveData `json:"systemData"`
}
type SessionVerifyRequest struct {
type SystemVerifyRequest struct {
ClientSessionId string `json:"clientSessionId"`
Slot int `json:"slot"`
}
func handleSessionVerify(w http.ResponseWriter, r *http.Request) {
func handleSystemVerify(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r)
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
return
}
var input SessionVerifyRequest
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)
@ -677,7 +671,7 @@ func handleSessionVerify(w http.ResponseWriter, r *http.Request) {
return
}
response := SessionVerifyResponse{
response := SystemVerifyResponse{
Valid: active,
}
@ -689,14 +683,14 @@ func handleSessionVerify(w http.ResponseWriter, r *http.Request) {
return
}
var storedSaveData defs.SessionSaveData
storedSaveData, err = db.ReadSessionSaveData(uuid, input.Slot)
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.SessionData = &storedSaveData
response.SystemData = &storedSaveData
}
jsonResponse(w, r, response)
@ -733,6 +727,7 @@ func handleGetSystemData(w http.ResponseWriter, r *http.Request) {
return
}
//TODO apply vouchers
jsonResponse(w, r, save)
}

View File

@ -57,6 +57,10 @@ func Get(uuid []byte, datatype, slot int) (any, error) {
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 {

View File

@ -47,11 +47,6 @@ func Update(uuid []byte, slot int, save any) error {
return fmt.Errorf("failed to update account stats: %s", err)
}
err = db.DeleteClaimedAccountCompensations(uuid)
if err != nil {
return fmt.Errorf("failed to delete claimed compensations: %s", err)
}
return db.StoreSystemSaveData(uuid, save)
case defs.SessionSaveData: // Session

View File

@ -212,7 +212,7 @@ func UpdateTrainerIds(trainerId, secretId int, uuid []byte) error {
func IsActiveSession(uuid []byte, clientSessionId string) (bool, error) {
var storedId string
err := handle.QueryRow("SELECT clientSessionId FROM activeClientSessions WHERE sessions.uuid = ?", uuid).Scan(&storedId)
err := handle.QueryRow("SELECT clientSessionId FROM activeClientSessions WHERE uuid = ?", uuid).Scan(&storedId)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return false, nil

View File

@ -167,9 +167,9 @@ func setupDb(tx *sql.Tx) error {
`CREATE TABLE IF NOT EXISTS accountDailyRuns (uuid BINARY(16) NOT NULL, date DATE NOT NULL, score INT(11) NOT NULL DEFAULT 0, wave INT(11) NOT NULL DEFAULT 0, timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (uuid, date), CONSTRAINT accountDailyRuns_ibfk_1 FOREIGN KEY (uuid) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT accountDailyRuns_ibfk_2 FOREIGN KEY (date) REFERENCES dailyRuns (date) ON DELETE NO ACTION ON UPDATE NO ACTION)`,
`CREATE INDEX IF NOT EXISTS accountDailyRunsByDate ON accountDailyRuns (date)`,
`CREATE TABLE IF NOT EXISTS systemSaveData (uuid BINARY(16) PRIMARY KEY, data LONGBLOB, timestamp TIMESTAMP)`,
`CREATE TABLE IF NOT EXISTS systemSaveData (uuid BINARY(16) PRIMARY KEY, data LONGBLOB, timestamp TIMESTAMP, FOREIGN KEY (uuid) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE)`,
`CREATE TABLE IF NOT EXISTS sessionSaveData (uuid BINARY(16), slot TINYINT, data LONGBLOB, timestamp TIMESTAMP, PRIMARY KEY (uuid, slot))`,
`CREATE TABLE IF NOT EXISTS sessionSaveData (uuid BINARY(16), slot TINYINT, data LONGBLOB, timestamp TIMESTAMP, PRIMARY KEY (uuid, slot), FOREIGN KEY (uuid) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE)`,
// ----------------------------------
// MIGRATION 001