Skip to content

Commit

Permalink
Merge #118
Browse files Browse the repository at this point in the history
118: Fix some jet failures r=charleskawczynski a=charleskawczynski

This PR fixes a bunch of JET failures, and the benchmarks correspondingly have improved a bit (~60μm -> ~55μm)

Co-authored-by: Charles Kawczynski <kawczynski.charles@gmail.com>
  • Loading branch information
bors[bot] and charleskawczynski committed Jul 15, 2023
2 parents cbdb0b9 + 674224a commit a341fd4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
4 changes: 2 additions & 2 deletions perf/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ players() = ntuple(i->(Player(BotCheckCall(), i)), 4)
# the creation of players and games:
const cntr = Int[1]
function do_work!(games)
with_logger(NullLogger()) do
Logging.with_logger(Logging.NullLogger()) do
play!(games[cntr[1]])
cntr[]+=1
end
Expand All @@ -34,7 +34,7 @@ show(stdout, MIME("text/plain"), trial)
# Also benchmark including the
# creation of players and games:
function do_work!()
with_logger(NullLogger()) do
Logging.with_logger(Logging.NullLogger()) do
play!(Game(players()))
end
return nothing
Expand Down
2 changes: 1 addition & 1 deletion src/game.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function play!(game::Game)
@assert small_blind_id(table) big_blind_id(table) "The small and big blinds cannot coincide"
@assert big_blind_id(table) first_to_act_id(table) "The big blind and first to act cannot coincide"

table.transactions = TransactionManager(players)
reset!(table.transactions, players)

@assert all(cards.(players) .== nothing)
@assert cards(table) == nothing
Expand Down
46 changes: 28 additions & 18 deletions src/transactions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ cap(sp::SidePot) = sp.cap
Handle pots and side pots
among multiple players.
"""
struct TransactionManager
sorted_players::Vector{Player}
struct TransactionManager{T}
sorted_players::T
initial_brs::Vector{Float64}
pot_id::Union{Nothing,Vector{Int}}
pot_id::Vector{Int}
side_pots::Vector{SidePot}
unsorted_to_sorted_map::Vector{Int}
end
Expand All @@ -38,29 +38,39 @@ end

function TransactionManager(players)
sorted_players = sort(collect(players); by = x->bank_roll(x))

cap = zeros(length(players))
for i in 1:length(players)
if i == 1
cap[i] = bank_roll(sorted_players[i])
else
cap[i] = bank_roll(sorted_players[i]) - sum(cap[1:i-1])
end
end

sps = sorted_players
unsorted_to_sorted_map = collect(map(players) do player
findfirst(seat_number.(sorted_players) .== seat_number(player))
findfirst(sp->seat_number(sp) == seat_number(player), sps)::Int
end)
side_pots = [SidePot(sn, 0, cap_i) for (cap_i, sn, amt) in zip(cap, seat_number.(sorted_players), bank_roll.(sorted_players))]

TransactionManager(
side_pots = map(enumerate(sps)) do (j, sp)
c = j == 1 ? bank_roll(sp) : bank_roll(sp) - sum(x->bank_roll(x), sps[1:j-1])
SidePot(seat_number(sp), 0, c)
end
initial_brs = collect(map(p->bank_roll(p), players))
T = typeof(sorted_players)
TransactionManager{T}(
sorted_players,
deepcopy(collect(bank_roll.(players))),
initial_brs,
Int[1],
side_pots,
unsorted_to_sorted_map,
)
end
function reset!(tm::TransactionManager, players)
sps = tm.sorted_players
sort!(sps; by = x->bank_roll(x))
tm.unsorted_to_sorted_map .= map(players) do player
findfirst(sp->seat_number(sp) == seat_number(player), sps)::Int
end
tm.side_pots .= map(enumerate(sps)) do (j, sp)
c = j == 1 ? bank_roll(sp) : bank_roll(sp) - sum(x->bank_roll(x), sps[1:j-1])
SidePot(seat_number(sp), 0, c)
end
tm.pot_id[1] = 1
tm.initial_brs .= collect(map(p->bank_roll(p), players))
return nothing
end

amount(tm::TransactionManager) = amount(tm.side_pots[tm.pot_id[1]])
cap(tm::TransactionManager) = cap(tm.side_pots[tm.pot_id[1]])

Expand Down

0 comments on commit a341fd4

Please sign in to comment.