From 74b14ef33714237d197cd3f9b5ae0c37395c8eca Mon Sep 17 00:00:00 2001 From: Settel Date: Mon, 6 Sep 2021 21:23:14 +0200 Subject: [PATCH] refactoring --- client/src/components/Source.vue | 1 + server/src/game/continueGame.go | 43 ++------------------- server/src/game/populateGetRoundInfo.go | 7 +--- server/src/game/revealShowCount.go | 50 +++++++++++++++++++++++++ server/src/game/revealSource.go | 13 +++++++ server/src/game/struct.go | 6 +++ 6 files changed, 74 insertions(+), 46 deletions(-) create mode 100644 server/src/game/revealShowCount.go create mode 100644 server/src/game/revealSource.go diff --git a/client/src/components/Source.vue b/client/src/components/Source.vue index 2bd3212..27ed68a 100644 --- a/client/src/components/Source.vue +++ b/client/src/components/Source.vue @@ -117,6 +117,7 @@ export default { &-count { width: 100%; text-align: center; + user-select: none; } } } diff --git a/server/src/game/continueGame.go b/server/src/game/continueGame.go index 81e7aad..9b10716 100644 --- a/server/src/game/continueGame.go +++ b/server/src/game/continueGame.go @@ -2,7 +2,6 @@ package game import ( "fmt" - "time" ) func (gm *Game) ContinueGame() { @@ -14,47 +13,11 @@ func (gm *Game) ContinueGame() { switch phase { case PHASE_SELECT_QUOTE: - gm.proceedToReveal() + gm.revealShowCount() + case PHASE_REVEAL_SHOW_COUNT: + gm.revealSource() default: fmt.Printf("invalid state, can't continue game in %s state, %s phase\n", state, phase) 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 -} diff --git a/server/src/game/populateGetRoundInfo.go b/server/src/game/populateGetRoundInfo.go index ee4000a..29686e0 100644 --- a/server/src/game/populateGetRoundInfo.go +++ b/server/src/game/populateGetRoundInfo.go @@ -54,13 +54,8 @@ func (gm *Game) getRevelation() *syncdata.Revelation { return nil } - votes := make(map[string][]string, 0) - for key, val := range gm.round.selections { - votes[val] = append(votes[val], key) - } - revelation := syncdata.Revelation{ - Votes: votes, + Votes: gm.round.revelation.votes, } return &revelation diff --git a/server/src/game/revealShowCount.go b/server/src/game/revealShowCount.go new file mode 100644 index 0000000..46aee73 --- /dev/null +++ b/server/src/game/revealShowCount.go @@ -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 +} diff --git a/server/src/game/revealSource.go b/server/src/game/revealSource.go new file mode 100644 index 0000000..9862ae5 --- /dev/null +++ b/server/src/game/revealSource.go @@ -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() +} diff --git a/server/src/game/struct.go b/server/src/game/struct.go index f3b97ac..500aaef 100644 --- a/server/src/game/struct.go +++ b/server/src/game/struct.go @@ -18,6 +18,7 @@ const ( PHASE_SELECT_QUOTE = "select-quote" PHASE_REVEAL_START = "reveal-start" PHASE_REVEAL_SHOW_COUNT = "reveal-show-count" + PHASE_REVEAL_SOURCE = "reveal-source" ) const ( @@ -37,10 +38,15 @@ type Source struct { name string } +type Revelation struct { + votes map[string][]string +} + type Round struct { quoteId string selections map[string]string sources []Source + revelation Revelation } type Game struct {