dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 34ecb0708e35e0b1db4e318f1a8f3e2b87abe2fb
parent 8a35fc72570cca114bfa733b77e4e5ac3ce81eba
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon,  4 Dec 2023 17:17:11 -0500

still trash

Diffstat:
Mpkg/web/handlers/handlers.go | 57+++++++++++++++++++++++++++++++++++++++++----------------
Mpkg/web/middlewares/middlewares.go | 2++
Mpkg/web/web.go | 2++
3 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -988,7 +988,7 @@ type PokerSeatTakenEvent struct { } type PokerSeatLeftEvent struct { - Player string + Idx int } var PokerPubSub = pubsub.NewPubSub[any]() @@ -1008,6 +1008,34 @@ func PokerDealHandler(c echo.Context) error { return c.HTML(http.StatusOK, `<form method="post"><button>Deal</button></form>`) } +func PokerUnSitHandler(c echo.Context) error { + authUser := c.Get("authUser").(*database.User) + roomID := c.Param("roomID") + PokerInstance.Lock() + g, found := PokerInstance.Games[roomID] + PokerInstance.Unlock() + if !found { + return c.NoContent(http.StatusNotFound) + } + if c.Request().Method == http.MethodPost { + var idx int + found := false + for i, p := range g.Players { + if p == authUser.Username.String() { + g.Players[i] = "" + idx = i + found = true + break + } + } + if found { + myTopic := "room_" + roomID + PokerPubSub.Pub(myTopic, PokerSeatLeftEvent{Idx: idx + 1}) + } + } + return c.HTML(http.StatusOK, `<form method="post"><button>UnSit</button></form>`) +} + func PokerSitHandler(c echo.Context) error { authUser := c.Get("authUser").(*database.User) pos, _ := strconv.Atoi(c.Param("pos")) @@ -1036,8 +1064,9 @@ func buildTakeSeatHtml(authUser *database.User, g *PokerGame, roomID string) str takeSeatBtns := "" if seated, _ := g.IsSeated(authUser.Username.String()); !seated { // if player not already seated for i, p := range g.Players { - if p == "" { - takeSeatBtns += `<iframe src="/poker/` + roomID + `/sit/` + strconv.Itoa(i+1) + `" class="takeSeat takeSeat` + strconv.Itoa(i+1) + `"></iframe>` + takeSeatBtns += `<iframe src="/poker/` + roomID + `/sit/` + strconv.Itoa(i+1) + `" class="takeSeat takeSeat` + strconv.Itoa(i+1) + `"></iframe>` + if p != "" { + takeSeatBtns += `<style>.takeSeat` + strconv.Itoa(i+1) + ` { display: none; }</style>` } } } @@ -1049,7 +1078,8 @@ func buildSeatsHtml(g *PokerGame) string { <div>` for i, p := range g.Players { if p != "" { - seats += `<div id="seat` + strconv.Itoa(i+1) + `">` + p + `</div>` + seats += `<div id="seat` + strconv.Itoa(i+1) + `"></div>` + seats += `<style>#seat` + strconv.Itoa(i+1) + `:before { content: "` + p + `"; }</style>` } else { seats += `<div id="seat` + strconv.Itoa(i+1) + `"></div>` } @@ -1167,6 +1197,7 @@ body { .takeSeat9 { position: absolute; top: 300px; left: 610px; display: none; } .takeSeat10 { position: absolute; top: 300px; left: 610px; display: none; } #dealBtn { position: absolute; top: 400px; left: 50px; } +#unSitBtn { position: absolute; top: 430px; left: 50px; } </style>`) cardsHtml := "" for i := 52; i >= 1; i-- { @@ -1184,6 +1215,7 @@ body { </div>` send(turnAction) actions := `<iframe src="/poker/` + roomID + `/deal" id="dealBtn"></iframe>` + actions += `<iframe src="/poker/` + roomID + `/unsit" id="unSitBtn"></iframe>` send(actions) deckHash := deckSha256 send(`<div>` + deckStr + `</div>`) @@ -1206,17 +1238,9 @@ Loop: } if evt, ok := payload.(PokerSeatLeftEvent); ok { - var idx int - for i, p := range g.Players { - if p == evt.Player { - g.Players[i] = "" - idx = i - break - } - } - - if seated, _ := g.IsSeated(authUser.Username.String()); !seated { - send(`<style>.takeSeat` + strconv.Itoa(idx+1) + ` { display: block; }</style>`) + seated, _ := g.IsSeated(authUser.Username.String()) + if !seated { + send(`<style>.takeSeat` + strconv.Itoa(evt.Idx) + ` { display: block; }</style>`) } for i, p := range g.Players { if p != "" { @@ -1226,6 +1250,8 @@ Loop: send(`<style>#seat` + strconv.Itoa(i+1) + `:before { content: ""; }</style>`) } } + c.Response().Flush() + continue } else if _, ok := payload.(PokerSeatTakenEvent); ok { @@ -1269,6 +1295,5 @@ Loop: c.Response().Flush() } } - PokerPubSub.Pub(myTopic, PokerSeatLeftEvent{Player: authUser.Username.String()}) return nil } diff --git a/pkg/web/middlewares/middlewares.go b/pkg/web/middlewares/middlewares.go @@ -182,6 +182,7 @@ func CSRFMiddleware() echo.MiddlewareFunc { c.Path() == "/api/v1/werewolf" || c.Path() == "/chess/:key" || c.Path() == "/poker/:roomID/sit/:pos" || + c.Path() == "/poker/:roomID/unsit" || c.Path() == "/poker/:roomID/deal" }, } @@ -293,6 +294,7 @@ func IsAuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc { !strings.Contains(c.Path(), "/api/v1/chat/top-bar") && !strings.Contains(c.Path(), "/api/v1/chat/controls") && !strings.Contains(c.Path(), "/poker/:roomID/sit/:pos") && + !strings.Contains(c.Path(), "/poker/:roomID/unsit") && !strings.Contains(c.Path(), "/poker/:roomID/deal") { c.Response().Header().Set("X-Frame-Options", "DENY") } diff --git a/pkg/web/web.go b/pkg/web/web.go @@ -100,6 +100,8 @@ func getMainServer(db *database.DkfDB, i18nBundle *i18n.Bundle, renderer *tmp.Te authGroup.GET("/poker/:roomID", handlers.PokerHandler) authGroup.GET("/poker/:roomID/deal", handlers.PokerDealHandler) authGroup.POST("/poker/:roomID/deal", handlers.PokerDealHandler) + authGroup.GET("/poker/:roomID/unsit", handlers.PokerUnSitHandler) + authGroup.POST("/poker/:roomID/unsit", handlers.PokerUnSitHandler) authGroup.GET("/poker/:roomID/sit/:pos", handlers.PokerSitHandler) authGroup.POST("/poker/:roomID/sit/:pos", handlers.PokerSitHandler) authGroup.GET("/chess", handlers.ChessHandler)