Compare commits

..

No commits in common. "cd13fe3cffa1052753a6d4c726701258c266ad72" and "4cc2d3431c2fa4869a1a2c21e37f11f04ec31290" have entirely different histories.

7 changed files with 35 additions and 46 deletions

View File

@ -22,7 +22,6 @@ import (
"crypto/rand"
"database/sql"
"encoding/base64"
"errors"
"fmt"
"github.com/pagefaultgames/rogueserver/db"
@ -44,7 +43,7 @@ func Login(username, password string) (LoginResponse, error) {
key, salt, err := db.FetchAccountKeySaltFromUsername(username)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
if err == sql.ErrNoRows {
return response, fmt.Errorf("account doesn't exist")
}

View File

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

View File

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

View File

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

View File

@ -219,27 +219,25 @@ func UpdateTrainerIds(trainerId, secretId int, uuid []byte) error {
return nil
}
func IsActiveSession(uuid []byte, sessionId string) (bool, error) {
var id string
err := handle.QueryRow("SELECT clientSessionId FROM activeClientSessions WHERE uuid = ?", uuid).Scan(&id)
func IsActiveSession(uuid []byte, clientSessionId string) (bool, error) {
var storedId string
err := handle.QueryRow("SELECT clientSessionId FROM activeClientSessions WHERE uuid = ?", uuid).Scan(&storedId)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
err = UpdateActiveSession(uuid, sessionId)
err = UpdateActiveSession(uuid, clientSessionId)
if err != nil {
return false, err
}
return true, nil
}
return false, err
}
return id == "" || id == sessionId, nil
return storedId == "" || storedId == clientSessionId, nil
}
func UpdateActiveSession(uuid []byte, clientSessionId string) error {
_, err := handle.Exec("INSERT INTO activeClientSessions (uuid, clientSessionId) VALUES (?, ?) ON DUPLICATE KEY UPDATE clientSessionId = ?", uuid, clientSessionId, clientSessionId)
_, err := handle.Exec("REPLACE INTO activeClientSessions VALUES (?, ?)", uuid, clientSessionId)
if err != nil {
return err
}

View File

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

View File

@ -21,6 +21,7 @@ import (
"database/sql"
"fmt"
"log"
"time"
_ "github.com/go-sql-driver/mysql"
)
@ -35,10 +36,12 @@ func Init(username, password, protocol, address, database string) error {
return fmt.Errorf("failed to open database connection: %s", err)
}
conns := 64
conns := 128
handle.SetMaxOpenConns(conns)
handle.SetMaxIdleConns(conns)
handle.SetMaxIdleConns(conns / 4)
handle.SetConnMaxIdleTime(time.Second * 10)
tx, err := handle.Begin()
if err != nil {