Change daily seed generation and update schema for recording failed daily runs
parent
4710617f23
commit
2cb7be52eb
@ -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 RandString(length int) string {
|
||||
b := make([]byte, length)
|
||||
|
||||
func SeedFromTime(seedTime time.Time) []byte {
|
||||
day := make([]byte, 8)
|
||||
binary.BigEndian.PutUint64(day, uint64(math.Floor(float64(seedTime.Unix())/float64(time.Hour*24))))
|
||||
rand.Read(b)
|
||||
|
||||
sum := md5.Sum(append(seedKey, day...))
|
||||
for i := range b {
|
||||
b[i] = randRunes[int(b[i])%lenRandRunes]
|
||||
}
|
||||
|
||||
return sum[:]
|
||||
return string(b)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue