From 783e7a8496a206893c57cb484f051dba96932982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller=20=F0=9F=9A=80?= Date: Mon, 2 Dec 2019 19:57:02 +0100 Subject: [PATCH 1/5] refactor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Robert Müller 🚀 --- main.go | 57 ++++++++++++++++++++++++++++++++++++++++++--------------- mqtt.go | 6 ++++++ 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index bd29061..5ffa4f3 100644 --- a/main.go +++ b/main.go @@ -11,8 +11,12 @@ import ( ) var ( - scoreRed = 0 - scoreWhite = 0 + scoreRed = 0 + scoreWhite = 0 + currentRound = 0 + goalHistory = []string{} + availableSoundModes = []string{"default", "meme", "quake", "techno"} + currentSoundMode = "random" ) func mqttURI() *url.URL { @@ -24,8 +28,17 @@ func mqttURI() *url.URL { return uri } -func main() { +func playSound(event string) { + if currentSoundMode == "random" { + publish("sound/play", event, false) + + } else { + publish("sound/play", event+"/"+currentSoundMode, false) + + } +} +func main() { connect("hkick-core", mqttURI()) go subscribe(mqttURI()) @@ -57,25 +70,19 @@ func decreaseScore(team string) { } func increaseScore(team string) { - if team == "red" { - scoreRed++ - } else if team == "white" { - scoreWhite++ - } + goalHistory = append(goalHistory, team) + playSound("goal") - if (scoreRed + scoreWhite) == 1 { - publish("sound/play", "firstgoal", false) - } else { - publish("sound/play", "goal", false) - } + updateScore() +} +func undoScore() { updateScore() } func resetScore() { scoreWhite = 0 scoreRed = 0 - publish("sound/play", "start", false) updateScore() } @@ -89,9 +96,29 @@ func updateScore() { if distance >= 2 { if (scoreRed >= 5) || (scoreWhite >= 5) { - gameEnd() + roundEnd() } } else if (scoreRed >= 8) || (scoreWhite >= 8) { + roundEnd() + } +} + +func startGame() { + currentRound = 1 + publish("game/round", strconv.Itoa(currentRound), true) + publish("sound/play", "start", false) + +} + +func nextRound() { + currentRound++ + resetScore() +} + +func roundEnd() { + if currentRound < 3 { + nextRound() + } else { gameEnd() } } diff --git a/mqtt.go b/mqtt.go index eed0f68..a5e82b0 100644 --- a/mqtt.go +++ b/mqtt.go @@ -39,9 +39,15 @@ func subscribe(uri *url.URL) { client.Subscribe("score/increase", 0, func(client mqtt.Client, msg mqtt.Message) { increaseScore(string(msg.Payload())) }) + client.Subscribe("score/undo", 0, func(client mqtt.Client, msg mqtt.Message) { + undoScore(string(msg.Payload())) + }) client.Subscribe("score/reset", 0, func(client mqtt.Client, msg mqtt.Message) { resetScore() }) + client.Subscribe("game/start", 0, func(client mqtt.Client, msg mqtt.Message) { + startGame() + }) } func publish(topic string, message string, retain bool) { From 02ee533c655c03a6d01905701788683d1f4b6f8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20St=C3=B6ckl?= Date: Wed, 18 Dec 2019 23:03:07 +0100 Subject: [PATCH 2/5] Finish implementing undoScore --- main.go | 4 ++++ mqtt.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 5ffa4f3..fd3bb7c 100644 --- a/main.go +++ b/main.go @@ -77,6 +77,10 @@ func increaseScore(team string) { } func undoScore() { + if len(goalHistory) > 0 { + goalHistory = goalHistory[:len(goalHistory)-1] + } + updateScore() } diff --git a/mqtt.go b/mqtt.go index a5e82b0..aa06446 100644 --- a/mqtt.go +++ b/mqtt.go @@ -40,7 +40,7 @@ func subscribe(uri *url.URL) { increaseScore(string(msg.Payload())) }) client.Subscribe("score/undo", 0, func(client mqtt.Client, msg mqtt.Message) { - undoScore(string(msg.Payload())) + undoScore() }) client.Subscribe("score/reset", 0, func(client mqtt.Client, msg mqtt.Message) { resetScore() From 96f583e4f2495840c4390f615d19bf9b27ee658f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20St=C3=B6ckl?= Date: Wed, 18 Dec 2019 23:18:22 +0100 Subject: [PATCH 3/5] Refactor updateScore --- main.go | 24 ++++++++++++------------ mqtt.go | 3 --- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index fd3bb7c..482a818 100644 --- a/main.go +++ b/main.go @@ -59,16 +59,6 @@ func leadingTeam() string { return "white" } -func decreaseScore(team string) { - if team == "red" { - scoreRed = int(math.Max(0, float64(scoreRed-1))) - } else if team == "white" { - scoreWhite = int(math.Max(0, float64(scoreWhite-1))) - } - publish("sound/play", "denied", false) - updateScore() -} - func increaseScore(team string) { goalHistory = append(goalHistory, team) playSound("goal") @@ -85,12 +75,22 @@ func undoScore() { } func resetScore() { - scoreWhite = 0 - scoreRed = 0 + goalHistory = []string{} updateScore() } func updateScore() { + scoreRed = 0 + scoreWhite = 0 + for _, team := range goalHistory { + switch team { + case "red": + scoreRed++ + case "white": + scoreWhite++ + } + } + distance := int(math.Abs(float64(scoreRed - scoreWhite))) fmt.Printf("red is %d and white is %d (distance %d)\n", scoreRed, scoreWhite, distance) diff --git a/mqtt.go b/mqtt.go index aa06446..a766a37 100644 --- a/mqtt.go +++ b/mqtt.go @@ -33,9 +33,6 @@ func subscribe(uri *url.URL) { client.Subscribe("goals", 0, func(client mqtt.Client, msg mqtt.Message) { increaseScore(string(msg.Payload())) }) - client.Subscribe("score/decrease", 0, func(client mqtt.Client, msg mqtt.Message) { - decreaseScore(string(msg.Payload())) - }) client.Subscribe("score/increase", 0, func(client mqtt.Client, msg mqtt.Message) { increaseScore(string(msg.Payload())) }) From 9da952919320d1ecaa11b1fb4e8f1ad7639150be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20St=C3=B6ckl?= Date: Wed, 18 Dec 2019 23:46:33 +0100 Subject: [PATCH 4/5] Fix round logic --- main.go | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 482a818..0403cb9 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,7 @@ import ( var ( scoreRed = 0 scoreWhite = 0 - currentRound = 0 + winHistory = []string{} goalHistory = []string{} availableSoundModes = []string{"default", "meme", "quake", "techno"} currentSoundMode = "random" @@ -31,10 +31,8 @@ func mqttURI() *url.URL { func playSound(event string) { if currentSoundMode == "random" { publish("sound/play", event, false) - } else { publish("sound/play", event+"/"+currentSoundMode, false) - } } @@ -108,32 +106,54 @@ func updateScore() { } func startGame() { - currentRound = 1 - publish("game/round", strconv.Itoa(currentRound), true) + publish("game/round", strconv.Itoa(currentRound()), true) publish("sound/play", "start", false) } +func currentRound() int { + return len(winHistory) +} + func nextRound() { - currentRound++ resetScore() } func roundEnd() { - if currentRound < 3 { - nextRound() + if scoreRed >= scoreWhite { + winHistory = append(winHistory, "red") + + } else { + winHistory = append(winHistory, "white") + } + + var redWins = 0 + var whiteWins = 0 + for _, team := range winHistory { + switch team { + case "red": + redWins++ + case "white": + whiteWins++ + } + } + + if redWins == 2 { + gameEnd("red") + } else if whiteWins == 2 { + gameEnd("white") } else { - gameEnd() + nextRound() } } -func gameEnd() { +func gameEnd(winner string) { fmt.Println("game is over") - winner := leadingTeam() fmt.Printf("%s is the winner \n", winner) publish("game/end", winner, false) resetScore() + winHistory = []string{} } From 4dc6982dad76d7c877044ec5ac357e8d15bfeb3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20St=C3=B6ckl?= Date: Wed, 18 Dec 2019 23:53:28 +0100 Subject: [PATCH 5/5] Implement temporary fix for dumb team naming --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 0403cb9..4f5ee7e 100644 --- a/main.go +++ b/main.go @@ -138,9 +138,9 @@ func roundEnd() { } } - if redWins == 2 { + if redWins == 1 { gameEnd("red") - } else if whiteWins == 2 { + } else if whiteWins == 1 { gameEnd("white") } else { nextRound()