commit ca0e3f617e2fa1dae432d49798cba5b6ee39036d
parent 387631267170fb65ef359e0ef121f26b61ca9e97
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Fri, 8 Dec 2023 16:46:25 -0500
simplify code
Diffstat:
2 files changed, 13 insertions(+), 61 deletions(-)
diff --git a/pkg/web/handlers/poker/poker.go b/pkg/web/handlers/poker/poker.go
@@ -332,20 +332,6 @@ func newLogEvent(g *PokerGame, roomLogsTopic, msg string) {
}
}
-func reorderPlayers[T any](players []T, dealerIdx int) (out []T) {
- 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 < smallBlindIdx; i++ {
- out = append(out, players[i])
- }
- return
-}
-
func showCards(g *PokerGame, roomTopic string, seats []Seat) {
for _, p := range g.Ongoing.Players {
if !p.Folded {
@@ -366,7 +352,6 @@ func showCards(g *PokerGame, roomTopic string, seats []Seat) {
// Return either or not the game ended because only 1 player left playing
func waitPlayersActionFn(g *PokerGame, roomID string, skip, minBet int) bool {
roomTopic := "room_" + roomID
- lastRaisePlayerIdx := -1
// Avoid asking for actions if only 1 player can do so (because others are all-in)
nbCanVote := 0
@@ -380,6 +365,8 @@ func waitPlayersActionFn(g *PokerGame, roomID string, skip, minBet int) bool {
}
dealerIdx := int(g.DealerIdx.Load())
+ playerToPlayIdx := (dealerIdx + skip) % len(g.Ongoing.Players)
+ lastRaisePlayerIdx := -1
playerAlive := 0
for _, p := range g.Ongoing.Players {
@@ -390,28 +377,21 @@ func waitPlayersActionFn(g *PokerGame, roomID string, skip, minBet int) bool {
// TODO: implement maximum re-raise
- // Used to skip small/big blinds on first round
- skipIsDone := false
-
newlyAllInPlayers := make([]*PokerPlayer, 0)
OUTER:
- for { // Loop until the round is settled
- players := reorderPlayers(g.Ongoing.Players, dealerIdx)
- for idx, p := range players {
- if !skipIsDone {
- if idx < skip {
- continue
- }
+ for {
+ for {
+ playerToPlayIdx = (playerToPlayIdx + 1) % len(g.Ongoing.Players)
+ p := g.Ongoing.Players[playerToPlayIdx]
+ if playerToPlayIdx == lastRaisePlayerIdx {
+ break
}
- if p.SeatIdx == lastRaisePlayerIdx {
- break OUTER
+ if lastRaisePlayerIdx == -1 {
+ lastRaisePlayerIdx = playerToPlayIdx
}
player := g.Ongoing.GetPlayer(p.Username)
- if player.Folded {
- continue
- }
- if player.Cash == 0 {
+ if player.Folded || player.Cash == 0 {
continue
}
evt := PokerWaitTurnEvent{Idx: p.SeatIdx}
@@ -503,7 +483,7 @@ OUTER:
} else if evt.AllIn {
bet := p.Cash
if (p.Bet + bet) > minBet {
- lastRaisePlayerIdx = p.SeatIdx
+ lastRaisePlayerIdx = playerToPlayIdx
}
minBet = p.Bet + bet
p.doBet(bet)
@@ -527,7 +507,7 @@ OUTER:
continue
}
if (p.Bet + bet) > minBet {
- lastRaisePlayerIdx = p.SeatIdx
+ lastRaisePlayerIdx = playerToPlayIdx
}
minBet = p.Bet + bet
p.doBet(bet)
@@ -546,9 +526,6 @@ OUTER:
break
}
}
-
- skipIsDone = true
-
// All settle when all players have the same bet amount
if isRoundSettled(g.Ongoing.Players) {
break
diff --git a/pkg/web/handlers/poker/poker_test.go b/pkg/web/handlers/poker/poker_test.go
@@ -2,34 +2,9 @@ package poker
import (
"github.com/stretchr/testify/assert"
- "reflect"
"testing"
)
-func Test_reorderPlayers(t *testing.T) {
- type args[T any] struct {
- players []T
- dealerIdx int
- }
- type testCase[T any] struct {
- name string
- args args[T]
- wantOut []T
- }
- tests := []testCase[int]{
- {"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) {
- if gotOut := reorderPlayers(tt.args.players, tt.args.dealerIdx); !reflect.DeepEqual(gotOut, tt.wantOut) {
- t.Errorf("reorderPlayers() = %v, want %v", gotOut, tt.wantOut)
- }
- })
- }
-}
-
func Test_sortGameResults(t *testing.T) {
p1 := &PokerPlayer{Cash: 0, RoundTotalBet: 10, Username: "p1"}
p2 := &PokerPlayer{Cash: 0, RoundTotalBet: 20, Username: "p2"}