Feat: start game
This commit is contained in:
		| @@ -4,7 +4,7 @@ | |||||||
|   <p>Room created by {{.Room.CreatorName}};</p> |   <p>Room created by {{.Room.CreatorName}};</p> | ||||||
|   <p>Game is running: {{.Room.IsRunning}}</p> |   <p>Game is running: {{.Room.IsRunning}}</p> | ||||||
|   <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> |       <button hx-get="/start-game" hx-target="#room" class="bg-amber-100 text-black px-4 py-2 rounded">Start Game</button> | ||||||
|     {{end}} |     {{end}} | ||||||
|   </p> |   </p> | ||||||
|   | |||||||
| @@ -161,3 +161,27 @@ func HandleEndTurn(w http.ResponseWriter, r *http.Request) { | |||||||
| 	} | 	} | ||||||
| 	tmpl.ExecuteTemplate(w, "base", fi) | 	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) | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								main.go
									
									
									
									
									
								
							| @@ -26,6 +26,7 @@ func ListenToRequests(port string) error { | |||||||
| 	mux.HandleFunc("POST /join-team", handlers.HandleJoinTeam) | 	mux.HandleFunc("POST /join-team", handlers.HandleJoinTeam) | ||||||
| 	mux.HandleFunc("GET /end-turn", handlers.HandleEndTurn) | 	mux.HandleFunc("GET /end-turn", handlers.HandleEndTurn) | ||||||
| 	mux.HandleFunc("POST /room-create", handlers.HandleCreateRoom) | 	mux.HandleFunc("POST /room-create", handlers.HandleCreateRoom) | ||||||
|  | 	mux.HandleFunc("GET /start-game", handlers.HandleStartGame) | ||||||
| 	//elements | 	//elements | ||||||
| 	mux.HandleFunc("GET /room/createform", handlers.HandleShowCreateForm) | 	mux.HandleFunc("GET /room/createform", handlers.HandleShowCreateForm) | ||||||
| 	mux.HandleFunc("GET /room/hideform", handlers.HandleHideCreateForm) | 	mux.HandleFunc("GET /room/hideform", handlers.HandleHideCreateForm) | ||||||
|   | |||||||
| @@ -60,6 +60,37 @@ type Room struct { | |||||||
| 	IsOver bool | 	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() { | func (r *Room) ChangeTurn() { | ||||||
| 	switch r.TeamTurn { | 	switch r.TeamTurn { | ||||||
| 	case "blue": | 	case "blue": | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder