Update account stats on system save

voucher-compensation
Flashfyre 2024-04-06 17:43:11 -04:00
parent d12a008259
commit 99d3490172
5 changed files with 72 additions and 7 deletions

View File

@ -73,7 +73,6 @@ func (s *Server) handleAccountInfo(w http.ResponseWriter, r *http.Request) {
w.Write(response) w.Write(response)
} }
type AccountRegisterRequest GenericAuthRequest type AccountRegisterRequest GenericAuthRequest
// /account/register - register account // /account/register - register account

View File

@ -1,6 +1,7 @@
package api package api
import ( import (
"encoding/gob"
"net/http" "net/http"
) )
@ -9,6 +10,9 @@ type Server struct {
} }
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
if s.Debug { if s.Debug {
w.Header().Set("Access-Control-Allow-Headers", "*") w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Methods", "*") w.Header().Set("Access-Control-Allow-Methods", "*")

View File

@ -13,9 +13,6 @@ import (
) )
func readSystemSaveData(uuid []byte) (defs.SystemSaveData, error) { func readSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
var system defs.SystemSaveData var system defs.SystemSaveData
save, err := os.ReadFile("userdata/" + hex.EncodeToString(uuid) + "/system.pzs") save, err := os.ReadFile("userdata/" + hex.EncodeToString(uuid) + "/system.pzs")
@ -44,9 +41,6 @@ func readSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
} }
func readSessionSaveData(uuid []byte, slotId int) (defs.SessionSaveData, error) { func readSessionSaveData(uuid []byte, slotId int) (defs.SessionSaveData, error) {
gob.Register([]interface{}{})
gob.Register(map[string]interface{}{})
var session defs.SessionSaveData var session defs.SessionSaveData
fileName := "session" fileName := "session"

View File

@ -101,6 +101,12 @@ func (s *Server) handleSavedataUpdate(w http.ResponseWriter, r *http.Request) {
return return
} }
err = db.UpdateAccountStats(uuid, system.GameStats)
if err != nil {
http.Error(w, fmt.Sprintf("failed to update account stats: %s", err), http.StatusBadRequest)
return
}
var gobBuffer bytes.Buffer var gobBuffer bytes.Buffer
err = gob.NewEncoder(&gobBuffer).Encode(system) err = gob.NewEncoder(&gobBuffer).Encode(system)
if err != nil { if err != nil {

View File

@ -2,7 +2,10 @@ package db
import ( import (
"database/sql" "database/sql"
"fmt"
"slices"
"github.com/Flashfyre/pokerogue-server/defs"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
@ -38,6 +41,65 @@ func UpdateAccountLastActivity(uuid []byte) error {
return nil return nil
} }
func UpdateAccountStats(uuid []byte, stats defs.GameStats) error {
var columns = []string{"playTime", "battles", "classicSessionsPlayed", "sessionsWon", "highestEndlessWave", "highestLevel", "pokemonSeen", "pokemonDefeated", "pokemonCaught", "pokemonHatched", "eggsPulled"}
var statCols []string
var statValues []interface{}
m, ok := stats.(map[string]interface{})
if !ok {
return fmt.Errorf("expected map[string]interface{}, got %T", stats)
}
for k, v := range m {
value, ok := v.(float64)
if !ok {
return fmt.Errorf("expected float64, got %T", v)
}
if slices.Contains(columns, k) {
statCols = append(statCols, k)
statValues = append(statValues, value)
}
}
var statArgs []interface{}
statArgs = append(statArgs, uuid)
for range 2 {
statArgs = append(statArgs, statValues...)
}
query := "INSERT INTO accountStats (uuid"
for _, col := range statCols {
query += ", " + col
}
query += ") VALUES (?"
for range len(statCols) {
query += ", ?"
}
query += ") ON DUPLICATE KEY UPDATE "
for i, col := range statCols {
if i > 0 {
query += ", "
}
query += col + " = ?"
}
_, err := handle.Exec(query, statArgs...)
if err != nil {
return err
}
return nil
}
func FetchUsernameFromToken(token []byte) (string, error) { func FetchUsernameFromToken(token []byte) (string, error) {
var username string var username string
err := handle.QueryRow("SELECT a.username FROM accounts a JOIN sessions s ON s.uuid = a.uuid WHERE s.token = ? AND s.expire > UTC_TIMESTAMP()", token).Scan(&username) err := handle.QueryRow("SELECT a.username FROM accounts a JOIN sessions s ON s.uuid = a.uuid WHERE s.token = ? AND s.expire > UTC_TIMESTAMP()", token).Scan(&username)