Various cleanup

pull/23/head
maru 7 months ago
parent 4cc2d3431c
commit 15a32c0e23
No known key found for this signature in database
GPG Key ID: 37689350E9CD0F0D

@ -21,11 +21,12 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http"
"github.com/pagefaultgames/rogueserver/api/account" "github.com/pagefaultgames/rogueserver/api/account"
"github.com/pagefaultgames/rogueserver/api/daily" "github.com/pagefaultgames/rogueserver/api/daily"
"github.com/pagefaultgames/rogueserver/db" "github.com/pagefaultgames/rogueserver/db"
"log"
"net/http"
) )
func Init(mux *http.ServeMux) error { func Init(mux *http.ServeMux) error {
@ -86,7 +87,11 @@ func tokenFromRequest(r *http.Request) ([]byte, error) {
func uuidFromRequest(r *http.Request) ([]byte, error) { func uuidFromRequest(r *http.Request) ([]byte, error) {
_, uuid, err := tokenAndUuidFromRequest(r) _, uuid, err := tokenAndUuidFromRequest(r)
return uuid, err if err != nil {
return nil, err
}
return uuid, nil
} }
func tokenAndUuidFromRequest(r *http.Request) ([]byte, []byte, error) { func tokenAndUuidFromRequest(r *http.Request) ([]byte, []byte, error) {
@ -108,7 +113,7 @@ func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
http.Error(w, err.Error(), code) http.Error(w, err.Error(), code)
} }
func jsonResponse(w http.ResponseWriter, r *http.Request, data any) { func writeJSON(w http.ResponseWriter, r *http.Request, data any) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data) err := json.NewEncoder(w).Encode(data)
if err != nil { if err != nil {

@ -59,7 +59,7 @@ func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
return return
} }
jsonResponse(w, r, response) writeJSON(w, r, response)
} }
func handleAccountRegister(w http.ResponseWriter, r *http.Request) { func handleAccountRegister(w http.ResponseWriter, r *http.Request) {
@ -91,7 +91,7 @@ func handleAccountLogin(w http.ResponseWriter, r *http.Request) {
return return
} }
jsonResponse(w, r, response) writeJSON(w, r, response)
} }
func handleAccountChangePW(w http.ResponseWriter, r *http.Request) { func handleAccountChangePW(w http.ResponseWriter, r *http.Request) {
@ -139,7 +139,7 @@ func handleGameTitleStats(w http.ResponseWriter, r *http.Request) {
BattleCount: battleCount, BattleCount: battleCount,
} }
jsonResponse(w, r, stats) writeJSON(w, r, stats)
} }
func handleGameClassicSessionCount(w http.ResponseWriter, r *http.Request) { func handleGameClassicSessionCount(w http.ResponseWriter, r *http.Request) {
@ -187,7 +187,7 @@ func handleGetSessionData(w http.ResponseWriter, r *http.Request) {
return return
} }
jsonResponse(w, r, save) writeJSON(w, r, save)
} }
const legacyClientSessionId = "LEGACY_CLIENT" const legacyClientSessionId = "LEGACY_CLIENT"
@ -237,11 +237,11 @@ func legacyHandleGetSaveData(w http.ResponseWriter, r *http.Request) {
return return
} }
jsonResponse(w, r, save) writeJSON(w, r, save)
} }
// FIXME UNFINISHED!!! // FIXME UNFINISHED!!!
func clearSessionData(w http.ResponseWriter, r *http.Request) { /*func clearSessionData(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r) uuid, err := uuidFromRequest(r)
if err != nil { if err != nil {
httpError(w, r, err, http.StatusBadRequest) httpError(w, r, err, http.StatusBadRequest)
@ -407,7 +407,7 @@ func deleteSystemSave(w http.ResponseWriter, r *http.Request) {
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }*/
func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) { func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) {
uuid, err := uuidFromRequest(r) uuid, err := uuidFromRequest(r)
@ -567,7 +567,7 @@ func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) {
return return
} }
jsonResponse(w, r, save) writeJSON(w, r, save)
} }
type CombinedSaveData struct { type CombinedSaveData struct {
@ -699,7 +699,7 @@ func handleSystemVerify(w http.ResponseWriter, r *http.Request) {
return return
} }
jsonResponse(w, r, response) writeJSON(w, r, response)
} }
func handleGetSystemData(w http.ResponseWriter, r *http.Request) { func handleGetSystemData(w http.ResponseWriter, r *http.Request) {
@ -735,7 +735,7 @@ func handleGetSystemData(w http.ResponseWriter, r *http.Request) {
} }
//TODO apply vouchers //TODO apply vouchers
jsonResponse(w, r, save) writeJSON(w, r, save)
} }
func legacyHandleNewClear(w http.ResponseWriter, r *http.Request) { func legacyHandleNewClear(w http.ResponseWriter, r *http.Request) {
@ -760,7 +760,7 @@ func legacyHandleNewClear(w http.ResponseWriter, r *http.Request) {
return return
} }
jsonResponse(w, r, newClear) writeJSON(w, r, newClear)
} }
// daily // daily
@ -804,7 +804,7 @@ func handleDailyRankings(w http.ResponseWriter, r *http.Request) {
return return
} }
jsonResponse(w, r, rankings) writeJSON(w, r, rankings)
} }
func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) { func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {

@ -19,9 +19,10 @@ package savedata
import ( import (
"fmt" "fmt"
"log"
"github.com/pagefaultgames/rogueserver/db" "github.com/pagefaultgames/rogueserver/db"
"github.com/pagefaultgames/rogueserver/defs" "github.com/pagefaultgames/rogueserver/defs"
"log"
) )
// /savedata/delete - delete save data // /savedata/delete - delete save data
@ -33,14 +34,20 @@ func Delete(uuid []byte, datatype, slot int) error {
switch datatype { switch datatype {
case 0: // System case 0: // System
return db.DeleteSystemSaveData(uuid) err = db.DeleteSystemSaveData(uuid)
case 1: // Session case 1: // Session
if slot < 0 || slot >= defs.SessionSlotCount { if slot < 0 || slot >= defs.SessionSlotCount {
return fmt.Errorf("slot id %d out of range", slot) err = fmt.Errorf("slot id %d out of range", slot)
break
} }
return db.DeleteSessionSaveData(uuid, slot) err = db.DeleteSessionSaveData(uuid, slot)
default: default:
return fmt.Errorf("invalid data type") err = fmt.Errorf("invalid data type")
} }
if err != nil {
return err
}
return nil
} }

@ -41,7 +41,6 @@ func GetDailyRunSeed() (string, error) {
} }
return seed, nil return seed, nil
} }
func AddOrUpdateAccountDailyRun(uuid []byte, score int, wave int) error { func AddOrUpdateAccountDailyRun(uuid []byte, score int, wave int) error {

Loading…
Cancel
Save