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



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

def initialize
  @players = []
end

Instance Attribute Details

#playersObject (readonly)

Returns the value of attribute players.



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

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.



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

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.



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

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