Skip to content

Commit 2f99dc4

Browse files
committed
improve score and round handling
Signed-off-by: Robert Müller 🚀 <code@brauser.io>
1 parent db642dd commit 2f99dc4

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

main.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var (
3030

3131
winHistory = []Round{}
3232
goalHistory = []Goal{}
33+
lastGoalHistory = []Goal{}
3334

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

8485
func undoScore() {
8586
if (!gameIsRunning) { return }
87+
88+
if (len(goalHistory) == 0 && len(winHistory) >= 1) {
89+
goalHistory = lastGoalHistory
90+
winHistory = winHistory[:len(winHistory)-1]
91+
}
92+
8693
if len(goalHistory) > 0 {
8794
goalHistory = goalHistory[:len(goalHistory)-1]
8895
}
@@ -117,6 +124,7 @@ func updateScore() {
117124

118125
publish("score/red", strconv.Itoa(scoreRed), true)
119126
publish("score/white", strconv.Itoa(scoreWhite), true)
127+
publish("round/current", strconv.Itoa(currentRound()), true)
120128

121129
goals, _ := json.Marshal(goalHistory)
122130
publish("round/goals", string(goals), true)
@@ -133,8 +141,8 @@ func updateScore() {
133141
func startGame() {
134142
clearAll()
135143
gameIsRunning = true
136-
publish("game/round", strconv.Itoa(currentRound()), true)
137144
publish("sound/play", "start", false)
145+
updateScore()
138146

139147
}
140148

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

153161
rounds, _ := json.Marshal(winHistory)
154162
fmt.Printf(string(rounds))
163+
lastGoalHistory = goalHistory
155164
resetScore()
156165
}
157166

@@ -198,7 +207,8 @@ func gameEnd(winner string) {
198207

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

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

203213
resetScore()
204214
clearAll()

structs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ type Round struct {
99
Winner string
1010
Time float64
1111
}
12+
13+
type Game struct {
14+
Winner string
15+
Time float64
16+
}

0 commit comments

Comments
 (0)