Module: AcpcTableManager::Opponents

Extended by:
SimpleLogging
Defined in:
lib/acpc_table_manager/opponents.rb

Class Method Summary collapse

Methods included from SimpleLogging

log, log_with, logger

Class Method Details

.start(match) ⇒ Array<Integer>

Returns PIDs of the opponents started.

Returns:

  • (Array<Integer>)

    PIDs of the opponents started



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/acpc_table_manager/opponents.rb', line 14

def self.start(match)
  @logger ||= ::AcpcTableManager.new_log 'opponents.log'

  opponents = match.bots(AcpcTableManager.config.dealer_host)
  log __method__, num_opponents: opponents.length

  if opponents.empty?
    raise StandardError.new("No opponents found to start for \"#{match.name}\" (#{match.id.to_s})!")
  end

  opponents_log_dir = File.join(AcpcTableManager.config.log_directory, 'opponents')
  FileUtils.mkdir(opponents_log_dir) unless File.directory?(opponents_log_dir)

  bot_start_commands = opponents.map do |name, info|
    {
      args: [info[:runner], info[:host], info[:port]],
      log: File.join(opponents_log_dir, "#{match.name}.#{match.id}.#{name}.log")
    }
  end

  bot_start_commands.map do |bot_start_command|
    log(
      __method__,
      {
        bot_start_command_parameters: bot_start_command[:args],
        command_to_be_run: bot_start_command[:args].join(' ')
      }
    )
    pid = Timeout::timeout(3) do
      ProcessRunner.go(
        bot_start_command[:args].map { |e| e.to_s },
        {
          [:err, :out] => [bot_start_command[:log], File::CREAT|File::WRONLY]
        }
      )
    end
    log(
      __method__,
      {
        bot_started?: true,
        pid: pid
      }
    )
    pid
  end
end