refactor: extract removePlayerById(), so it's reusable
This commit is contained in:
parent
e4a0b06271
commit
decb7ad693
@ -23,12 +23,15 @@ func (app *Application) RemoveGame(usr *user.User, w http.ResponseWriter, r *htt
|
||||
return
|
||||
}
|
||||
|
||||
if true {
|
||||
err := fmt.Errorf("not yet implmemented (%s)", gm.GetId())
|
||||
log.ErrorLog(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "couldn't save game state")
|
||||
}
|
||||
log.Info("remove game %s\n", gm.GetId())
|
||||
delete(app.games, gameRef)
|
||||
|
||||
// if true {
|
||||
// err := fmt.Errorf("not yet implmemented (%s)", gm.GetId())
|
||||
// log.ErrorLog(err)
|
||||
// w.WriteHeader(http.StatusInternalServerError)
|
||||
// fmt.Fprintf(w, "couldn't save game state")
|
||||
// }
|
||||
|
||||
fmt.Fprintf(w, "ok")
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"sirlab.de/go/knowyt/game"
|
||||
"sirlab.de/go/knowyt/log"
|
||||
"sirlab.de/go/knowyt/user"
|
||||
)
|
||||
@ -24,28 +25,34 @@ func (app *Application) RemovePlayer(usr *user.User, w http.ResponseWriter, r *h
|
||||
}
|
||||
|
||||
id := r.URL.Query().Get("id")
|
||||
deleteUser := app.users[id]
|
||||
if deleteUser.GetId() != id || deleteUser.GetGameId() != gm.GetId() {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
log.Warn("couldn't find player %s in game %s\n", id, gm.GetId())
|
||||
fmt.Fprintf(w, "forbidden")
|
||||
return
|
||||
}
|
||||
if deleteUser.GetId() == usr.GetId() {
|
||||
if id == usr.GetId() {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
log.Warn("can't delete yourself\n")
|
||||
fmt.Fprintf(w, "forbidden")
|
||||
return
|
||||
}
|
||||
|
||||
if err := deleteUser.DeleteUser(); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
log.ErrorLog(err)
|
||||
fmt.Fprintf(w, "internal server error")
|
||||
if err := app.removePlayerById(id, gm); err != nil {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
log.Warn("%v\n", err.Error())
|
||||
fmt.Fprintf(w, "forbidden")
|
||||
return
|
||||
}
|
||||
gm.RemovePlayer(deleteUser)
|
||||
delete(app.users, id)
|
||||
|
||||
fmt.Fprintf(w, "ok")
|
||||
}
|
||||
|
||||
func (app *Application) removePlayerById(id string, gm *game.Game) error {
|
||||
userToRemove := app.users[id]
|
||||
if userToRemove.GetId() != id || userToRemove.GetGameId() != gm.GetId() {
|
||||
return fmt.Errorf("couldn't find player %s in game %s\n", id, gm.GetId())
|
||||
}
|
||||
|
||||
if err := userToRemove.RemovePlayer(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gm.RemovePlayer(userToRemove)
|
||||
delete(app.users, id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ func (usr *User) SaveUser() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (usr *User) DeleteUser() error {
|
||||
func (usr *User) RemovePlayer() error {
|
||||
return os.Remove(usr.filename)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user