Class: EloRating::Match::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/elo_rating/match.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match:, rating:, place: nil, winner: nil) ⇒ Player



54
55
56
57
58
59
60
# File 'lib/elo_rating/match.rb', line 54

def initialize(match:, rating:, place: nil, winner: nil)
  validate_attributes!(rating: rating, place: place, winner: winner)
  @match = match
  @rating = rating
  @place = place
  @winner = winner
end

Instance Attribute Details

#matchObject (readonly)

:nodoc:



53
54
55
# File 'lib/elo_rating/match.rb', line 53

def match
  @match
end

#placeObject (readonly)

:nodoc:



53
54
55
# File 'lib/elo_rating/match.rb', line 53

def place
  @place
end

#ratingObject (readonly)

:nodoc:



53
54
55
# File 'lib/elo_rating/match.rb', line 53

def rating
  @rating
end

Instance Method Details

#actual_score_against(opponent) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/elo_rating/match.rb', line 97

def actual_score_against(opponent)
  if won_against?(opponent)
    1
  elsif opponent.won_against?(self)
    0
  else # draw
    0.5
  end
end

#expected_score_against(opponent) ⇒ Object



93
94
95
# File 'lib/elo_rating/match.rb', line 93

def expected_score_against(opponent)
  EloRating.expected_score(rating, opponent.rating)
end

#opponentsObject



72
73
74
# File 'lib/elo_rating/match.rb', line 72

def opponents
  match.players - [self]
end

#placed_ahead_of?(opponent) ⇒ Boolean



111
112
113
114
115
# File 'lib/elo_rating/match.rb', line 111

def placed_ahead_of?(opponent)
  if place && opponent.place
    place < opponent.place
  end
end

#rating_adjustment_against(opponent) ⇒ Object



86
87
88
89
90
91
# File 'lib/elo_rating/match.rb', line 86

def rating_adjustment_against(opponent)
  EloRating.rating_adjustment(
    expected_score_against(opponent),
    actual_score_against(opponent)
  )
end

#total_rating_adjustmentsObject



80
81
82
83
84
# File 'lib/elo_rating/match.rb', line 80

def total_rating_adjustments
  opponents.map do |opponent|
    rating_adjustment_against(opponent)
  end.reduce(0, :+)
end

#updated_ratingObject



76
77
78
# File 'lib/elo_rating/match.rb', line 76

def updated_rating
  (rating + total_rating_adjustments).round
end

#validate_attributes!(rating:, place:, winner:) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
# File 'lib/elo_rating/match.rb', line 66

def validate_attributes!(rating:, place:, winner:)
  raise ArgumentError, 'Rating must be numeric' unless rating.is_a? Numeric
  raise ArgumentError, 'Winner and place cannot both be specified' if place && winner
  raise ArgumentError, 'Place must be numeric' unless place.nil? || place.is_a?(Numeric)
end

#winner?Boolean



62
63
64
# File 'lib/elo_rating/match.rb', line 62

def winner?
  @winner
end

#won_against?(opponent) ⇒ Boolean



107
108
109
# File 'lib/elo_rating/match.rb', line 107

def won_against?(opponent)
  winner? || placed_ahead_of?(opponent)
end