Skip to content

Commit

Permalink
improve score and round handling
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Müller 🚀 <code@brauser.io>
  • Loading branch information
Flipez committed Jan 13, 2020
1 parent db642dd commit 2f99dc4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (

winHistory = []Round{}
goalHistory = []Goal{}
lastGoalHistory = []Goal{}

availableSoundModes = []string{"default", "meme", "quake", "techno"}
currentSoundMode = "random"
Expand Down Expand Up @@ -83,6 +84,12 @@ func increaseScore(team string) {

func undoScore() {
if (!gameIsRunning) { return }

if (len(goalHistory) == 0 && len(winHistory) >= 1) {
goalHistory = lastGoalHistory
winHistory = winHistory[:len(winHistory)-1]
}

if len(goalHistory) > 0 {
goalHistory = goalHistory[:len(goalHistory)-1]
}
Expand Down Expand Up @@ -117,6 +124,7 @@ func updateScore() {

publish("score/red", strconv.Itoa(scoreRed), true)
publish("score/white", strconv.Itoa(scoreWhite), true)
publish("round/current", strconv.Itoa(currentRound()), true)

goals, _ := json.Marshal(goalHistory)
publish("round/goals", string(goals), true)
Expand All @@ -133,8 +141,8 @@ func updateScore() {
func startGame() {
clearAll()
gameIsRunning = true
publish("game/round", strconv.Itoa(currentRound()), true)
publish("sound/play", "start", false)
updateScore()

}

Expand All @@ -152,6 +160,7 @@ func nextRound() {

rounds, _ := json.Marshal(winHistory)
fmt.Printf(string(rounds))
lastGoalHistory = goalHistory
resetScore()
}

Expand Down Expand Up @@ -198,7 +207,8 @@ func gameEnd(winner string) {

fmt.Printf("%s is the winner \n", winner)

publish("game/end", winner, false)
game, _ := json.Marshal(Game{Winner: winner, Time: time.Since(gameStartTime).Seconds()})
publish("game/end", string(game), false)

resetScore()
clearAll()
Expand Down
5 changes: 5 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ type Round struct {
Winner string
Time float64
}

type Game struct {
Winner string
Time float64
}

0 comments on commit 2f99dc4

Please sign in to comment.