Skip to content

Commit

Permalink
Merge #139
Browse files Browse the repository at this point in the history
139: Define reset for transactions r=charleskawczynski a=charleskawczynski

This PR adds `reset!` for transactions, to avoid calling the default constructor.

Co-authored-by: Charles Kawczynski <kawczynski.charles@gmail.com>
  • Loading branch information
bors[bot] and charleskawczynski committed Jul 18, 2023
2 parents 263c805 + f91dd69 commit ff60127
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/game.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function play!(game::Game)
@assert small_blind_pidx(table) big_blind_pidx(table) "The small and big blinds cannot coincide"
@assert big_blind_pidx(table) first_to_act_pidx(table) "The big blind and first to act cannot coincide"

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

@assert all(p->cards(p) == nothing, players)
@assert cards(table) == nothing
Expand Down
8 changes: 7 additions & 1 deletion src/players.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ Base.iterate(players::Players, ncpidx = 1) =
Base.@propagate_inbounds Base.getindex(players::Players, i::Int) =
Base.getindex(players.players, i)
Base.filter(fn, players::Players) = Base.filter(fn, players.players)
Base.findfirst(fn::Function, players::Players) =
Base.findfirst(fn, players.players)

sorted(players::Players) =
function Base.sort!(players::Players{<:AbstractArray})
Base.sort!(players.players; by=x->bank_roll(x))
return nothing
end
sorted_collect(players::Players) =
Players(sort(collect(players.players); by = x->bank_roll(x)))
n_players(players::Players) = length(players)

Expand Down
30 changes: 26 additions & 4 deletions src/transactions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ end

TransactionManager(players) = TransactionManager(Players(players))
function TransactionManager(players::Players)
# sorted_players = sort(collect(players); by = x->bank_roll(x))
sorted_players = sorted(players)
sorted_players = sorted_collect(players)

cap = zeros(length(players))
for i in 1:length(players)
Expand All @@ -55,15 +54,38 @@ function TransactionManager(players::Players)
end)
side_pots = [SidePot(seat_number(sp), 0, cap_i) for (cap_i, sp) in zip(cap, sorted_players)]

initial_brs = deepcopy(collect(bank_roll.(players)))
pot_id = Int[1]
SP = typeof(sorted_players)
TransactionManager{SP}(
sorted_players,
deepcopy(collect(bank_roll.(players))),
Int[1],
initial_brs,
pot_id,
side_pots,
unsorted_to_sorted_map,
)
end

function reset!(tm::TransactionManager, players::Players)
splayers = tm.sorted_players
sort!(splayers)
@inbounds for i in 1:length(splayers)
sp = splayers[i]
player = players[i]
br = bank_roll(sp)
cap_i = i == 1 ? br : br - bank_roll(splayers[i-1])
ssn = seat_number(sp)::Int
tm.side_pots[i].seat_number = ssn
tm.side_pots[i].amt = Float64(0)
tm.side_pots[i].cap = cap_i
j = findfirst(sp->seat_number(sp) == seat_number(player), splayers)::Int
tm.unsorted_to_sorted_map[i] = j
tm.initial_brs[i] = bank_roll(player)
end
@inbounds tm.pot_id[1] = 1
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 ff60127

Please sign in to comment.