Class: EloRating::Match::Player
- Inherits:
-
Object
- Object
- EloRating::Match::Player
- Defined in:
- lib/elo_rating/match.rb
Instance Attribute Summary collapse
-
#match ⇒ Object
readonly
:nodoc:.
-
#place ⇒ Object
readonly
:nodoc:.
-
#rating ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #actual_score_against(opponent) ⇒ Object
- #expected_score_against(opponent) ⇒ Object
-
#initialize(match:, rating:, place: nil, winner: nil) ⇒ Player
constructor
A new instance of Player.
- #opponents ⇒ Object
- #placed_ahead_of?(opponent) ⇒ Boolean
- #rating_adjustment_against(opponent) ⇒ Object
- #total_rating_adjustments ⇒ Object
- #updated_rating ⇒ Object
- #validate_attributes!(rating:, place:, winner:) ⇒ Object
- #winner? ⇒ Boolean
- #won_against?(opponent) ⇒ Boolean
Constructor Details
#initialize(match:, rating:, place: nil, winner: nil) ⇒ Player
Returns a new instance of 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: , place: place, winner: winner) @match = match = @place = place @winner = winner end |
Instance Attribute Details
#match ⇒ Object (readonly)
:nodoc:
53 54 55 |
# File 'lib/elo_rating/match.rb', line 53 def match @match end |
#place ⇒ Object (readonly)
:nodoc:
53 54 55 |
# File 'lib/elo_rating/match.rb', line 53 def place @place end |
#rating ⇒ Object (readonly)
:nodoc:
53 54 55 |
# File 'lib/elo_rating/match.rb', line 53 def 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(, opponent.) end |
#opponents ⇒ Object
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 (opponent) EloRating.( expected_score_against(opponent), actual_score_against(opponent) ) end |
#total_rating_adjustments ⇒ Object
80 81 82 83 84 |
# File 'lib/elo_rating/match.rb', line 80 def opponents.map do |opponent| (opponent) end.reduce(0, :+) end |
#updated_rating ⇒ Object
76 77 78 |
# File 'lib/elo_rating/match.rb', line 76 def ( + ).round end |
#validate_attributes!(rating:, place:, winner:) ⇒ Object
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 .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 |