send quote and sources to clients

This commit is contained in:
Settel 2021-08-15 17:43:00 +02:00
parent 7ccef35f3d
commit 5f5033e846
6 changed files with 78 additions and 22 deletions

View File

@ -24,6 +24,8 @@ export default {
right: 0;
bottom: 0;
background-color: #002040;
max-width: 600px;
overflow: auto;
&__json {
margin: 1em 2em;

View File

@ -1,6 +1,7 @@
package game
import (
"fmt"
"sirlab.de/go/knyt/syncdata"
)
@ -17,6 +18,7 @@ func (gm *Game) populateGameInfo() syncdata.GameInfo {
State: gm.state,
Phase: gm.phase,
Players: gm.getPlayers(),
Round: gm.getRoundInfo(),
}
}
@ -34,3 +36,22 @@ func (gm *Game) getPlayers() []syncdata.PlayerInfo {
return players
}
func (gm *Game) getRoundInfo() *syncdata.RoundInfo {
if gm.state != STATE_PLAY {
return nil
}
quote := gm.quotes[gm.round.quoteId]
if quote == nil {
fmt.Printf("fatal error: quote id \"%s\" not found\n", gm.round.quoteId)
return nil
}
roundInfo := syncdata.RoundInfo{
Quote: quote.GetQuote(),
Sources: gm.round.sources,
}
return &roundInfo
}

View File

@ -0,0 +1,37 @@
package game
import (
"fmt"
)
func (gm *Game) runRound() {
state := gm.GetState()
if state != STATE_IDLE && state != STATE_PLAY {
fmt.Println(fmt.Errorf("expected state \"IDLE\" | \"PLAY\" != \"%s\"", state))
return
}
err := gm.changeGameState(STATE_ANY, STATE_PLAY, PHASE_SELECT_QUOTE)
if err != nil {
fmt.Println(err)
return
}
gm.selectQuote()
gm.notifyClients()
}
func (gm *Game) selectQuote() {
gm.mu.Lock()
defer gm.mu.Unlock()
gm.round.quoteId = "455df6bc-070d-4728-83ab-481ceafa8590"
gm.round.sources = []string{
"Herbert Grönemeyer",
"Frank Sinatra",
"Gaius Julius Cäsar",
"Thomas Alva Edison",
"Konfuzius",
"George W. Bush jun.",
}
}

View File

@ -9,22 +9,6 @@ func (gm *Game) StartGame() {
go gm.runRound()
}
func (gm *Game) runRound() {
state := gm.GetState()
if state != STATE_IDLE && state != STATE_PLAY {
fmt.Println(fmt.Errorf("expected state \"IDLE\" | \"PLAY\" != \"%s\"", state))
return
}
err := gm.changeGameState(STATE_ANY, STATE_PLAY, PHASE_NONE)
if err != nil {
fmt.Println(err)
return
}
gm.notifyClients()
}
func (gm *Game) ResetGame() {
err := gm.changeGameState(STATE_ANY, STATE_IDLE, PHASE_NONE)
if err != nil {

View File

@ -7,15 +7,15 @@ import (
)
const (
STATE_ANY = "*"
STATE_IDLE = "idle"
STATE_ENTER_STATEMENTS = "enter-quotes"
STATE_READY_SET = "ready-set"
STATE_PLAY = "play"
STATE_ANY = "*"
STATE_IDLE = "idle"
STATE_READY_SET = "ready-set"
STATE_PLAY = "play"
)
const (
PHASE_NONE = ""
PHASE_NONE = ""
PHASE_SELECT_QUOTE = "select-quote"
)
type playerInfo struct {
@ -25,6 +25,11 @@ type playerInfo struct {
isIdle bool
}
type Round struct {
quoteId string
sources []string
}
type Game struct {
mu sync.Mutex
id string
@ -34,6 +39,7 @@ type Game struct {
state string
phase string
quotes map[string]*quote.Quote
round Round
}
type GameJson struct {

View File

@ -6,11 +6,17 @@ type PlayerInfo struct {
IsIdle bool `json:"isIdle"`
}
type RoundInfo struct {
Quote string `json:"quote"`
Sources []string `json:"sources"`
}
type GameInfo struct {
GameId string `json:"id"`
State string `json:"state"`
Phase string `json:"phase"`
Players []PlayerInfo `json:"players"`
Round *RoundInfo `json:"round"`
}
type SyncData struct {