Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server-side Code for Run History #33

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func Init(mux *http.ServeMux) error {
mux.HandleFunc("/savedata/session/{action}", handleSession)
mux.HandleFunc("/savedata/system/{action}", handleSystem)

//run history
mux.HandleFunc("GET /savedata/runHistory", handleGetRunHistory)
mux.HandleFunc("POST /savedata/runHistory", handleRunHistory)

// new session
mux.HandleFunc("POST /savedata/updateall", handleUpdateAll)

Expand Down
51 changes: 51 additions & 0 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,57 @@ func handleSession(w http.ResponseWriter, r *http.Request) {
}
}

//functions for run history
func handleGetRunHistory(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r)
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
return
}

var runHistory any
runHistory, err = db.GetRunHistoryData(uuid);
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, runHistory)
}

func handleRunHistory(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r)
if err != nil {
httpError(w, r, err, http.StatusBadRequest)
return
}

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)
return
}

err = db.UpdateRunHistoryData(uuid, data)
if err != nil {
httpError(w, r, err, http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}

type CombinedSaveData struct {
System defs.SystemSaveData `json:"system"`
Session defs.SessionSaveData `json:"session"`
Expand Down
2 changes: 2 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func setupDb(tx *sql.Tx) error {
// MIGRATION 002

`DROP TABLE accountCompensations`,

`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 {
Expand Down
73 changes: 73 additions & 0 deletions db/savedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,76 @@ func RetrievePlaytime(uuid []byte) (int, error) {

return playtime, nil
}

func GetRunHistoryData(uuid []byte) (defs.RunHistoryData, error) {
var runEntry defs.RunEntryData
var runHistory defs.RunHistoryData
var err error
var data []byte

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
}
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
}

return runHistory, err
}

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

err = gob.NewEncoder(&buf).Encode(data)
if err != nil {
return err
}
_, err = handle.Exec("INSERT INTO runHistoryData (uuid, timestamp, mode, highestWave, runEntry) VALUES (?, UTC_TIMESTAMP(), ?, ?, ?)", uuid, mode, highestWave, buf.Bytes())
if err != nil {
return err
}

return nil
}
10 changes: 10 additions & 0 deletions defs/savedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,13 @@ type SessionHistoryData struct {
}

type SessionHistoryResult int

type RunEntryData struct {
victory int `json:"victory"`
favorite int `json:"favorite"`
entry SessionSaveData `json:"entry"`
}

type RunHistoryData map[int]RunEntryData