From 321b79b25840bdeaa7378883d7e22542c48c9db6 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Sat, 10 May 2025 14:35:24 +0300 Subject: [PATCH] Feat: card counter --- components/cardcounter.html | 6 ++++++ components/room.html | 3 +++ handlers/elements.go | 11 +++++++++++ handlers/game.go | 1 + handlers/handlers.go | 2 ++ models/main.go | 28 ++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+) create mode 100644 components/cardcounter.html diff --git a/components/cardcounter.html b/components/cardcounter.html new file mode 100644 index 0000000..0eb3b43 --- /dev/null +++ b/components/cardcounter.html @@ -0,0 +1,6 @@ +{{define "cardcounter"}} +
+

Blue cards left: {{.BlueCounter}}

+

Red cards left: {{.RedCounter}}

+
+{{end}} diff --git a/components/room.html b/components/room.html index a39409a..ea4c239 100644 --- a/components/room.html +++ b/components/room.html @@ -19,6 +19,9 @@


+ {{if .Room.IsRunning}} + {{template "cardcounter" .Room}} + {{end}}
{{template "teamlist" .Room.BlueTeam}} diff --git a/handlers/elements.go b/handlers/elements.go index 46f2acf..32f2995 100644 --- a/handlers/elements.go +++ b/handlers/elements.go @@ -65,6 +65,17 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { Revealed: true, } fi.Room.RevealSpecificWord(word) + fi.Room.UpdateCounter() + // if opened card is of color of opp team, change turn + switch color { + case "black": + // game over + fi.Room.IsRunning = false + fi.Room.IsOver = true + case "white", fi.Room.GetOppositeTeamColor(): + // end turn + fi.Room.TeamTurn = fi.Room.GetOppositeTeamColor() + } if err := saveFullInfo(fi); err != nil { abortWithError(w, err.Error()) return diff --git a/handlers/game.go b/handlers/game.go index a882e90..0165bca 100644 --- a/handlers/game.go +++ b/handlers/game.go @@ -196,6 +196,7 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } + fi.Room.UpdateCounter() // reveal all cards if fi.State.Role == "mime" { fi.Room.RevealAllCards() diff --git a/handlers/handlers.go b/handlers/handlers.go index 96ac54a..6bcc45b 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -70,6 +70,8 @@ func HandleHome(w http.ResponseWriter, r *http.Request) { if fi != nil && fi.Room != nil && fi.State != nil { if fi.State.Role == "mime" { fi.Room.RevealAllCards() + } else { + fi.Room.UpdateCounter() } } if fi != nil && fi.Room == nil { diff --git a/models/main.go b/models/main.go index 4fc215e..4fb6d86 100644 --- a/models/main.go +++ b/models/main.go @@ -62,6 +62,34 @@ type Room struct { IsOver bool } +func (r *Room) GetOppositeTeamColor() string { + switch r.TeamTurn { + case "red": + return "blue" + case "blue": + return "red" + } + return "" +} + +func (r *Room) UpdateCounter() { + redCounter := uint8(0) + blueCounter := uint8(0) + for _, card := range r.Cards { + if card.Revealed { + continue + } + switch card.Color { + case WordColorRed: + redCounter++ + case WordColorBlue: + blueCounter++ + } + } + r.RedCounter = redCounter + r.BlueCounter = blueCounter +} + func (r *Room) LoadTestCards() { cards := []WordCard{ {Word: "hamster", Color: "blue"},