knowyt/server/src/application/finishGame.go
2021-09-26 23:36:47 +02:00

30 lines
571 B
Go

package application
import (
"fmt"
"net/http"
"sirlab.de/go/knyt/user"
)
func (app *Application) FinishGame(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
}
if err := app.saveGameState(gm); err != nil {
fmt.Println(err)
}
gm.FinishGame()
}