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"> <div class="page-gamemaster__tiles">
<AdminTileMyself :user="user" /> <AdminTileMyself :user="user" />
<AdminTileGameinfo :gameinfo="gameinfo" :players="players" /> <AdminTileGameinfo :gameinfo="gameinfo" :players="players" />
<AdminTilePlayers :gameinfo="gameinfo" :players="players" @edit="editPlayer" /> <AdminTilePlayers
:gameinfo="gameinfo"
:players="players"
@edit="editPlayer"
@reload="$fetch"
/>
</div> </div>
</div> </div>
</template> </template>

View File

@ -3,10 +3,12 @@ package application
import ( import (
"fmt" "fmt"
"github.com/google/uuid" "github.com/google/uuid"
"net/http" "net/http"
"path" "path"
"sirlab.de/go/knowyt/game" "sirlab.de/go/knowyt/game"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
"strconv"
) )
func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *http.Request) { 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") id := r.URL.Query().Get("id")
name := r.URL.Query().Get("name") name := r.URL.Query().Get("name")
score := r.URL.Query().Get("score")
authcode := r.URL.Query().Get("authcode") authcode := r.URL.Query().Get("authcode")
if name == "" { if name == "" {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -48,13 +51,38 @@ func (app *Application) SavePlayer(usr *user.User, w http.ResponseWriter, r *htt
return 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") 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 { func (app *Application) modifyUser(id string, gm *game.Game, name, authcode string) error {
if authcode != "" {
if usr, err := app.GetUserByAuthcode(authcode); err == nil && usr.GetId() != id { if usr, err := app.GetUserByAuthcode(authcode); err == nil && usr.GetId() != id {
return fmt.Errorf("%s authcode already in use", gm.GetId()) return fmt.Errorf("%s authcode already in use", gm.GetId())
} }
}
modifyUser := app.users[id] modifyUser := app.users[id]
if modifyUser.GetId() != id || modifyUser.GetGameId() != gm.GetId() { if modifyUser.GetId() != id || modifyUser.GetGameId() != gm.GetId() {

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
}