dkforest

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

commit 8903c28cf9f86ea3dc2c0513db9aba138d51063d
parent d0e4e40fb2bfdb986324cabc67af452840c1ea41
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu,  7 Dec 2023 02:35:12 -0500

small/big blinds

Diffstat:
Mpkg/web/handlers/poker/poker.go | 59++++++++++++++++++++++++++++++++++++++++++++++-------------
Mpkg/web/handlers/poker/poker_test.go | 6+++---
2 files changed, 49 insertions(+), 16 deletions(-)

diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go @@ -116,6 +116,9 @@ type PokerGame struct { Players []*PokerStandingPlayer Ongoing *Ongoing DealerIdx int + smallBlindIdx int + bigBlindIdx int + underTheGunIdx int DealerIdxMtx sync.RWMutex IsGameDone atomic.Bool IsGameOver atomic.Bool @@ -199,6 +202,15 @@ func (g *PokerGame) incrDealerIdx() { g.DealerIdxMtx.Lock() g.DealerIdx = dealerIdx g.DealerIdxMtx.Unlock() + + nbPlayers := len(g.Ongoing.Players) + g.smallBlindIdx = (dealerIdx + 1) % nbPlayers + g.bigBlindIdx = (dealerIdx + 2) % nbPlayers + g.underTheGunIdx = (dealerIdx + 3) % nbPlayers +} + +func rotateIdx[T any](players []T) int { + return 0 } func (g *PokerGame) SitPlayer(authUser *database.User, pos int) error { @@ -239,20 +251,23 @@ func newLogEvent(g *PokerGame, roomLogsTopic, msg string) { } func reorderPlayers[T any](players []T, dealerIdx int) (out []T) { - for i := dealerIdx; i < len(players); i++ { + smallBlindIdx := dealerIdx + 1 + if smallBlindIdx >= len(players) { + smallBlindIdx = 0 + } + for i := smallBlindIdx; i < len(players); i++ { out = append(out, players[i]) } - for i := 0; i < dealerIdx; i++ { + for i := 0; i < smallBlindIdx; i++ { out = append(out, players[i]) } return } // Return either or not the game ended because only 1 player left playing -func waitPlayersActionFn(g *PokerGame, roomID string) bool { +func waitPlayersActionFn(g *PokerGame, roomID string, skip, minBet int) bool { roomTopic := "room_" + roomID lastRisePlayerIdx := -1 - minBet := 0 g.DealerIdxMtx.RLock() dealerIdx := g.DealerIdx @@ -266,10 +281,16 @@ func waitPlayersActionFn(g *PokerGame, roomID string) bool { } // TODO: implement maximum re-rise + skipIsDone := false OUTER: for { // Loop until the round is settled players := reorderPlayers(g.Ongoing.Players, dealerIdx) - for _, p := range players { + for idx, p := range players { + if !skipIsDone { + if idx < skip { + continue + } + } if p.SeatIdx == lastRisePlayerIdx { break OUTER } @@ -392,6 +413,8 @@ OUTER: } } + skipIsDone = true + // All settle when all players have the same bet amount if isRoundSettled(g.Ongoing.Players) { break @@ -425,6 +448,7 @@ OUTER: func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) { roomTopic := "room_" + roomID + roomLogsTopic := "room_" + roomID + "_logs" var winner *PokerPlayer var winnerHand string var minScore int32 = math.MaxInt32 @@ -488,10 +512,19 @@ func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) { PokerPubSub.Pub(roomTopic, GameStartedEvent{DeckHash: deckHash}) // TODO: implement small/big blinds - //p := g.Ongoing.GetNextPlayer() - // Small blind - // Big blind - // Next player starts + p := g.Ongoing.Players[g.smallBlindIdx] + bet := SmallBlindBet + p.Bet += bet + p.Cash -= bet + PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerIdx: p.SeatIdx, Player: p.Username, Bet: bet, TotalBet: p.Bet, Cash: p.Cash}) + newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s small blind %d", p.Username, bet)) + + p = g.Ongoing.Players[g.bigBlindIdx] + bet = BigBlindBet + p.Bet += bet + p.Cash -= bet + PokerPubSub.Pub(roomTopic, PlayerBetEvent{PlayerIdx: p.SeatIdx, Player: p.Username, Bet: bet, TotalBet: p.Bet, Cash: p.Cash}) + newLogEvent(g, roomLogsTopic, fmt.Sprintf("%s big blind %d", p.Username, bet)) // Deal cards for cardIdx := 1; cardIdx <= NbCardsPerPlayer; cardIdx++ { @@ -533,7 +566,7 @@ func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) { // Wait for players to bet/call/check/fold... time.Sleep(time.Second) - if waitPlayersActionFn(g, roomID) { + if waitPlayersActionFn(g, roomID, 2, BigBlindBet) { goto END } @@ -549,7 +582,7 @@ func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) { // Wait for players to bet/call/check/fold... time.Sleep(time.Second) - if waitPlayersActionFn(g, roomID) { + if waitPlayersActionFn(g, roomID, 0, 0) { goto END } @@ -563,7 +596,7 @@ func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) { // Wait for players to bet/call/check/fold... time.Sleep(time.Second) - if waitPlayersActionFn(g, roomID) { + if waitPlayersActionFn(g, roomID, 0, 0) { goto END } @@ -577,7 +610,7 @@ func dealerThread(db *database.DkfDB, g *PokerGame, roomID string) { // Wait for players to bet/call/check/fold... time.Sleep(time.Second) - if waitPlayersActionFn(g, roomID) { + if waitPlayersActionFn(g, roomID, 0, 0) { goto END } diff --git a/pkg/web/handlers/poker/poker_test.go b/pkg/web/handlers/poker/poker_test.go @@ -16,9 +16,9 @@ func Test_reorderPlayers(t *testing.T) { wantOut []T } tests := []testCase[int]{ - {"1", args[int]{[]int{1, 2, 3, 4, 5, 6}, 0}, []int{1, 2, 3, 4, 5, 6}}, - {"2", args[int]{[]int{1, 2, 3, 4, 5, 6}, 1}, []int{2, 3, 4, 5, 6, 1}}, - {"2", args[int]{[]int{1, 2, 3, 4, 5, 6}, 5}, []int{6, 1, 2, 3, 4, 5}}, + {"1", args[int]{[]int{1, 2, 3, 4, 5, 6}, 0}, []int{2, 3, 4, 5, 6, 1}}, + {"2", args[int]{[]int{1, 2, 3, 4, 5, 6}, 1}, []int{3, 4, 5, 6, 1, 2}}, + {"2", args[int]{[]int{1, 2, 3, 4, 5, 6}, 5}, []int{1, 2, 3, 4, 5, 6}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {