From da8131a0a4826807349e4a97d6dbc921e1ee413b Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Mon, 12 May 2025 18:25:52 +0300 Subject: [PATCH] Feat: roomlist update --- components/roomlist.html | 5 +---- handlers/actions.go | 5 +++++ handlers/game.go | 11 +---------- handlers/handlers.go | 10 ++++++++++ models/keys.go | 1 + todos.md | 1 + 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/components/roomlist.html b/components/roomlist.html index 8cc4ca4..e6fa8c1 100644 --- a/components/roomlist.html +++ b/components/roomlist.html @@ -1,5 +1,5 @@ {{define "roomlist"}} -
+
{{range .}}

{{.ID}} @@ -15,9 +15,6 @@ {{if .IsRunning}}Game Active{{else}}Waiting Room{{end}} - - -

diff --git a/handlers/actions.go b/handlers/actions.go index 7156156..76c2e0f 100644 --- a/handlers/actions.go +++ b/handlers/actions.go @@ -46,6 +46,11 @@ func getRoomByID(roomID string) (*models.Room, error) { return resp, nil } +func removeRoom(roomID string) { + key := models.CacheRoomPrefix + roomID + memcache.RemoveKey(key) +} + // context func getRoomIDFromCtx(ctx context.Context) string { id, _ := ctx.Value(models.CtxRoomIDKey).(string) diff --git a/handlers/game.go b/handlers/game.go index a4a3908..7740f84 100644 --- a/handlers/game.go +++ b/handlers/game.go @@ -8,16 +8,6 @@ import ( "net/http" ) -// func HandleRoomList(w http.ResponseWriter, r *http.Request) { -// pubRooms := listPublicRooms() -// tmpl, err := template.ParseGlob("components/*.html") -// if err != nil { -// abortWithError(w, err.Error()) -// return -// } -// tmpl.ExecuteTemplate(w, "base", pubRooms) -// } - func HandleCreateRoom(w http.ResponseWriter, r *http.Request) { // parse payload payload := &models.RoomReq{ @@ -49,6 +39,7 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) { abortWithError(w, msg) return } + notify(models.NotifyRoomListUpdate, "") // send msg of created room // h.Broker.Notifier <- broker.NotificationEvent{ // EventName: models.MsgRoomListUpdate, diff --git a/handlers/handlers.go b/handlers/handlers.go index 0288df3..0197b78 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -3,6 +3,7 @@ package handlers import ( "golias/broker" "golias/config" + "golias/models" "golias/pkg/cache" "html/template" "log/slog" @@ -93,11 +94,20 @@ func HandleExit(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } + var creatorLeft bool + if fi.Room.CreatorName == fi.State.Username { + creatorLeft = true + } exitedRoom := fi.ExitRoom() if err := saveRoom(exitedRoom); err != nil { abortWithError(w, err.Error()) return } + if len(exitedRoom.PlayerList) == 0 || creatorLeft { + removeRoom(exitedRoom.ID) + // TODO: notify users if creator left + notify(models.NotifyRoomListUpdate, "") + } if err := saveState(fi.State.Username, fi.State); err != nil { abortWithError(w, err.Error()) return diff --git a/models/keys.go b/models/keys.go index e8dd4b9..46308b9 100644 --- a/models/keys.go +++ b/models/keys.go @@ -8,6 +8,7 @@ var ( CacheRoomPrefix = "room#" CacheStatePrefix = "state-" // sse + NotifyRoomListUpdate = "roomlistupdate" NotifyRoomUpdatePrefix = "roomupdate_" NotifyBacklogPrefix = "backlog_" ) diff --git a/todos.md b/todos.md index 88dba96..9861381 100644 --- a/todos.md +++ b/todos.md @@ -1,6 +1,7 @@ ### feats - ability to leave room; - close room if creator left (or pass role to other player); +- notify on creation of new room; #### sse points - clue sse update;