Class: Sc2::MapFile

Inherits:
Object
  • Object
show all
Defined in:
lib/sc2ai/local_play/map_file.rb

Overview

Helps easily locate a map and fetch input for Api::LocalMap

Constant Summary collapse

EXTENSION =

File extension used for maps

".SC2Map"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ MapFile

Accepts a map file name and initializes a local map object

Examples:

map = Sc2::Map.new("2000AtmospheresAIE")
map = Sc2::Map.new("2000AtmospheresAIE.SC2Map")
map = Sc2::Map.new("/absolute/path/to/2000AtmospheresAIE.SC2Map")
# If within your Sc2 Maps folder, you have a sub-folder "sc2ai_2022_season3"
map = Sc2::Map.new("sc2ai_2022_season3/2000AtmospheresAIE.SC2Map")

Parameters:

  • name (String)

    absolute path or path relative to maps

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sc2ai/local_play/map_file.rb', line 21

def initialize(name)
  raise Error if name.empty?

  name = "#{name}#{EXTENSION}" unless name.end_with? EXTENSION

  @path = name.to_s
  return if Pathname(name).absolute?

  @path = Pathname(Paths.maps_dir).glob("**/#{name}").first.to_s
  if Paths.wsl?
    @path = Paths.wsl_to_win(path: @path)
  end
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#dataString?

Returns contents of map file for user with LocalMap.map_data

Returns:

  • (String, nil)

    contents of file



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

def data
  file = Pathname(@path)
  return file.read if file.exist?

  nil
end