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:



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

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.



34
35
36
# File 'lib/sc2ai/local_play/match.rb', line 34

def map
  @map
end

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

Returns the Map for the match.

Returns:



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

attr_reader :map

#playersObject

Returns the value of attribute players.



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

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)



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

attr_accessor :players

Instance Method Details

#on_status_change(status) ⇒ Object

Callback when game status changes



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sc2ai/local_play/match.rb', line 11

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

  # if status == :ended
  #   # Go through each player, looking for result if we don't have one.
  #   api_players.each do |player|
  #     Sc2.logger.debug { "TODO: Get results for players" }
  #     # result = player.result
  #     Sc2.logger.debug { "Leaving Game and Disconnecting players" }
  #     player.leave_game
  #     player.disconnect
  #   end
  # end
end

#runvoid

This method returns an undefined value.

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sc2ai/local_play/match.rb', line 60

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)

    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)


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

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