Class: AcpcDealer::DealerRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/acpc_dealer/dealer_runner.rb

Defined Under Namespace

Classes: Arguments

Constant Summary collapse

DEALER_COMMAND_FORMAT =

> array should correspond to those in the dealer_arguments of ::start.

> (see ::start)

Returns:

  • (Array<Symbol>)

    The format of the dealer command. The symbols in this

[
  # @note The name of the match to start.
  :match_name,
  # @note The path of the game definition to use.
  :game_def_file_name,
  # @note The number of hands to play.
  :hands,
  # @note The random seed to use.
  :random_seed,
  # @note The names of the players in the game. Should be specified as a space delimited string of names.
  :player_names,
  # @note Should be specified as an option string that the dealer will understand.
  :options
]

Class Method Summary collapse

Class Method Details

.command(dealer_arguments, port_numbers = nil) ⇒ String

> Defaults to the ACPC Dealer’s default.

Parameters:

  • dealer_arguments (Array)

    Arguments to the new dealer instance.

  • port_numbers (Array) (defaults to: nil)

    The port numbers to which each player will connect.

Returns:

  • (String)

    The ACPC Dealer command that would be run



43
44
45
# File 'lib/acpc_dealer/dealer_runner.rb', line 43

def self.command(dealer_arguments, port_numbers=nil)
  command_components(dealer_arguments, port_numbers).join(' ')
end

.command_components(dealer_arguments, port_numbers = nil) ⇒ Array

> Defaults to the ACPC Dealer’s default.

Parameters:

  • dealer_arguments (Array)

    Arguments to the new dealer instance.

  • port_numbers (Array) (defaults to: nil)

    The port numbers to which each player will connect.

Returns:

  • (Array)

    The components of the ACPC Dealer command that would be run (the executable, followed by arguments)



31
32
33
34
35
36
37
# File 'lib/acpc_dealer/dealer_runner.rb', line 31

def self.command_components(dealer_arguments, port_numbers=nil)
  dealer_start_command = DEALER_COMMAND_FORMAT.inject([AcpcDealer::DEALER_PATH]) do |command_, parameter|
    command_ += dealer_arguments[parameter].to_s.split
  end
  dealer_start_command << "-p" << "#{port_numbers.join(',')}" if port_numbers
  dealer_start_command
end

.ports_for_players(number_of_players) ⇒ Array<Integer>

> so it’s possible that the ports will come into use between calling this method and

> using them.

Returns:

  • (Array<Integer>)

    List of random open ports. Does NOT reserve the ports though,



91
92
93
94
95
# File 'lib/acpc_dealer/dealer_runner.rb', line 91

def self.ports_for_players(number_of_players)
  number_of_players.times.inject([]) do |ports, i|
    ports << TCPServer.open('localhost', 0) { |s| s.addr[1] }
  end
end

.start(dealer_arguments, log_directory = nil, port_numbers = nil, save_action_log = true) ⇒ Hash

> Defaults to <dealer_arguments>.logs.

> Defaults to the ACPC’s default.

> use to connect to the new dealer instance (key :port_numbers).

Parameters:

  • dealer_arguments (Array)

    Arguments to the new dealer instance.

  • log_directory (String) (defaults to: nil)

    The directory in which logs will be placed.

  • port_numbers (Array) (defaults to: nil)

    The port numbers to which each player will connect.

Returns:

  • (Hash)

    The process ID of the started dealer (key :pid) and the array of ports that players may



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
# File 'lib/acpc_dealer/dealer_runner.rb', line 56

def self.start(dealer_arguments, log_directory=nil, port_numbers=nil, save_action_log=true)
  unless log_directory
    log_directory = File.expand_path("../#{dealer_arguments[:match_name]}.logs", __FILE__)
  end

  FileUtils.mkdir_p log_directory unless Dir.exist?(log_directory)

  IO.pipe do |read_io, write_io|
    pid = if save_action_log
      ProcessRunner.go(
        command_components(dealer_arguments, port_numbers),
        err: [
          File.join(log_directory, "#{dealer_arguments[:match_name]}.actions.log"),
          File::CREAT|File::WRONLY
        ],
        out: write_io,
        chdir: log_directory
      )
    else
      ProcessRunner.go(
        command_components(dealer_arguments, port_numbers),
        out: write_io,
        chdir: log_directory
      )
    end

    {pid: pid, port_numbers: read_io.gets.split, log_directory: log_directory}
  end
end