diff --git a/components/room.html b/components/room.html
index e9058fd..2613c6b 100644
--- a/components/room.html
+++ b/components/room.html
@@ -52,5 +52,8 @@
{{template "actionhistory" .Room.ActionHistory}}
+
+
+
{{end}}
diff --git a/handlers/actions.go b/handlers/actions.go
index e83b771..7156156 100644
--- a/handlers/actions.go
+++ b/handlers/actions.go
@@ -79,8 +79,7 @@ func saveFullInfo(fi *models.FullInfo) error {
if err := saveState(fi.State.Username, fi.State); err != nil {
return err
}
- // can room be nil?
- // if fi.Room == nil {
+ // if fi.Room == nil { // can be null on exit
// return nil
// }
if err := saveRoom(fi.Room); err != nil {
diff --git a/handlers/handlers.go b/handlers/handlers.go
index 6bcc45b..0288df3 100644
--- a/handlers/handlers.go
+++ b/handlers/handlers.go
@@ -81,3 +81,29 @@ func HandleHome(w http.ResponseWriter, r *http.Request) {
log.Error("failed to exec templ;", "error", err, "templ", "base")
}
}
+
+func HandleExit(w http.ResponseWriter, r *http.Request) {
+ tmpl, err := template.ParseGlob("components/*.html")
+ if err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ fi, err := getFullInfoByCtx(r.Context())
+ if err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ exitedRoom := fi.ExitRoom()
+ if err := saveRoom(exitedRoom); err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ if err := saveState(fi.State.Username, fi.State); err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ fi.List = listPublicRooms()
+ if err := tmpl.ExecuteTemplate(w, "base", fi); err != nil {
+ log.Error("failed to exec templ;", "error", err, "templ", "base")
+ }
+}
diff --git a/main.go b/main.go
index 604dd09..c545629 100644
--- a/main.go
+++ b/main.go
@@ -30,6 +30,7 @@ func ListenToRequests(port string) error {
mux.HandleFunc("GET /start-game", handlers.HandleStartGame)
mux.HandleFunc("GET /room-join", handlers.HandleJoinRoom)
mux.HandleFunc("POST /give-clue", handlers.HandleGiveClue)
+ mux.HandleFunc("GET /room/exit", handlers.HandleExit)
//elements
mux.HandleFunc("GET /actionhistory", handlers.HandleActionHistory)
mux.HandleFunc("GET /room/createform", handlers.HandleShowCreateForm)
diff --git a/models/main.go b/models/main.go
index f62db2b..a837093 100644
--- a/models/main.go
+++ b/models/main.go
@@ -1,6 +1,7 @@
package models
import (
+ "golias/utils"
"time"
"github.com/rs/xid"
@@ -200,3 +201,19 @@ type FullInfo struct {
Room *Room
List []*Room
}
+
+func (f *FullInfo) ExitRoom() *Room {
+ f.Room.PlayerList = utils.RemoveFromSlice(f.State.Username, f.Room.PlayerList)
+ f.Room.RedTeam.Guessers = utils.RemoveFromSlice(f.State.Username, f.Room.RedTeam.Guessers)
+ f.Room.BlueTeam.Guessers = utils.RemoveFromSlice(f.State.Username, f.Room.BlueTeam.Guessers)
+ if f.Room.RedTeam.Mime == f.State.Username {
+ f.Room.RedTeam.Mime = ""
+ }
+ if f.Room.BlueTeam.Mime == f.State.Username {
+ f.Room.BlueTeam.Mime = ""
+ }
+ f.State.ExitRoom()
+ resp := f.Room
+ f.Room = nil
+ return resp
+}
diff --git a/models/state.go b/models/state.go
index c21376d..9d09b5d 100644
--- a/models/state.go
+++ b/models/state.go
@@ -47,6 +47,12 @@ type UserState struct {
Role UserRole
}
+func (u *UserState) ExitRoom() {
+ u.RoomID = ""
+ u.Team = UserTeamNone
+ u.Role = UserRoleNone
+}
+
func MakeTestState(creatorName string) *FullInfo {
cards := []WordCard{
{Word: "hamster", Color: "blue"},
diff --git a/todos.md b/todos.md
index 31bca99..88dba96 100644
--- a/todos.md
+++ b/todos.md
@@ -1,7 +1,6 @@
### feats
-- end game: who won.
-- end game: two reasons (black word; all words);
- ability to leave room;
+- close room if creator left (or pass role to other player);
#### sse points
- clue sse update;