From 45761446e51408858c3f89fe506829fb9052c41e Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Fri, 9 May 2025 11:54:01 +0300 Subject: [PATCH] Feat: start game --- components/room.html | 2 +- handlers/game.go | 24 ++++++++++++++++++++++++ main.go | 1 + models/main.go | 31 +++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/components/room.html b/components/room.html index 0e20c35..644d9da 100644 --- a/components/room.html +++ b/components/room.html @@ -4,7 +4,7 @@

Room created by {{.Room.CreatorName}};

Game is running: {{.Room.IsRunning}}

- {{if eq .State.Username .Room.CreatorName}} + {{if and (eq .State.Username .Room.CreatorName) (not .Room.IsRunning)}} {{end}}

diff --git a/handlers/game.go b/handlers/game.go index 73889d5..cf0a323 100644 --- a/handlers/game.go +++ b/handlers/game.go @@ -161,3 +161,27 @@ func HandleEndTurn(w http.ResponseWriter, r *http.Request) { } tmpl.ExecuteTemplate(w, "base", fi) } + +func HandleStartGame(w http.ResponseWriter, r *http.Request) { + fi, err := getFullInfoByCtx(r.Context()) + if err != nil { + abortWithError(w, err.Error()) + return + } + // TODO: check if enough players; also button should be hidden if already running + fi.Room.IsRunning = true + fi.Room.IsOver = false + fi.Room.TeamTurn = "blue" + fi.Room.LoadTestCards() + if err := saveFullInfo(fi); err != nil { + abortWithError(w, err.Error()) + return + } + // return html + tmpl, err := template.ParseGlob("components/*.html") + if err != nil { + abortWithError(w, err.Error()) + return + } + tmpl.ExecuteTemplate(w, "room", fi) +} diff --git a/main.go b/main.go index f06fd9f..0468bbc 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ func ListenToRequests(port string) error { mux.HandleFunc("POST /join-team", handlers.HandleJoinTeam) mux.HandleFunc("GET /end-turn", handlers.HandleEndTurn) mux.HandleFunc("POST /room-create", handlers.HandleCreateRoom) + mux.HandleFunc("GET /start-game", handlers.HandleStartGame) //elements mux.HandleFunc("GET /room/createform", handlers.HandleShowCreateForm) mux.HandleFunc("GET /room/hideform", handlers.HandleHideCreateForm) diff --git a/models/main.go b/models/main.go index 4cae2e3..b3d8941 100644 --- a/models/main.go +++ b/models/main.go @@ -60,6 +60,37 @@ type Room struct { IsOver bool } +func (r *Room) LoadTestCards() { + cards := []WordCard{ + {Word: "hamster", Color: "blue"}, + {Word: "child", Color: "red"}, + {Word: "wheel", Color: "white"}, + {Word: "condition", Color: "black"}, + {Word: "test", Color: "white"}, + {Word: "ball", Color: "blue"}, + {Word: "violin", Color: "red"}, + {Word: "rat", Color: "white"}, + {Word: "perplexity", Color: "blue"}, + {Word: "notion", Color: "red"}, + {Word: "guitar", Color: "blue"}, + {Word: "ocean", Color: "blue"}, + {Word: "moon", Color: "blue"}, + {Word: "coffee", Color: "blue"}, + {Word: "mountain", Color: "blue"}, + {Word: "book", Color: "blue"}, + {Word: "camera", Color: "blue"}, + {Word: "apple", Color: "red"}, + {Word: "fire", Color: "red"}, + {Word: "rose", Color: "red"}, + {Word: "sun", Color: "red"}, + {Word: "cherry", Color: "red"}, + {Word: "heart", Color: "red"}, + {Word: "tomato", Color: "red"}, + {Word: "cloud", Color: "white"}, + } + r.Cards = cards +} + func (r *Room) ChangeTurn() { switch r.TeamTurn { case "blue":