dkforest

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

commit 789cd1562e4f9b02ef822004a3c7ed5ace83d414
parent b55b2abbf3dadc9279fdf6fb2255a12f14d4eee1
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue,  5 Dec 2023 20:35:06 -0500

cleanup

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

diff --git a/pkg/web/handlers/poker.go b/pkg/web/handlers/poker.go @@ -26,25 +26,35 @@ const DealerStackY = 30 type Poker struct { sync.Mutex - Games map[string]*PokerGame + games map[string]*PokerGame } func NewPoker() *Poker { p := &Poker{} - p.Games = make(map[string]*PokerGame) + p.games = make(map[string]*PokerGame) return p } func (p *Poker) GetOrCreateGame(roomID string) *PokerGame { p.Lock() defer p.Unlock() - g, found := p.Games[roomID] + g, found := p.games[roomID] if !found { g = &PokerGame{ PlayersEventCh: make(chan PlayerEvent), Players: make([]PokerStandingPlayer, NbPlayers), } - p.Games[roomID] = g + p.games[roomID] = g + } + return g +} + +func (p *Poker) GetGame(roomID string) *PokerGame { + p.Lock() + defer p.Unlock() + g, found := PokerInstance.games[roomID] + if !found { + return nil } return g } @@ -563,10 +573,8 @@ var PokerPubSub = pubsub.NewPubSub[any]() func PokerCheckHandler(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 { + g := PokerInstance.GetGame(roomID) + if g == nil { return c.NoContent(http.StatusNotFound) } if c.Request().Method == http.MethodPost { @@ -581,10 +589,8 @@ func PokerCheckHandler(c echo.Context) error { 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 { + g := PokerInstance.GetGame(roomID) + if g == nil { return c.NoContent(http.StatusNotFound) } bet := 100 @@ -601,10 +607,8 @@ func PokerBetHandler(c echo.Context) error { 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 { + g := PokerInstance.GetGame(roomID) + if g == nil { return c.NoContent(http.StatusNotFound) } if c.Request().Method == http.MethodPost { @@ -619,10 +623,8 @@ func PokerCallHandler(c echo.Context) error { 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 { + g := PokerInstance.GetGame(roomID) + if g == nil { return c.NoContent(http.StatusNotFound) } if c.Request().Method == http.MethodPost { @@ -636,10 +638,8 @@ func PokerFoldHandler(c echo.Context) error { func PokerDealHandler(c echo.Context) error { roomID := c.Param("roomID") - PokerInstance.Lock() - g, found := PokerInstance.Games[roomID] - PokerInstance.Unlock() - if !found { + g := PokerInstance.GetGame(roomID) + if g == nil { return c.NoContent(http.StatusNotFound) } if c.Request().Method == http.MethodPost { @@ -651,10 +651,8 @@ func PokerDealHandler(c echo.Context) error { 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 { + g := PokerInstance.GetGame(roomID) + if g == nil { return c.NoContent(http.StatusNotFound) } if c.Request().Method == http.MethodPost { @@ -686,10 +684,8 @@ func PokerSitHandler(c echo.Context) error { pos-- roomID := c.Param("roomID") roomTopic := "room_" + roomID - PokerInstance.Lock() - g, found := PokerInstance.Games[roomID] - PokerInstance.Unlock() - if !found { + g := PokerInstance.GetGame(roomID) + if g == nil { return c.HTML(http.StatusOK, html) } if c.Request().Method == http.MethodPost {