mirror of
https://github.com/pagefaultgames/rogueserver.git
synced 2025-07-05 16:02:23 +02:00
Compare commits
5 Commits
4cc2d3431c
...
cd13fe3cff
Author | SHA1 | Date | |
---|---|---|---|
|
cd13fe3cff | ||
|
476e667572 | ||
|
c76746ad35 | ||
|
3489ae91bf | ||
|
15a32c0e23 |
@ -22,6 +22,7 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/pagefaultgames/rogueserver/db"
|
"github.com/pagefaultgames/rogueserver/db"
|
||||||
@ -43,7 +44,7 @@ func Login(username, password string) (LoginResponse, error) {
|
|||||||
|
|
||||||
key, salt, err := db.FetchAccountKeySaltFromUsername(username)
|
key, salt, err := db.FetchAccountKeySaltFromUsername(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
return response, fmt.Errorf("account doesn't exist")
|
return response, fmt.Errorf("account doesn't exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
||||||
@ -532,7 +532,7 @@ func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) {
|
|||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/savedata/get":
|
case "/savedata/get":
|
||||||
save, err = savedata.Get(uuid, datatype, slot)
|
save, err = savedata.Get(uuid, datatype, slot)
|
||||||
if err == sql.ErrNoRows {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
http.Error(w, err.Error(), http.StatusNotFound)
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -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
|
||||||
}
|
}
|
||||||
|
@ -219,25 +219,27 @@ func UpdateTrainerIds(trainerId, secretId int, uuid []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsActiveSession(uuid []byte, clientSessionId string) (bool, error) {
|
func IsActiveSession(uuid []byte, sessionId string) (bool, error) {
|
||||||
var storedId string
|
var id string
|
||||||
err := handle.QueryRow("SELECT clientSessionId FROM activeClientSessions WHERE uuid = ?", uuid).Scan(&storedId)
|
err := handle.QueryRow("SELECT clientSessionId FROM activeClientSessions WHERE uuid = ?", uuid).Scan(&id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
err = UpdateActiveSession(uuid, clientSessionId)
|
err = UpdateActiveSession(uuid, sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return storedId == "" || storedId == clientSessionId, nil
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return id == "" || id == sessionId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateActiveSession(uuid []byte, clientSessionId string) error {
|
func UpdateActiveSession(uuid []byte, clientSessionId string) error {
|
||||||
_, err := handle.Exec("REPLACE INTO activeClientSessions VALUES (?, ?)", uuid, clientSessionId)
|
_, err := handle.Exec("INSERT INTO activeClientSessions (uuid, clientSessionId) VALUES (?, ?) ON DUPLICATE KEY UPDATE clientSessionId = ?", uuid, clientSessionId, clientSessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
7
db/db.go
7
db/db.go
@ -21,7 +21,6 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
)
|
)
|
||||||
@ -36,12 +35,10 @@ func Init(username, password, protocol, address, database string) error {
|
|||||||
return fmt.Errorf("failed to open database connection: %s", err)
|
return fmt.Errorf("failed to open database connection: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
conns := 128
|
conns := 64
|
||||||
|
|
||||||
handle.SetMaxOpenConns(conns)
|
handle.SetMaxOpenConns(conns)
|
||||||
handle.SetMaxIdleConns(conns / 4)
|
handle.SetMaxIdleConns(conns)
|
||||||
|
|
||||||
handle.SetConnMaxIdleTime(time.Second * 10)
|
|
||||||
|
|
||||||
tx, err := handle.Begin()
|
tx, err := handle.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user