From cf5643227be094df2c6810c0ceaeb4c9342201fb Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Sun, 11 May 2025 10:03:18 +0300 Subject: [PATCH] Feat: add game over --- components/room.html | 4 ++++ handlers/elements.go | 21 +++++++++++++++++---- handlers/game.go | 3 ++- models/main.go | 3 ++- todos.md | 8 ++++---- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/components/room.html b/components/room.html index e174109..e9058fd 100644 --- a/components/room.html +++ b/components/room.html @@ -9,7 +9,11 @@ {{end}}

+ {{if .Room.IsOver}} +

GAME OVER; team {{.Room.TeamWon}} won! 🧚

+ {{else}}

Turn of the {{.Room.TeamTurn}} team

+ {{end}}

{{if eq .State.Team ""}} join the team! diff --git a/handlers/elements.go b/handlers/elements.go index 999409e..98808c7 100644 --- a/handlers/elements.go +++ b/handlers/elements.go @@ -51,8 +51,6 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } - log.Debug("got state", "state", fi) - // TODO: update room score color, exists := roundWords[word] log.Debug("got show-color request", "word", word, "color", color) if !exists { @@ -75,14 +73,29 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { } fi.Room.ActionHistory = append(fi.Room.ActionHistory, action) // if opened card is of color of opp team, change turn + oppositeColor := fi.Room.GetOppositeTeamColor() switch color { case "black": // game over fi.Room.IsRunning = false fi.Room.IsOver = true - case "white", fi.Room.GetOppositeTeamColor(): + fi.Room.TeamWon = oppositeColor + case "white", oppositeColor: // end turn - fi.Room.TeamTurn = fi.Room.GetOppositeTeamColor() + fi.Room.TeamTurn = oppositeColor + } + // check if no cards left => game over + if fi.Room.BlueCounter == 0 { + // blue won + fi.Room.IsRunning = false + fi.Room.IsOver = true + fi.Room.TeamWon = "blue" + } + if fi.Room.RedCounter == 0 { + // red won + fi.Room.IsRunning = false + fi.Room.IsOver = true + fi.Room.TeamWon = "red" } if err := saveFullInfo(fi); err != nil { abortWithError(w, err.Error()) diff --git a/handlers/game.go b/handlers/game.go index 410ab01..a4a3908 100644 --- a/handlers/game.go +++ b/handlers/game.go @@ -192,11 +192,12 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) { fi.Room.IsOver = false fi.Room.TeamTurn = "blue" fi.Room.LoadTestCards() + fi.Room.UpdateCounter() + fi.Room.TeamWon = "" if err := saveFullInfo(fi); err != nil { abortWithError(w, err.Error()) return } - fi.Room.UpdateCounter() // reveal all cards if fi.State.Role == "mime" { fi.Room.RevealAllCards() diff --git a/models/main.go b/models/main.go index d5e110d..f62db2b 100644 --- a/models/main.go +++ b/models/main.go @@ -70,7 +70,8 @@ type Room struct { Language string `json:"language" example:"en" form:"language"` RoundTime int32 `json:"round_time"` // ProgressPct uint32 `json:"progress_pct"` - IsOver bool + IsOver bool + TeamWon string // blue | red } func (r *Room) GetOppositeTeamColor() string { diff --git a/todos.md b/todos.md index 3123ed1..31bca99 100644 --- a/todos.md +++ b/todos.md @@ -1,10 +1,7 @@ ### feats -- game state (running/not); -- start game: btn, endpoint; -- mime: clue + number; -- game log: what clues were given, who opened what cards; - end game: who won. - end game: two reasons (black word; all words); +- ability to leave room; #### sse points - clue sse update; @@ -19,3 +16,6 @@ ### issues - after the game started (isrunning) players should be able join guessers, but not switch team, or join as a mime; - do not let wrong team press buttons; +- mime can give clues out of turn; +- guesser can guess the word before the clue; +- cleanup backlog after new game is started;