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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if true {
|
log.Info("remove game %s\n", gm.GetId())
|
||||||
err := fmt.Errorf("not yet implmemented (%s)", gm.GetId())
|
delete(app.games, gameRef)
|
||||||
log.ErrorLog(err)
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
// if true {
|
||||||
fmt.Fprintf(w, "couldn't save game state")
|
// 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")
|
fmt.Fprintf(w, "ok")
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"sirlab.de/go/knowyt/game"
|
||||||
"sirlab.de/go/knowyt/log"
|
"sirlab.de/go/knowyt/log"
|
||||||
"sirlab.de/go/knowyt/user"
|
"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")
|
id := r.URL.Query().Get("id")
|
||||||
deleteUser := app.users[id]
|
if id == usr.GetId() {
|
||||||
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() {
|
|
||||||
w.WriteHeader(http.StatusForbidden)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
log.Warn("can't delete yourself\n")
|
log.Warn("can't delete yourself\n")
|
||||||
fmt.Fprintf(w, "forbidden")
|
fmt.Fprintf(w, "forbidden")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if err := app.removePlayerById(id, gm); err != nil {
|
||||||
if err := deleteUser.DeleteUser(); err != nil {
|
w.WriteHeader(http.StatusForbidden)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
log.Warn("%v\n", err.Error())
|
||||||
log.ErrorLog(err)
|
fmt.Fprintf(w, "forbidden")
|
||||||
fmt.Fprintf(w, "internal server error")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
gm.RemovePlayer(deleteUser)
|
|
||||||
delete(app.users, id)
|
|
||||||
|
|
||||||
fmt.Fprintf(w, "ok")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (usr *User) DeleteUser() error {
|
func (usr *User) RemovePlayer() error {
|
||||||
return os.Remove(usr.filename)
|
return os.Remove(usr.filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user