Class: MatchSlice

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/match_slice.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_in(state, game_def) ⇒ Object

Over round



152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/match_slice.rb', line 152

def self.all_in(state, game_def)
  return 0 if state.hand_ended?(game_def)

  (
    state.players(game_def)[state.next_to_act(game_def)].stack +
    (
      state.players(game_def)[state.next_to_act(game_def)]
        .contributions[state.round] || 0
    )
  ).floor
end

.amount_to_call(state, game_def) ⇒ Object



164
165
166
167
168
# File 'app/models/match_slice.rb', line 164

def self.amount_to_call(state, game_def)
  return 0 if state.next_to_act(game_def).nil?

  state.players(game_def).amount_to_call(state.next_to_act(game_def))
end

.betting_sequence(match_state, game_def) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/match_slice.rb', line 55

def self.betting_sequence(match_state, game_def)
  sequence = ''
  match_state.betting_sequence(game_def).each_with_index do |actions_per_round, round|
    actions_per_round.each_with_index do |action, action_index|
      adjusted_action = adjust_action_amount(
        action,
        round,
        match_state,
        game_def
      )

      sequence << if (
        match_state.player_acting_sequence(game_def)[round][action_index].to_i ==
        match_state.position_relative_to_dealer
      )
        adjusted_action.capitalize
      else
        adjusted_action
      end
    end
    sequence << '/' unless round == match_state.betting_sequence(game_def).length - 1
  end
  sequence
end

.chip_contribution_after_calling(state, game_def) ⇒ Object

Over round



132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/match_slice.rb', line 132

def self.chip_contribution_after_calling(state, game_def)
  return 0 unless state.next_to_act(game_def)

  (
    (
      state.players(game_def)[
        state.next_to_act(game_def)
      ].contributions[state.round] || 0
    ) + amount_to_call(state, game_def)
  )
end

.from_players_at_the_table!(patt, match_has_ended, match) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/match_slice.rb', line 27

def self.from_players_at_the_table!(patt, match_has_ended, match)
  match.slices.create!(
    hand_has_ended: patt.hand_ended?,
    match_has_ended: match_has_ended,
    seat_with_dealer_button: patt.dealer_player.seat.to_i,
    seat_next_to_act: if patt.next_player_to_act
      patt.next_player_to_act.seat.to_i
    end,
    state_string: patt.match_state.to_s,
    # Not necessary to be in the database, but more performant than processing on the
    # Rails server
    betting_sequence: betting_sequence(patt.match_state, patt.game_def),
    pot_at_start_of_round: pot_at_start_of_round(patt.match_state, patt.game_def).to_i,
    players: players(patt, match.player_names),
    minimum_wager_to: minimum_wager_to(patt.match_state, patt.game_def).to_i,
    chip_contribution_after_calling: chip_contribution_after_calling(patt.match_state, patt.game_def).to_i,
    pot_after_call: pot_after_call(patt.match_state, patt.game_def).to_i,
    all_in: all_in(patt.match_state, patt.game_def).to_i,
    is_users_turn_to_act: patt.users_turn_to_act?,
    legal_actions: patt.legal_actions.map { |action| action.to_s },
    amount_to_call: amount_to_call(patt.match_state, patt.game_def).to_i
  )
end

.minimum_wager_to(state, game_def) ⇒ Object

Over round



122
123
124
125
126
127
128
129
# File 'app/models/match_slice.rb', line 122

def self.minimum_wager_to(state, game_def)
  return 0 unless state.next_to_act(game_def)

  (
    state.min_wager_by(game_def) +
    chip_contribution_after_calling(state, game_def)
  ).ceil
end

.players(patt, player_names) ⇒ Array<Hash>

Each player hash should contain values for the following keys: ‘name’, ‘seat’ ‘chip_stack’ ‘chip_contributions’ ‘chip_balance’ ‘hole_cards’ ‘winnings’

Returns:

  • (Array<Hash>)

    Player information ordered by seat.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/match_slice.rb', line 98

def self.players(patt, player_names)
  player_names_queue = player_names.dup
  patt.players.map do |player|
    hole_cards = if !(player.hand.empty? || player.folded?)
      player.hand.to_acpc
    elsif player.folded?
      ''
    else
      '_' * patt.game_def.number_of_hole_cards
    end

    {
      'name' => player_names_queue.shift,
      'seat' => player.seat,
      'chip_stack' => player.stack.to_i,
      'chip_contributions' => player.contributions.map { |contrib| contrib.to_i },
      'chip_balance' => player.balance,
      'hole_cards' => hole_cards,
      'winnings' => player.winnings.to_f
    }
  end
end

.pot_after_call(state, game_def) ⇒ Object

Over round



145
146
147
148
149
# File 'app/models/match_slice.rb', line 145

def self.pot_after_call(state, game_def)
  return state.pot(game_def) if state.hand_ended?(game_def)

  state.pot(game_def) + state.players(game_def).amount_to_call(state.next_to_act(game_def))
end

.pot_at_start_of_round(match_state, game_def) ⇒ Object



80
81
82
83
84
85
86
# File 'app/models/match_slice.rb', line 80

def self.pot_at_start_of_round(match_state, game_def)
  return 0 if match_state.round == 0

  match_state.players(game_def).inject(0) do |sum, pl|
    sum += pl.contributions[0..match_state.round - 1].inject(:+)
  end
end

Instance Method Details

#hand_ended?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'app/models/match_slice.rb', line 170

def hand_ended?
  hand_has_ended
end

#match_ended?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'app/models/match_slice.rb', line 174

def match_ended?
  match_has_ended
end

#users_turn_to_act?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/models/match_slice.rb', line 51

def users_turn_to_act?
  is_users_turn_to_act
end