send player state to clients

This commit is contained in:
Settel 2021-08-30 12:24:28 +02:00
parent 597aa65834
commit 5d5c4a68aa
6 changed files with 50 additions and 27 deletions

View File

@ -23,7 +23,8 @@ export default {
position: absolute;
right: 0;
bottom: 0;
background-color: #002040;
max-height: 100%;
background-color: rgba(0, 32, 64, 0.75);
max-width: 600px;
overflow: auto;

View File

@ -22,13 +22,5 @@ func (app *Application) SaveSelection(usr *user.User, w http.ResponseWriter, r *
}
selection := r.URL.Query().Get("selection")
if err := gm.SaveSelection(usr, selection); err != nil {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
fmt.Fprintf(w, "ok")
return
gm.SaveSelection(usr, selection)
}

View File

@ -61,9 +61,30 @@ func (gm *Game) getRoundInfo() *syncdata.RoundInfo {
})
}
playerState := make([]syncdata.PlayerState, 0)
for id, playerInfo := range gm.players {
if !playerInfo.isPlaying {
continue
}
state := syncdata.PLAYER_STATE_UNDECIDED
selection := gm.round.selection[id]
if len(selection) > 0 {
state = syncdata.PLAYER_STATE_DECIDED
} else if playerInfo.isIdle {
state = syncdata.PLAYER_STATE_IDLE
}
playerState = append(playerState, syncdata.PlayerState{
Id: id,
State: state,
})
}
roundInfo := syncdata.RoundInfo{
Quote: quote.GetQuote(),
Sources: sources,
Quote: quote.GetQuote(),
Sources: sources,
PlayerState: playerState,
}
return &roundInfo

View File

@ -26,7 +26,9 @@ func (gm *Game) setupRound() {
gm.mu.Lock()
defer gm.mu.Unlock()
gm.round.selection = make(map[string]string, 0)
gm.round = Round{
selection: make(map[string]string, 0),
}
}
func (gm *Game) selectQuote() {

View File

@ -1,22 +1,17 @@
package game
import (
"fmt"
"sirlab.de/go/knyt/user"
)
func (gm *Game) SaveSelection(usr *user.User, selection string) error {
func (gm *Game) SaveSelection(usr *user.User, selection string) {
gm.updateSelection(usr, selection)
gm.notifyClients()
}
func (gm *Game) updateSelection(usr *user.User, selection string) {
gm.mu.Lock()
defer gm.mu.Unlock()
id := usr.GetId()
if gm.players[id].id != id {
return fmt.Errorf("invalid user")
}
gm.round.selection[id] = selection
fmt.Printf("selections: %d\n", len(gm.round.selection))
fmt.Printf("SaveSelection: \"%s\"\n", selection)
return nil
gm.round.selection[usr.GetId()] = selection
}

View File

@ -1,5 +1,11 @@
package syncdata
const (
PLAYER_STATE_IDLE = "idle"
PLAYER_STATE_UNDECIDED = "undecided"
PLAYER_STATE_DECIDED = "decided"
)
type PlayerInfo struct {
Id string `json:"id"`
Name string `json:"name"`
@ -11,9 +17,15 @@ type SourceInfo struct {
Name string `json:"name"`
}
type PlayerState struct {
Id string `json:"id"`
State string `json:"state"`
}
type RoundInfo struct {
Quote string `json:"quote"`
Sources []SourceInfo `json:"sources"`
Quote string `json:"quote"`
Sources []SourceInfo `json:"sources"`
PlayerState []PlayerState `json:"state"`
}
type GameInfo struct {