From 589e22fffe595060188cca0212465a51a19498b4 Mon Sep 17 00:00:00 2001 From: Charles Kawczynski Date: Tue, 4 Jul 2023 15:13:54 -0700 Subject: [PATCH] Add inferable logger path Add and pass custom logger --- perf/benchmark.jl | 2 +- perf/flame.jl | 4 +- perf/jet.jl | 10 ++-- src/TexasHoldem.jl | 2 + src/custom_logger.jl | 40 ++++++++++++++++ src/game.jl | 103 +++++++++++++++++++++++------------------- src/player_actions.jl | 39 +++++++++------- src/player_options.jl | 3 +- src/table.jl | 28 +++++++----- src/transactions.jl | 56 ++++++++++++----------- test/runtests.jl | 8 ++-- test/table.jl | 2 +- 12 files changed, 181 insertions(+), 116 deletions(-) create mode 100644 src/custom_logger.jl diff --git a/perf/benchmark.jl b/perf/benchmark.jl index 8aa92bf3..219852e9 100644 --- a/perf/benchmark.jl +++ b/perf/benchmark.jl @@ -5,7 +5,7 @@ import TexasHoldem const TH = TexasHoldem using TexasHoldem using BenchmarkTools -using Logging +import Logging struct BotCheckCall <: AbstractAI end diff --git a/perf/flame.jl b/perf/flame.jl index 6cb42322..c006ae6c 100644 --- a/perf/flame.jl +++ b/perf/flame.jl @@ -5,7 +5,7 @@ import TexasHoldem const TH = TexasHoldem using TexasHoldem using BenchmarkTools -using Logging +import Logging struct BotCheckCall <: AbstractAI end @@ -17,7 +17,7 @@ TH.player_option!(game::Game, player::Player{BotCheckCall}, ::CallFold) = call!( players() = ntuple(i->(Player(BotCheckCall(), i)), 4) function do_work!(games) - with_logger(NullLogger()) do + Logging.with_logger(Logging.NullLogger()) do for game in games play!(game) end diff --git a/perf/jet.jl b/perf/jet.jl index 18eb1cb7..d52e3a3a 100644 --- a/perf/jet.jl +++ b/perf/jet.jl @@ -5,7 +5,6 @@ import TexasHoldem const TH = TexasHoldem using TexasHoldem using BenchmarkTools -using Logging struct BotCheckCall <: AbstractAI end @@ -17,16 +16,15 @@ TH.player_option!(game::Game, player::Player{BotCheckCall}, ::CallFold) = call!( players() = ntuple(i->(Player(BotCheckCall(), i)), 4) function do_work!(game) - with_logger(NullLogger()) do - play!(game) - end + play!(game) return nothing end # Make sure it runs without errors -game = Game(players()) +game = Game(players();logger=TH.ByPassLogger()) +println("------------ about to do work") do_work!(game) import JET -game = Game(players()) +game = Game(players();logger=TH.ByPassLogger()) JET.@test_opt do_work!(game) diff --git a/src/TexasHoldem.jl b/src/TexasHoldem.jl index de8dfa60..e159e619 100644 --- a/src/TexasHoldem.jl +++ b/src/TexasHoldem.jl @@ -19,6 +19,8 @@ using Printf export AbstractGameState, PreFlop, Flop, Turn, River +include("custom_logger.jl") + abstract type AbstractGameState end struct PreFlop <: AbstractGameState end struct Flop <: AbstractGameState end diff --git a/src/custom_logger.jl b/src/custom_logger.jl new file mode 100644 index 00000000..17b88a5e --- /dev/null +++ b/src/custom_logger.jl @@ -0,0 +1,40 @@ +#= +Custom logging was added so type-unstable logging +functions in the Logging infrastructure is elided. +=# +import Logging + +struct StandardLogger end +struct ByPassLogger end + +macro cdebug(logger, expr) + return quote + if !($(esc(logger)) isa ByPassLogger) + @debug $(esc(expr)) + end + end +end + +macro cwarn(logger, expr) + return quote + if !($(esc(logger)) isa ByPassLogger) + @warn $(esc(expr)) + end + end +end + +macro cerror(logger, expr) + return quote + if !($(esc(logger)) isa ByPassLogger) + @error $(esc(expr)) + end + end +end + +macro cinfo(logger, expr) + return quote + if !($(esc(logger)) isa ByPassLogger) + @info $(esc(expr)) + end + end +end diff --git a/src/game.jl b/src/game.jl index 490b8854..94cb6518 100644 --- a/src/game.jl +++ b/src/game.jl @@ -4,8 +4,8 @@ export Game, play!, tournament! -mutable struct Game - table::Table +mutable struct Game{T} + table::T end function Base.show(io::IO, game::Game) @@ -23,6 +23,7 @@ function Game( table = nothing, dealer_id::Int = default_dealer_id(), blinds::Blinds = Blinds(1,2), + logger = StandardLogger(), ) n_player_cards = sum(map(x->cards(x)==nothing ? 0 : length(cards(x)), players)) @@ -44,7 +45,8 @@ function Game( deck=deck, players=players, dealer_id=dealer_id, - blinds=blinds + blinds=blinds, + logger=logger, ) @assert length(deck) == 52 @@ -52,7 +54,7 @@ function Game( @assert cards(table) == nothing end - return Game(table) + return Game{typeof(table)}(table) end players_at_table(game::Game) = players_at_table(game.table) @@ -61,10 +63,10 @@ any_actions_required(game::Game) = any_actions_required(game.table) state(game::Game) = state(game.table) move_buttons!(game) = move_buttons!(game.table) -print_game_state(table, state::PreFlop) = @info "Pre-flop!" -print_game_state(table, state::Flop) = @info "Flop: $(repeat(" ", 44)) $(table.cards[1:3])" -print_game_state(table, state::Turn) = @info "Turn: $(repeat(" ", 44)) $(table.cards[4])" -print_game_state(table, state::River) = @info "River: $(repeat(" ", 43)) $(table.cards[5])" +print_game_state(table, state::PreFlop) = @cinfo table.logger "Pre-flop!" +print_game_state(table, state::Flop) = @cinfo table.logger "Flop: $(repeat(" ", 44)) $(table.cards[1:3])" +print_game_state(table, state::Turn) = @cinfo table.logger "Turn: $(repeat(" ", 44)) $(table.cards[4])" +print_game_state(table, state::River) = @cinfo table.logger "River: $(repeat(" ", 43)) $(table.cards[5])" set_preflop_blind_raise!(table::Table, player, ::AbstractGameState, i::Int) = nothing function set_preflop_blind_raise!(table::Table, player::Player, ::PreFlop, i::Int) @@ -82,16 +84,17 @@ reset_round_bank_rolls!(game::Game, state::AbstractGameState) = reset_round_bank # TODO: compactify. Some of these cases/conditions may be redundant function end_of_actions(table::Table, player) players = players_at_table(table) - @debug " last_to_raise.(players) = $(last_to_raise.(players))" - @debug " checked.(players) = $(checked.(players))" - @debug " folded.(players) = $(folded.(players))" - @debug " action_required.(players) = $(action_required.(players))" - @debug " !any(action_required.(players)) = $(!any(action_required.(players)))" - @debug " all_playing_all_in(table) = $(all_playing_all_in(table))" - @debug " all_playing_checked(table) = $(all_playing_checked(table))" - @debug " all_all_in_or_checked(table) = $(all_all_in_or_checked(table))" - @debug " all_all_in_except_bank_roll_leader(table) = $(all_all_in_except_bank_roll_leader(table))" - @debug " all_oppononents_all_in(table, player) = $(all_oppononents_all_in(table, player))" + logger = table.logger + @cdebug logger " last_to_raise.(players) = $(last_to_raise.(players))" + @cdebug logger " checked.(players) = $(checked.(players))" + @cdebug logger " folded.(players) = $(folded.(players))" + @cdebug logger " action_required.(players) = $(action_required.(players))" + @cdebug logger " !any(action_required.(players)) = $(!any(action_required.(players)))" + @cdebug logger " all_playing_all_in(table) = $(all_playing_all_in(table))" + @cdebug logger " all_playing_checked(table) = $(all_playing_checked(table))" + @cdebug logger " all_all_in_or_checked(table) = $(all_all_in_or_checked(table))" + @cdebug logger " all_all_in_except_bank_roll_leader(table) = $(all_all_in_except_bank_roll_leader(table))" + @cdebug logger " all_oppononents_all_in(table, player) = $(all_oppononents_all_in(table, player))" case_1 = last_to_raise(player) case_2 = all_playing_checked(table) @@ -100,7 +103,7 @@ function end_of_actions(table::Table, player) case_5 = all_all_in_or_checked(table) # TODO: likely replaceable with case_6 case_6 = !any(action_required.(players)) case_7 = all_oppononents_all_in(table, player) && paid_current_raise_amount(table, player) - @debug " cases = $((case_1, case_2, case_3, case_4, case_5, case_6, case_7))" + @cdebug logger " cases = $((case_1, case_2, case_3, case_4, case_5, case_6, case_7))" return any((case_1, case_2, case_3, case_4, case_5, case_6, case_7)) end @@ -115,13 +118,14 @@ end function all_raises_were_called(table::Table) lptr = last_player_to_raise(table) lptr===nothing && return true + logger = table.logger players = players_at_table(table) ltr = last_to_raise.(players) @assert count(ltr) == 1 players_who_called = [] - @debug "Checking if all raises were called" - @debug " table.winners.declared = $(table.winners.declared)" + @cdebug logger "Checking if all raises were called" + @cdebug logger " table.winners.declared = $(table.winners.declared)" arwc = false conds = map(players) do player cond1 = seat_number(player) == seat_number(lptr) @@ -130,7 +134,7 @@ function all_raises_were_called(table::Table) cond4 = pot_investment(player) ≈ pot_investment(lptr) (cond1, cond2, cond3, cond4) end - @debug begin + @cdebug logger begin conds_debug = map(players) do player sn = seat_number(player) cond1 = seat_number(player) == seat_number(lptr) @@ -140,9 +144,9 @@ function all_raises_were_called(table::Table) (sn, cond1, cond2, cond3, cond4) end for cond in conds_debug - @debug "sn, cond = $(cond[1]), $(cond[2:end])" + @cdebug logger "sn, cond = $(cond[1]), $(cond[2:end])" end - @debug "snlptr = $(seat_number(lptr))" + @cdebug logger "snlptr = $(seat_number(lptr))" end return all(map(cond->any(cond), conds)) end @@ -157,6 +161,7 @@ end function act_generic!(game::Game, state::AbstractGameState) table = game.table + logger = table.logger table.winners.declared && return set_state!(table, state) print_game_state(table, state) @@ -166,16 +171,16 @@ function act_generic!(game::Game, state::AbstractGameState) play_out_game(table) && return set_play_out_game!(table) for (i, player) in enumerate(circle(table, FirstToAct())) - @debug "Checking to see if it's $(name(player))'s turn to act" - @debug " not_playing(player) = $(not_playing(player))" - @debug " all_in(player) = $(all_in(player))" + @cdebug logger "Checking to see if it's $(name(player))'s turn to act" + @cdebug logger " not_playing(player) = $(not_playing(player))" + @cdebug logger " all_in(player) = $(all_in(player))" not_playing(player) && continue # skip players not playing set_preflop_blind_raise!(table, player, state, i) if end_of_actions(table, player) break end all_in(player) && continue - @debug "$(name(player))'s turn to act" + @cdebug logger "$(name(player))'s turn to act" player_option!(game, player) table.winners.declared && break end_preflop_actions(table, player, state) && break @@ -183,7 +188,7 @@ function act_generic!(game::Game, state::AbstractGameState) error("Too many actions have occured, please open an issue.") end end - @info "Betting is finished." + @cinfo logger "Betting is finished." @assert all_raises_were_called(table) end @@ -200,7 +205,8 @@ expects no cards (players and table) to be dealt. """ function play!(game::Game) - @info "------ Playing game!" + logger = game.table.logger + @cinfo logger "------ Playing game!" table = game.table set_active_status!(table) @@ -209,12 +215,12 @@ function play!(game::Game) initial_brs = deepcopy(bank_roll.(players)) initial_∑brs = sum(initial_brs) - @info "Initial bank roll summary: $(bank_roll.(players))" + @cinfo logger "Initial bank roll summary: $(bank_roll.(players))" did = dealer_id(table) sb = seat_number(small_blind(table)) bb = seat_number(big_blind(table)) f2a = seat_number(first_to_act(table)) - @info "Buttons (dealer, small, big, 1ˢᵗToAct): ($did, $sb, $bb, $f2a)" + @cinfo logger "Buttons (dealer, small, big, 1ˢᵗToAct): ($did, $sb, $bb, $f2a)" @assert still_playing(dealer(table)) "The button must be placed on a non-folded player" @assert still_playing(small_blind(table)) "The small blind button must be placed on a non-folded player" @@ -240,22 +246,22 @@ function play!(game::Game) winners.declared || act!(game, Turn()) # Deal turn , then bet/check/raise winners.declared || act!(game, River()) # Deal river, then bet/check/raise - winners.players = distribute_winnings!(players, table.transactions, cards(table)) + winners.players = distribute_winnings!(players, table.transactions, cards(table), logger) winners.declared = true - @debug "amount.(table.transactions.side_pots) = $(amount.(table.transactions.side_pots))" - @debug "initial_∑brs = $(initial_∑brs)" - @debug "sum(bank_roll.(players_at_table(table))) = $(sum(bank_roll.(players_at_table(table))))" - @debug "initial_brs = $(initial_brs)" - @debug "bank_roll.(players_at_table(table)) = $(bank_roll.(players_at_table(table)))" + @cdebug logger "amount.(table.transactions.side_pots) = $(amount.(table.transactions.side_pots))" + @cdebug logger "initial_∑brs = $(initial_∑brs)" + @cdebug logger "sum(bank_roll.(players_at_table(table))) = $(sum(bank_roll.(players_at_table(table))))" + @cdebug logger "initial_brs = $(initial_brs)" + @cdebug logger "bank_roll.(players_at_table(table)) = $(bank_roll.(players_at_table(table)))" @assert initial_∑brs ≈ sum(bank_roll.(players_at_table(table))) # eventual assertion @assert sum(amount.(table.transactions.side_pots)) ≈ 0 - @info "Final bank roll summary: $(bank_roll.(players))" + @cinfo logger "Final bank roll summary: $(bank_roll.(players))" @assert winners.declared - @info "------ Finished game!" + @cinfo logger "------ Finished game!" return winners end @@ -276,13 +282,15 @@ end function reset_game!(game::Game) table = game.table + logger = table.logger players = players_at_table(table) game.table = Table(; deck=ordered_deck(), players=players, dealer_id=dealer_id(table), - blinds=table.blinds + blinds=table.blinds, + logger=logger, ) table = game.table players = players_at_table(table) @@ -306,9 +314,10 @@ end Play until a single player remains! """ function tournament!(game::Game) - @info "********************************" - @info "******************************** Playing tournament!" - @info "********************************" + logger = game.table.logger + @cinfo logger "********************************" + @cinfo logger "******************************** Playing tournament!" + @cinfo logger "********************************" table = game.table players = players_at_table(table) while length(players) > 1 @@ -321,9 +330,9 @@ function tournament!(game::Game) reset_game!(game) move_buttons!(game) end - @info "********************************" - @info "******************************** Finished tournament!" - @info "********************************" + @cinfo logger "********************************" + @cinfo logger "******************************** Finished tournament!" + @cinfo logger "********************************" return game.table.winners end diff --git a/src/player_actions.jl b/src/player_actions.jl index fbfa8f4e..0eacc11f 100644 --- a/src/player_actions.jl +++ b/src/player_actions.jl @@ -25,7 +25,8 @@ function fold!(game::Game, player::Player) player.action_required = false player.folded = true check_for_and_declare_winner!(game.table) - @info "$(name(player)) folded!" + logger = game.table.logger + @cinfo logger "$(name(player)) folded!" end ##### @@ -36,7 +37,8 @@ function check!(game::Game, player::Player) push!(player.action_history, Check()) player.action_required = false player.checked = true - @info "$(name(player)) checked!" + logger = game.table.logger + @cinfo logger "$(name(player)) checked!" end ##### @@ -47,7 +49,8 @@ function call_amount(table::Table, player::Player) cra = current_raise_amt(table) prc = round_contribution(player) call_amt = cra - prc - @debug "cra = $cra, prc = $prc, call_amt = $call_amt" + logger = table.logger + @cdebug logger "cra = $cra, prc = $prc, call_amt = $call_amt" if cra ≈ 0 @assert prc ≈ 0 "Round contribution must be zero if current raise is zero." end @@ -67,16 +70,17 @@ function call!(table::Table, player::Player) end function call_valid_amount!(table::Table, player::Player, amt::Real) - @debug "$(name(player)) calling $(amt)." + logger = table.logger + @cdebug logger "$(name(player)) calling $(amt)." push!(player.action_history, Call(amt)) player.action_required = false player.checked = false blind_str = is_blind_call(table, player, amt) ? " (blind)" : "" contribute!(table, player, amt, true) if all_in(player) - @info "$(name(player)) called $(amt)$blind_str (now all-in)." + @cinfo logger "$(name(player)) called $(amt)$blind_str (now all-in)." else - @info "$(name(player)) called $(amt)$blind_str." + @cinfo logger "$(name(player)) called $(amt)$blind_str." end end @@ -140,10 +144,11 @@ function valid_raise_bounds(table::Table, player::Player) mra = minimum_raise_amt(table) rbr = round_bank_roll(player) max_orbr = max_opponent_round_bank_roll(table, player) - @debug "determining valid_raise_bounds" - @debug " rbr = $rbr, max_orbr = $max_orbr" - @debug " cra ≈ 0 = $(cra ≈ 0)" - @debug " max_orbr > rbr = $(max_orbr > rbr)" + logger = table.logger + @cdebug logger "determining valid_raise_bounds" + @cdebug logger " rbr = $rbr, max_orbr = $max_orbr" + @cdebug logger " cra ≈ 0 = $(cra ≈ 0)" + @cdebug logger " max_orbr > rbr = $(max_orbr > rbr)" lim = cra ≈ 0 ? mra : (cra+irra) vrb = custom_clamp(min(max_orbr, rbr), lim) @assert vrb[2] ≥ vrb[1] "Min valid raise bound must be ≤ max valid raise bound." @@ -159,10 +164,11 @@ A `Tuple` of two elements: - A `String`, `msg`, of the error message (`msg` = "" if `is_valid = true`). """ function is_valid_raise_amount(table::Table, player::Player, amt::Real) + logger = table.logger prc = round_contribution(player) rbr = round_bank_roll(player) vrb = valid_raise_bounds(table, player) - @debug "vrb = $vrb, amt = $amt, prc = $prc" + @cdebug logger "vrb = $vrb, amt = $amt, prc = $prc" @assert !(vrb[1] == vrb[2] ≈ 0) "Cannot raise 0." if amt ≈ 0 return false, "Cannot raise $amt. Raise must be between [\$$(vrb[1]), \$$(vrb[2])]" @@ -233,8 +239,9 @@ function opponents_being_put_all_in(table::Table, player::Player, amt::Real) end function raise_to_valid_raise_amount!(table::Table, player::Player, amt::Real) + logger = table.logger pbpai = opponents_being_put_all_in(table, player, amt) - @debug "$(name(player)) raising to $(amt)." + @cdebug logger "$(name(player)) raising to $(amt)." prc = round_contribution(player) contribute!(table, player, amt - prc, false) table.current_raise_amt = amt @@ -260,15 +267,15 @@ function raise_to_valid_raise_amount!(table::Table, player::Player, amt::Real) end if bank_roll(player) ≈ 0 if isempty(pbpai) - @info "$(name(player)) raised to $(amt) (all-in)." + @cinfo logger "$(name(player)) raised to $(amt) (all-in)." else - @info "$(name(player)) raised to $(amt). Puts player(s) $(join(pbpai, ", ")) all-in." + @cinfo logger "$(name(player)) raised to $(amt). Puts player(s) $(join(pbpai, ", ")) all-in." end else if isempty(pbpai) - @info "$(name(player)) raised to $(amt)." + @cinfo logger "$(name(player)) raised to $(amt)." else - @info "$(name(player)) raised to $(amt). Puts player(s) $(join(pbpai, ", ")) all-in." + @cinfo logger "$(name(player)) raised to $(amt). Puts player(s) $(join(pbpai, ", ")) all-in." end end end diff --git a/src/player_options.jl b/src/player_options.jl index ea97ced7..505b7aba 100644 --- a/src/player_options.jl +++ b/src/player_options.jl @@ -32,6 +32,7 @@ validate_action(::Raise, ::CallAllInFold) = nothing validate_action(::Raise, ::CallFold) = error("Cannot Raise. Available options: CallFold") function player_option!(game::Game, player::Player) + logger = game.table.logger table = game.table call_amt = call_amount(table, player) if !(call_amt ≈ 0) # must call to stay in @@ -51,7 +52,7 @@ function player_option!(game::Game, player::Player) else option = CheckRaiseFold() end - @debug "option = $option" + @cdebug logger "option = $option" n_actions = length(action_history(player)) diff --git a/src/table.jl b/src/table.jl index 1f77b1f6..7e4cd815 100644 --- a/src/table.jl +++ b/src/table.jl @@ -50,7 +50,7 @@ buttons(b::Buttons) = ( b.first_to_act, ) -mutable struct Table{P} +mutable struct Table{P,L} deck::PlayingCards.Deck players::P cards::Union{Nothing,Tuple{<:Card,<:Card,<:Card,<:Card,<:Card}} @@ -64,6 +64,7 @@ mutable struct Table{P} winners::Winners play_out_game::Bool n_max_actions::Int + logger::L end buttons(table::Table) = table.buttons @@ -108,6 +109,7 @@ function Table(; transactions = nothing, winners = Winners(), play_out_game = false, + logger = StandardLogger(), ) P = typeof(players) if transactions == nothing @@ -115,8 +117,9 @@ function Table(; end buttons = Buttons(dealer_id, players) n_max_actions = compute_n_max_actions(players, blinds.big) - @debug "n_max_actions = $n_max_actions" - return Table{P}(deck, + @cdebug logger "n_max_actions = $n_max_actions" + L = typeof(logger) + return Table{P,L}(deck, players, cards, blinds, @@ -128,7 +131,8 @@ function Table(; transactions, winners, play_out_game, - n_max_actions) + n_max_actions, + logger) end function Buttons(dealer_id, players::Tuple) @@ -305,7 +309,8 @@ blinds(table::Table) = table.blinds function is_blind_call(table::Table, player::Player, amt = call_amount(table, player)) pot_inv = pot_investment(player) - @debug "amt = $amt, pot_investment(player) = $pot_inv" + logger = table.logger + @cdebug logger "amt = $amt, pot_investment(player) = $pot_inv" bb = blinds(table).big sb = blinds(table).small if is_small_blind(table, player) @@ -472,6 +477,7 @@ function deal!(table::Table, blinds::Blinds) players = players_at_table(table) shuffle!(table.deck) call_blinds = true + logger = table.logger for (i, player) in enumerate(circle(table, SmallBlind())) i>length(players) && break # deal cards to each player once @@ -482,24 +488,24 @@ function deal!(table::Table, blinds::Blinds) if is_small_blind(table, player) && bank_roll(player) ≤ blinds.small contribute!(table, player, bank_roll(player), call_blinds) - @info "$(name(player)) paid the small blind (all-in) and dealt cards: $(show_cards(table, player))" + @cinfo logger "$(name(player)) paid the small blind (all-in) and dealt cards: $(show_cards(table, player))" elseif is_big_blind(table, player) && bank_roll(player) ≤ blinds.big contribute!(table, player, bank_roll(player), call_blinds) - @info "$(name(player)) paid the big blind (all-in) and dealt cards: $(show_cards(table, player))" + @cinfo logger "$(name(player)) paid the big blind (all-in) and dealt cards: $(show_cards(table, player))" else if is_small_blind(table, player) contribute!(table, player, blinds.small, call_blinds) - @info "$(name(player)) paid the small blind and dealt cards: $(show_cards(table, player))" + @cinfo logger "$(name(player)) paid the small blind and dealt cards: $(show_cards(table, player))" elseif is_big_blind(table, player) contribute!(table, player, blinds.big, call_blinds) - @info "$(name(player)) paid the big blind and dealt cards: $(show_cards(table, player))" + @cinfo logger "$(name(player)) paid the big blind and dealt cards: $(show_cards(table, player))" else - @info "$(name(player)) dealt (free) cards: $(show_cards(table, player))" + @cinfo logger "$(name(player)) dealt (free) cards: $(show_cards(table, player))" end end end table.cards = get_table_cards!(table.deck) - @info "Table cards dealt (face-down)." + @cinfo logger "Table cards dealt (face-down)." end diff --git a/src/transactions.jl b/src/transactions.jl index f0abf03d..d99ee922 100644 --- a/src/transactions.jl +++ b/src/transactions.jl @@ -66,10 +66,11 @@ cap(tm::TransactionManager) = cap(tm.side_pots[tm.pot_id[1]]) function last_action_of_round(table, player, call) laor = all_oppononents_all_in(table, player) || (count(action_required.(players_at_table(table))) == 0 && call) - @debug "last_action_of_round = $laor" - # @debug " action_required.(players_at_table(table)) = $(action_required.(players_at_table(table)))" - # @debug " all_oppononents_all_in(table, player) = $(all_oppononents_all_in(table, player))" - # @debug " call = $call" + logger = table.logger + @cdebug logger "last_action_of_round = $laor" + # @cdebug logger " action_required.(players_at_table(table)) = $(action_required.(players_at_table(table)))" + # @cdebug logger " all_oppononents_all_in(table, player) = $(all_oppononents_all_in(table, player))" + # @cdebug logger " call = $call" return laor end @@ -107,7 +108,8 @@ of the (sorted) players at the start of the game. """ function contribute!(table, player, amt, call=false) tm = table.transactions - @debug "$(name(player))'s bank roll (pre-contribute) = \$$(bank_roll(player))" + logger = table.logger + @cdebug logger "$(name(player))'s bank roll (pre-contribute) = \$$(bank_roll(player))" if !(0 ≤ amt ≤ bank_roll(player)) msg1 = "$(name(player)) has insufficient bank" msg2 = "roll (\$$(bank_roll(player))) to add \$$amt to pot." @@ -119,9 +121,9 @@ function contribute!(table, player, amt, call=false) player.round_contribution += amt player.pot_investment += amt amt_remaining = amt - @debug "$(name(player)) contributing $amt to side-pots" - @debug "caps = $(cap.(tm.side_pots))" - @debug "$(name(player))'s pot_investment = $(player.pot_investment)" + @cdebug logger "$(name(player)) contributing $amt to side-pots" + @cdebug logger "caps = $(cap.(tm.side_pots))" + @cdebug logger "$(name(player))'s pot_investment = $(player.pot_investment)" for i in 1:length(tm.side_pots) @assert 0 ≤ amt_remaining @@ -131,8 +133,8 @@ function contribute!(table, player, amt, call=false) amt_contrib = cond ? amt_remaining : cap_i contributing = !side_pot_full(tm, i) && !(cap_i ≈ 0) # This is a bit noisy: - # @debug "$(name(player)) potentially contributing $amt_contrib to side-pot $(i) ($cond). cap_i=$cap_i, amt_remaining=$amt_remaining" - @debug "contributing = $contributing" + # @cdebug logger "$(name(player)) potentially contributing $amt_contrib to side-pot $(i) ($cond). cap_i=$cap_i, amt_remaining=$amt_remaining" + @cdebug logger "contributing = $contributing" contributing || continue @assert !(amt_contrib ≈ 0) tm.side_pots[i].amt += amt_contrib @@ -151,8 +153,8 @@ function contribute!(table, player, amt, call=false) if is_side_pot_full(tm, table, player, call) set_side_pot_full!(tm) end - @debug "$(name(player))'s bank roll (post-contribute) = \$$(bank_roll(player))" - @debug "all_in($(name(player))) = $(all_in(player))" + @cdebug logger "$(name(player))'s bank roll (post-contribute) = \$$(bank_roll(player))" + @cdebug logger "all_in($(name(player))) = $(all_in(player))" end function is_side_pot_full(tm::TransactionManager, table, player, call) @@ -168,7 +170,7 @@ side_pot_full(tm::TransactionManager, i) = i < tm.pot_id[1] sidepot_winnings(tm::TransactionManager, id::Int) = sum(map(x->x.amt, tm.side_pots[1:id])) -function distribute_winnings_1_player_left!(players, tm::TransactionManager, table_cards) +function distribute_winnings_1_player_left!(players, tm::TransactionManager, table_cards, logger) @assert count(still_playing.(players)) == 1 n = length(tm.side_pots) for (player, initial_br) in zip(players, tm.initial_brs) @@ -176,7 +178,7 @@ function distribute_winnings_1_player_left!(players, tm::TransactionManager, tab amt_contributed = initial_br - bank_roll(player) ∑spw = sidepot_winnings(tm, n) prof = ∑spw-amt_contributed - @info "$(name(player)) wins \$$(∑spw) (\$$(prof) profit) (all opponents folded)" + @cinfo logger "$(name(player)) wins \$$(∑spw) (\$$(prof) profit) (all opponents folded)" player.bank_roll += ∑spw for j in 1:n tm.side_pots[j].amt = 0 # empty out distributed winnings @@ -186,11 +188,11 @@ function distribute_winnings_1_player_left!(players, tm::TransactionManager, tab return nothing end -function distribute_winnings!(players, tm::TransactionManager, table_cards) - @debug "Distributing winnings..." - @debug "Pot amounts = $(amount.(tm.side_pots))" +function distribute_winnings!(players, tm::TransactionManager, table_cards, logger=StandardLogger()) + @cdebug logger "Distributing winnings..." + @cdebug logger "Pot amounts = $(amount.(tm.side_pots))" if count(still_playing.(players)) == 1 - return distribute_winnings_1_player_left!(players, tm, table_cards) + return distribute_winnings_1_player_left!(players, tm, table_cards, logger) end hand_evals_sorted = map(enumerate(tm.sorted_players)) do (ssn, player) fhe = inactive(player) ? nothing : FullHandEval((player.cards..., table_cards...)) @@ -214,7 +216,7 @@ function distribute_winnings!(players, tm::TransactionManager, table_cards) (; eligible=false, player=player, fhe=fhe, ssn=ssn) end end - @debug begin + @cdebug logger begin s = "Sorted hand evals: \n" for hes in hand_evals_sorted inactive(hes.player) && continue # (don't have cards to eval) @@ -232,10 +234,10 @@ function distribute_winnings!(players, tm::TransactionManager, table_cards) x.eligible ? hand_rank(x.fhe)==all_valid_min_hrs : false end n_winners = length(winner_ids) - @debug "winner_ids = $(winner_ids)" - @debug "length(players) = $(length(players))" - @debug "length(hand_evals_sorted) = $(length(hand_evals_sorted))" - @debug "length(tm.sorted_players) = $(length(tm.sorted_players))" + @cdebug logger "winner_ids = $(winner_ids)" + @cdebug logger "length(players) = $(length(players))" + @cdebug logger "length(hand_evals_sorted) = $(length(hand_evals_sorted))" + @cdebug logger "length(tm.sorted_players) = $(length(tm.sorted_players))" for winner_id in winner_ids win_seat = seat_number(tm.sorted_players[winner_id]) winning_player = players[win_seat] @@ -258,20 +260,20 @@ function distribute_winnings!(players, tm::TransactionManager, table_cards) hand_name = hand_type(hand) bc = best_cards(hand) f_str = folded(player) ? " (folded)" : "" - @info "$(name(player)) loses$f_str \$$(pot_investment(player)) with $bc ($hand_name)!" + @cinfo logger "$(name(player)) loses$f_str \$$(pot_investment(player)) with $bc ($hand_name)!" end else hand = hand_evals_sorted[ssn].fhe hand_name = hand_type(hand) bc = best_cards(hand) amt_contributed = initial_br - bank_roll(player) - @debug "$(name(player))'s side-pot wins: \$$(player_winnings)!" + @cdebug logger "$(name(player))'s side-pot wins: \$$(player_winnings)!" prof = ∑spw-amt_contributed - @info "$(name(player)) wins \$$∑spw (\$$prof profit) with $bc ($hand_name)!" + @cinfo logger "$(name(player)) wins \$$∑spw (\$$prof profit) with $bc ($hand_name)!" player.bank_roll += ∑spw end end - @debug "Distributed winnings..." + @cdebug logger "Distributed winnings..." return Tuple(all_winning_players) end diff --git a/test/runtests.jl b/test/runtests.jl index 7db0eb1e..6e2544c0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using Logging +import Logging metafmt(level, _module, group, id, file, line) = Logging.default_metafmt(level, nothing, group, id, nothing, nothing) @@ -27,12 +27,12 @@ for submodule in submodules t = 0 local logger if any(submodule in tests_to_debug) - logger = ConsoleLogger(stderr,Logging.Info) + logger = Logging.ConsoleLogger(stderr,Logging.Info) # logger = ConsoleLogger(stderr,Logging.Debug;meta_formatter=metafmt) else - logger = NullLogger() + logger = Logging.NullLogger() end - with_logger(logger) do + Logging.with_logger(logger) do t = @elapsed include(submodule*".jl") end println("Completed tests for $submodule, $(round(Int, t)) seconds elapsed") diff --git a/test/table.jl b/test/table.jl index 8eb860f2..21509237 100644 --- a/test/table.jl +++ b/test/table.jl @@ -1,6 +1,6 @@ using Test using PlayingCards -using Logging +import Logging using TexasHoldem using TexasHoldem: seat_number TH = TexasHoldem