package application import ( "fmt" "net/http" "sirlab.de/go/knyt/game" "sirlab.de/go/knyt/user" ) func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *http.Request) { gameRef := r.URL.Query().Get("g") gm, err := app.GetGameById(gameRef) if err != nil { w.WriteHeader(http.StatusNotFound) fmt.Fprintf(w, "game not found") return } if usr.GetGameId() != gameRef || !usr.IsGamemaster() { w.WriteHeader(http.StatusForbidden) fmt.Fprintf(w, "forbidden") return } id := r.URL.Query().Get("id") name := r.URL.Query().Get("name") authcode := r.URL.Query().Get("authcode") if name == "" { w.WriteHeader(http.StatusBadRequest) fmt.Printf("player name must not be empty\n") fmt.Fprintf(w, "server error") return } if id != "" { if err := app.modifyUser(id, gm, name, authcode); err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Printf("%v\n", err) fmt.Fprintf(w, "server error") return } } else { if err := app.createUser(gm, name, authcode); err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Printf("%v\n", err) fmt.Fprintf(w, "server error") return } } fmt.Fprintf(w, "ok") } func (app *Application) modifyUser(id string, gm *game.Game, name, authcode string) error { modifyUser := app.users[id] if modifyUser.GetId() != id || modifyUser.GetGameId() != gm.GetId() { return fmt.Errorf("couldn't find player %s in game %s", id, gm.GetId()) } modifyUser.SetName(name) modifyUser.SetAuthcode(authcode) if err := modifyUser.SaveUser(); err != nil { return err } gm.UpdatePlayer(modifyUser) return nil } func (app *Application) createUser(gm *game.Game, name, authcode string) error { return fmt.Errorf("Application.createUser() not yet implemented.") }