Class: Sc2::Client
- Inherits:
-
Object
- Object
- Sc2::Client
- 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
-
#base_build ⇒ Integer
Sc2 build number determines where to look for correct executable version binary.
-
#data_version ⇒ String
Sc2 data param, typically only required when launching older versions and Linux Launch param: -dataVersion “B89B5D6FA7CBF6452E721311BFBC6CB2”.
-
#port ⇒ Integer
Sc2 port param on which to listen for connections, default is randomly selected
Launch param: -port 12345.
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
-
#initialize(host:, port:, **options) ⇒ Client
constructor
Initialize new Sc2 client (starts with #launch).
-
#launch ⇒ Object
Launches and returns pid or proc.
-
#running? ⇒ Boolean
Whether the Sc2 process is running or not.
-
#stop ⇒ void
Stops the Sc2 instance<br/> This naturally disconnects attached players too.
-
#use_version(version) ⇒ Object
Reads “base-version” and “data-hash” for corresponding version return [Array<Integer,String>] tuple base_build and data_version.
Methods included from ConfigurableOptions
Constructor Details
#initialize(host:, port:, **options) ⇒ Client
Initialize new Sc2 client (starts with #launch)
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:, **) raise Error, "Invalid port: #{port}" if port.to_s.empty? .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_build ⇒ Integer
Sc2 build number determines where to look for correct executable version binary
19 20 21 |
# File 'lib/sc2ai/local_play/client.rb', line 19 def base_build @base_build end |
#data_version ⇒ String
Sc2 data param, typically only required when launching older versions and Linux Launch param: -dataVersion “B89B5D6FA7CBF6452E721311BFBC6CB2”
25 26 27 |
# File 'lib/sc2ai/local_play/client.rb', line 25 def data_version @data_version end |
#port ⇒ Integer
Sc2 port param on which to listen for connections, default is randomly selected
Launch param: -port 12345
14 15 16 |
# File 'lib/sc2ai/local_play/client.rb', line 14 def port @port end |
Instance Method Details
#launch ⇒ Object
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| = {} if Gem.win_platform? [:new_pgroup] = true else [:pgroup] = true end unless cwd.nil? [:chdir] = cwd end begin ::Async::Process.spawn(command_string, **) rescue Sc2.logger.info("Client exited unexpectedly") task&.stop end end end |
#running? ⇒ Boolean
Whether the Sc2 process is running or not
29 30 31 |
# File 'lib/sc2ai/local_play/client.rb', line 29 def running? !!@task&.running? end |
#stop ⇒ void
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
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 |