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 Method Summary collapse

Instance Method Details

#get(player_index) ⇒ Sc2::Connection?

Gets Sc2::Client client for player index

Parameters:

  • player_index (Integer)

    normally 0,1

Returns:



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

def get(player_index)
  @clients[player_index]
end

#obtain(player_index) ⇒ Object

Gets client for player X or starts an instance



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

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:

  • player_index (Integer)

    normally 0,1

Returns:



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

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

  client = Client.new(host: @host, port: @ports[player_index], **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:

  • player_index (Integer)


49
50
51
52
53
54
# File 'lib/sc2ai/local_play/client_manager.rb', line 49

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



58
59
60
61
62
63
64
# File 'lib/sc2ai/local_play/client_manager.rb', line 58

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