bugfix: fixed crash b/c state.json had empty UUID as key for a newly created user

This commit is contained in:
Settel 2022-04-03 14:37:57 +02:00
parent 8caf619c12
commit 8f87f331cc

View File

@ -44,11 +44,13 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt
return return
} }
} else { } else {
if err := app.createUser(gm, name, authcode); err != nil { if newPlayerId, err := app.createUser(gm, name, authcode); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err) fmt.Printf("%v\n", err)
fmt.Fprintf(w, "server error") fmt.Fprintf(w, "server error")
return return
} else {
id = newPlayerId
} }
} }
@ -97,9 +99,11 @@ func (app *Application) modifyUser(id string, gm *game.Game, name, authcode stri
return nil return nil
} }
func (app *Application) createUser(gm *game.Game, name, authcode string) error { func (app *Application) createUser(gm *game.Game, name, authcode string) (string, error) {
if authcode != "" {
if _, err := app.GetUserByAuthcode(authcode); err == nil { if _, err := app.GetUserByAuthcode(authcode); err == nil {
return fmt.Errorf("%s authcode already in use", gm.GetId()) return "", fmt.Errorf("%s authcode already in use", gm.GetId())
}
} }
dirName := path.Join(app.config.DataDir, "users") dirName := path.Join(app.config.DataDir, "users")
@ -110,9 +114,9 @@ func (app *Application) createUser(gm *game.Game, name, authcode string) error {
newUser.SetName(name) newUser.SetName(name)
newUser.SetAuthcode(authcode) newUser.SetAuthcode(authcode)
if err := newUser.SaveUser(); err != nil { if err := newUser.SaveUser(); err != nil {
return err return "", err
} }
app.users[newUser.GetId()] = newUser app.users[newUser.GetId()] = newUser
gm.AddPlayer(newUser) gm.AddPlayer(newUser)
return nil return newUser.GetId(), nil
} }