dkforest

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

commit e5b37834d9eae8a4ef8683bf9442ce3acf11f51d
parent f263d4f82db1f1a8744db7b4e57670fa12a3d152
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Tue,  5 Dec 2023 21:17:01 -0500

cleanup

Diffstat:
Mpkg/web/handlers/poker/poker.go | 211++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 106 insertions(+), 105 deletions(-)

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -179,130 +179,131 @@ func NewOngoing(g *PokerGame) *Ongoing { return &Ongoing{Deck: deck, Players: players, WaitTurnEvent: PokerWaitTurnEvent{Idx: -1}} } -func dealerThread(g *PokerGame, roomID string) { +func waitPlayersActionFn(g *PokerGame, roomID string) bool { roomTopic := "room_" + roomID + lastRisePlayerIdx := -1 + minBet := 0 - waitPlayersActionFn := func() bool { - lastRisePlayerIdx := -1 - minBet := 0 - - playerAlive := 0 - for _, p := range g.Ongoing.Players { - if p != nil && !p.Folded { - playerAlive++ - } + playerAlive := 0 + for _, p := range g.Ongoing.Players { + if p != nil && !p.Folded { + playerAlive++ } + } - // TODO: implement maximum re-rise - OUTER: - for { // Loop until the round is settled - for i, p := range g.Ongoing.Players { - if p == nil { - continue - } - if i == lastRisePlayerIdx { - break OUTER - } - player := g.Ongoing.GetPlayer(p.Username) - if player.Folded { - continue - } - evt := PokerWaitTurnEvent{Idx: i} - PokerPubSub.Pub(roomTopic, evt) - g.Ongoing.WaitTurnEvent = evt - - // Maximum time allowed for the player to send his action - waitCh := time.After(MaxUserCountdown * time.Second) - LOOP: - for { // Repeat until we get an event from the player we're interested in - var evt PlayerEvent - - select { - case evt = <-g.PlayersEventCh: - case <-waitCh: - // Waited too long, either auto-check or auto-fold - if p.Cash == 0 { // all-in - break LOOP - } - if p.Bet < minBet { - player.Folded = true - PokerPubSub.Pub(roomTopic, PlayerFoldEvent{Card1Idx: player.Cards[0].Idx, Card2Idx: player.Cards[1].Idx}) - playerAlive-- - if playerAlive == 1 { - break OUTER - } - } + // TODO: implement maximum re-rise +OUTER: + for { // Loop until the round is settled + for i, p := range g.Ongoing.Players { + if p == nil { + continue + } + if i == lastRisePlayerIdx { + break OUTER + } + player := g.Ongoing.GetPlayer(p.Username) + if player.Folded { + continue + } + evt := PokerWaitTurnEvent{Idx: i} + PokerPubSub.Pub(roomTopic, evt) + g.Ongoing.WaitTurnEvent = evt + + // Maximum time allowed for the player to send his action + waitCh := time.After(MaxUserCountdown * time.Second) + LOOP: + for { // Repeat until we get an event from the player we're interested in + var evt PlayerEvent + + select { + case evt = <-g.PlayersEventCh: + case <-waitCh: + // Waited too long, either auto-check or auto-fold + if p.Cash == 0 { // all-in break LOOP } - - if evt.Player != p.Username { - continue - } - roomUserTopic := "room_" + roomID + "_" + p.Username - if evt.Fold { + if p.Bet < minBet { player.Folded = true PokerPubSub.Pub(roomTopic, PlayerFoldEvent{Card1Idx: player.Cards[0].Idx, Card2Idx: player.Cards[1].Idx}) playerAlive-- if playerAlive == 1 { break OUTER } - } else if evt.Check { - if p.Bet < minBet { - msg := fmt.Sprintf("Need to bet %d", minBet-p.Bet) - PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg}) - continue - } - } else if evt.Call { - bet := minBet - p.Bet - if p.Cash < bet { - bet = p.Cash - p.Bet += bet - p.Cash = 0 - // All in - } else { - p.Bet += bet - p.Cash -= bet - } - PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerIdx: i, Player: p.Username, Bet: bet, TotalBet: p.Bet, Cash: p.Cash}) - } else if evt.Bet > 0 { - if (p.Bet + evt.Bet) < minBet { - msg := fmt.Sprintf("Bet (%d) is too low. Must bet at least %d", evt.Bet, minBet-p.Bet) - PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg}) - continue - } - if (p.Bet + evt.Bet) > minBet { - lastRisePlayerIdx = i - } - minBet = p.Bet + evt.Bet - p.Bet += evt.Bet - p.Cash -= evt.Bet - PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerIdx: i, Player: p.Username, Bet: evt.Bet, TotalBet: p.Bet, Cash: p.Cash}) } - break + break LOOP } - } - // All settle when all players have the same bet amount - if isRoundSettled(g.Ongoing.Players) { + if evt.Player != p.Username { + continue + } + roomUserTopic := "room_" + roomID + "_" + p.Username + if evt.Fold { + player.Folded = true + PokerPubSub.Pub(roomTopic, PlayerFoldEvent{Card1Idx: player.Cards[0].Idx, Card2Idx: player.Cards[1].Idx}) + playerAlive-- + if playerAlive == 1 { + break OUTER + } + } else if evt.Check { + if p.Bet < minBet { + msg := fmt.Sprintf("Need to bet %d", minBet-p.Bet) + PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg}) + continue + } + } else if evt.Call { + bet := minBet - p.Bet + if p.Cash < bet { + bet = p.Cash + p.Bet += bet + p.Cash = 0 + // All in + } else { + p.Bet += bet + p.Cash -= bet + } + PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerIdx: i, Player: p.Username, Bet: bet, TotalBet: p.Bet, Cash: p.Cash}) + } else if evt.Bet > 0 { + if (p.Bet + evt.Bet) < minBet { + msg := fmt.Sprintf("Bet (%d) is too low. Must bet at least %d", evt.Bet, minBet-p.Bet) + PokerPubSub.Pub(roomUserTopic, ErrorMsgEvent{Message: msg}) + continue + } + if (p.Bet + evt.Bet) > minBet { + lastRisePlayerIdx = i + } + minBet = p.Bet + evt.Bet + p.Bet += evt.Bet + p.Cash -= evt.Bet + PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerIdx: i, Player: p.Username, Bet: evt.Bet, TotalBet: p.Bet, Cash: p.Cash}) + } break } } - // Transfer players bets into the main pot - for i := range g.Ongoing.Players { - if g.Ongoing.Players[i] != nil { - g.Ongoing.MainPot += g.Ongoing.Players[i].Bet - g.Ongoing.Players[i].Bet = 0 - } + // All settle when all players have the same bet amount + if isRoundSettled(g.Ongoing.Players) { + break } + } - evt := PokerWaitTurnEvent{Idx: -1, MainPot: g.Ongoing.MainPot} - PokerPubSub.Pub(roomTopic, evt) - g.Ongoing.WaitTurnEvent = evt - - return playerAlive == 1 + // Transfer players bets into the main pot + for i := range g.Ongoing.Players { + if g.Ongoing.Players[i] != nil { + g.Ongoing.MainPot += g.Ongoing.Players[i].Bet + g.Ongoing.Players[i].Bet = 0 + } } + evt := PokerWaitTurnEvent{Idx: -1, MainPot: g.Ongoing.MainPot} + PokerPubSub.Pub(roomTopic, evt) + g.Ongoing.WaitTurnEvent = evt + + return playerAlive == 1 +} + +func dealerThread(g *PokerGame, roomID string) { + roomTopic := "room_" + roomID + type Seat struct { Top int Left int @@ -388,7 +389,7 @@ func dealerThread(g *PokerGame, roomID string) { // Wait for players to bet/call/check/fold... time.Sleep(time.Second) - if waitPlayersActionFn() { + if waitPlayersActionFn(g, roomID) { goto END } @@ -404,7 +405,7 @@ func dealerThread(g *PokerGame, roomID string) { // Wait for players to bet/call/check/fold... time.Sleep(time.Second) - if waitPlayersActionFn() { + if waitPlayersActionFn(g, roomID) { goto END } @@ -418,7 +419,7 @@ func dealerThread(g *PokerGame, roomID string) { // Wait for players to bet/call/check/fold... time.Sleep(time.Second) - if waitPlayersActionFn() { + if waitPlayersActionFn(g, roomID) { goto END } @@ -432,7 +433,7 @@ func dealerThread(g *PokerGame, roomID string) { // Wait for players to bet/call/check/fold... time.Sleep(time.Second) - if waitPlayersActionFn() { + if waitPlayersActionFn(g, roomID) { goto END }