From c4ec477b5e4b318089038f9ce460ec63536aa0c3 Mon Sep 17 00:00:00 2001 From: Frutescens Date: Tue, 30 Jul 2024 20:41:43 -0700 Subject: [PATCH] Does not work. Some changes --- api/endpoints.go | 8 +++++++- db/db.go | 2 +- db/savedata.go | 48 ++++++++++++++++++++++++++++++++++++++++++++---- defs/savedata.go | 10 +++++++++- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/api/endpoints.go b/api/endpoints.go index 9472efe..59f8133 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -289,7 +289,13 @@ func handleRunHistory(w http.ResponseWriter, r *http.Request) { return } - var data defs.RunHistoryData + err = db.CheckRunHistoryData(uuid) + if err != nil { + httpError(w, r, err, http.StatusBadRequest) + return + } + + var data defs.RunEntryData err = json.NewDecoder(r.Body).Decode(&data) if err != nil { httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest) diff --git a/db/db.go b/db/db.go index 10af2b8..0ef20e7 100644 --- a/db/db.go +++ b/db/db.go @@ -98,7 +98,7 @@ func setupDb(tx *sql.Tx) error { `DROP TABLE accountCompensations`, - `CREATE TABLE IF NOT EXISTS runHistoryData (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 runHistoryData (uuid BINARY(16) PRIMARY KEY, runEntry BLOB, highestWave INT(11) NOT NULL DEFAULT 0, mode INT(11) NOT NULL DEFAULT 0, timestamp TIMESTAMP, FOREIGN KEY (uuid) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE)`, } for _, q := range queries { diff --git a/db/savedata.go b/db/savedata.go index f8bd56e..8657f6a 100644 --- a/db/savedata.go +++ b/db/savedata.go @@ -154,15 +154,29 @@ func RetrievePlaytime(uuid []byte) (int, error) { } func GetRunHistoryData(uuid []byte) (defs.RunHistoryData, error) { + var runEntry defs.RunEntryData var runHistory defs.RunHistoryData var err error var data []byte - err = handle.QueryRow("SELECT data FROM runHistoryData WHERE uuid = ?", uuid).Scan(&data) + type row struct { + var ts int + var entry defs.RunEntryData + } + + rows, err := handle.Query("SELECT runEntry FROM runHistoryData WHERE uuid = ?", uuid) if err != nil { return runHistory, err } - err = gob.NewDecoder(bytes.NewReader(data)).Decode(&runHistory) + defer rows.close() + + for rows.Next() { + var r row + rows.Scan(&r.ts, &r.entry) + err = gob.NewDecoder(bytes.NewReader(r.entry)).Decode(&runEntry) + runHistory[r.ts] = runEntry + } + if err != nil { return runHistory, err } @@ -170,7 +184,33 @@ func GetRunHistoryData(uuid []byte) (defs.RunHistoryData, error) { return runHistory, err } -func UpdateRunHistoryData(uuid []byte, data defs.RunHistoryData) error { +func CheckRunHistoryData(uuid []byte) error { + var count int + + err := db.QueryRow("SELECT COUNT(runEntry) FROM runHistoryData WHERE uuid = ?", uuid).Scan(&count) + + if err != nil { + return err + } + + if count <= 25 { + return nil + } + + _, err = handle.Exec("DELETE FROM runHistoryData ORDER BY timestamp DESC limit 1") + if err != nil { + return err + } + + return nil +} + +func UpdateRunHistoryData(uuid []byte, data defs.RunEntryData) error { + var mode GameMode + var highestWave int + mode = data.entry.GameMode + highestWave = data.entry.WaveIndex + var buf bytes.Buffer var err error @@ -178,7 +218,7 @@ func UpdateRunHistoryData(uuid []byte, data defs.RunHistoryData) error { if err != nil { return err } - _, err = handle.Exec("INSERT INTO runHistoryData (uuid, data, timestamp) VALUES (?, ?, UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE data = ?, timestamp = UTC_TIMESTAMP()", uuid, buf.Bytes(), buf.Bytes()) + _, err = handle.Exec("INSERT INTO runHistoryData (uuid, timestamp, mode, highestWave, runEntry) VALUES (?, UTC_TIMESTAMP(), ?, ?, ?)", uuid, mode, highestWave, buf.Bytes()) if err != nil { return err } diff --git a/defs/savedata.go b/defs/savedata.go index 95554e3..5d09ddb 100644 --- a/defs/savedata.go +++ b/defs/savedata.go @@ -154,4 +154,12 @@ type SessionHistoryData struct { type SessionHistoryResult int -type RunHistoryData map[string]interface{} +type RunEntryData struct { + victory int `json:"victory"` + favorite int `json:"favorite"` + entry SessionSaveData `json:"entry"` +} + +type RunHistoryData map[int]RunEntryData + +