dkforest

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

commit 887476925b40491a3647b75f86d4358d32fe9dc1
parent cabac784feb226da808d1495e241540d901bea96
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon, 18 Dec 2023 03:46:47 -0500

cleanup

Diffstat:
Mpkg/web/handlers/handlers.go | 82+++++++++++++++++++++++++++++++++++--------------------------------------------
1 file changed, 36 insertions(+), 46 deletions(-)

diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go @@ -844,55 +844,45 @@ func PokerHomeHandler(c echo.Context) error { if formName == "join_table" { pokerTableSlug := c.Request().PostFormValue("table_slug") playerBuyIn := database.PokerChip(utils.DoParseUint64(c.Request().PostFormValue("buy_in"))) - tx := db.Begin() - g := poker.PokerInstance.GetGame(poker.RoomID(pokerTableSlug)) - if g != nil { - g.Players.Lock() - defer g.Players.Unlock() - if g.IsSeated2(authUser.Username) { - tx.Rollback() - data.ErrorTable = "Cannot buy-in while seated" - return c.Render(http.StatusOK, "poker", data) + if err := db.WithE(func(tx *database.DkfDB) error { + g := poker.PokerInstance.GetGame(poker.RoomID(pokerTableSlug)) + if g != nil { + g.Players.Lock() + defer g.Players.Unlock() + if g.IsSeated2(authUser.Username) { + return errors.New("cannot buy-in while seated") + } } - } - pokerTable, err := tx.GetPokerTableBySlug(pokerTableSlug) - if err != nil { - tx.Rollback() - data.ErrorTable = "table mot found" - return c.Render(http.StatusOK, "poker", data) - } - if playerBuyIn < pokerTable.MinBuyIn { - tx.Rollback() - data.ErrorTable = "buy in too small" - return c.Render(http.StatusOK, "poker", data) - } - if playerBuyIn > pokerTable.MaxBuyIn { - tx.Rollback() - data.ErrorTable = "buy in too high" - return c.Render(http.StatusOK, "poker", data) - } - userChips := authUser.GetUserChips(pokerTable.IsTest) - if userChips < playerBuyIn { - tx.Rollback() - data.ErrorTable = "not enough chips to buy-in" - return c.Render(http.StatusOK, "poker", data) - } - tableAccount, err := tx.GetPokerTableAccount(authUser.ID, pokerTable.ID) - if err != nil { - tx.Rollback() - data.ErrorTable = "failed to get table account" - return c.Render(http.StatusOK, "poker", data) - } - if tableAccount.Amount+playerBuyIn > pokerTable.MaxBuyIn { - tx.Rollback() - data.ErrorTable = "Buy-in exceed table max buy-in" + pokerTable, err := tx.GetPokerTableBySlug(pokerTableSlug) + if err != nil { + return errors.New("table mot found") + } + if playerBuyIn < pokerTable.MinBuyIn { + return errors.New("buy in too small") + } + if playerBuyIn > pokerTable.MaxBuyIn { + return errors.New("buy in too high") + } + userChips := authUser.GetUserChips(pokerTable.IsTest) + if userChips < playerBuyIn { + return errors.New("not enough chips to buy-in") + } + tableAccount, err := tx.GetPokerTableAccount(authUser.ID, pokerTable.ID) + if err != nil { + return errors.New("failed to get table account") + } + if tableAccount.Amount+playerBuyIn > pokerTable.MaxBuyIn { + return errors.New("buy-in exceed table max buy-in") + } + tableAccount.Amount += playerBuyIn + authUser.IncrUserChips(pokerTable.IsTest, playerBuyIn, true) + authUser.DoSave(tx) + tableAccount.DoSave(tx) + return nil + }); err != nil { + data.ErrorTable = err.Error() return c.Render(http.StatusOK, "poker", data) } - tableAccount.Amount += playerBuyIn - authUser.IncrUserChips(pokerTable.IsTest, playerBuyIn, true) - authUser.DoSave(tx) - tableAccount.DoSave(tx) - tx.Commit() return c.Redirect(http.StatusFound, "/poker/"+pokerTableSlug) } else if formName == "cash_out" {