dkforest

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

commit 0c92b580f782451293d2ab42a162a0acffde0b48
parent 6c3d7b310858a849a79c0f294ebefdcf9f7bf7fa
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon,  4 Dec 2023 21:37:18 -0500

bet call fold actions

Diffstat:
Mpkg/web/handlers/poker.go | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mpkg/web/middlewares/middlewares.go | 6++++++
Mpkg/web/web.go | 6++++++
3 files changed, 76 insertions(+), 4 deletions(-)

diff --git a/pkg/web/handlers/poker.go b/pkg/web/handlers/poker.go @@ -299,6 +299,62 @@ func PokerCheckHandler(c echo.Context) error { return c.HTML(http.StatusOK, `<form method="post"><button>Check</button></form>`) } +func PokerBetHandler(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) + } + bet := 100 + if c.Request().Method == http.MethodPost { + bet, _ = strconv.Atoi(c.Request().PostFormValue("bet")) + select { + case g.PlayersEventCh <- PlayerEvent{Player: authUser.Username.String(), Bet: bet}: + default: + } + } + return c.HTML(http.StatusOK, `<form method="post"><input type="number" name="bet" value="`+strconv.Itoa(bet)+`" /><button>Bet</button></form>`) +} + +func PokerCallHandler(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 { + select { + case g.PlayersEventCh <- PlayerEvent{Player: authUser.Username.String(), Call: true}: + default: + } + } + return c.HTML(http.StatusOK, `<form method="post"><button>Call</button></form>`) +} + +func PokerFoldHandler(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 { + select { + case g.PlayersEventCh <- PlayerEvent{Player: authUser.Username.String(), Fold: true}: + default: + } + } + return c.HTML(http.StatusOK, `<form method="post"><button>Fold</button></form>`) +} + func PokerDealHandler(c echo.Context) error { roomID := c.Param("roomID") PokerInstance.Lock() @@ -497,7 +553,10 @@ body { .takeSeat10 { position: absolute; top: 300px; left: 250px; } #dealBtn { position: absolute; top: 400px; left: 50px; width: 50px; height: 30px; } #unSitBtn { position: absolute; top: 430px; left: 50px; width: 50px; height: 30px; } -#checkBtn { width: 100px; height: 30px; } +#checkBtn { width: 80px; height: 30px; } +#foldBtn { width: 80px; height: 30px; } +#callBtn { width: 80px; height: 30px; } +#betBtn { width: 250px; height: 30px; } </style>`) drawSeats := func() { @@ -550,10 +609,11 @@ body { send(buildSeatsHtml(g)) turnAction := ` <div style="position: absolute; top: 400px; left: 200px;"> - <form><input type="number" /><button>Bet</button></form> - <form><button>call</button></form> + <iframe src="/poker/` + roomID + `/bet" id="betBtn"></iframe> + <iframe src="/poker/` + roomID + `/call" id="callBtn"></iframe> <iframe src="/poker/` + roomID + `/check" id="checkBtn"></iframe> - <form><button>fold</button></form> + <iframe src="/poker/` + roomID + `/fold" id="foldBtn"></iframe> + </div>` send(turnAction) actions := `<iframe src="/poker/` + roomID + `/deal" id="dealBtn"></iframe>` diff --git a/pkg/web/middlewares/middlewares.go b/pkg/web/middlewares/middlewares.go @@ -184,6 +184,9 @@ func CSRFMiddleware() echo.MiddlewareFunc { c.Path() == "/poker/:roomID/sit/:pos" || c.Path() == "/poker/:roomID/unsit" || c.Path() == "/poker/:roomID/check" || + c.Path() == "/poker/:roomID/fold" || + c.Path() == "/poker/:roomID/call" || + c.Path() == "/poker/:roomID/bet" || c.Path() == "/poker/:roomID/deal" }, } @@ -297,6 +300,9 @@ func IsAuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc { !strings.Contains(c.Path(), "/poker/:roomID/sit/:pos") && !strings.Contains(c.Path(), "/poker/:roomID/unsit") && !strings.Contains(c.Path(), "/poker/:roomID/check") && + !strings.Contains(c.Path(), "/poker/:roomID/fold") && + !strings.Contains(c.Path(), "/poker/:roomID/call") && + !strings.Contains(c.Path(), "/poker/:roomID/bet") && !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,12 @@ func getMainServer(db *database.DkfDB, i18nBundle *i18n.Bundle, renderer *tmp.Te authGroup.GET("/poker/:roomID", handlers.PokerHandler) authGroup.GET("/poker/:roomID/check", handlers.PokerCheckHandler) authGroup.POST("/poker/:roomID/check", handlers.PokerCheckHandler) + authGroup.GET("/poker/:roomID/fold", handlers.PokerFoldHandler) + authGroup.POST("/poker/:roomID/fold", handlers.PokerFoldHandler) + authGroup.GET("/poker/:roomID/call", handlers.PokerCallHandler) + authGroup.POST("/poker/:roomID/call", handlers.PokerCallHandler) + authGroup.GET("/poker/:roomID/bet", handlers.PokerBetHandler) + authGroup.POST("/poker/:roomID/bet", handlers.PokerBetHandler) authGroup.GET("/poker/:roomID/deal", handlers.PokerDealHandler) authGroup.POST("/poker/:roomID/deal", handlers.PokerDealHandler) authGroup.GET("/poker/:roomID/unsit", handlers.PokerUnSitHandler)