Class: Bcpm::Tests::TestMatch

Inherits:
Object
  • Object
show all
Defined in:
lib/bcpm/tests/test_match.rb

Overview

A match run for simulation purposes.

Each test case is its own anonymous class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(side, vs, map, environment, options = {}) ⇒ TestMatch

Skeleton for a match.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bcpm/tests/test_match.rb', line 32

def initialize(side, vs, map, environment, options = {})
  @side = side
  @vs = vs
  @map = map
  @options = options.clone
  @environment = environment
  @output = nil
  @data = nil
  
  @output_lines = nil
  @outcome = nil
  @winner = nil
  @reason = nil
end

Instance Attribute Details

#dataObject (readonly)

Detailed match data.



29
30
31
# File 'lib/bcpm/tests/test_match.rb', line 29

def data
  @data
end

#environmentObject (readonly)

The environment that the match runs in.



23
24
25
# File 'lib/bcpm/tests/test_match.rb', line 23

def environment
  @environment
end

#mapObject (readonly)

Name of map for the match.



18
19
20
# File 'lib/bcpm/tests/test_match.rb', line 18

def map
  @map
end

#optionsObject (readonly)

Custom options for the battlecode simulator.



20
21
22
# File 'lib/bcpm/tests/test_match.rb', line 20

def options
  @options
end

#outputObject (readonly)

Match output. Nil if the match hasn’t completed.



26
27
28
# File 'lib/bcpm/tests/test_match.rb', line 26

def output
  @output
end

#sideObject (readonly)

Side of the tested player in the match.



14
15
16
# File 'lib/bcpm/tests/test_match.rb', line 14

def side
  @side
end

#vsObject (readonly)

Name of opposing player in the match.



16
17
18
# File 'lib/bcpm/tests/test_match.rb', line 16

def vs
  @vs
end

Class Method Details

.default_gamesave_pathObject

Path where game data (output, replay binlog) is saved.



146
147
148
# File 'lib/bcpm/tests/test_match.rb', line 146

def self.default_gamesave_path
  File.join Dir.tmpdir, 'bcpm'
end

.gamesave_pathObject

Path where game data (output, replay binlog) is saved.



141
142
143
# File 'lib/bcpm/tests/test_match.rb', line 141

def self.gamesave_path
  Bcpm::Config[:gamesave_path] ||= default_gamesave_path
end

.stashed_outputsObject

All game outputs saved by calls to stash_data.



136
137
138
# File 'lib/bcpm/tests/test_match.rb', line 136

def self.stashed_outputs
  Dir.glob File.join(gamesave_path, '*.txt')
end

.stashed_replaysObject

All game replays saved by calls to stash_data.



131
132
133
# File 'lib/bcpm/tests/test_match.rb', line 131

def self.stashed_replays
  Dir.glob File.join(gamesave_path, '*.rms')
end

Instance Method Details

#chatterObject

The output printed by map units, without the [source] prefixess.



83
84
85
# File 'lib/bcpm/tests/test_match.rb', line 83

def chatter
  @chatter ||= output_lines.map { |line| line.gsub /^\[[^\]]+\]\s/, '' }.reject(&:empty?).join("\n")
end

#descriptionObject

User-readable description of match conditions.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bcpm/tests/test_match.rb', line 64

def description
  if File.basename(map) == map
    map_name = map
  else
    map_name = "suite/maps/#{File.basename(map).sub(/\.xml$/, '')}"
  end
  desc = "as team #{side.to_s.upcase} vs #{vs} on #{map_name}"
  unless @options.empty?
    desc += ' with ' + options.map { |k, v| "#{k}=#{v}" }.join(",")
  end
  desc
end

#open_binaryObject

Name of program for opening text files.



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/bcpm/tests/test_match.rb', line 151

def open_binary
  return ENV['EDITOR'] if ENV['EDITOR']
  case RUBY_PLATFORM
  when /darwin/
    'open'
  when /win/
    'notepad'
  when /linux/
    'gedit'
  end
end

#outcomeObject

The output line showing who won the game.



88
89
90
91
92
93
# File 'lib/bcpm/tests/test_match.rb', line 88

def outcome
  return @outcome if @outcome
  @outcome = output_lines[-3] || ''
  @outcome = '(no victory)' unless outcome.index('wins')
  @outcome
end

#output_linesObject

The match output, split into lines.



78
79
80
# File 'lib/bcpm/tests/test_match.rb', line 78

def output_lines
  @output_lines ||= output.split("\n")
end

#ran?Boolean

True if the test match has run, and its results are available.

Returns:

  • (Boolean)


59
60
61
# File 'lib/bcpm/tests/test_match.rb', line 59

def ran?
  !output.nil?
end

#reasonObject

The output line showing the reason the game ended.



108
109
110
111
112
113
# File 'lib/bcpm/tests/test_match.rb', line 108

def reason
  return @reason if @reason
  @reason = output_lines[-2] || ''
  @reason = '(no reason)' unless reason.index('Reason:')
  @reason
end

#run(live = false) ⇒ Object

Run the game.



48
49
50
51
52
53
54
55
56
# File 'lib/bcpm/tests/test_match.rb', line 48

def run(live = false)
  case @side
  when :a
    @data = Bcpm::Match.match_data @environment.player_name, @vs, true, @map, live, @options
  when :b
    @data = Bcpm::Match.match_data @vs, @environment.player_name, false, @map, live, @options
  end
  @output = data[:ant]
end

#stash_dataObject

Stashes the match data somewhere on the system.

Returns a string containing user-friendly instructions for accessing the match data.



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bcpm/tests/test_match.rb', line 118

def stash_data
  path = self.class.gamesave_path
  FileUtils.mkdir_p path
  
  txt_path = File.join path, data[:uid] + '.txt'
  File.open(txt_path, 'wb') { |f| f.write output } unless File.exist?(txt_path)
  rms_path = File.join path, data[:uid] + '.rms'
  File.open(rms_path, 'wb') { |f| f.write data[:rms] }  unless File.exist?(rms_path)

  "Output: #{open_binary} #{txt_path}\nReplay: bcpm replay #{rms_path}\n"
end

#winnerObject

The side that own the game



96
97
98
99
100
101
102
103
104
105
# File 'lib/bcpm/tests/test_match.rb', line 96

def winner
  return @winner if @winner
  win_match = /\((.)\) wins/.match outcome
  @winner = if win_match
    (win_match[1] == 'A') ? :a : :b
  else
    :error
  end
  @winner
end