Class: Sc2::Client

Inherits:
Object
  • Object
show all
Includes:
ConfigurableOptions
Defined in:
lib/sc2ai/local_play/client.rb,
lib/sc2ai/local_play/client/configurable_options.rb

Overview

Manages client connection to the Api

Defined Under Namespace

Modules: ConfigurableOptions

Instance Attribute Summary collapse

Attributes included from ConfigurableOptions

#data_dir, #display_mode, #egl_path, #host, #osmesa_path, #temp_dir, #verbose, #version, #windowheight, #windowwidth, #windowx, #windowy

Instance Method Summary collapse

Methods included from ConfigurableOptions

#load_default_launch_options

Constructor Details

#initialize(host:, port:, **options) ⇒ Client

Initialize new Sc2 client (starts with #launch)

Parameters:

  • host (String)

    to listen on, i.e. “0.0.0.0”, “127.0.0.1”

  • port (Integer)

    5001

  • options (Hash)

    (see Sc2::Client::ConfigurableOptions)

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sc2ai/local_play/client.rb', line 37

def initialize(host:, port:, **options)
  raise Error, "Invalid port: #{port}" if port.to_s.empty?

  load_default_launch_options
  options.each do |key, value|
    instance_variable_set(:"@#{key}", value)
  end

  # Require these at a minimum
  @port = port
  @host = host
  return if @version.nil?

  use_version(@version.to_s)
end

Instance Attribute Details

#base_buildInteger

Sc2 build number determines where to look for correct executable version binary

Returns:

  • (Integer)


19
20
21
# File 'lib/sc2ai/local_play/client.rb', line 19

def base_build
  @base_build
end

#data_versionString

Sc2 data param, typically only required when launching older versions and Linux Launch param: -dataVersion “B89B5D6FA7CBF6452E721311BFBC6CB2”

Returns:

  • (String)


25
26
27
# File 'lib/sc2ai/local_play/client.rb', line 25

def data_version
  @data_version
end

#portInteger

Sc2 port param on which to listen for connections, default is randomly selected
Launch param: -port 12345

Returns:

  • (Integer)


14
15
16
# File 'lib/sc2ai/local_play/client.rb', line 14

def port
  @port
end

Instance Method Details

#launchObject

Launches and returns pid or proc



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/sc2ai/local_play/client.rb', line 54

def launch
  cwd = Paths.exec_working_dir
  @task = Async do |task|
    options = {}

    if Gem.win_platform?
      options[:new_pgroup] = true
    else
      options[:pgroup] = true
    end
    unless cwd.nil?
      options[:chdir] = cwd
    end
    begin
      ::Async::Process.spawn(command_string, **options)
    rescue
      Sc2.logger.info("Client exited unexpectedly")
      task&.stop
    end
  end
end

#running?Boolean

Whether the Sc2 process is running or not

Returns:

  • (Boolean)


29
30
31
# File 'lib/sc2ai/local_play/client.rb', line 29

def running?
  !!@task&.running?
end

#stopvoid

This method returns an undefined value.

Stops the Sc2 instance<br/> This naturally disconnects attached players too



79
80
81
# File 'lib/sc2ai/local_play/client.rb', line 79

def stop
  @task&.stop
end

#use_version(version) ⇒ Object

Reads “base-version” and “data-hash” for corresponding version return [Array<Integer,String>] tuple base_build and data_version

Examples:

client.base_build => nil
client.data_version => nil
client.use_version("4.10")
client.base_build => 75689
client.data_version => "B89B5D6FA7CBF6452E721311BFBC6CB2"

Parameters:

  • version (String)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sc2ai/local_play/client.rb', line 92

def use_version(version)
  found_base_build = nil
  found_data_version = nil
  versions_json.each do |node|
    version_clean = version.gsub(".#{node["base-version"]}", "")
    if version_clean == node["label"]
      found_base_build = node["base-version"]
      found_data_version = node["data-hash"]
      @version = version_clean
      break
    end
  end
  if found_base_build.nil? || found_data_version.nil?
    Sc2.logger.warn "Requested version #{version} not found. Omit to auto-discover latest installed version"
    return false
  end

  @base_build = found_base_build
  @data_version = found_data_version
  true
end