Skip to content

Commit

Permalink
Add logger to TM, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskawczynski committed Aug 26, 2023
1 parent b01e336 commit b3363f5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
10 changes: 4 additions & 6 deletions src/game.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,22 @@ function end_of_actions(table::Table, player)
return any((case_1, case_2, case_3, case_4, case_5, case_6, case_7))
end

function last_player_to_raise(table::Table)
for player in players_at_table(table)
function last_player_to_raise(players)
for player in players
last_to_raise(player) && return player
end
return nothing
end

function all_bets_were_called(table::Table)
lptr = last_player_to_raise(table)
players = players_at_table(table)
lptr = last_player_to_raise(players)
lptr===nothing && return true
logger = table.logger

players = players_at_table(table)
@assert count(x->last_to_raise(x), players) == 1
players_who_called = []
@cdebug logger "Checking if all bets were called"
@cdebug logger " table.winners.declared = $(table.winners.declared)"
arwc = false
@cdebug logger begin
conds_debug = map(players) do player
sn = seat_number(player)
Expand Down
4 changes: 2 additions & 2 deletions src/table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ function Table(players::Players;
dealer_pidx = default_dealer_pidx(),
current_raise_amt = 0,
initial_round_raise_amt = blinds.small,
transactions = TransactionManager(players),
logger = InfoLogger(),
transactions = TransactionManager(players, logger),
winners = Winners(),
play_out_game = false,
logger = InfoLogger(),
)
P = typeof(players)
buttons = Buttons(players, dealer_pidx)
Expand Down
13 changes: 8 additions & 5 deletions src/transactions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ end
Handle pots and side pots
among multiple players.
"""
struct TransactionManager
struct TransactionManager{L}
logger::L
perm::Vector{Int}
bank_rolls::Vector{Chips} # cache
initial_brs::Vector{Chips}
Expand All @@ -50,8 +51,8 @@ function Base.show(io::IO, tm::TransactionManager, include_type = true)
println(io, "Pot(s) = $(tm.side_pots)")
end

TransactionManager(players) = TransactionManager(Players(players))
function TransactionManager(players::Players)
TransactionManager(players, logger) = TransactionManager(Players(players), logger)
function TransactionManager(players::Players, logger)
perm = collect(sortperm(players))
bank_rolls = collect(map(x->bank_roll_chips(x), players))

Expand All @@ -68,13 +69,15 @@ function TransactionManager(players::Players)
findfirst(p -> seat_number(players[p]) == seat_number(player), perm)
end)
side_pots = [SidePot(seat_number(players[p]), 0, cap_i) for (cap_i, p) in zip(cap, perm)]
@cdebug logger "initial caps = $(cap.(side_pots))"

initial_brs = deepcopy(collect(bank_roll_chips.(players)))
sorted_hand_evals = map(x->HandEval(), 1:length(players))
pot_id = Int[1]
FT = eltype(initial_brs)
side_pot_winnings = collect(map(x->collect(map(x->FT(0), players)), players))
TransactionManager(
TransactionManager{typeof(logger)}(
logger,
perm,
bank_rolls,
initial_brs,
Expand Down Expand Up @@ -192,7 +195,7 @@ function contribute!(table, player, amt, call=false)
end
@assert amt_remaining == 0 # pots better be emptied

if bank_roll(player) == 0 # went all-in, set exactly.
if bank_roll(player) == 0 # went all-in.
player.all_in = true
player.action_required = false
end
Expand Down
1 change: 1 addition & 0 deletions test/chips.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ end
b = Chips(2, SimpleRatio(3, 2))
@test a + b == Chips(4, SimpleRatio(1, 2))
@test Chips(12, SimpleRatio(0, 1)) == Chips(12, SimpleRatio(0, 2))
@test Chips(12, SimpleRatio(1, 3)) Chips(12, SimpleRatio(1, 2))
end
21 changes: 14 additions & 7 deletions test/transactions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ check!(t, p) = TH.update_given_valid_action!(t, p, Check())

@testset "TransactionManagers - Lowest bank roll goes all-in and wins it all" begin
table_cards = (A♢, K♢, Q♢, 2♠, 3♠)
logger = TH.InfoLogger()
players = (
Player(Bot5050(), 1, (A♠, A♣); bank_roll = 1*100),
Player(Bot5050(), 2, (K♠, K♣); bank_roll = 2*100),
Player(Bot5050(), 3, (Q♠, Q♣); bank_roll = 3*100),
)
tm = TH.TransactionManager(players)
tm = TH.TransactionManager(players, logger)
table = Table(players;cards=table_cards,transactions=tm, logger=TH.ByPassLogger())
@test TH.seat_number.(tm.side_pots) == [1,2,3]

Expand All @@ -40,12 +41,13 @@ end

@testset "TransactionManagers - Highest bank roll goes all-in and wins it all" begin
table_cards = (A♢, K♢, Q♢, 2♠, 3♠)
logger = TH.InfoLogger()
players = (
Player(Bot5050(), 1, (A♠, A♣); bank_roll = 3*100),
Player(Bot5050(), 2, (K♠, K♣); bank_roll = 2*100),
Player(Bot5050(), 3, (Q♠, Q♣); bank_roll = 1*100),
)
tm = TH.TransactionManager(players)
tm = TH.TransactionManager(players, logger)
table = Table(players;cards=table_cards,transactions=tm, logger=TH.ByPassLogger())

raise_to!(table, players[1], 100) # Raise
Expand All @@ -63,12 +65,13 @@ end

@testset "TransactionManagers - Lowest bank roll goes all-in and wins a split pot" begin
table_cards = (A♢, K♢, Q♢, 2♠, 3♠)
logger = TH.InfoLogger()
players = (
Player(Bot5050(), 1, (A♠, A♣); bank_roll = 1*100),
Player(Bot5050(), 2, (K♠, K♣); bank_roll = 2*100),
Player(Bot5050(), 3, (Q♠, Q♣); bank_roll = 3*100),
)
tm = TH.TransactionManager(players)
tm = TH.TransactionManager(players, logger)
table = Table(players;cards=table_cards,transactions=tm, logger=TH.ByPassLogger())

raise_to!(table, players[1], 100) # Raise all-in
Expand All @@ -95,12 +98,13 @@ end

@testset "TransactionManagers - Highest bank roll goes all-in and wins it all" begin
table_cards = (A♢, K♢, Q♢, 2♠, 3♠)
logger = TH.InfoLogger()
players = (
Player(Bot5050(), 1, (A♠, A♣); bank_roll = 3*100),
Player(Bot5050(), 2, (K♠, K♣); bank_roll = 2*100),
Player(Bot5050(), 3, (Q♠, Q♣); bank_roll = 1*100),
)
tm = TH.TransactionManager(players)
tm = TH.TransactionManager(players, logger)
table = Table(players;cards=table_cards,transactions=tm, logger=TH.ByPassLogger())

raise_to!(table, players[1], 100) # Raise
Expand All @@ -127,6 +131,7 @@ end

@testset "TransactionManagers - Semi-complicated split pot (shared winners)" begin
table_cards = (T♢, Q♢, A♠, 8♠, 9♠)
logger = TH.InfoLogger()
players = (
Player(Bot5050(), 1, (4♠, 5♣); bank_roll = 1*100), # bust
Player(Bot5050(), 2, (K♠, K♣); bank_roll = 2*100), # win, split with player 3
Expand All @@ -135,7 +140,7 @@ end
Player(Bot5050(), 5, (7♠, 7♣); bank_roll = 5*100), # 2nd to players 2 and 3, win remaining pot
Player(Bot5050(), 6, (2♠, 3♣); bank_roll = 6*100), # lose, but not bust
)
tm = TH.TransactionManager(players)
tm = TH.TransactionManager(players, logger)
table = Table(players;cards=table_cards,transactions=tm, logger=TH.ByPassLogger())

raise_to!(table, players[1], 100) # raise all-in
Expand Down Expand Up @@ -190,6 +195,7 @@ end

@testset "TransactionManagers - Single round split pot (shared winners), with simple re-raises" begin
table_cards = (T♢, Q♢, A♠, 8♠, 9♠)
logger = TH.InfoLogger()
players = (
Player(Bot5050(), 1, (4♠, 5♣); bank_roll = 1*100), # bust
Player(Bot5050(), 2, (K♠, K♣); bank_roll = 2*100), # win, split with player 3
Expand All @@ -198,7 +204,7 @@ end
Player(Bot5050(), 5, (7♠, 7♣); bank_roll = 5*100), # 2nd to players 2 and 3, win remaining pot
Player(Bot5050(), 6, (2♠, 3♣); bank_roll = 6*100), # lose, but not bust
)
tm = TH.TransactionManager(players)
tm = TH.TransactionManager(players, logger)
table = Table(players;cards=table_cards,transactions=tm, logger=TH.ByPassLogger())
@test TH.amount.(tm.side_pots) == [0, 0, 0, 0, 0, 0]

Expand Down Expand Up @@ -234,6 +240,7 @@ end

@testset "TransactionManagers - Single round split pot (shared winners), with simple re-raises, reversed bank roll order" begin
table_cards = (T♢, Q♢, A♠, 8♠, 9♠)
logger = TH.InfoLogger()
players = (
Player(Bot5050(), 1, (2♠, 3♣); bank_roll = 6*100), # lose, but not bust
Player(Bot5050(), 2, (7♠, 7♣); bank_roll = 5*100), # 2nd to players 2 and 3, win remaining pot
Expand All @@ -242,7 +249,7 @@ end
Player(Bot5050(), 5, (K♠, K♣); bank_roll = 2*100), # win, split with player 3
Player(Bot5050(), 6, (4♠, 5♣); bank_roll = 1*100), # bust
)
tm = TH.TransactionManager(players)
tm = TH.TransactionManager(players, logger)
table = Table(players;cards=table_cards,transactions=tm, logger=TH.ByPassLogger())
@test TH.amount.(tm.side_pots) == [0, 0, 0, 0, 0, 0]

Expand Down

0 comments on commit b3363f5

Please sign in to comment.