Class: AcpcTableManager::Proxy

Inherits:
Object
  • Object
show all
Includes:
AcpcPokerTypes, SimpleLogging
Defined in:
lib/acpc_table_manager/proxy.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SimpleLogging

#log, #log_with, #logger

Constructor Details

#initialize(match_id, dealer_information, users_seat, game_definition, player_names = 'user p2', number_of_hands = 1, must_send_ready = false) ⇒ Proxy



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/acpc_table_manager/proxy.rb', line 50

def initialize(
  match_id,
  dealer_information,
  users_seat,
  game_definition,
  player_names='user p2',
  number_of_hands=1,
  must_send_ready=false
)
  @logger = AcpcTableManager.new_log File.join('proxies', "#{match_id}.#{users_seat}.log")

  log __method__, {
    dealer_information: dealer_information,
    users_seat: users_seat,
    game_definition: game_definition,
    player_names: player_names,
    number_of_hands: number_of_hands
  }

  @match_id = match_id
  @player_proxy = AcpcPokerPlayerProxy::PlayerProxy.new(
    dealer_information,
    game_definition,
    users_seat,
    must_send_ready
  ) do |players_at_the_table|

    if players_at_the_table.match_state
      update_database! players_at_the_table

      yield players_at_the_table if block_given?
    else
      log __method__, {before_first_match_state: true}
    end
  end
end

Class Method Details

.start(match, must_send_ready = false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/acpc_table_manager/proxy.rb', line 22

def self.start(match, must_send_ready = false)
  game_definition = GameDefinition.parse_file(match.game_definition_file_name)
  match.game_def_hash = game_definition.to_h
  match.save!

  proxy = new(
    match.id,
    AcpcDealer::ConnectionInformation.new(
      match.port_numbers[match.seat - 1],
      ::AcpcTableManager.config.dealer_host
    ),
    match.seat - 1,
    game_definition,
    match.player_names.join(' '),
    match.number_of_hands,
    must_send_ready
  ) do |players_at_the_table|
    yield players_at_the_table if block_given?
  end
end

Instance Method Details

#match_ended?Boolean

See Also:

  • PlayerProxy#match_ended?


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/acpc_table_manager/proxy.rb', line 151

def match_ended?
  return false if @player_proxy.nil?

  @match ||= begin
    Match.find(@match_id)
  rescue Mongoid::Errors::DocumentNotFound
    log(
      __method__,
      {
        msg: "Unable to find match",
        match_id: @match_id,
        match_ended: @player_proxy.match_ended?
      }
    )
    return true
  end

  (
    @player_proxy.match_ended? ||
    (
      @player_proxy.hand_ended? &&
      @player_proxy.match_state.hand_number >= @match.number_of_hands - 1
    )
  )
end

#next_hand!Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/acpc_table_manager/proxy.rb', line 87

def next_hand!
  log __method__

  @player_proxy.next_hand! do |players_at_the_table|
    update_database! players_at_the_table

    yield players_at_the_table if block_given?
  end

  log(
    __method__,
    {
      users_turn_to_act?: @player_proxy.users_turn_to_act?,
      match_ended?: @player_proxy.match_ended?
    }
  )

  self
end

#play!(action, fast_forward = false) ⇒ Object

Player action interface

See Also:

  • PlayerProxy#play!


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/acpc_table_manager/proxy.rb', line 109

def play!(action, fast_forward = false)
  log __method__, action: action

  action = PokerAction.new(action) unless action.is_a?(PokerAction)

  @player_proxy.play! action do |players_at_the_table|
    update_database! players_at_the_table, fast_forward

    yield players_at_the_table if block_given?
  end

  log(
    __method__,
    {
      users_turn_to_act?: @player_proxy.users_turn_to_act?,
      match_ended?: @player_proxy.match_ended?
    }
  )

  self
end

#play_check_fold!Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/acpc_table_manager/proxy.rb', line 131

def play_check_fold!
  log __method__
  if @player_proxy.users_turn_to_act?
    play!(
      if (
        @player_proxy.legal_actions.any? do |a|
          a == AcpcPokerTypes::PokerAction::FOLD
        end
      )
        AcpcPokerTypes::PokerAction::FOLD
      else
        AcpcPokerTypes::PokerAction::CALL
      end,
      true
    )
  end
  self
end