Does not work. Some changes

This commit is contained in:
Frutescens 2024-07-30 20:41:43 -07:00
parent 77b244c51e
commit c4ec477b5e
4 changed files with 61 additions and 7 deletions

View File

@ -289,7 +289,13 @@ func handleRunHistory(w http.ResponseWriter, r *http.Request) {
return 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) err = json.NewDecoder(r.Body).Decode(&data)
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 decode request body: %s", err), http.StatusBadRequest)

View File

@ -98,7 +98,7 @@ func setupDb(tx *sql.Tx) error {
`DROP TABLE accountCompensations`, `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 { for _, q := range queries {

View File

@ -154,15 +154,29 @@ func RetrievePlaytime(uuid []byte) (int, error) {
} }
func GetRunHistoryData(uuid []byte) (defs.RunHistoryData, error) { func GetRunHistoryData(uuid []byte) (defs.RunHistoryData, error) {
var runEntry defs.RunEntryData
var runHistory defs.RunHistoryData var runHistory defs.RunHistoryData
var err error var err error
var data []byte 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 { if err != nil {
return runHistory, err 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 { if err != nil {
return runHistory, err return runHistory, err
} }
@ -170,7 +184,33 @@ func GetRunHistoryData(uuid []byte) (defs.RunHistoryData, error) {
return runHistory, err 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 buf bytes.Buffer
var err error var err error
@ -178,7 +218,7 @@ func UpdateRunHistoryData(uuid []byte, data defs.RunHistoryData) error {
if err != nil { if err != nil {
return err 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 { if err != nil {
return err return err
} }

View File

@ -154,4 +154,12 @@ type SessionHistoryData struct {
type SessionHistoryResult int 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