dkforest

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

commit 6c5b00a84e11d1614daf01967978b91f1ac0c2e2
parent 91f3e11d17651376307d962bde441809d95afd48
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon,  4 Dec 2023 06:42:47 -0500

stuff

Diffstat:
Mpkg/web/handlers/handlers.go | 34+++++++++++++++++++++++++++++-----
Mpkg/web/middlewares/middlewares.go | 6++++--
Mpkg/web/web.go | 2++
3 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -765,8 +765,21 @@ type PokerEvent struct { Angle string } +type PokerSeatTakenEvent struct { +} + var PokerPubSub = pubsub.NewPubSub[PokerEvent]() +func PokerSitHandler(c echo.Context) error { + authUser := c.Get("authUser").(*database.User) + if c.Request().Method == http.MethodPost { + myTopic := "room_" + authUser.ID.String() + "_seats" + PokerPubSub.Pub(myTopic, PokerEvent{}) + return c.HTML(http.StatusOK, ``) + } + return c.HTML(http.StatusOK, `<form method="post"><button>SIT</button></form>`) +} + func PokerHandler(c echo.Context) error { // players 2-10 // $5 -> 1000 chips @@ -792,6 +805,7 @@ func PokerHandler(c echo.Context) error { quit := hutils.CloseSignalChan(c) myTopic := "room_" + authUser.ID.String() + myTopic1 := "room_" + authUser.ID.String() + "_seats" go func() { type Seat struct { @@ -954,7 +968,7 @@ func PokerHandler(c echo.Context) error { }) }() - sub := PokerPubSub.Subscribe([]string{myTopic}) + sub := PokerPubSub.Subscribe([]string{myTopic, myTopic1}) defer sub.Close() c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8) @@ -1019,6 +1033,10 @@ body { display:block; border:1px solid black; } +.takeSeat { + width: 100px; + height: 50px; +} .takeSeat1 { position: absolute; top: 80px; left: 700px; } .takeSeat2 { position: absolute; top: 200px; left: 670px; } .takeSeat3 { position: absolute; top: 300px; left: 610px; } @@ -1032,13 +1050,13 @@ body { takeSeatBtns := "" if true { // if player not already seated if true { // if seat 1 available - takeSeatBtns += `<button class="takeSeat takeSeat1">Take seat 1</button>` + takeSeatBtns += `<iframe src="/poker/123/sit/1" class="takeSeat takeSeat1"></iframe>` } if true { // if seat 2 available - takeSeatBtns += `<button class="takeSeat takeSeat2">Take seat 2</button>` + takeSeatBtns += `<iframe src="/poker/123/sit/2" class="takeSeat takeSeat2"></iframe>` } if true { // if seat 3 available - takeSeatBtns += `<button class="takeSeat takeSeat3">Take seat 3</button>` + takeSeatBtns += `<iframe src="/poker/123/sit/3" class="takeSeat takeSeat3"></iframe>` } } send(takeSeatBtns) @@ -1047,6 +1065,7 @@ body { <form><input type="number" /><button>Bet</button></form> <form><button>call</button></form> <form><button>check</button></form> + <form><button>fold</button></form> </div>` send(turnAction) deckHash := deckSha256 @@ -1061,7 +1080,7 @@ Loop: default: } - _, payload, err := sub.ReceiveTimeout2(1*time.Second, quit) + topic, payload, err := sub.ReceiveTimeout2(1*time.Second, quit) if err != nil { if errors.Is(err, pubsub.ErrCancelled) { break Loop @@ -1069,6 +1088,11 @@ Loop: continue } + if topic == myTopic1 { + send(`<style>.takeSeat { display: none; }</style>`) + continue + } + color := "black" if strings.Contains(payload.Name, "♥") || strings.Contains(payload.Name, "♦") { diff --git a/pkg/web/middlewares/middlewares.go b/pkg/web/middlewares/middlewares.go @@ -180,7 +180,8 @@ func CSRFMiddleware() echo.MiddlewareFunc { return (apiKey != "" && strings.HasPrefix(c.Path(), "/api/v1/")) || c.Path() == "/api/v1/battleship" || c.Path() == "/api/v1/werewolf" || - c.Path() == "/chess/:key" + c.Path() == "/chess/:key" || + c.Path() == "/poker/:roomID/sit/:pos" }, } return CSRFWithConfig(csrfConfig) @@ -289,7 +290,8 @@ func IsAuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc { !strings.Contains(c.Path(), "/api/v1/chat/messages") && !strings.Contains(c.Path(), "/api/v1/chat/messages/:roomName/stream") && !strings.Contains(c.Path(), "/api/v1/chat/top-bar") && - !strings.Contains(c.Path(), "/api/v1/chat/controls") { + !strings.Contains(c.Path(), "/api/v1/chat/controls") && + !strings.Contains(c.Path(), "/poker/:roomID/sit/:pos") { c.Response().Header().Set("X-Frame-Options", "DENY") } diff --git a/pkg/web/web.go b/pkg/web/web.go @@ -98,6 +98,8 @@ func getMainServer(db *database.DkfDB, i18nBundle *i18n.Bundle, renderer *tmp.Te authGroup.GET("/donate", handlers.DonateHandler) authGroup.GET("/shop", handlers.ShopHandler) authGroup.GET("/poker", handlers.PokerHandler) + authGroup.GET("/poker/:roomID/sit/:pos", handlers.PokerSitHandler) + authGroup.POST("/poker/:roomID/sit/:pos", handlers.PokerSitHandler) authGroup.GET("/chess", handlers.ChessHandler) authGroup.POST("/chess", handlers.ChessHandler) authGroup.GET("/chess/analyze", handlers.ChessAnalyzeHandler)