Class: EloRating::Match

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

Overview

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

Defined Under Namespace

Classes: Player

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMatch

Creates a new match with no players.



9
10
11
# File 'lib/elo_rating/match.rb', line 9

def initialize
  @players = []
end

Instance Attribute Details

#playersObject (readonly)

All the players of the match.



6
7
8
# File 'lib/elo_rating/match.rb', line 6

def players
  @players
end

Instance Method Details

#add_player(player_attributes) ⇒ Object

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.



22
23
24
25
# File 'lib/elo_rating/match.rb', line 22

def add_player(player_attributes)
  players << Player.new(player_attributes.merge(match: self))
  self
end

#updated_ratingsObject

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.



31
32
33
34
# File 'lib/elo_rating/match.rb', line 31

def updated_ratings
  validate_players!
  players.map(&:updated_rating)
end