Sha256: 617a8adbdee413d3ae16a568f9472345d040b1810fc930481cc0409fbb0a700b
Contents?: true
Size: 1.91 KB
Versions: 3
Compression:
Stored size: 1.91 KB
Contents
# frozen_string_literal: true require "singleton" require "forwardable" module Sc2 # Starts, stops and holds reference to clients class ClientManager include Singleton class << self extend Forwardable def_delegators :instance, :obtain, :get, :start, :stop, :stop_all end # Gets client for player X or starts an instance def obtain(player_index) client = get(player_index) if client.nil? || !client.running? client = start(player_index) @clients[player_index] = client end client end # Gets Sc2::Client client for player index # @param player_index [Integer] normally 0,1 # @return [Sc2::Connection, nil] running client or nil if not set def get(player_index) @clients[player_index] end # Starts an Sc2 client for player_index. Will stop existing client if present. # @param player_index [Integer] normally 0,1 # @return [Sc2::Client] started client 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 # Stops client at player index # @param player_index [Integer] # @return [void] def stop(player_index) return unless @clients[player_index] @clients[player_index]&.stop @clients[player_index] = nil end def stop_all @clients.compact.each do |client| client.stop client = nil @clients.delete(client) end end private attr_accessor :clients, :ports def initialize super @ports = Sc2.config.ports @ports = [] unless ports.is_a? Array @ports.push(Ports.random_available_port) while @ports.size < 3 @host = Sc2.config.host @clients = [] end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
sc2ai-0.0.8 | lib/sc2ai/local_play/client_manager.rb |
sc2ai-0.0.7 | lib/sc2ai/local_play/client_manager.rb |
sc2ai-0.0.5 | lib/sc2ai/local_play/client_manager.rb |