Module: PokerEngine::Reducer::Actions

Defined in:
lib/poker_engine/reducer.rb

Class Method Summary collapse

Class Method Details

.call(state, **action) ⇒ Object



56
57
58
59
60
61
62
# 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) ⇒ Object



71
72
73
74
75
# 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) ⇒ Object



41
42
43
44
45
46
47
48
# 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) ⇒ Object



32
33
34
35
36
37
38
39
# 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) ⇒ Object



77
78
79
80
81
82
83
# 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) ⇒ Object



94
95
96
97
98
99
# 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) ⇒ Object



20
21
22
# File 'lib/poker_engine/reducer.rb', line 20

def game_start(state, **_action)
  state
end

.move_request(state, player_id:, **_action) ⇒ Object



50
51
52
53
54
# 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) ⇒ Object



85
86
87
88
89
90
91
92
# 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) ⇒ Object



64
65
66
67
68
69
# 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) ⇒ Object



28
29
30
# 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) ⇒ Object



24
25
26
# File 'lib/poker_engine/reducer.rb', line 24

def take_small_blind(state, **action)
  take_blind state, action.merge(blind_kind: :small_blind)
end