module PokerEngine::Reducer::Actions

Public Instance Methods

call(state, **action) click to toggle source
# File lib/poker_engine/reducer.rb, line 56
def call(state, **action)
  bet = state.dig(:players, state[:aggressor_id], :last_move, :bet) || state[:big_blind]
  money_to_give = bet - state.dig(:players, action[:player_id], :money_in_pot)

  pay(state, action.merge(money_to_give: money_to_give))
    .put(:aggressor_id) { |id| id || action[:player_id] } # HACK: try to get rid of it.
end
check(state, player_id:, **_action) click to toggle source
# File lib/poker_engine/reducer.rb, line 71
def check(state, player_id:, **_action)
  state
    .put(:pending_request, false)
    .put(:aggressor_id) { |id| id || player_id } # HACK: try to get rid of it.
end
distribute_to_board(state, cards_count:, **_action) click to toggle source
# File lib/poker_engine/reducer.rb, line 41
def distribute_to_board(state, cards_count:, **_action)
  new_deck, poped_cards =
    state[:deck].partition.with_index { |_, i| i + 1 <= state[:deck].count - cards_count }

  state
    .put(:deck, new_deck)
    .put(:board) { |board| board + poped_cards }
end
distribute_to_player(state, player_id:, **_action) click to toggle source
# File lib/poker_engine/reducer.rb, line 32
def distribute_to_player(state, player_id:, **_action)
  new_deck, poped_cards =
    state[:deck].partition.with_index { |_, i| i + 1 <= state[:deck].count - 2 }

  state
    .put(:deck, new_deck)
    .update_in(:players, player_id, :cards) { poped_cards }
end
fold(state, **action) click to toggle source
# File lib/poker_engine/reducer.rb, line 77
def fold(state, **action)
  state
    .update_in(:players, action[:player_id]) do |player|
      player.put(:active, false).put(:last_move, action)
    end
    .put(:pending_request, false)
end
game_end(state, top_hands: [], winner_ids:, **_action) click to toggle source
# File lib/poker_engine/reducer.rb, line 94
def game_end(state, top_hands: [], winner_ids:, **_action)
  state
    .put(:top_hands, top_hands)
    .put(:winner_ids, winner_ids)
    .put(:game_ended, true)
end
game_start(state, **_action) click to toggle source
# File lib/poker_engine/reducer.rb, line 20
def game_start(state, **_action)
  state
end
move_request(state, player_id:, **_action) click to toggle source
# File lib/poker_engine/reducer.rb, line 50
def move_request(state, player_id:, **_action)
  state
    .put(:current_player_id, player_id)
    .put(:pending_request, true)
end
next_stage(state, stage:, **_action) click to toggle source
# File lib/poker_engine/reducer.rb, line 85
def next_stage(state, stage:, **_action)
  state
    .update_in(:players) do |players|
      players.map { |id, player| [id, player.put(:money_in_pot, 0)] }
    end
    .put(:current_stage, stage)
    .put(:aggressor_id, nil)
end
raise(state, **action) click to toggle source
# File lib/poker_engine/reducer.rb, line 64
def raise(state, **action)
  money_to_give = action[:bet] - state.dig(:players, action[:player_id], :money_in_pot)

  pay(state, action.merge(money_to_give: money_to_give))
    .put(:aggressor_id, action[:player_id])
end
take_big_blind(state, **action) click to toggle source
# File lib/poker_engine/reducer.rb, line 28
def take_big_blind(state, **action)
  take_blind state, action.merge(blind_kind: :big_blind)
end
take_small_blind(state, **action) click to toggle source
# File lib/poker_engine/reducer.rb, line 24
def take_small_blind(state, **action)
  take_blind state, action.merge(blind_kind: :small_blind)
end

Private Instance Methods

pay(state, money_to_give:, **action) click to toggle source

TODO: handle the case when the bet is higher then the current balance

# File lib/poker_engine/reducer.rb, line 114
                     def pay(state, money_to_give:, **action)
  state
    .update_in(:players, action[:player_id]) do |player|
      player
        .put(:balance) { |b| b - money_to_give }
        .put(:money_in_pot) { |mip| mip + money_to_give }
        .put(:last_move, action)
    end
    .put(:pot) { |pot| pot + money_to_give }
    .put(:pending_request, false)
end
take_blind(state, player_id:, blind_kind:, **_action) click to toggle source
# File lib/poker_engine/reducer.rb, line 101
                     def take_blind(state, player_id:, blind_kind:, **_action)
  blind_size = state.fetch blind_kind

  state
    .update_in(:players, player_id) do |player|
      player
        .put(:balance) { |b| b - blind_size }
        .put(:money_in_pot) { |mip| mip + blind_size }
    end
    .put(:pot) { |pot| pot + blind_size }
end