feat: gamemaster: change score
This commit is contained in:
parent
fe04237d0b
commit
278dcbc0b0
@ -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>
|
||||
|
@ -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,13 +51,38 @@ 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 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]
|
||||
if modifyUser.GetId() != id || modifyUser.GetGameId() != gm.GetId() {
|
||||
|
15
server/src/game/setScoreByPlayer.go
Normal file
15
server/src/game/setScoreByPlayer.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user