mirror of
https://github.com/pagefaultgames/rogueserver.git
synced 2025-07-11 19:02:17 +02:00
Compare commits
6 Commits
834d1e62a0
...
a063b1740c
Author | SHA1 | Date | |
---|---|---|---|
|
a063b1740c | ||
|
e4713e6ea3 | ||
|
2aab022ce3 | ||
|
e7cff35d69 | ||
|
174b962f19 | ||
|
e2efcd550c |
@ -56,7 +56,7 @@ func Init(mux *http.ServeMux) error {
|
||||
|
||||
// new session
|
||||
mux.HandleFunc("POST /savedata/updateall", handleUpdateAll)
|
||||
mux.HandleFunc("POST /savedata/verify", handleSessionVerify)
|
||||
mux.HandleFunc("POST /savedata/system/verify", handleSystemVerify)
|
||||
mux.HandleFunc("GET /savedata/system", handleGetSystemData)
|
||||
mux.HandleFunc("GET /savedata/session", handleGetSessionData)
|
||||
|
||||
|
@ -585,23 +585,18 @@ func handleUpdateAll(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var clientSessionId string
|
||||
if r.URL.Query().Has("clientSessionId") {
|
||||
clientSessionId = r.URL.Query().Get("clientSessionId")
|
||||
}
|
||||
if clientSessionId == "" {
|
||||
clientSessionId = legacyClientSessionId
|
||||
}
|
||||
|
||||
var data CombinedSaveData
|
||||
err = json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if data.ClientSessionId == "" {
|
||||
data.ClientSessionId = legacyClientSessionId
|
||||
}
|
||||
|
||||
var active bool
|
||||
active, err = db.IsActiveSession(uuid, clientSessionId)
|
||||
active, err = db.IsActiveSession(uuid, data.ClientSessionId)
|
||||
if err != nil {
|
||||
httpError(w, r, fmt.Errorf("failed to check active session: %s", err), http.StatusBadRequest)
|
||||
return
|
||||
@ -627,7 +622,7 @@ func handleUpdateAll(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if err := db.UpdateTrainerIds(trainerId, secretId, uuid); err != nil {
|
||||
if err = db.UpdateTrainerIds(trainerId, secretId, uuid); err != nil {
|
||||
httpError(w, r, err, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@ -646,24 +641,23 @@ func handleUpdateAll(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
type SessionVerifyResponse struct {
|
||||
Valid bool `json:"valid"`
|
||||
SessionData *defs.SessionSaveData `json:"sessionData"`
|
||||
type SystemVerifyResponse struct {
|
||||
Valid bool `json:"valid"`
|
||||
SystemData *defs.SystemSaveData `json:"systemData"`
|
||||
}
|
||||
|
||||
type SessionVerifyRequest struct {
|
||||
type SystemVerifyRequest struct {
|
||||
ClientSessionId string `json:"clientSessionId"`
|
||||
Slot int `json:"slot"`
|
||||
}
|
||||
|
||||
func handleSessionVerify(w http.ResponseWriter, r *http.Request) {
|
||||
func handleSystemVerify(w http.ResponseWriter, r *http.Request) {
|
||||
uuid, err := uuidFromRequest(r)
|
||||
if err != nil {
|
||||
httpError(w, r, err, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var input SessionVerifyRequest
|
||||
var input SystemVerifyRequest
|
||||
err = json.NewDecoder(r.Body).Decode(&input)
|
||||
if err != nil {
|
||||
httpError(w, r, fmt.Errorf("failed to decode request body: %s", err), http.StatusBadRequest)
|
||||
@ -677,7 +671,7 @@ func handleSessionVerify(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
response := SessionVerifyResponse{
|
||||
response := SystemVerifyResponse{
|
||||
Valid: active,
|
||||
}
|
||||
|
||||
@ -689,14 +683,14 @@ func handleSessionVerify(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var storedSaveData defs.SessionSaveData
|
||||
storedSaveData, err = db.ReadSessionSaveData(uuid, input.Slot)
|
||||
var storedSaveData defs.SystemSaveData
|
||||
storedSaveData, err = db.ReadSystemSaveData(uuid)
|
||||
if err != nil {
|
||||
httpError(w, r, fmt.Errorf("failed to read session save data: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.SessionData = &storedSaveData
|
||||
response.SystemData = &storedSaveData
|
||||
}
|
||||
|
||||
jsonResponse(w, r, response)
|
||||
@ -733,6 +727,7 @@ func handleGetSystemData(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
return
|
||||
}
|
||||
//TODO apply vouchers
|
||||
|
||||
jsonResponse(w, r, save)
|
||||
}
|
||||
|
@ -57,6 +57,10 @@ func Get(uuid []byte, datatype, slot int) (any, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to update system save data: %s", err)
|
||||
}
|
||||
err = db.DeleteClaimedAccountCompensations(uuid)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to delete claimed compensations: %s", err)
|
||||
}
|
||||
|
||||
err = db.UpdateAccountStats(uuid, system.GameStats, system.VoucherCounts)
|
||||
if err != nil {
|
||||
|
@ -47,11 +47,6 @@ func Update(uuid []byte, slot int, save any) error {
|
||||
return fmt.Errorf("failed to update account stats: %s", err)
|
||||
}
|
||||
|
||||
err = db.DeleteClaimedAccountCompensations(uuid)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete claimed compensations: %s", err)
|
||||
}
|
||||
|
||||
return db.StoreSystemSaveData(uuid, save)
|
||||
|
||||
case defs.SessionSaveData: // Session
|
||||
|
@ -212,7 +212,7 @@ func UpdateTrainerIds(trainerId, secretId int, uuid []byte) error {
|
||||
|
||||
func IsActiveSession(uuid []byte, clientSessionId string) (bool, error) {
|
||||
var storedId string
|
||||
err := handle.QueryRow("SELECT clientSessionId FROM activeClientSessions WHERE sessions.uuid = ?", uuid).Scan(&storedId)
|
||||
err := handle.QueryRow("SELECT clientSessionId FROM activeClientSessions WHERE uuid = ?", uuid).Scan(&storedId)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return false, nil
|
||||
|
4
db/db.go
4
db/db.go
@ -167,9 +167,9 @@ func setupDb(tx *sql.Tx) error {
|
||||
`CREATE TABLE IF NOT EXISTS accountDailyRuns (uuid BINARY(16) NOT NULL, date DATE NOT NULL, score INT(11) NOT NULL DEFAULT 0, wave INT(11) NOT NULL DEFAULT 0, timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (uuid, date), CONSTRAINT accountDailyRuns_ibfk_1 FOREIGN KEY (uuid) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT accountDailyRuns_ibfk_2 FOREIGN KEY (date) REFERENCES dailyRuns (date) ON DELETE NO ACTION ON UPDATE NO ACTION)`,
|
||||
`CREATE INDEX IF NOT EXISTS accountDailyRunsByDate ON accountDailyRuns (date)`,
|
||||
|
||||
`CREATE TABLE IF NOT EXISTS systemSaveData (uuid BINARY(16) PRIMARY KEY, data LONGBLOB, timestamp TIMESTAMP)`,
|
||||
`CREATE TABLE IF NOT EXISTS systemSaveData (uuid BINARY(16) PRIMARY KEY, data LONGBLOB, timestamp TIMESTAMP, FOREIGN KEY (uuid) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE)`,
|
||||
|
||||
`CREATE TABLE IF NOT EXISTS sessionSaveData (uuid BINARY(16), slot TINYINT, data LONGBLOB, timestamp TIMESTAMP, PRIMARY KEY (uuid, slot))`,
|
||||
`CREATE TABLE IF NOT EXISTS sessionSaveData (uuid BINARY(16), slot TINYINT, data LONGBLOB, timestamp TIMESTAMP, PRIMARY KEY (uuid, slot), FOREIGN KEY (uuid) REFERENCES accounts (uuid) ON DELETE CASCADE ON UPDATE CASCADE)`,
|
||||
|
||||
// ----------------------------------
|
||||
// MIGRATION 001
|
||||
|
Loading…
Reference in New Issue
Block a user