Feat: roomlist update
This commit is contained in:
		| @@ -1,5 +1,5 @@ | ||||
| {{define "roomlist"}} | ||||
| <div id="roomlist"> | ||||
| <div id="roomlist" hx-get="/" hx-trigger="sse:roomlistupdate" hx-target="#ancestor"> | ||||
| {{range .}} | ||||
| <p> | ||||
|     {{.ID}} | ||||
| @@ -15,9 +15,6 @@ | ||||
|                 <span class="px-2 py-1 text-xs font-medium rounded-full {{if .IsGameRunning}}bg-green-100 text-green-800{{else}}bg-gray-100 text-gray-600{{end}}"> | ||||
|                     {{if .IsRunning}}Game Active{{else}}Waiting Room{{end}} | ||||
|                 </span> | ||||
|                 <!-- <span class="text-sm text-gray-600"> --> | ||||
|                 <!--     {{.PlayersCount}} player{{if ne .PlayersCount 1}}s{{end}} --> | ||||
|                 <!-- </span> --> | ||||
|             </div> | ||||
|         </div> | ||||
|     </div> | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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, | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -8,6 +8,7 @@ var ( | ||||
| 	CacheRoomPrefix  = "room#" | ||||
| 	CacheStatePrefix = "state-" | ||||
| 	// sse | ||||
| 	NotifyRoomListUpdate   = "roomlistupdate" | ||||
| 	NotifyRoomUpdatePrefix = "roomupdate_" | ||||
| 	NotifyBacklogPrefix    = "backlog_" | ||||
| ) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder