dkforest

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

commit b55b2abbf3dadc9279fdf6fb2255a12f14d4eee1
parent b8e3c437cb30a94be5fcba14e6c44dc2f0b1b6d1
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue,  5 Dec 2023 20:31:31 -0500

cleanup

Diffstat:
Mpkg/web/handlers/poker.go | 28++++++++++++++++++----------
1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/pkg/web/handlers/poker.go b/pkg/web/handlers/poker.go @@ -140,6 +140,15 @@ func isRoundSettled(players []*PokerPlayer) bool { return allSettled } +func (g *PokerGame) SitPlayer(authUser *database.User, pos int) error { + if g.Players[pos].Username != "" { + return errors.New("seat already taken") + } + g.Players[pos].Username = authUser.Username.String() + g.Players[pos].Cash = 1000 + return nil +} + func (g *PokerGame) Deal(roomID string) { roomTopic := "room_" + roomID @@ -668,30 +677,29 @@ func PokerUnSitHandler(c echo.Context) error { } func PokerSitHandler(c echo.Context) error { + html := `<form method="post"><button>SIT</button></form>` authUser := c.Get("authUser").(*database.User) pos, _ := strconv.Atoi(c.Param("pos")) if pos < 1 || pos > NbPlayers { - return c.HTML(http.StatusOK, `<form method="post"><button>SIT</button></form>`) + return c.HTML(http.StatusOK, html) } pos-- roomID := c.Param("roomID") + roomTopic := "room_" + roomID PokerInstance.Lock() g, found := PokerInstance.Games[roomID] PokerInstance.Unlock() if !found { - return c.HTML(http.StatusOK, `<form method="post"><button>SIT</button></form>`) + return c.HTML(http.StatusOK, html) } if c.Request().Method == http.MethodPost { - if g.Players[pos].Username != "" { - fmt.Println("seat already taken") - return c.HTML(http.StatusOK, `<form method="post"><button>SIT</button></form>`) + if err := g.SitPlayer(authUser, pos); err != nil { + fmt.Println(err) + return c.HTML(http.StatusOK, html) } - myTopic := "room_" + roomID - g.Players[pos].Username = authUser.Username.String() - g.Players[pos].Cash = 1000 - PokerPubSub.Pub(myTopic, PokerSeatTakenEvent{}) + PokerPubSub.Pub(roomTopic, PokerSeatTakenEvent{}) } - return c.HTML(http.StatusOK, `<form method="post"><button>SIT</button></form>`) + return c.HTML(http.StatusOK, html) } func isHeartOrDiamond(name string) bool {