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

TODO:

Reduce the # of params

Returns a new instance of Proxy.

Parameters:

  • match_id (String)

    The ID of the match in which this player is participating.

  • dealer_information (DealerInformation)

    Information about the dealer to which this bot should connect.

  • game_definition (GameDefinition, #to_s)

    A game definition; either a GameDefinition or the name of the file containing a game definition.

  • player_names (String) (defaults to: 'user p2')

    The names of the players in this match.

  • number_of_hands (Integer) (defaults to: 1)

    The number of hands in this match.



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

Returns:

  • (Boolean)

See Also:

  • PlayerProxy#match_ended?


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

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) ⇒ 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)
  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

    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
# 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
    )
  end
  self
end