Change daily seed generation and update schema for recording failed daily runs
parent
4710617f23
commit
2cb7be52eb
@ -1,23 +1,20 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/rand"
|
||||||
"encoding/binary"
|
|
||||||
"math"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var seedKey []byte // 32 bytes
|
const randRunes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
||||||
|
const lenRandRunes = len(randRunes)
|
||||||
|
|
||||||
func SetSeedKey(key []byte) {
|
func RandString(length int) string {
|
||||||
seedKey = key
|
b := make([]byte, length)
|
||||||
}
|
|
||||||
|
|
||||||
func SeedFromTime(seedTime time.Time) []byte {
|
rand.Read(b)
|
||||||
day := make([]byte, 8)
|
|
||||||
binary.BigEndian.PutUint64(day, uint64(math.Floor(float64(seedTime.Unix())/float64(time.Hour*24))))
|
|
||||||
|
|
||||||
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
|
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 {
|
if len(seed) < 24 {
|
||||||
for range 24 - len(seed) {
|
for range 24 - len(seed) {
|
||||||
seed += "0"
|
seed += "0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newCompletion := true
|
|
||||||
|
|
||||||
var count int
|
var count int
|
||||||
err := handle.QueryRow("SELECT COUNT(*) FROM seedCompletions WHERE uuid = ? AND seed = ?", uuid, seed).Scan(&count)
|
err := handle.QueryRow("SELECT COUNT(*) FROM seedCompletions WHERE uuid = ? AND seed = ?", uuid, seed).Scan(&count)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
} else if count > 0 {
|
} 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 {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return newCompletion, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue