feat: output game id with every log message

This commit is contained in:
Settel 2022-03-16 09:16:40 +01:00
parent c90fabf022
commit 5f58208140
11 changed files with 21 additions and 14 deletions

View File

@ -52,6 +52,10 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt
} }
func (app *Application) modifyUser(id string, gm *game.Game, name, authcode string) error { func (app *Application) modifyUser(id string, gm *game.Game, name, authcode string) error {
if usr, err := app.GetUserByAuthcode(authcode); err == nil && usr.GetId() != id {
return fmt.Errorf("authcode already in use")
}
modifyUser := app.users[id] modifyUser := app.users[id]
if modifyUser.GetId() != id || modifyUser.GetGameId() != gm.GetId() { if modifyUser.GetId() != id || modifyUser.GetGameId() != gm.GetId() {
return fmt.Errorf("couldn't find player %s in game %s", id, gm.GetId()) return fmt.Errorf("couldn't find player %s in game %s", id, gm.GetId())
@ -66,6 +70,10 @@ func (app *Application) modifyUser(id string, gm *game.Game, name, authcode stri
} }
func (app *Application) createUser(gm *game.Game, name, authcode string) error { func (app *Application) createUser(gm *game.Game, name, authcode string) error {
if _, err := app.GetUserByAuthcode(authcode); err == nil {
return fmt.Errorf("authcode already in use")
}
dirName := path.Join(app.config.DataDir, "users") dirName := path.Join(app.config.DataDir, "users")
userNewUuid := uuid.NewString() userNewUuid := uuid.NewString()
fileName := path.Join(dirName, userNewUuid+".json") fileName := path.Join(dirName, userNewUuid+".json")

View File

@ -18,6 +18,6 @@ func (eng *Engine) publish(populateSyncDataCb PopulateSyncDataCb) {
populateSyncDataCb(&data) populateSyncDataCb(&data)
} }
fmt.Printf("engine versionRef %d\n", eng.versionRef) fmt.Printf("%s engine versionRef %d\n", data.GameInfo.GameId, eng.versionRef)
eng.obs.Update(data) eng.obs.Update(data)
} }

View File

@ -9,7 +9,7 @@ func (gm *Game) changeGameState(expectedState, newState, newPhase string) error
defer gm.mu.Unlock() defer gm.mu.Unlock()
if expectedState != STATE_ANY && gm.state != expectedState { if expectedState != STATE_ANY && gm.state != expectedState {
return fmt.Errorf("game state \"%s\" != expected state \"%s\"", gm.state, expectedState) return fmt.Errorf("%s game state \"%s\" != expected state \"%s\"", gm.id, gm.state, expectedState)
} }
gm.state = newState gm.state = newState
gm.phase = newPhase gm.phase = newPhase
@ -22,10 +22,10 @@ func (gm *Game) changeGamePhase(expectedState, expectedPhase, newPhase string) e
defer gm.mu.Unlock() defer gm.mu.Unlock()
if expectedState != STATE_ANY && gm.state != expectedState { if expectedState != STATE_ANY && gm.state != expectedState {
return fmt.Errorf("game state \"%s\" != expected state \"%s\"", gm.state, expectedState) return fmt.Errorf("%s game state \"%s\" != expected state \"%s\"", gm.id, gm.state, expectedState)
} }
if gm.phase != expectedPhase { if gm.phase != expectedPhase {
return fmt.Errorf("game phase \"%s\" != expected phase \"%s\"", gm.phase, expectedPhase) return fmt.Errorf("%s game phase \"%s\" != expected phase \"%s\"", gm.id, gm.phase, expectedPhase)
} }
gm.phase = newPhase gm.phase = newPhase

View File

@ -7,7 +7,7 @@ import (
func (gm *Game) FinishGame() { func (gm *Game) FinishGame() {
state, _ := gm.GetState() state, _ := gm.GetState()
if state != STATE_IDLE && state != STATE_PLAY { if state != STATE_IDLE && state != STATE_PLAY {
fmt.Printf("invalid state, can't finish game in %s state\n", state) fmt.Printf("%s invalid state, can't finish game in %s state\n", gm.id, state)
return return
} }

View File

@ -23,9 +23,9 @@ func (gm *Game) EventPlayerJoins(usr *user.User) {
gm.players[usrId] = player gm.players[usrId] = player
if prevPlayer.id != "" { if prevPlayer.id != "" {
fmt.Printf("player \"%s\" re-joined\n", gm.players[usrId].name) fmt.Printf("%s player \"%s\" re-joined\n", gm.id, gm.players[usrId].name)
} else { } else {
fmt.Printf("player \"%s\" joined\n", usrName) fmt.Printf("%s player \"%s\" joined\n", gm.id, usrName)
} }
gm.eng.Update() gm.eng.Update()
@ -44,7 +44,7 @@ func (gm *Game) EventPlayerLeaves(usr *user.User) {
player.isIdle = true player.isIdle = true
gm.players[usrId] = player gm.players[usrId] = player
fmt.Printf("player \"%s\" is idle\n", usr.GetName()) fmt.Printf("%s player \"%s\" is idle\n", gm.id, usr.GetName())
gm.eng.Update() gm.eng.Update()
} }

View File

@ -12,7 +12,7 @@ func (gm *Game) populateGetRoundInfo() *syncdata.RoundInfo {
quote := gm.quotes[gm.round.quoteId] quote := gm.quotes[gm.round.quoteId]
if quote == nil { if quote == nil {
fmt.Printf("fatal error: quote id \"%s\" not found\n", gm.round.quoteId) fmt.Printf("%s fatal error: quote id \"%s\" not found\n", gm.id, gm.round.quoteId)
return nil return nil
} }

View File

@ -6,7 +6,7 @@ import (
func (gm *Game) revealShowCount() { func (gm *Game) revealShowCount() {
if !gm.allPlayersHaveSelectedAQuote() { if !gm.allPlayersHaveSelectedAQuote() {
fmt.Println("not all players have selected a quote yet") fmt.Printf("%s not all players have selected a quote yet\n", gm.GetId())
return return
} }

View File

@ -7,7 +7,7 @@ import (
func (gm *Game) runFinal() { func (gm *Game) runFinal() {
state, _ := gm.GetState() state, _ := gm.GetState()
if state != STATE_IDLE && state != STATE_PLAY { if state != STATE_IDLE && state != STATE_PLAY {
fmt.Println(fmt.Errorf("expected state \"IDLE\" | \"PLAY\" != \"%s\"", state)) fmt.Printf("%s expected state \"IDLE\" | \"PLAY\" != \"%s\"", gm.GetId(), state)
return return
} }

View File

@ -7,7 +7,7 @@ import (
func (gm *Game) runRound() { func (gm *Game) runRound() {
state, _ := gm.GetState() state, _ := gm.GetState()
if state != STATE_IDLE && state != STATE_PLAY && state != STATE_COLLECT { if state != STATE_IDLE && state != STATE_PLAY && state != STATE_COLLECT {
fmt.Println(fmt.Errorf("expected state \"IDLE\" | \"PLAY\" | \"COLLECT\" != \"%s\"", state)) fmt.Printf("%s expected state \"IDLE\" | \"PLAY\" | \"COLLECT\" != \"%s\"", gm.GetId(), state)
return return
} }

View File

@ -26,5 +26,5 @@ func (gm *Game) updateSelection(usr *user.User, selection string) {
} }
} }
fmt.Printf("invalid selection id \"%s\"\n", selection) fmt.Printf("%s invalid selection id \"%s\"\n", gm.id, selection)
} }

View File

@ -27,7 +27,6 @@ func (authMux *AuthMux) accessDenied(w http.ResponseWriter, r *http.Request) {
func (authMux *AuthMux) getUserFromSession(r *http.Request) (*user.User, error) { func (authMux *AuthMux) getUserFromSession(r *http.Request) (*user.User, error) {
authCookie, err := r.Cookie("knowyt-auth") authCookie, err := r.Cookie("knowyt-auth")
if err != nil { if err != nil {
fmt.Printf("%v\n", err)
return nil, fmt.Errorf("invalid cookie") return nil, fmt.Errorf("invalid cookie")
} }