Feat: start game

This commit is contained in:
Grail Finder
2025-05-09 11:54:01 +03:00
parent 2b4bf2ec29
commit 45761446e5
4 changed files with 57 additions and 1 deletions

View File

@ -4,7 +4,7 @@
<p>Room created by {{.Room.CreatorName}};</p>
<p>Game is running: {{.Room.IsRunning}}</p>
<p>
{{if eq .State.Username .Room.CreatorName}}
{{if and (eq .State.Username .Room.CreatorName) (not .Room.IsRunning)}}
<button hx-get="/start-game" hx-target="#room" class="bg-amber-100 text-black px-4 py-2 rounded">Start Game</button>
{{end}}
</p>

View File

@ -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)
}

View File

@ -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)

View File

@ -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":