Class: TableManager

Inherits:
Object
  • Object
show all
Includes:
AcpcPokerTypes, Sidekiq::Worker, SimpleLogging, WorkerHelpers
Defined in:
app/workers/table_manager.rb

Constant Summary collapse

DEALER_HOST =
Socket.gethostname

Instance Method Summary collapse

Methods included from SimpleLogging

#log, #logger

Methods included from WorkerHelpers

#delete_state!, #handle_exception, #match_instance, #save_match_instance

Constructor Details

#initializeTableManager

Returns a new instance of TableManager.



51
52
53
54
55
56
57
58
# File 'app/workers/table_manager.rb', line 51

def initialize
  @logger = Logger.from_file_name(
    File.join(
      ApplicationDefs::LOG_DIRECTORY,
      'table_manager.log'
    )
  ).with_metadata!
end

Instance Method Details

#perform(request, match_id, params = nil) ⇒ Object



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
86
87
88
89
90
91
92
# File 'app/workers/table_manager.rb', line 60

def perform(request, match_id, params=nil)
  if request == ApplicationDefs::START_MATCH_REQUEST_CODE
    refresh_module('Bots', File.expand_path('../../../bots/bots.rb', __FILE__), 'bots')
    refresh_module('ApplicationDefs', File.expand_path('../../../lib/application_defs.rb', __FILE__), 'application_defs')
  end
  
  log __method__, table_information_length: @@table_information.length, request: request, match_id: match_id, params: params

  begin
    ->(&block) { block.call match_instance(match_id) }.call do |match|
      case request
      when ApplicationDefs::START_MATCH_REQUEST_CODE
        start_dealer!(params, match)

        opponents = []
        match.every_bot(DEALER_HOST) do |bot_command|
          opponents << bot_command
        end

        start_opponents!(opponents).start_proxy!(match)
      when ApplicationDefs::START_PROXY_REQUEST_CODE
        start_proxy! match
      when ApplicationDefs::PLAY_ACTION_REQUEST_CODE
        play! params, match
      else
        log __method__, message: "Unrecognized request", request: params[ApplicationDefs::REQUEST_KEY]
      end
    end
  rescue => e
    handle_exception match_id, e
    Rusen.notify e # Send an email notification
  end
end

#play!(params, match) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/workers/table_manager.rb', line 212

def play!(params, match)
  unless @@table_information[match.id]
    log(__method__, msg: "Ignoring request to play in match #{match.id} that doesn't exist.")
    return self
  end

  proxy = @@table_information[match.id][:proxy]

  unless proxy
    log(__method__, msg: "Ignoring request to play in match #{match.id} in seat #{match.seat} when no such proxy exists.")
    return self
  end

  action = PokerAction.new(
    params.retrieve_parameter_or_raise_exception('action')
  )

  log __method__, {
    match_id: match.id,
    num_tables: @@table_information.length
  }

  proxy.play!(action)

  if proxy.match_ended?
    log __method__, msg: "Deleting background processes with match ID #{match.id}"
    @@table_information.delete match.id
  end

  self
end

#refresh_module(mod, mod_file, mode_file_base) ⇒ Object

TODO:

Poorly named arguments



41
42
43
44
45
46
47
48
49
# File 'app/workers/table_manager.rb', line 41

def refresh_module(mod, mod_file, mode_file_base)
  if Object.const_defined?(mod)
    Object.send(:remove_const, mod)
  end
  $".delete_if {|s| s.include?(mode_file_base) }
  load mod_file

  log __method__, msg: "RELOADED #{mod}"
end

#start_dealer!(params, match) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/workers/table_manager.rb', line 94

def start_dealer!(params, match)
  log __method__, params: params

  # Clean up data from dead matches
  @@table_information.each do |match_id, match_processes|
    unless match_processes[:dealer] && match_processes[:dealer][:pid] && match_processes[:dealer][:pid].process_exists?
      log __method__, msg: "Deleting background processes with match ID #{match_id}"
      @@table_information.delete(match_id)
    end
  end

  dealer_arguments = {
    match_name: "\"#{match.name}\"",
    game_def_file_name: match.game_definition_file_name,
    hands: match.number_of_hands,
    random_seed: match.random_seed.to_s,
    player_names: match.player_names.map { |name| "\"#{name}\"" }.join(' '),
    options: (params['options'] || {})
  }
  log_directory = params['log_directory']

  match_processes = @@table_information[match.id] || {}

  log __method__, {
    match_id: match.id,
    dealer_arguments: dealer_arguments,
    log_directory: log_directory,
    num_tables: @@table_information.length
  }

  dealer_information = match_processes[:dealer] || {}

  return self if dealer_information[:pid] && dealer_information[:pid].process_exists? # The dealer is already started

  # Start the dealer
  dealer_information = AcpcDealer::DealerRunner.start(
    dealer_arguments,
    log_directory
  )
  match_processes[:dealer] = dealer_information
  @@table_information[match.id] = match_processes

  # Get the player port numbers
  port_numbers = dealer_information[:port_numbers]

  # Store the port numbers in the database so the web app can access them
  match.port_numbers = port_numbers
  save_match_instance match

  self
end

#start_opponent!(bot_start_command) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/workers/table_manager.rb', line 154

def start_opponent!(bot_start_command)
  log(
    __method__,
    {
      bot_start_command_parameters: bot_start_command,
      command_to_be_run: bot_start_command.join(' '),
      pid: ProcessRunner.go(bot_start_command)
    }
  )

  self
end

#start_opponents!(bot_start_commands) ⇒ Object



146
147
148
149
150
151
152
# File 'app/workers/table_manager.rb', line 146

def start_opponents!(bot_start_commands)
  bot_start_commands.each do |bot_start_command|
    start_opponent! bot_start_command
  end

  self
end

#start_proxy!(match) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/workers/table_manager.rb', line 167

def start_proxy!(match)
  match_processes = @@table_information[match.id] || {}
  proxy = match_processes[:proxy]

  log __method__, {
    match_id: match.id,
    num_tables: @@table_information.length,
    num_match_processes: match_processes.length,
    proxy_present: !proxy.nil?
  }

  return self if proxy

  game_definition = GameDefinition.parse_file(match.game_definition_file_name)
  match.game_def_hash = game_definition.to_h
  save_match_instance match

  proxy = WebApplicationPlayerProxy.new(
    match.id,
    AcpcDealer::ConnectionInformation.new(
      match.port_numbers[match.seat - 1],
      DEALER_HOST
    ),
    match.seat - 1,
    game_definition,
    match.player_names.join(' '),
    match.number_of_hands
  ) do |players_at_the_table|
    log "#{__method__}: Initializing proxy", {
      match_id: match.id,
      at_least_one_state: !players_at_the_table.match_state.nil?
    }
  end

  log "#{__method__}: After starting proxy", {
    match_id: match.id,
    proxy_present: !proxy.nil?
  }

  match_processes[:proxy] = proxy
  @@table_information[match.id] = match_processes

  self
end