Module: EloRating

Defined in:
lib/elo_rating.rb,
lib/elo_rating/version.rb

Overview

EloRating helps you calculate Elo ratings.

See the README for an introduction.

Defined Under Namespace

Classes: Match

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.expected_score(player_rating, opponent_rating) ⇒ Object

Calculates the expected score of a player given their rating (player_rating) and their opponent’s rating (opponent_rating).

Returns a float between 0 and 1 where 0.999 represents high certainty of the first player winning.



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

def self.expected_score(player_rating, opponent_rating)
  1.0/(1 + (10 ** ((opponent_rating - player_rating)/400.0)))
end

.k_factor(rating = nil) ⇒ Object

Calls the K-factor function with the provided rating.



17
18
19
# File 'lib/elo_rating.rb', line 17

def self.k_factor(rating = nil)
  @k_factor.call(rating)
end

.k_factor=(k_factor) ⇒ Object

Sets the K-factor to a number.



41
42
43
44
45
# File 'lib/elo_rating.rb', line 41

def self.k_factor=(k_factor)
  @k_factor = Proc.new do
    k_factor
  end
end

.rating_adjustment(expected_score, actual_score, rating: nil, k_factor: nil) ⇒ Object

Calculates the amount a player’s rating should change.

Arguments

  • expected_score: a float between 0 and 1, representing the probability of

the player winning

  • actual_score: 0, 0.5, or 1, whether the outcome was a loss, draw, or win

(respectively)

  • rating (optional): the rating of the player, used by the K-factor function

  • k_factor (optional): the K-factor to use for this calculation to be used

instead of the normal K-factor or K-factor function

Returns a positive or negative float representing the amount the player’s rating should change.



69
70
71
72
# File 'lib/elo_rating.rb', line 69

def self.rating_adjustment(expected_score, actual_score, rating: nil, k_factor: nil)
  k_factor ||= k_factor(rating)
  k_factor * (actual_score - expected_score)
end

.set_k_factor(&k_factor) ⇒ Object

Sets the K-factor by providing a block that optionally takes a rating argument:

EloRating::set_k_factor do |rating|
  if rating && rating > 2500
    24
  else
    16
  end
end

Raises an ArgumentError if an exception is encountered when calling the provided block with nil as the argument



33
34
35
36
37
38
# File 'lib/elo_rating.rb', line 33

def self.set_k_factor(&k_factor)
  k_factor.call(nil)
  @k_factor = k_factor
rescue => e
  raise ArgumentError, "Error encountered in K-factor block when passed nil rating: #{e}"
end