Module: Bcpm::Dist

Defined in:
lib/bcpm/dist.rb

Overview

Battle-code distribution management.

Class Method Summary collapse

Class Method Details

.add_player(player_path) ⇒ Object

Hooks a player’s code into the installed battlecode distribution.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bcpm/dist.rb', line 10

def self.add_player(player_path)
  team_path = File.join dist_path, 'teams', File.basename(player_path)    
  if /mingw/ =~ RUBY_PLATFORM || (/win/ =~ RUBY_PLATFORM && /darwin/ !~ RUBY_PLATFORM)
    Dir.rmdir team_path if File.exist? team_path
    command = Shellwords.shelljoin(['cmd', '/C', 'mklink', '/D', team_path.gsub('/', '\\'),
                                    player_path.gsub('/', '\\')])
    Kernel.`(command)
  else
    File.unlink team_path if File.exist? team_path
    FileUtils.ln_s player_path, team_path
  end
end

.ant_fileObject

Path to the battlecode ant file.



74
75
76
# File 'lib/bcpm/dist.rb', line 74

def self.ant_file
  File.join dist_path, 'build.xml'
end

.conf_fileObject

Path to the battlecode simulator configuration file.



84
85
86
# File 'lib/bcpm/dist.rb', line 84

def self.conf_file
  File.join dist_path, 'bc.conf'
end

.contains_player?(player_path) ⇒ Boolean

True if the given path is a player that is hooked into the distribution.

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/bcpm/dist.rb', line 37

def self.contains_player?(player_path)
  team_path = File.join dist_path, 'teams', File.basename(player_path)
  if /mingw/ =~ RUBY_PLATFORM || (/win/ =~ RUBY_PLATFORM && /darwin/ !~ RUBY_PLATFORM)
    File.exist? team_path
  else
    File.exist?(team_path) && File.symlink?(team_path) && File.readlink(team_path) == player_path
  end
end

.copy_map(map_name, destination) ⇒ Object

Copies a battlecode distribution map.



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/bcpm/dist.rb', line 120

def self.copy_map(map_name, destination)
  map_file = File.join maps_path, map_name + '.xml'
  unless File.exist?(map_file)
    puts "No map found at#{map_file}"
    return
  end
  if File.exist?(destination) && File.directory?(destination)
    destionation = File.join destination, map_name + '.xml'
  end
  FileUtils.cp map_file, destination
end

.default_dist_pathObject

Path to the locally installed battlecode distribution.



94
95
96
97
98
99
100
# File 'lib/bcpm/dist.rb', line 94

def self.default_dist_path
  if File.exist?('.metadata') && File.directory?('.metadata')
    File.expand_path './battlecode'
  else
    File.expand_path '~/battlecode'
  end
end

.default_repo_uriObject

Git URI to the distribution repository.



108
109
110
# File 'lib/bcpm/dist.rb', line 108

def self.default_repo_uri
  '[email protected]:six370/battlecode2011.git'
end

.dist_pathObject

Path to the locally installed battlecode distribution.



89
90
91
# File 'lib/bcpm/dist.rb', line 89

def self.dist_path
  Bcpm::Config[:dist_path] || default_dist_path
end

.installObject

Installs the latest battlecode distribution.



55
56
57
58
59
# File 'lib/bcpm/dist.rb', line 55

def self.install
  Bcpm::Git.clone_repo repo_uri, 'master', dist_path
  Bcpm::Config[:dist_repo_uri] = repo_uri
  Bcpm::Config[:dist_path] = dist_path
end

.installed?Boolean

True if a battlecode distribution is installed on the local machine.

Returns:

  • (Boolean)


62
63
64
# File 'lib/bcpm/dist.rb', line 62

def self.installed?
  Bcpm::Config.config.has_key? :dist_path
end

.mapsObject

Maps installed in the battlecode distribution.



113
114
115
116
117
# File 'lib/bcpm/dist.rb', line 113

def self.maps
  Dir.glob(File.join(maps_path, '*.xml')).map do |f|
    File.basename(f).sub(/\.xml$/, '')
  end
end

.maps_pathObject

Path to the battlecode maps directory.



79
80
81
# File 'lib/bcpm/dist.rb', line 79

def self.maps_path
  File.join dist_path, 'maps'
end

.remove_player(player_path) ⇒ Object

Unhooks a player’s code from the installed battlecode distribution.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bcpm/dist.rb', line 24

def self.remove_player(player_path)
  return unless contains_player?(player_path)
  team_path = File.join dist_path, 'teams', File.basename(player_path)
  if /mingw/ =~ RUBY_PLATFORM || (/win/ =~ RUBY_PLATFORM && /darwin/ !~ RUBY_PLATFORM)
    Dir.rmdir team_path
  else
    File.unlink team_path
  end
  bin_path = File.join dist_path, 'bin', File.basename(player_path)
  FileUtils.rm_rf bin_path if File.exist?(bin_path)
end

.repo_uriObject

Git URI to the distribution repository.



103
104
105
# File 'lib/bcpm/dist.rb', line 103

def self.repo_uri
  Bcpm::Config[:dist_repo_uri] || default_repo_uri
end

.uninstallObject

Removes the battlecode distribution installed on the location machine.



67
68
69
70
71
# File 'lib/bcpm/dist.rb', line 67

def self.uninstall
  return unless installed?
  FileUtils.rm_rf dist_path
  Bcpm::Config[:dist_path] = nil
end

.upgradeObject

Upgrades the installed battlecode distribution to the latest version.

Does a fresh install if no distribution is configured.



49
50
51
52
# File 'lib/bcpm/dist.rb', line 49

def self.upgrade
  return install unless installed?
  Bcpm::Git.update_repo dist_path
end