2024-03-16 18:51:13 -07:00
|
|
|
package api
|
|
|
|
|
|
|
|
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
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-03-16 18:51:13 -07:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-03-18 16:55:02 -07:00
|
|
|
"os"
|
2024-03-17 10:36:32 -07:00
|
|
|
"strconv"
|
2024-03-18 16:55:02 -07:00
|
|
|
"time"
|
2024-03-16 18:51:13 -07:00
|
|
|
|
|
|
|
"github.com/Flashfyre/pokerogue-server/db"
|
2024-03-19 19:05:37 -07:00
|
|
|
"github.com/go-co-op/gocron"
|
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-03-19 19:05:37 -07:00
|
|
|
dailyRunScheduler = gocron.NewScheduler(time.UTC)
|
|
|
|
dailyRunSecret []byte
|
|
|
|
dailyRunSeed string
|
2024-03-16 18:51:13 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func ScheduleDailyRunRefresh() {
|
2024-03-19 19:05:37 -07:00
|
|
|
dailyRunScheduler.Every(1).Day().At("00:00").Do(InitDailyRun)
|
|
|
|
dailyRunScheduler.StartAsync()
|
2024-03-16 18:51:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func InitDailyRun() {
|
2024-03-18 16:55:02 -07:00
|
|
|
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) {
|
|
|
|
log.Fatalf("failed to read daily seed secret: %s", err)
|
|
|
|
}
|
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-03-18 16:55:02 -07:00
|
|
|
log.Fatalf("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 {
|
|
|
|
log.Fatalf("failed to write daily seed secret: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret = newSecret
|
|
|
|
}
|
|
|
|
|
|
|
|
dailyRunSecret = secret
|
|
|
|
|
|
|
|
dailyRunSeed = base64.StdEncoding.EncodeToString(DeriveDailyRunSeed(time.Now().UTC()))
|
|
|
|
|
|
|
|
err = db.TryAddDailyRun(dailyRunSeed)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err.Error())
|
2024-03-16 18:51:13 -07:00
|
|
|
}
|
2024-03-19 19:05:37 -07:00
|
|
|
log.Printf("Daily Run Seed: %s", dailyRunSeed)
|
2024-03-16 18:51:13 -07:00
|
|
|
}
|
|
|
|
|
2024-03-18 16:55:02 -07:00
|
|
|
func DeriveDailyRunSeed(seedTime time.Time) []byte {
|
|
|
|
day := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(day, uint64(seedTime.Unix()/secondsPerDay))
|
|
|
|
|
|
|
|
hashedSeed := md5.Sum(append(day, dailyRunSecret...))
|
|
|
|
|
|
|
|
return hashedSeed[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// /daily/seed - fetch daily run seed
|
2024-03-16 18:51:13 -07:00
|
|
|
|
|
|
|
func (s *Server) HandleSeed(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(dailyRunSeed))
|
|
|
|
}
|
2024-03-17 10:18:51 -07:00
|
|
|
|
|
|
|
// /daily/rankings - fetch daily rankings
|
|
|
|
|
|
|
|
func (s *Server) HandleRankings(w http.ResponseWriter, r *http.Request) {
|
2024-03-17 10:36:32 -07:00
|
|
|
var err error
|
2024-03-23 13:26:00 -07:00
|
|
|
var category int
|
2024-03-17 10:36:32 -07:00
|
|
|
var page int
|
|
|
|
|
2024-03-24 16:00:04 -07:00
|
|
|
var uuid []byte
|
|
|
|
uuid, err = GetUuidFromRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.UpdateAccountLastActivity(uuid)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("failed to update account last activity")
|
|
|
|
}
|
|
|
|
|
2024-03-23 13:26:00 -07:00
|
|
|
if r.URL.Query().Has("category") {
|
|
|
|
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
category = 0
|
|
|
|
}
|
|
|
|
|
2024-03-17 10:36:32 -07:00
|
|
|
if r.URL.Query().Has("page") {
|
|
|
|
page, err = strconv.Atoi(r.URL.Query().Get("page"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("failed to convert page: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
2024-03-23 13:26:00 -07:00
|
|
|
rankings, err := db.FetchRankings(category, page)
|
2024-03-17 10:18:51 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Print("failed to retrieve rankings")
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := json.Marshal(rankings)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
}
|
2024-03-23 13:26:00 -07:00
|
|
|
|
|
|
|
// /daily/rankingpagecount - fetch daily ranking page count
|
|
|
|
|
|
|
|
func (s *Server) HandleRankingPageCount(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
var category int
|
|
|
|
|
|
|
|
if r.URL.Query().Has("category") {
|
|
|
|
category, err = strconv.Atoi(r.URL.Query().Get("category"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("failed to convert category: %s", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
category = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pageCount, err := db.FetchRankingPageCount(category)
|
|
|
|
if err != nil {
|
|
|
|
log.Print("failed to retrieve ranking page count")
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := json.Marshal(pageCount)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("failed to marshal response json: %s", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
}
|