Class: Sc2::Match

Inherits:
Object
  • Object
show all
Includes:
Connection::StatusListener
Defined in:
lib/sc2ai/local_play/match.rb

Overview

Runs a match using a map and player configuration

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(players:, map: nil) ⇒ Sc2::Match

Parameters:



26
27
28
29
30
31
32
33
# File 'lib/sc2ai/local_play/match.rb', line 26

def initialize(players:, map: nil)
  @players = players || []
  @map = if map.is_a?(String)
    MapFile.new(map.to_s)
  else
    map
  end
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



21
22
23
# File 'lib/sc2ai/local_play/match.rb', line 21

def map
  @map
end

#map Sets the Map for the match(SetstheMap) ⇒ Sc2::MapFile

Returns the Map for the match.

Returns:



21
# File 'lib/sc2ai/local_play/match.rb', line 21

attr_reader :map

#playersObject

Returns the value of attribute players.



17
18
19
# File 'lib/sc2ai/local_play/match.rb', line 17

def players
  @players
end

#players Sets the Player(s) for the match(SetsthePlayer(s)) ⇒ Array<Sc2::Player>

Returns an array of assigned players (ai,bots,humans,observers).

Returns:

  • (Array<Sc2::Player>)

    an array of assigned players (ai,bots,humans,observers)



17
# File 'lib/sc2ai/local_play/match.rb', line 17

attr_accessor :players

Instance Method Details

#on_status_change(status) ⇒ Object

Callback when game status changes



11
12
13
# File 'lib/sc2ai/local_play/match.rb', line 11

def on_status_change(status)
  Sc2.logger.debug { "Status from Match: #{status}" }
end

#runvoid

This method returns an undefined value.

Connects players to instances, creates a game and joins everyone to play!



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sc2ai/local_play/match.rb', line 47

def run
  validate
  Sc2.logger.debug { "Connecting players to client..." }

  # Holds the game process and finishes when a status triggers it to end
  Async do |run_task|
    connect_players
    setup_player_hooks

    player_host.create_game(map:, players: @players, realtime: player_host.realtime)

    api_players.each_with_index do |player, player_index|
      run_task.async do
        player.join_game(
          server_host: ClientManager.get(player_index).host,
          port_config:
        )

        result = player.play
        Sc2.logger.debug { "Player(#{player_index}) Result: #{result}" }
        autosave_replay(player)
      ensure
        Sc2.logger.debug { "Game over, disconnect players." }
        # Suppress interrupt errors #$stderr.reopen File.new(File::NULL, "w")
        player.disconnect
        ClientManager.stop(player_index) # unless keep_clients_alive
      end
    end
  rescue
    # no op - clean exit from game may cause ws disconnection error
  end.wait

  nil
end

#validatevoid

This method returns an undefined value.

Validates a runnable match and raises an error if invalid

Raises:

  • (Sc2:Error)


38
39
40
41
42
43
# File 'lib/sc2ai/local_play/match.rb', line 38

def validate
  @players.select! { |player| player.is_a?(Player) }
  raise Error, "player count greater than 1 expected" unless @players.length >= 2

  raise Error, "invalid map" if !@map.is_a?(MapFile) || @map.path.empty?
end