2024-04-14 17:03:53 -07:00
|
|
|
package daily
|
2024-03-16 18:51:13 -07:00
|
|
|
|
|
|
|
import (
|
2024-03-18 16:55:02 -07:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
2024-03-17 10:18:51 -07:00
|
|
|
"fmt"
|
2024-03-16 18:51:13 -07:00
|
|
|
"log"
|
2024-03-18 16:55:02 -07:00
|
|
|
"os"
|
|
|
|
"time"
|
2024-03-16 18:51:13 -07:00
|
|
|
|
2024-04-14 16:25:36 -07:00
|
|
|
"github.com/pagefaultgames/pokerogue-server/db"
|
2024-04-16 18:01:24 -07:00
|
|
|
"github.com/robfig/cron/v3"
|
2024-03-16 18:51:13 -07:00
|
|
|
)
|
|
|
|
|
2024-03-18 16:55:02 -07:00
|
|
|
const secondsPerDay = 60 * 60 * 24
|
|
|
|
|
2024-03-16 18:51:13 -07:00
|
|
|
var (
|
2024-04-17 16:03:25 -07:00
|
|
|
scheduler = cron.New(cron.WithLocation(time.UTC))
|
2024-04-14 17:13:45 -07:00
|
|
|
secret []byte
|
2024-03-16 18:51:13 -07:00
|
|
|
)
|
|
|
|
|
2024-04-14 17:03:53 -07:00
|
|
|
func Init() error {
|
2024-04-14 17:13:45 -07:00
|
|
|
var err error
|
|
|
|
|
|
|
|
secret, err = os.ReadFile("secret.key")
|
2024-03-16 18:51:13 -07:00
|
|
|
if err != nil {
|
2024-03-18 16:55:02 -07:00
|
|
|
if !os.IsNotExist(err) {
|
2024-04-08 15:15:09 -07:00
|
|
|
return fmt.Errorf("failed to read daily seed secret: %s", err)
|
2024-03-18 16:55:02 -07:00
|
|
|
}
|
2024-03-17 17:48:49 -07:00
|
|
|
|
2024-03-18 16:55:02 -07:00
|
|
|
newSecret := make([]byte, 32)
|
|
|
|
_, err := rand.Read(newSecret)
|
2024-03-17 17:48:49 -07:00
|
|
|
if err != nil {
|
2024-04-08 15:15:09 -07:00
|
|
|
return fmt.Errorf("failed to generate daily seed secret: %s", err)
|
2024-03-17 17:48:49 -07:00
|
|
|
}
|
2024-03-18 16:55:02 -07:00
|
|
|
|
|
|
|
err = os.WriteFile("secret.key", newSecret, 0400)
|
|
|
|
if err != nil {
|
2024-04-08 15:15:09 -07:00
|
|
|
return fmt.Errorf("failed to write daily seed secret: %s", err)
|
2024-03-18 16:55:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
secret = newSecret
|
|
|
|
}
|
|
|
|
|
2024-04-14 17:15:25 -07:00
|
|
|
err = recordNewDaily()
|
2024-03-18 16:55:02 -07:00
|
|
|
if err != nil {
|
2024-04-01 19:54:55 -07:00
|
|
|
log.Print(err)
|
2024-03-16 18:51:13 -07:00
|
|
|
}
|
2024-04-01 19:54:55 -07:00
|
|
|
|
2024-04-14 17:03:53 -07:00
|
|
|
log.Printf("Daily Run Seed: %s", Seed())
|
|
|
|
|
2024-04-16 18:01:24 -07:00
|
|
|
scheduler.AddFunc("@daily", func() {
|
2024-04-17 21:46:00 -07:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2024-04-16 18:01:24 -07:00
|
|
|
err := recordNewDaily()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error while recording new daily: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
scheduler.Start()
|
2024-04-08 15:15:09 -07:00
|
|
|
|
|
|
|
return nil
|
2024-03-16 18:51:13 -07:00
|
|
|
}
|
|
|
|
|
2024-04-14 17:03:53 -07:00
|
|
|
func Seed() string {
|
2024-04-14 17:13:45 -07:00
|
|
|
return base64.StdEncoding.EncodeToString(deriveSeed(time.Now().UTC()))
|
2024-04-14 17:03:53 -07:00
|
|
|
}
|
|
|
|
|
2024-04-14 17:13:45 -07:00
|
|
|
func deriveSeed(seedTime time.Time) []byte {
|
2024-03-18 16:55:02 -07:00
|
|
|
day := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(day, uint64(seedTime.Unix()/secondsPerDay))
|
|
|
|
|
2024-04-14 17:13:45 -07:00
|
|
|
hashedSeed := md5.Sum(append(day, secret...))
|
2024-03-18 16:55:02 -07:00
|
|
|
|
|
|
|
return hashedSeed[:]
|
|
|
|
}
|
2024-04-14 17:13:45 -07:00
|
|
|
|
2024-04-14 17:15:25 -07:00
|
|
|
func recordNewDaily() error {
|
2024-04-14 17:13:45 -07:00
|
|
|
err := db.TryAddDailyRun(Seed())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|