Change daily seed generation and update schema for recording failed daily runs
parent
4710617f23
commit
2cb7be52eb
20
api/daily.go
20
api/daily.go
|
@ -1,13 +1,11 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/Flashfyre/pokerogue-server/db"
|
||||
)
|
||||
|
@ -23,12 +21,20 @@ func ScheduleDailyRunRefresh() {
|
|||
}
|
||||
|
||||
func InitDailyRun() {
|
||||
dailyRunSeed = base64.StdEncoding.EncodeToString(SeedFromTime(time.Now().UTC()))
|
||||
err := db.TryAddDailyRun(dailyRunSeed)
|
||||
var err error
|
||||
dailyRunSeed, err = db.GetDailyRunSeed()
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
} else {
|
||||
log.Printf("Daily Run Seed: %s", dailyRunSeed)
|
||||
log.Printf("failed to generated daily run seed: %s", err.Error())
|
||||
}
|
||||
|
||||
if dailyRunSeed == "" {
|
||||
dailyRunSeed = RandString(24)
|
||||
err := db.TryAddDailyRun(dailyRunSeed)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
} else {
|
||||
log.Printf("Daily Run Seed: %s", dailyRunSeed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -276,11 +276,22 @@ func (s *Server) HandleSavedataClear(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
sessionCompleted := ValidateSessionCompleted(session)
|
||||
sessionCompleted := session.BattleType == 2 && ValidateSessionCompleted(session)
|
||||
newCompletion := false
|
||||
|
||||
if session.GameMode == 3 {
|
||||
waveCompleted := session.WaveIndex
|
||||
if session.BattleType != 2 {
|
||||
waveCompleted--
|
||||
}
|
||||
err = db.AddOrUpdateAccountDailyRun(uuid, session.Score, waveCompleted)
|
||||
if err != nil {
|
||||
log.Printf("failed to add or update daily run record: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if sessionCompleted {
|
||||
newCompletion, err = db.TryAddSeedCompletion(uuid, session.Seed, int(session.GameMode), session.Score)
|
||||
newCompletion, err = db.TryAddSeedCompletion(uuid, session.Seed, int(session.GameMode))
|
||||
if err != nil {
|
||||
log.Printf("failed to mark seed as completed: %s", err.Error())
|
||||
}
|
||||
|
|
29
api/utils.go
29
api/utils.go
|
@ -1,23 +1,20 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/binary"
|
||||
"math"
|
||||
"time"
|
||||
"crypto/rand"
|
||||
)
|
||||
|
||||
var seedKey []byte // 32 bytes
|
||||
const randRunes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
||||
const lenRandRunes = len(randRunes)
|
||||
|
||||
func SetSeedKey(key []byte) {
|
||||
seedKey = key
|
||||
}
|
||||
|
||||
func SeedFromTime(seedTime time.Time) []byte {
|
||||
day := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(day, uint64(math.Floor(float64(seedTime.Unix())/float64(time.Hour*24))))
|
||||
|
||||
sum := md5.Sum(append(seedKey, day...))
|
||||
|
||||
return sum[:]
|
||||
func RandString(length int) string {
|
||||
b := make([]byte, length)
|
||||
|
||||
rand.Read(b)
|
||||
|
||||
for i := range b {
|
||||
b[i] = randRunes[int(b[i])%lenRandRunes]
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
|
29
db/daily.go
29
db/daily.go
|
@ -1,6 +1,8 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/Flashfyre/pokerogue-server/defs"
|
||||
)
|
||||
|
||||
|
@ -13,12 +15,35 @@ func TryAddDailyRun(seed string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func GetDailyRunSeed() (string, error) {
|
||||
var seed string
|
||||
err := handle.QueryRow("SELECT seed FROM dailyRuns WHERE date = UTC_DATE()").Scan(&seed)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return "", err
|
||||
}
|
||||
|
||||
return seed, nil
|
||||
}
|
||||
|
||||
func AddOrUpdateAccountDailyRun(uuid []byte, score int, wave int) error {
|
||||
_, err := handle.Exec("INSERT INTO accountDailyRuns (uuid, date, score, wave, timestamp) VALUES (?, UTC_DATE(), ?, ?, UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE score = ?, wave = GREATEST(wave, ?), timestamp = IF(score < ?, UTC_TIMESTAMP(), timestamp)", uuid, score, wave, score, wave, score)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetRankings(page int) ([]defs.DailyRanking, error) {
|
||||
var rankings []defs.DailyRanking
|
||||
|
||||
offset := (page - 1) * 10
|
||||
|
||||
results, err := handle.Query("SELECT RANK() OVER (ORDER BY sc.score DESC, sc.timestamp), a.username, sc.score FROM seedCompletions sc JOIN dailyRuns dr ON dr.seed = sc.seed JOIN accounts a ON sc.uuid = a.uuid WHERE dr.date = UTC_DATE() LIMIT 10 OFFSET ?", offset)
|
||||
results, err := handle.Query("SELECT RANK() OVER (ORDER BY adr.score DESC, adr.timestamp), a.username, adr.score, adr.wave FROM accountDailyRuns adr JOIN dailyRuns dr ON dr.date = adr.date JOIN accounts a ON adr.uuid = a.uuid WHERE dr.date = UTC_DATE() LIMIT 10 OFFSET ?", offset)
|
||||
if err != nil {
|
||||
return rankings, err
|
||||
}
|
||||
|
@ -27,7 +52,7 @@ func GetRankings(page int) ([]defs.DailyRanking, error) {
|
|||
|
||||
for results.Next() {
|
||||
ranking := defs.DailyRanking{}
|
||||
err = results.Scan(&ranking.Rank, &ranking.Username, &ranking.Score)
|
||||
err = results.Scan(&ranking.Rank, &ranking.Username, &ranking.Score, &ranking.Wave)
|
||||
if err != nil {
|
||||
return rankings, err
|
||||
}
|
||||
|
|
|
@ -1,26 +1,24 @@
|
|||
package db
|
||||
|
||||
func TryAddSeedCompletion(uuid []byte, seed string, mode int, score int) (bool, error) {
|
||||
func TryAddSeedCompletion(uuid []byte, seed string, mode int) (bool, error) {
|
||||
if len(seed) < 24 {
|
||||
for range 24 - len(seed) {
|
||||
seed += "0"
|
||||
}
|
||||
}
|
||||
|
||||
newCompletion := true
|
||||
|
||||
var count int
|
||||
err := handle.QueryRow("SELECT COUNT(*) FROM seedCompletions WHERE uuid = ? AND seed = ?", uuid, seed).Scan(&count)
|
||||
if err != nil {
|
||||
return false, err
|
||||
} else if count > 0 {
|
||||
newCompletion = false
|
||||
return false, nil
|
||||
}
|
||||
|
||||
_, err = handle.Exec("INSERT INTO seedCompletions (uuid, seed, mode, score, timestamp) VALUES (?, ?, ?, ?, UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE score = ?, timestamp = IF(score < ?, UTC_TIMESTAMP(), timestamp)", uuid, seed, mode, score, score, score)
|
||||
_, err = handle.Exec("INSERT INTO seedCompletions (uuid, seed, mode, timestamp) VALUES (?, ?, ?, UTC_TIMESTAMP())", uuid, seed, mode)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return newCompletion, nil
|
||||
return true, nil
|
||||
}
|
||||
|
|
|
@ -4,4 +4,5 @@ type DailyRanking struct {
|
|||
Rank int `json:"rank"`
|
||||
Username string `json:"username"`
|
||||
Score int `json:"score"`
|
||||
Wave int `json:"wave"`
|
||||
}
|
||||
|
|
|
@ -11,8 +11,6 @@ import (
|
|||
"github.com/Flashfyre/pokerogue-server/db"
|
||||
)
|
||||
|
||||
var key []byte
|
||||
|
||||
func main() {
|
||||
debug := flag.Bool("debug", false, "debug mode")
|
||||
|
||||
|
@ -45,12 +43,6 @@ func main() {
|
|||
os.Chmod(*addr, 0777)
|
||||
}
|
||||
|
||||
key, err = os.ReadFile("key")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to read key file!")
|
||||
}
|
||||
|
||||
api.SetSeedKey(key)
|
||||
api.ScheduleDailyRunRefresh()
|
||||
api.InitDailyRun()
|
||||
|
||||
|
|
Loading…
Reference in New Issue