refactoring

This commit is contained in:
Settel 2021-09-06 21:23:14 +02:00
parent c18c1351da
commit 74b14ef337
6 changed files with 74 additions and 46 deletions

View File

@ -117,6 +117,7 @@ export default {
&-count { &-count {
width: 100%; width: 100%;
text-align: center; text-align: center;
user-select: none;
} }
} }
} }

View File

@ -2,7 +2,6 @@ package game
import ( import (
"fmt" "fmt"
"time"
) )
func (gm *Game) ContinueGame() { func (gm *Game) ContinueGame() {
@ -14,47 +13,11 @@ func (gm *Game) ContinueGame() {
switch phase { switch phase {
case PHASE_SELECT_QUOTE: case PHASE_SELECT_QUOTE:
gm.proceedToReveal() gm.revealShowCount()
case PHASE_REVEAL_SHOW_COUNT:
gm.revealSource()
default: default:
fmt.Printf("invalid state, can't continue game in %s state, %s phase\n", state, phase) fmt.Printf("invalid state, can't continue game in %s state, %s phase\n", state, phase)
return return
} }
} }
func (gm *Game) proceedToReveal() {
if !gm.allPlayersHaveSelectedAQuote() {
fmt.Println("not all players have selected a quote yet")
return
}
if err := gm.changeGamePhase(STATE_PLAY, PHASE_SELECT_QUOTE, PHASE_REVEAL_START); err != nil {
fmt.Println(err)
return
}
gm.notifyClients()
time.Sleep(1 * time.Second)
if err := gm.changeGamePhase(STATE_PLAY, PHASE_REVEAL_START, PHASE_REVEAL_SHOW_COUNT); err != nil {
fmt.Println(err)
return
}
gm.notifyClients()
}
func (gm *Game) allPlayersHaveSelectedAQuote() bool {
gm.mu.Lock()
defer gm.mu.Unlock()
for id, playerInfo := range gm.players {
if !playerInfo.isPlaying || playerInfo.isIdle {
continue
}
if len(gm.round.selections[id]) == 0 {
return false
}
}
return true
}

View File

@ -54,13 +54,8 @@ func (gm *Game) getRevelation() *syncdata.Revelation {
return nil return nil
} }
votes := make(map[string][]string, 0)
for key, val := range gm.round.selections {
votes[val] = append(votes[val], key)
}
revelation := syncdata.Revelation{ revelation := syncdata.Revelation{
Votes: votes, Votes: gm.round.revelation.votes,
} }
return &revelation return &revelation

View File

@ -0,0 +1,50 @@
package game
import (
"fmt"
"time"
)
func (gm *Game) revealShowCount() {
if !gm.allPlayersHaveSelectedAQuote() {
fmt.Println("not all players have selected a quote yet")
return
}
if err := gm.changeGamePhase(STATE_PLAY, PHASE_SELECT_QUOTE, PHASE_REVEAL_START); err != nil {
fmt.Println(err)
return
}
gm.round.revelation = Revelation{
votes: make(map[string][]string, 0),
}
gm.notifyClients()
time.Sleep(1 * time.Second)
if err := gm.changeGamePhase(STATE_PLAY, PHASE_REVEAL_START, PHASE_REVEAL_SHOW_COUNT); err != nil {
fmt.Println(err)
return
}
for key, val := range gm.round.selections {
gm.round.revelation.votes[val] = append(gm.round.revelation.votes[val], key)
}
gm.notifyClients()
}
func (gm *Game) allPlayersHaveSelectedAQuote() bool {
gm.mu.Lock()
defer gm.mu.Unlock()
for id, playerInfo := range gm.players {
if !playerInfo.isPlaying || playerInfo.isIdle {
continue
}
if len(gm.round.selections[id]) == 0 {
return false
}
}
return true
}

View File

@ -0,0 +1,13 @@
package game
import (
"fmt"
)
func (gm *Game) revealSource() {
if err := gm.changeGamePhase(STATE_PLAY, PHASE_REVEAL_SHOW_COUNT, PHASE_REVEAL_SOURCE); err != nil {
fmt.Println(err)
return
}
gm.notifyClients()
}

View File

@ -18,6 +18,7 @@ const (
PHASE_SELECT_QUOTE = "select-quote" PHASE_SELECT_QUOTE = "select-quote"
PHASE_REVEAL_START = "reveal-start" PHASE_REVEAL_START = "reveal-start"
PHASE_REVEAL_SHOW_COUNT = "reveal-show-count" PHASE_REVEAL_SHOW_COUNT = "reveal-show-count"
PHASE_REVEAL_SOURCE = "reveal-source"
) )
const ( const (
@ -37,10 +38,15 @@ type Source struct {
name string name string
} }
type Revelation struct {
votes map[string][]string
}
type Round struct { type Round struct {
quoteId string quoteId string
selections map[string]string selections map[string]string
sources []Source sources []Source
revelation Revelation
} }
type Game struct { type Game struct {