Class: ICU::Result

Inherits:
Object
  • Object
show all
Extended by:
Accessor
Defined in:
lib/icu_tournament/result.rb,
lib/icu_tournament/tournament_sp.rb,
lib/icu_tournament/tournament_krause.rb

Overview

A result is the outcome of a game from the perspective of one of the players. If the game was not a bye or a walkover and involved a second player, then that second player will also have a result for the same game, and the two results will be mirror images of each other.

A result always involves a round number, a player number and a score, so these three attributes must be supplied in the constructor.

result = ICU::Result.new(2, 10, 'W')

The above example represents player 10 winning in round 2. As it stands, it represends a bye or walkover since there is no opponent. Without an opponent, it is unrateable.

result.rateable     # => false

The player’s colour and the number of their opponent can be set as follows:

result.colour = 'B'
result.opponent = 13

Specifying an opponent always makes a result rateable.

result.rateable     # => true

This example now represents a win by player 10 with the black pieces over player number 13 in round 2. Alternatively, all this can been specified in the constructor.

result = ICU::Result.new(2, 10, 'W', :opponent => 13, :colour => 'B')

To make a game unratable, even if it involves an opponent, set the rateable atribute explicity:

result.rateable = false

or include it in the constructor:

result = ICU::Result.new(2, 10, 'W', :opponent => 13, :colour => 'B', :rateable => false)

The result of the same game from the perspective of the opponent is:

tluser = result.reverse

which, with the above example, would be:

tluser.player       # => 13
tluser.opponent     # => 10
tluser.score        # => 'L'
tluser.colour       # => 'B'
tluser.round        # => 2

The reversed result copies the rateable attribute of the original unless an explicit override is supplied.

result.rateable                 # => true
result.reverse.rateable         # => true (copied from original)
result.reverse(false).rateable  # => false (overriden)

A result which has no opponent is not reversible (the reverse method returns nil).

The return value from the score method is always one of W, L or D. However, when setting the score, a certain amount of variation is permitted as long as it is clear what is meant. For eample, the following would all be converted to D:

result.score = ' D '
result.score = 'd'
result.score = '='
result.score = '0.5'
result.score = '½'

The points read-only accessor always returns a floating point number: either 0.0, 0.5 or 1.0.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Accessor

attr_accessor, attr_date, attr_date_or_nil, attr_integer, attr_integer_or_nil, attr_positive, attr_positive_or_nil, attr_string, attr_string_or_nil

Constructor Details

#initialize(round, player, score, opt = {}) ⇒ Result

Constructor. Round number, player number and score must be supplied. Optional hash attribute are opponent, colour and rateable.



84
85
86
87
88
89
90
# File 'lib/icu_tournament/result.rb', line 84

def initialize(round, player, score, opt={})
  self.round  = round
  self.player = player
  self.score  = score
  [:colour, :opponent].each { |a| self.send("#{a}=", opt[a]) unless opt[a].nil? }
  self.rateable = opt[:rateable]  # always attempt to set this, and do it last, to get the right default
end

Instance Attribute Details

#colourObject

Returns the value of attribute colour.



80
81
82
# File 'lib/icu_tournament/result.rb', line 80

def colour
  @colour
end

#opponentObject

Returns the value of attribute opponent.



80
81
82
# File 'lib/icu_tournament/result.rb', line 80

def opponent
  @opponent
end

#rateableObject

Returns the value of attribute rateable.



80
81
82
# File 'lib/icu_tournament/result.rb', line 80

def rateable
  @rateable
end

#scoreObject

Returns the value of attribute score.



80
81
82
# File 'lib/icu_tournament/result.rb', line 80

def score
  @score
end

Instance Method Details

#==(other) ⇒ Object

Loose equality. True if the round, player and opponent numbers, colour and score all match.



172
173
174
175
176
177
178
# File 'lib/icu_tournament/result.rb', line 172

def ==(other)
  return unless other.is_a? Result
  [:round, :player, :opponent, :colour, :score].each do |m|
    return false unless self.send(m) == other.send(m)
  end
  true
end

#eql?(other) ⇒ Boolean

Strict equality. True if the there’s loose equality and also the rateablity is the same.

Returns:

  • (Boolean)


181
182
183
184
185
# File 'lib/icu_tournament/result.rb', line 181

def eql?(other)
  return true if equal?(other)
  return false unless self == other
  self.rateable == other.rateable
end

#pointsObject

Return the score as a floating point number.



103
104
105
106
107
108
109
# File 'lib/icu_tournament/result.rb', line 103

def points
  case @score
    when 'W' then 1.0
    when 'L' then 0.0
    else 0.5
  end
end

#renumber(map) ⇒ Object

Renumber the player and opponent (if there is one) according to the supplied hash. Return self.



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/icu_tournament/result.rb', line 159

def renumber(map)
  raise "result player number #{@player} not found in renumbering hash" unless map[@player]
  self.player = map[@player]
  if @opponent
    raise "result opponent number #{@opponent} not found in renumbering hash" unless map[@opponent]
    old_rateable = @rateable
    self.opponent = map[@opponent]
    self.rateable = old_rateable  # because setting the opponent has a side-effect which is undesirable in this context
  end
  self
end

#reverse(rateable = nil) ⇒ Object

Return a reversed version (from the opponent’s perspective) of a result.



148
149
150
151
152
153
154
155
156
# File 'lib/icu_tournament/result.rb', line 148

def reverse(rateable=nil)
  return unless @opponent
  r = Result.new(@round, @opponent, @score == 'W' ? 'L' : (@score == 'L' ? 'W' : 'D'))
  r.opponent = @player
  r.colour = 'W' if @colour == 'B'
  r.colour = 'B' if @colour == 'W'
  r.rateable = rateable || @rateable
  r
end

#to_krauseObject

Format a player’s result as it would appear in a Krause formatted file (exactly 8 characters long, including leading whitespace).



354
355
356
357
358
359
360
361
# File 'lib/icu_tournament/tournament_krause.rb', line 354

def to_krause
  return ' ' * 8 if !@opponent && !@colour && @score == 'L'
  krause = sprintf('%4s ', @opponent || '0000')
  krause << sprintf('%1s ', @colour ? @colour.downcase : '-')
  krause << case @score; when 'W' then '1'; when 'L' then '0'; else '='; end if  @rateable
  krause << case @score; when 'W' then '+'; when 'L' then '-'; else '='; end if !@rateable
  krause
end

#to_sp_textObject

Format a player’s result as it would appear in an SP text export file.



379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/icu_tournament/tournament_sp.rb', line 379

def to_sp_text
  sp = opponent ? opponent.to_s : '0'
  sp << ':'
  if rateable
    sp << score
  else
    sp << case score
    when 'W' then '+'
    when 'L' then '-'
    else '='
    end
  end
end