dkforest

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

commit 2e09d2ea94731a81657bab03d4531c11388a5995
parent 56c56e86d77fffb437a43ce60bcd906a5027a56a
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Mon, 18 Dec 2023 22:36:14 -0500

cleanup

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

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -594,9 +594,9 @@ func foldPlayer(g *PokerGame, p *PokerPlayer) { g.Ongoing.AddEvent(evt1, evt2) } -func doTimeout(g *PokerGame, p *PokerPlayer, minBet database.PokerChip, playerAlive *int) int { +func doTimeout(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip, playerAlive *int) int { pUsername := p.Username - if p.GetBet() < minBet { + if p.GetBet() < *minBet { foldPlayer(g, p) g.newLogEvent(fmt.Sprintf("%s auto fold", pUsername)) @@ -631,9 +631,9 @@ func doFold(g *PokerGame, p *PokerPlayer, playerAlive *int) int { return doNothing } -func doCheck(g *PokerGame, p *PokerPlayer, minBet database.PokerChip) int { - if p.GetBet() < minBet { - msg := fmt.Sprintf("Need to bet %d", minBet-p.GetBet()) +func doCheck(g *PokerGame, p *PokerPlayer, minBet *database.PokerChip) int { + if p.GetBet() < *minBet { + msg := fmt.Sprintf("Need to bet %d", *minBet-p.GetBet()) PokerPubSub.Pub(g.RoomID.UserTopic(p.Username), ErrorMsgEvent{Message: msg}) return continueGetPlayerEventLoop } @@ -641,13 +641,13 @@ func doCheck(g *PokerGame, p *PokerPlayer, minBet database.PokerChip) int { return doNothing } -func doCall(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet database.PokerChip, newlyAllInPlayers *[]*PokerPlayer, pokerTableID int64) int { +func doCall(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet *database.PokerChip, newlyAllInPlayers *[]*PokerPlayer) int { pUsername := p.Username - bet := utils.MinInt(minBet-p.GetBet(), p.GetCash()) + bet := utils.MinInt(*minBet-p.GetBet(), p.GetCash()) if bet == 0 { g.newLogEvent(fmt.Sprintf("%s check", pUsername)) } else { - p.doBetAndNotif(db, pokerTableID, bet, g.RoomID.Topic()) + p.doBetAndNotif(db, g.PokerTableID, bet, g.RoomID.Topic()) logMsg := fmt.Sprintf("%s call (%d)", pUsername, bet) if p.isAllIn() { logMsg += " (all-in)" @@ -658,13 +658,13 @@ func doCall(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet database.Po return doNothing } -func doAllIn(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet database.PokerChip, newlyAllInPlayers *[]*PokerPlayer, pokerTableID int64, lastRaisePlayerIdx *int, playerToPlayIdx int) int { +func doAllIn(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet *database.PokerChip, newlyAllInPlayers *[]*PokerPlayer, lastRaisePlayerIdx *int, playerToPlayIdx int) int { bet := p.GetCash() - if (p.GetBet() + bet) > minBet { + if (p.GetBet() + bet) > *minBet { *lastRaisePlayerIdx = playerToPlayIdx } - minBet = utils.MaxInt(p.GetBet()+bet, minBet) - p.doBetAndNotif(db, pokerTableID, bet, g.RoomID.Topic()) + *minBet = utils.MaxInt(p.GetBet()+bet, *minBet) + p.doBetAndNotif(db, g.PokerTableID, bet, g.RoomID.Topic()) logMsg := fmt.Sprintf("%s all-in (%d)", p.Username, bet) if p.isAllIn() { *newlyAllInPlayers = append(*newlyAllInPlayers, p) @@ -673,19 +673,19 @@ func doAllIn(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet database.P return doNothing } -func doBet(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet database.PokerChip, newlyAllInPlayers *[]*PokerPlayer, pokerTableID int64, lastRaisePlayerIdx *int, playerToPlayIdx int, evt PlayerEvent) int { +func doBet(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet *database.PokerChip, newlyAllInPlayers *[]*PokerPlayer, lastRaisePlayerIdx *int, playerToPlayIdx int, evt PlayerEvent) int { roomTopic := g.RoomID.Topic() roomUserTopic := g.RoomID.UserTopic(p.Username) pokerTableMinBet := g.PokerTableMinBet bet := evt.Bet // Ensure the player cannot bet below the table minimum bet (amount of the big blind) - if p.GetBet()+bet != minBet && bet < pokerTableMinBet { + if p.GetBet()+bet != *minBet && bet < pokerTableMinBet { msg := fmt.Sprintf("Bet (%d) is too low. Must bet at least %d", bet, pokerTableMinBet) PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg}) return continueGetPlayerEventLoop } - if (p.GetBet() + bet) < minBet { - msg := fmt.Sprintf("Bet (%d) is too low. Must bet at least %d", bet, minBet-p.GetBet()) + if (p.GetBet() + bet) < *minBet { + msg := fmt.Sprintf("Bet (%d) is too low. Must bet at least %d", bet, *minBet-p.GetBet()) PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg}) return continueGetPlayerEventLoop } @@ -694,11 +694,11 @@ func doBet(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet database.Pok PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg}) return continueGetPlayerEventLoop } - if (p.GetBet() + bet) > minBet { + if (p.GetBet() + bet) > *minBet { *lastRaisePlayerIdx = playerToPlayIdx } - minBet = utils.MaxInt(p.GetBet()+bet, minBet) - p.doBetAndNotif(db, pokerTableID, bet, roomTopic) + *minBet = utils.MaxInt(p.GetBet()+bet, *minBet) + p.doBetAndNotif(db, g.PokerTableID, bet, roomTopic) logMsg := fmt.Sprintf("%s bet %d", p.Username, bet) if p.isAllIn() { logMsg += " (all-in)" @@ -727,8 +727,8 @@ func handleAutoActionReceived(g *PokerGame, autoCache map[database.Username]Auto return continueGetPlayerEventLoop } -func applyAutoAction(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet database.PokerChip, - newlyAllInPlayers *[]*PokerPlayer, pokerTableID int64, +func applyAutoAction(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet *database.PokerChip, + newlyAllInPlayers *[]*PokerPlayer, lastRaisePlayerIdx, playerAlive *int, playerToPlayIdx int, autoAction AutoAction, autoCache map[database.Username]AutoAction) (actionResult int) { @@ -744,11 +744,11 @@ func applyAutoAction(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet da case CheckAction: actionResult = doCheck(g, p, minBet) case CallAction: - actionResult = doCall(g, p, db, minBet, newlyAllInPlayers, pokerTableID) - case BetAction: - actionResult = doBet(g, p, db, minBet, newlyAllInPlayers, pokerTableID, lastRaisePlayerIdx, playerToPlayIdx, autoAction.evt) + actionResult = doCall(g, p, db, minBet, newlyAllInPlayers) case AllInAction: - actionResult = doAllIn(g, p, db, minBet, newlyAllInPlayers, pokerTableID, lastRaisePlayerIdx, playerToPlayIdx) + actionResult = doAllIn(g, p, db, minBet, newlyAllInPlayers, lastRaisePlayerIdx, playerToPlayIdx) + case BetAction: + actionResult = doBet(g, p, db, minBet, newlyAllInPlayers, lastRaisePlayerIdx, playerToPlayIdx, autoAction.evt) } } delete(autoCache, pUsername) @@ -756,8 +756,8 @@ func applyAutoAction(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet da return } -func handlePlayerActionEvent(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet database.PokerChip, - newlyAllInPlayers *[]*PokerPlayer, pokerTableID int64, +func handlePlayerActionEvent(g *PokerGame, p *PokerPlayer, db *database.DkfDB, minBet *database.PokerChip, + newlyAllInPlayers *[]*PokerPlayer, lastRaisePlayerIdx, playerAlive *int, playerToPlayIdx int, evt PlayerEvent) (actionResult int) { p.LastActionTS = time.Now() @@ -766,11 +766,11 @@ func handlePlayerActionEvent(g *PokerGame, p *PokerPlayer, db *database.DkfDB, m } else if evt.Check { actionResult = doCheck(g, p, minBet) } else if evt.Call { - actionResult = doCall(g, p, db, minBet, newlyAllInPlayers, pokerTableID) + actionResult = doCall(g, p, db, minBet, newlyAllInPlayers) } else if evt.AllIn { - actionResult = doAllIn(g, p, db, minBet, newlyAllInPlayers, pokerTableID, lastRaisePlayerIdx, playerToPlayIdx) + actionResult = doAllIn(g, p, db, minBet, newlyAllInPlayers, lastRaisePlayerIdx, playerToPlayIdx) } else if evt.Bet > 0 { - actionResult = doBet(g, p, db, minBet, newlyAllInPlayers, pokerTableID, lastRaisePlayerIdx, playerToPlayIdx, evt) + actionResult = doBet(g, p, db, minBet, newlyAllInPlayers, lastRaisePlayerIdx, playerToPlayIdx, evt) } else { actionResult = continueGetPlayerEventLoop } @@ -781,7 +781,6 @@ func handlePlayerActionEvent(g *PokerGame, p *PokerPlayer, db *database.DkfDB, m func execBettingRound(g *PokerGame, skip int, minBet database.PokerChip) bool { db := g.DB ongoing := g.Ongoing - pokerTableID := g.PokerTableID roomID := g.RoomID roomTopic := roomID.Topic() _, dealerIdx := ongoing.getPlayerBySeatIdx(int(g.DealerSeatIdx.Load())) @@ -832,14 +831,15 @@ RoundIsSettledLoop: // Check for pre-selected action if autoAction, ok := autoCache[pUsername]; ok { - actionResult = applyAutoAction(g, p, db, minBet, &newlyAllInPlayers, pokerTableID, &lastRaisePlayerIdx, &playerAlive, playerToPlayIdx, autoAction, autoCache) + actionResult = applyAutoAction(g, p, db, &minBet, &newlyAllInPlayers, + &lastRaisePlayerIdx, &playerAlive, playerToPlayIdx, autoAction, autoCache) goto checkActionResult } select { case evt = <-g.PlayersEventCh: case <-waitCh: // Waited too long, either auto-check or auto-fold - actionResult = doTimeout(g, p, minBet, &playerAlive) + actionResult = doTimeout(g, p, &minBet, &playerAlive) goto checkActionResult } @@ -853,7 +853,8 @@ RoundIsSettledLoop: goto checkActionResult } - actionResult = handlePlayerActionEvent(g, p, db, minBet, &newlyAllInPlayers, pokerTableID, &lastRaisePlayerIdx, &playerAlive, playerToPlayIdx, evt) + actionResult = handlePlayerActionEvent(g, p, db, &minBet, &newlyAllInPlayers, + &lastRaisePlayerIdx, &playerAlive, playerToPlayIdx, evt) goto checkActionResult checkActionResult: @@ -893,7 +894,7 @@ RoundIsSettled: // Always refund the difference between the first-biggest bet and the second-biggest bet. // We refund the "uncalled bet" so that it does not go in the main pot and does not get raked. // Also, if a player goes all-in and a fraction of his bet is not matched, it will be refunded. - refundUncalledBet(db, ongoing, pokerTableID, roomTopic) + refundUncalledBet(db, ongoing, g.PokerTableID, roomTopic) // Transfer players bets into the main pot mainPot += resetPlayersBet(ongoing)