Class: Sc2::ClientManager

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/sc2ai/local_play/client_manager.rb

Overview

Starts, stops and holds reference to clients

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hostObject

Returns the value of attribute host.



69
70
71
# File 'lib/sc2ai/local_play/client_manager.rb', line 69

def host
  @host
end

Instance Method Details

#get(player_index) ⇒ Sc2::Connection?

Gets Sc2::Client client for player index

Parameters:

  • normally 0,1

Returns:

  • running client or nil if not set



30
31
32
# File 'lib/sc2ai/local_play/client_manager.rb', line 30

def get(player_index)
  @clients[player_index]
end

#obtain(player_index) ⇒ Object

Gets client for player X or starts an instance



18
19
20
21
22
23
24
25
# File 'lib/sc2ai/local_play/client_manager.rb', line 18

def obtain(player_index)
  client = get(player_index)
  if client.nil? || !client.running?
    client = start(player_index)
    @clients[player_index] = client
  end
  client
end

#start(player_index) ⇒ Sc2::Client

Starts an Sc2 client for player_index. Will stop existing client if present.

Parameters:

  • normally 0,1

Returns:

  • started client



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sc2ai/local_play/client_manager.rb', line 37

def start(player_index)
  existing = @clients[player_index]
  stop(player_index) if !existing.nil? && existing.running?

  port = @ports[player_index] || (@ports[player_index] = Ports.random_available_port)

  client = Client.new(host: @host, port: port, **Sc2.config.to_h)
  client.launch
  @clients[player_index] = client
  client
end

#stop(player_index) ⇒ void

This method returns an undefined value.

Stops client at player index

Parameters:



52
53
54
55
56
57
# File 'lib/sc2ai/local_play/client_manager.rb', line 52

def stop(player_index)
  return unless @clients[player_index]

  @clients[player_index]&.stop
  @clients[player_index] = nil
end

#stop_allvoid

This method returns an undefined value.

Stops all clients



61
62
63
64
65
66
67
# File 'lib/sc2ai/local_play/client_manager.rb', line 61

def stop_all
  @clients.compact.each do |client|
    client.stop
    client = nil
    @clients.delete(client)
  end
end