class EloRating::Match

This class represents a single game between a number of players.

Attributes

players[R]

All the players of the match.

Public Class Methods

new() click to toggle source

Creates a new match with no players.

# File lib/elo_rating/match.rb, line 9
def initialize
  @players = []
end

Public Instance Methods

add_player(player_attributes) click to toggle source

Adds a player to the match

Attributes

  • rating: the Elo rating of the player

  • winner (optional): boolean, whether this player is the winner of the match

  • place (optional): a number representing the rank of the player within the match; the lower the number, the higher they placed

Raises an ArgumentError if the rating or place is not numeric, or if both winner and place is specified.

# File lib/elo_rating/match.rb, line 22
def add_player(player_attributes)
  players << Player.new(player_attributes.merge(match: self))
  self
end
updated_ratings() click to toggle source

Calculates the updated ratings for each of the players in the match.

Raises an ArgumentError if more than one player is marked as the winner or if some but not all players have place specified.

# File lib/elo_rating/match.rb, line 31
def updated_ratings
  validate_players!
  players.map(&:updated_rating)
end

Private Instance Methods

inconsistent_places?() click to toggle source
# File lib/elo_rating/match.rb, line 47
def inconsistent_places?
  players.select { |player| player.place }.any? &&
    players.select { |player| !player.place }.any?
end
multiple_winners?() click to toggle source
# File lib/elo_rating/match.rb, line 43
def multiple_winners?
  players.select { |player| player.winner? }.size > 1
end
validate_players!() click to toggle source
# File lib/elo_rating/match.rb, line 38
def validate_players!
  raise ArgumentError, 'Only one player can be the winner' if multiple_winners?
  raise ArgumentError, 'All players must have places if any do' if inconsistent_places?
end