From b803dff3e9630c346255195c4370d7d3f95c1d85 Mon Sep 17 00:00:00 2001 From: Up Date: Wed, 8 May 2024 12:58:11 +0200 Subject: [PATCH] ensure uniqueness of daily seed --- api/daily/common.go | 22 +++++++++++----------- api/endpoints.go | 25 +++++++++++++++++++++++-- db/daily.go | 20 ++++++++++++++++---- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/api/daily/common.go b/api/daily/common.go index a1247b5..678e0fd 100644 --- a/api/daily/common.go +++ b/api/daily/common.go @@ -61,22 +61,27 @@ func Init() error { secret = newSecret } - err = recordNewDaily() + seed, err := recordNewDaily() if err != nil { log.Print(err) } - log.Printf("Daily Run Seed: %s", Seed()) + log.Printf("Daily Run Seed: %s", seed) - scheduler.AddFunc("@daily", func() { + _, err = scheduler.AddFunc("@daily", func() { time.Sleep(time.Second) - err := recordNewDaily() + seed, err = recordNewDaily() + log.Printf("Daily Run Seed: %s", seed) if err != nil { log.Printf("error while recording new daily: %s", err) } }) + if err != nil { + return err + } + scheduler.Start() return nil @@ -95,11 +100,6 @@ func deriveSeed(seedTime time.Time) []byte { return hashedSeed[:] } -func recordNewDaily() error { - err := db.TryAddDailyRun(Seed()) - if err != nil { - return err - } - - return nil +func recordNewDaily() (string, error) { + return db.TryAddDailyRun(Seed()) } diff --git a/api/endpoints.go b/api/endpoints.go index fb41047..7da61e0 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -18,6 +18,7 @@ package api import ( + "encoding/base64" "encoding/json" "fmt" "net/http" @@ -302,7 +303,13 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) { } // doesn't return a save, but it works - save, err = savedata.Clear(uuid, slot, daily.Seed(), s) + var seed string + seed, err = db.GetDailyRunSeed() + if err != nil { + httpError(w, r, err, http.StatusInternalServerError) + return + } + save, err = savedata.Clear(uuid, slot, seed, s) } if err != nil { httpError(w, r, err, http.StatusInternalServerError) @@ -326,7 +333,21 @@ func handleSaveData(w http.ResponseWriter, r *http.Request) { // daily func handleDailySeed(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(daily.Seed())) + seed, err := db.GetDailyRunSeed() + if err != nil { + httpError(w, r, err, http.StatusInternalServerError) + return + } + bytes, err := base64.StdEncoding.DecodeString(seed) + if err != nil { + httpError(w, r, err, http.StatusInternalServerError) + return + } + _, err = w.Write(bytes) + if err != nil { + httpError(w, r, err, http.StatusInternalServerError) + return + } } func handleDailyRankings(w http.ResponseWriter, r *http.Request) { diff --git a/db/daily.go b/db/daily.go index 55eb04e..4b0f953 100644 --- a/db/daily.go +++ b/db/daily.go @@ -23,13 +23,25 @@ import ( "github.com/pagefaultgames/rogueserver/defs" ) -func TryAddDailyRun(seed string) error { - _, err := handle.Exec("INSERT INTO dailyRuns (seed, date) VALUES (?, UTC_DATE()) ON DUPLICATE KEY UPDATE date = date", seed) +func TryAddDailyRun(seed string) (string, error) { + var actualSeed string + err := handle.QueryRow("INSERT INTO dailyRuns (seed, date) VALUES (?, UTC_DATE()) ON DUPLICATE KEY UPDATE date = date RETURNING seed", seed).Scan(&actualSeed) if err != nil { - return err + return "INVALID", err } - return nil + return actualSeed, nil +} + +func GetDailyRunSeed() (string, error) { + var seed string + err := handle.QueryRow("SELECT seed FROM dailyRuns WHERE date = UTC_DATE()").Scan(&seed) + if err != nil { + return "INVALID", err + } + + return seed, nil + } func AddOrUpdateAccountDailyRun(uuid []byte, score int, wave int) error {