feat: gamemaster: change score

This commit is contained in:
Settel 2022-03-25 11:30:51 +01:00
parent fe04237d0b
commit 278dcbc0b0
3 changed files with 51 additions and 3 deletions

View File

@ -9,7 +9,12 @@
<div class="page-gamemaster__tiles">
<AdminTileMyself :user="user" />
<AdminTileGameinfo :gameinfo="gameinfo" :players="players" />
<AdminTilePlayers :gameinfo="gameinfo" :players="players" @edit="editPlayer" />
<AdminTilePlayers
:gameinfo="gameinfo"
:players="players"
@edit="editPlayer"
@reload="$fetch"
/>
</div>
</div>
</template>

View File

@ -3,10 +3,12 @@ package application
import (
"fmt"
"github.com/google/uuid"
"net/http"
"path"
"sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/user"
"strconv"
)
func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *http.Request) {
@ -26,6 +28,7 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt
id := r.URL.Query().Get("id")
name := r.URL.Query().Get("name")
score := r.URL.Query().Get("score")
authcode := r.URL.Query().Get("authcode")
if name == "" {
w.WriteHeader(http.StatusBadRequest)
@ -48,12 +51,37 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt
return
}
}
if err := app.updateScore(gm, id, score); err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Printf("%v\n", err)
fmt.Fprintf(w, "server error")
return
}
fmt.Fprintf(w, "ok")
}
func (app *Application) updateScore(gm *game.Game, id string, scoreString string) error {
scoreInt, err := strconv.Atoi(scoreString)
if err != nil {
return nil
}
if err := gm.SetScoreByPlayer(id, scoreInt); err != nil {
return err
}
if err := app.saveGameState(gm); err != nil {
return err
}
return nil
}
func (app *Application) modifyUser(id string, gm *game.Game, name, authcode string) error {
if usr, err := app.GetUserByAuthcode(authcode); err == nil && usr.GetId() != id {
return fmt.Errorf("%s authcode already in use", gm.GetId())
if authcode != "" {
if usr, err := app.GetUserByAuthcode(authcode); err == nil && usr.GetId() != id {
return fmt.Errorf("%s authcode already in use", gm.GetId())
}
}
modifyUser := app.users[id]

View File

@ -0,0 +1,15 @@
package game
import (
"fmt"
)
func (gm *Game) SetScoreByPlayer(id string, score int) error {
p := gm.players[id]
if p.id != id {
return fmt.Errorf("player id \"%s\" not found")
}
p.score = score
gm.players[id] = p
return nil
}