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

Class Method Summary collapse

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. “127.0.0.1”

  • port (Integer)

    5001

  • options (Hash)

    (see Sc2::Client::ConfigurableOptions)

Raises:



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

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)


40
41
42
# File 'lib/sc2ai/local_play/client.rb', line 40

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)


46
47
48
# File 'lib/sc2ai/local_play/client.rb', line 46

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)


35
36
37
# File 'lib/sc2ai/local_play/client.rb', line 35

def port
  @port
end

Class Method Details

.versions_jsonArray

Examples:

{
  "base-version": 92028, # <- from "build-name" [1..-1] = "B92028"
  "data-hash": "2B7746A6706F919775EF1BADFC95EA1C", # <- from "root"
  "fixed-hash": "163B1CDF46F09B621F6312CD6901228E", # <- from "build-fixed-hash"
  "label": "5.0.13", # <- check game client
  "replay-hash": "BE64E420B329BD2A7D10EEBC0039D6E5", # <- from "build-replay-hash"
  "version": 92028 <- always same as base-version these days
},

Returns:

  • (Array)

    JSON contents of versions.json



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

def versions_json
  JSON.load_file(Paths.gem_data_versions_path)
end

Instance Method Details

#launchObject

Launches and returns pid or proc



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sc2ai/local_play/client.rb', line 75

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")
      task&.stop
    end
  end
end

#running?Boolean

Whether the Sc2 process is running or not

Returns:

  • (Boolean)


50
51
52
# File 'lib/sc2ai/local_play/client.rb', line 50

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

#stopvoid

This method returns an undefined value.

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



100
101
102
# File 'lib/sc2ai/local_play/client.rb', line 100

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)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/sc2ai/local_play/client.rb', line 113

def use_version(version)
  found_base_build = nil
  found_data_version = nil
  Sc2::Client.versions_json.each do |node|
    if version == node["label"]
      found_base_build = node["base-version"]
      found_data_version = node["data-hash"]
      @version = version
      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