class PokerEngine::StateOperations

Constants

CARDS_COUNT_PER_STAGE_START
NO_ACTIVE_FILTER
STAGES

Attributes

state[R]

Public Class Methods

new(state) click to toggle source
# File lib/poker_engine/state_operations.rb, line 8
def initialize(state)
  @state = state
end

Public Instance Methods

active_players() click to toggle source
# File lib/poker_engine/state_operations.rb, line 41
def active_players
  players.select { |_, player| player[:active] }
end
first_player_id() click to toggle source
# File lib/poker_engine/state_operations.rb, line 58
def first_player_id
  ordered_player_ids(active: true).first
end
next_player_id() click to toggle source
# File lib/poker_engine/state_operations.rb, line 28
def next_player_id
  raise 'Invalid state' if active_players.count.zero?

  ordered_player_ids.cycle.each_with_index.find do |id, order_index|
    order_index > ordered_player_ids.index(state[:current_player_id]) &&
      players[id][:active]
  end.first
end
next_stage() click to toggle source
# File lib/poker_engine/state_operations.rb, line 20
def next_stage
  STAGES.fetch STAGES.index(state[:current_stage]) + 1
end
next_stage?() click to toggle source
# File lib/poker_engine/state_operations.rb, line 16
def next_stage?
  state[:current_stage] != :river
end
one_player_left?() click to toggle source
# File lib/poker_engine/state_operations.rb, line 37
def one_player_left?
  players.count { |_, player| player[:active] } == 1
end
ordered_player_ids(active: NO_ACTIVE_FILTER) click to toggle source
# File lib/poker_engine/state_operations.rb, line 46
def ordered_player_ids(active: NO_ACTIVE_FILTER)
  raise 'Unexpected state' unless state[:current_stage]

  positions = Game::POSITIONS_ORDER[state[:current_stage] == :preflop ? :preflop : :postflop]

  players.each_with_object(Array.new(players.count)) do |(id, player), ordered|
    next if active != NO_ACTIVE_FILTER && active != player[:active]

    ordered[positions.index player[:position]] = id
  end.compact
end
player_id_by(position:) click to toggle source
# File lib/poker_engine/state_operations.rb, line 12
def player_id_by(position:)
  players.find { |_id, player| player[:position] == position }.first
end
stage_cards_count() click to toggle source
# File lib/poker_engine/state_operations.rb, line 24
def stage_cards_count
  StateOperations::CARDS_COUNT_PER_STAGE_START.fetch state[:current_stage]
end

Private Instance Methods

players() click to toggle source
# File lib/poker_engine/state_operations.rb, line 64
def players
  state[:players]
end