Skip to content

Commit

Permalink
Merge branch 'pagefaultgames:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
StrayanDropbear authored May 27, 2024
2 parents 3ca8a72 + b113ffc commit b34a1a8
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 37 deletions.
3 changes: 2 additions & 1 deletion api/account/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"crypto/rand"
"database/sql"
"encoding/base64"
"errors"
"fmt"

"github.com/pagefaultgames/rogueserver/db"
Expand All @@ -43,7 +44,7 @@ func Login(username, password string) (LoginResponse, error) {

key, salt, err := db.FetchAccountKeySaltFromUsername(username)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, sql.ErrNoRows) {
return response, fmt.Errorf("account doesn't exist")
}

Expand Down
21 changes: 15 additions & 6 deletions api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ 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 {
if err := scheduleStatRefresh(); err != nil {
err := scheduleStatRefresh()
if err != nil {
return err
}
if err := daily.Init(); err != nil {

err = daily.Init()
if err != nil {
return err
}

Expand Down Expand Up @@ -64,6 +68,7 @@ func Init(mux *http.ServeMux) error {
mux.HandleFunc("GET /daily/seed", handleDailySeed)
mux.HandleFunc("GET /daily/rankings", handleDailyRankings)
mux.HandleFunc("GET /daily/rankingpagecount", handleDailyRankingPageCount)

return nil
}

Expand All @@ -86,7 +91,11 @@ func tokenFromRequest(r *http.Request) ([]byte, error) {

func uuidFromRequest(r *http.Request) ([]byte, error) {
_, uuid, err := tokenAndUuidFromRequest(r)
return uuid, err
if err != nil {
return nil, err
}

return uuid, nil
}

func tokenAndUuidFromRequest(r *http.Request) ([]byte, []byte, error) {
Expand All @@ -108,7 +117,7 @@ func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
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")
err := json.NewEncoder(w).Encode(data)
if err != nil {
Expand Down
26 changes: 13 additions & 13 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
return
}

jsonResponse(w, r, response)
writeJSON(w, r, response)
}

func handleAccountRegister(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -91,7 +91,7 @@ func handleAccountLogin(w http.ResponseWriter, r *http.Request) {
return
}

jsonResponse(w, r, response)
writeJSON(w, r, response)
}

func handleAccountChangePW(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -139,7 +139,7 @@ func handleGameTitleStats(w http.ResponseWriter, r *http.Request) {
BattleCount: battleCount,
}

jsonResponse(w, r, stats)
writeJSON(w, r, stats)
}

func handleGameClassicSessionCount(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -187,7 +187,7 @@ func handleGetSessionData(w http.ResponseWriter, r *http.Request) {
return
}

jsonResponse(w, r, save)
writeJSON(w, r, save)
}

const legacyClientSessionId = "LEGACY_CLIENT"
Expand Down Expand Up @@ -237,11 +237,11 @@ func legacyHandleGetSaveData(w http.ResponseWriter, r *http.Request) {
return
}

jsonResponse(w, r, save)
writeJSON(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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 err == sql.ErrNoRows {
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
Expand Down Expand Up @@ -567,7 +567,7 @@ func legacyHandleSaveData(w http.ResponseWriter, r *http.Request) {
return
}

jsonResponse(w, r, save)
writeJSON(w, r, save)
}

type CombinedSaveData struct {
Expand Down Expand Up @@ -699,7 +699,7 @@ func handleSystemVerify(w http.ResponseWriter, r *http.Request) {
return
}

jsonResponse(w, r, response)
writeJSON(w, r, response)
}

func handleGetSystemData(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -735,7 +735,7 @@ func handleGetSystemData(w http.ResponseWriter, r *http.Request) {
}
//TODO apply vouchers

jsonResponse(w, r, save)
writeJSON(w, r, save)
}

func legacyHandleNewClear(w http.ResponseWriter, r *http.Request) {
Expand All @@ -760,7 +760,7 @@ func legacyHandleNewClear(w http.ResponseWriter, r *http.Request) {
return
}

jsonResponse(w, r, newClear)
writeJSON(w, r, newClear)
}

// daily
Expand Down Expand Up @@ -804,7 +804,7 @@ func handleDailyRankings(w http.ResponseWriter, r *http.Request) {
return
}

jsonResponse(w, r, rankings)
writeJSON(w, r, rankings)
}

func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {
Expand Down
17 changes: 12 additions & 5 deletions api/savedata/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ package savedata

import (
"fmt"
"log"

"github.com/pagefaultgames/rogueserver/db"
"github.com/pagefaultgames/rogueserver/defs"
"log"
)

// /savedata/delete - delete save data
Expand All @@ -33,14 +34,20 @@ func Delete(uuid []byte, datatype, slot int) error {

switch datatype {
case 0: // System
return db.DeleteSystemSaveData(uuid)
err = db.DeleteSystemSaveData(uuid)
case 1: // Session
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:
return fmt.Errorf("invalid data type")
err = fmt.Errorf("invalid data type")
}
if err != nil {
return err
}

return nil
}
14 changes: 8 additions & 6 deletions db/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,25 +219,27 @@ func UpdateTrainerIds(trainerId, secretId int, uuid []byte) error {
return nil
}

func IsActiveSession(uuid []byte, clientSessionId string) (bool, error) {
var storedId string
err := handle.QueryRow("SELECT clientSessionId FROM activeClientSessions WHERE uuid = ?", uuid).Scan(&storedId)
func IsActiveSession(uuid []byte, sessionId string) (bool, error) {
var id string
err := handle.QueryRow("SELECT clientSessionId FROM activeClientSessions WHERE uuid = ?", uuid).Scan(&id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
err = UpdateActiveSession(uuid, clientSessionId)
err = UpdateActiveSession(uuid, sessionId)
if err != nil {
return false, err
}

return true, nil
}

return false, err
}

return storedId == "" || storedId == clientSessionId, nil
return id == "" || id == sessionId, nil
}

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 {
return err
}
Expand Down
1 change: 0 additions & 1 deletion db/daily.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func GetDailyRunSeed() (string, error) {
}

return seed, nil

}

func AddOrUpdateAccountDailyRun(uuid []byte, score int, wave int) error {
Expand Down
7 changes: 2 additions & 5 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"database/sql"
"fmt"
"log"
"time"

_ "github.com/go-sql-driver/mysql"
)
Expand All @@ -36,12 +35,10 @@ func Init(username, password, protocol, address, database string) error {
return fmt.Errorf("failed to open database connection: %s", err)
}

conns := 128
conns := 64

handle.SetMaxOpenConns(conns)
handle.SetMaxIdleConns(conns / 4)

handle.SetConnMaxIdleTime(time.Second * 10)
handle.SetMaxIdleConns(conns)

tx, err := handle.Begin()
if err != nil {
Expand Down

0 comments on commit b34a1a8

Please sign in to comment.