dkforest

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

commit 0e40b1ef9e2bc3781a8ea43b8ae8ef8ea25b7fa6
parent 37f3ed308a251bdf111acfa500688487a62aa86c
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu, 21 Dec 2023 21:14:41 -0500

Add poker slash commands

Diffstat:
Mpkg/web/handlers/interceptors/msgInterceptor.go | 1+
Mpkg/web/handlers/interceptors/slashInterceptor.go | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpkg/web/handlers/poker.go | 2+-
Mpkg/web/handlers/poker/poker.go | 6+++---
4 files changed, 85 insertions(+), 4 deletions(-)

diff --git a/pkg/web/handlers/interceptors/msgInterceptor.go b/pkg/web/handlers/interceptors/msgInterceptor.go @@ -81,6 +81,7 @@ var memesRgx = regexp.MustCompile(`^/memes$`) var locateRgx = regexp.MustCompile(`^/locate ` + optAtGUser) var chipsRgx = regexp.MustCompile(`^/chips ` + optAtGUser + ` (\d+)`) var chipsSendRgx = regexp.MustCompile(`^/chips-send ` + optAtGUser + ` (\d+)`) +var betRgx = regexp.MustCompile(`^/bet (\d+)$`) type MsgInterceptor struct{} diff --git a/pkg/web/handlers/interceptors/slashInterceptor.go b/pkg/web/handlers/interceptors/slashInterceptor.go @@ -8,6 +8,7 @@ import ( "dkforest/pkg/managers" "dkforest/pkg/utils" "dkforest/pkg/web/handlers/interceptors/command" + "dkforest/pkg/web/handlers/poker" "dkforest/pkg/web/handlers/streamModals" "errors" "fmt" @@ -92,6 +93,12 @@ var userCmdsMap = map[string]CmdHandler{ "/chips-reset": handleChipsResetCmd, "/wizz": handleWizzCmd, "/itr": handleInThisRoomCmd, + "/check": handleCheckCmd, + "/call": handleCallCmd, + "/fold": handleFoldCmd, + "/allin": handleAllInCmd, + "/bet": handleBetCmd, + "/deal": handleDealCmd, //"/chips-send": handleChipsSendCmd, } @@ -1911,3 +1918,76 @@ func handleUpdateReadMarkerCmd(c *command.Command) (handled bool) { } return } + +func handleCheckCmd(c *command.Command) (handled bool) { + if c.Message == "/check" { + roomID := poker.RoomID(strings.ReplaceAll(c.Room.Name, "_", "-")) + if g := poker.PokerInstance.GetGame(roomID); g != nil { + g.Check(c.AuthUser.ID) + } + c.Err = command.ErrRedirect + return true + } + return false +} + +func handleCallCmd(c *command.Command) (handled bool) { + if c.Message == "/call" { + roomID := poker.RoomID(strings.ReplaceAll(c.Room.Name, "_", "-")) + if g := poker.PokerInstance.GetGame(roomID); g != nil { + g.Call(c.AuthUser.ID) + } + c.Err = command.ErrRedirect + return true + } + return false +} + +func handleFoldCmd(c *command.Command) (handled bool) { + if c.Message == "/fold" { + roomID := poker.RoomID(strings.ReplaceAll(c.Room.Name, "_", "-")) + if g := poker.PokerInstance.GetGame(roomID); g != nil { + g.Fold(c.AuthUser.ID) + } + c.Err = command.ErrRedirect + return true + } + return false +} + +func handleBetCmd(c *command.Command) (handled bool) { + if m := betRgx.FindStringSubmatch(c.Message); len(m) == 2 { + roomID := poker.RoomID(strings.ReplaceAll(c.Room.Name, "_", "-")) + if g := poker.PokerInstance.GetGame(roomID); g != nil { + bet := database.PokerChip(utils.DoParseUint64(m[1])) + g.Bet(c.AuthUser.ID, bet) + } + c.Err = command.ErrRedirect + return true + } + return false +} + +func handleAllInCmd(c *command.Command) (handled bool) { + if c.Message == "/allin" { + roomID := poker.RoomID(strings.ReplaceAll(c.Room.Name, "_", "-")) + if g := poker.PokerInstance.GetGame(roomID); g != nil { + g.AllIn(c.AuthUser.ID) + } + c.Err = command.ErrRedirect + return true + } + return false +} + +func handleDealCmd(c *command.Command) (handled bool) { + if c.Message == "/deal" { + roomID := poker.RoomID(strings.ReplaceAll(c.Room.Name, "_", "-")) + if g := poker.PokerInstance.GetGame(roomID); g != nil { + g.Deal(c.AuthUser.ID) + } + c.Err = command.ErrRedirect + return true + } + return false +} diff --git a/pkg/web/handlers/poker.go b/pkg/web/handlers/poker.go @@ -536,7 +536,7 @@ func PokerDealHandler(c echo.Context) error { return c.NoContent(http.StatusNotFound) } if c.Request().Method == http.MethodPost { - g.Deal(roomID, authUser.ID) + g.Deal(authUser.ID) } html := hutils.HtmlCssReset html += `<form method="post"><button>Deal</button></form>` diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -1491,9 +1491,9 @@ func cardToPokerCard(name string) string { return r.Replace(name) } -func (g *PokerGame) Deal(roomID RoomID, userID database.UserID) { - roomTopic := roomID.Topic() - roomUserTopic := roomID.UserTopic(userID) +func (g *PokerGame) Deal(userID database.UserID) { + roomTopic := g.roomID.Topic() + roomUserTopic := g.roomID.UserTopic(userID) eligiblePlayers := g.getEligibles() if len(eligiblePlayers) < 2 { PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: "need at least 2 players"})