Class: PokerEngine::StateOperations
- Inherits:
-
Object
- Object
- PokerEngine::StateOperations
- Defined in:
- lib/poker_engine/state_operations.rb
Constant Summary collapse
- CARDS_COUNT_PER_STAGE_START =
{ flop: 3, turn: 1, river: 1 }.freeze
- STAGES =
%i(preflop flop turn river).freeze
- NO_ACTIVE_FILTER =
Object.new
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #active_players ⇒ Object
- #first_player_id ⇒ Object
-
#initialize(state) ⇒ StateOperations
constructor
A new instance of StateOperations.
- #next_player_id ⇒ Object
- #next_stage ⇒ Object
- #next_stage? ⇒ Boolean
- #one_player_left? ⇒ Boolean
- #ordered_player_ids(active: NO_ACTIVE_FILTER) ⇒ Object
- #player_id_by(position:) ⇒ Object
- #stage_cards_count ⇒ Object
Constructor Details
#initialize(state) ⇒ StateOperations
Returns a new instance of StateOperations.
8 9 10 |
# File 'lib/poker_engine/state_operations.rb', line 8 def initialize(state) @state = state end |
Instance Attribute Details
#state ⇒ Object (readonly)
Returns the value of attribute state.
6 7 8 |
# File 'lib/poker_engine/state_operations.rb', line 6 def state @state end |
Instance Method Details
#active_players ⇒ Object
41 42 43 |
# File 'lib/poker_engine/state_operations.rb', line 41 def active_players players.select { |_, player| player[:active] } end |
#first_player_id ⇒ Object
58 59 60 |
# File 'lib/poker_engine/state_operations.rb', line 58 def first_player_id ordered_player_ids(active: true).first end |
#next_player_id ⇒ Object
28 29 30 31 32 33 34 35 |
# 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 ⇒ Object
20 21 22 |
# File 'lib/poker_engine/state_operations.rb', line 20 def next_stage STAGES.fetch STAGES.index(state[:current_stage]) + 1 end |
#next_stage? ⇒ Boolean
16 17 18 |
# File 'lib/poker_engine/state_operations.rb', line 16 def next_stage? state[:current_stage] != :river end |
#one_player_left? ⇒ Boolean
37 38 39 |
# 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) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# 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:) ⇒ Object
12 13 14 |
# 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 ⇒ Object
24 25 26 |
# File 'lib/poker_engine/state_operations.rb', line 24 def stage_cards_count StateOperations::CARDS_COUNT_PER_STAGE_START.fetch state[:current_stage] end |