dkforest

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

commit 244c4b6e6c8e56e3a7619148dadecfe1b350ed35
parent 3bf371e63b8da5b4e6daca8dabfd6de8dabcb78c
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sat, 16 Dec 2023 04:20:14 -0500

cleanup auto actions

Diffstat:
Mpkg/web/handlers/poker/poker.go | 134++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
1 file changed, 75 insertions(+), 59 deletions(-)

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -568,6 +568,15 @@ RoundIsSettledLoop: return breakGetPlayerEventLoop } + doUnsit := func() int { + playerAlive = g.Ongoing.CountAlivePlayers() + if playerAlive == 1 { + p.countChancesToAction-- + return breakRoundIsSettledLoop + } + return continueGetPlayerEventLoop + } + doFold := func() int { foldPlayer(p) newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s fold", pUsername)) @@ -654,6 +663,65 @@ RoundIsSettledLoop: return doNothing } + handleAutoActionReceived := func(evt PlayerEvent) int { + roomUserTopic := roomID.UserTopic(evt.Player) + autoAction := autoCache[evt.Player] + if evt.Fold && autoAction == FoldAction || + evt.Call && autoAction == CallAction || + evt.Check && autoAction == CheckAction { + delete(autoCache, evt.Player) + PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: ""}) + return continueGetPlayerEventLoop + } + if evt.Fold { + autoCache[evt.Player] = FoldAction + PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: "Will auto fold"}) + } else if evt.Call { + autoCache[evt.Player] = CallAction + PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: "Will auto call"}) + } else if evt.Check { + autoCache[evt.Player] = CheckAction + PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: "Will auto check"}) + } + return continueGetPlayerEventLoop + } + + handlePlayerActionEvent := func(evt PlayerEvent) (actionResult int) { + p.LastActionTS = time.Now() + if evt.Fold { + actionResult = doFold() + } else if evt.Check { + actionResult = doCheck() + } else if evt.Call { + actionResult = doCall() + } else if evt.AllIn { + actionResult = doAllIn() + } else if evt.Bet > 0 { + actionResult = doBet(evt) + } else { + actionResult = continueGetPlayerEventLoop + } + return actionResult + } + + applyAutoAction := func(autoAction int) (actionResult int) { + if autoAction > NoAction { + time.Sleep(500 * time.Millisecond) + p.LastActionTS = time.Now() + switch autoAction { + case NoAction: + case FoldAction: + actionResult = doFold() + case CheckAction: + actionResult = doCheck() + case CallAction: + actionResult = doCall() + } + } + delete(autoCache, pUsername) + return + } + // Maximum time allowed for the player to send his action waitCh := time.After(MaxUserCountdown * time.Second) GetPlayerEventLoop: @@ -663,20 +731,7 @@ RoundIsSettledLoop: // Check for pre-selected action if autoAction, ok := autoCache[pUsername]; ok { - if autoAction > NoAction { - time.Sleep(500 * time.Millisecond) - p.LastActionTS = time.Now() - switch autoAction { - case NoAction: - case FoldAction: - actionResult = doFold() - case CheckAction: - actionResult = doCheck() - case CallAction: - actionResult = doCall() - } - } - delete(autoCache, pUsername) + actionResult = applyAutoAction(autoAction) goto checkActionResult } @@ -688,34 +743,17 @@ RoundIsSettledLoop: } if evt.Unsit { - playerAlive = g.Ongoing.CountAlivePlayers() - if playerAlive == 1 { - p.countChancesToAction-- - break RoundIsSettledLoop - } - continue GetPlayerEventLoop + actionResult = doUnsit() + goto checkActionResult } if evt.Player != pUsername { - handleAutoAction(evt, roomID, autoCache) - continue GetPlayerEventLoop + actionResult = handleAutoActionReceived(evt) + goto checkActionResult } - p.LastActionTS = time.Now() - - if evt.Fold { - actionResult = doFold() - } else if evt.Check { - actionResult = doCheck() - } else if evt.Call { - actionResult = doCall() - } else if evt.AllIn { - actionResult = doAllIn() - } else if evt.Bet > 0 { - actionResult = doBet(evt) - } else { - actionResult = continueGetPlayerEventLoop - } + actionResult = handlePlayerActionEvent(evt) + goto checkActionResult checkActionResult: switch actionResult { @@ -780,28 +818,6 @@ RoundIsSettled: return playerAlive <= 1 } -func handleAutoAction(evt PlayerEvent, roomID RoomID, autoCache map[database.Username]int) { - roomUserTopic := roomID.UserTopic(evt.Player) - autoAction := autoCache[evt.Player] - if evt.Fold && autoAction == FoldAction || - evt.Call && autoAction == CallAction || - evt.Check && autoAction == CheckAction { - delete(autoCache, evt.Player) - PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: ""}) - return - } - if evt.Fold { - autoCache[evt.Player] = FoldAction - PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: "Will auto fold"}) - } else if evt.Call { - autoCache[evt.Player] = CallAction - PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: "Will auto call"}) - } else if evt.Check { - autoCache[evt.Player] = CheckAction - PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: "Will auto check"}) - } -} - type Seat struct { Top int Left int