Class: Elo::Player

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/elo/player.rb

Overview

A player. You need at least two play a Game.

Instance Method Summary collapse

Methods included from Helper

included, #initialize

Instance Method Details

#gamesObject

A list of games played by the player.



19
20
21
# File 'lib/elo/player.rb', line 19

def games
  @games ||= []
end

#games_playedObject

The number of games played is needed for calculating the K-factor.



14
15
16
# File 'lib/elo/player.rb', line 14

def games_played
  @games_played ||= games.size
end

#inspectObject



93
94
95
# File 'lib/elo/player.rb', line 93

def inspect
  "player"
end

#k_factorObject

Calculates the K-factor for the player. Elo allows you specify custom Rules (see Elo::Configuration).

You can set it manually, if you wish:

Elo::Player.new(:k_factor => 10)

This stops this player from using the K-factor rules.



61
62
63
64
65
66
67
# File 'lib/elo/player.rb', line 61

def k_factor
  return @k_factor if @k_factor
  Elo.config.applied_k_factors.each do |rule|
    return rule[:factor] if instance_eval(&rule[:rule])
  end
  Elo.config.default_k_factor
end

#loses_from(other_player, options = {}) ⇒ Object

Start a game with another player and set the score immediately.



89
90
91
# File 'lib/elo/player.rb', line 89

def loses_from(other_player, options = {})
  versus(other_player, options).lose
end

#plays_draw(other_player, options = {}) ⇒ Object

Start a game with another player and set the score immediately.



83
84
85
# File 'lib/elo/player.rb', line 83

def plays_draw(other_player, options = {})
  versus(other_player, options).draw
end

#pro?Boolean

FIDE regulations specify that once you reach a pro status (see pro_rating?), you are considered a pro for life.

You might need to specify it manually, when depending on external persistence of players.

Elo::Player.new(:pro => true)



43
44
45
# File 'lib/elo/player.rb', line 43

def pro?
  !!@pro
end

#pro_rating?Boolean

Is the player considered a pro, because his/her rating crossed the threshold configured? This is needed for calculating the K-factor.



25
26
27
# File 'lib/elo/player.rb', line 25

def pro_rating?
  rating >= Elo.config.pro_rating_boundry
end

#ratingObject

The rating you provided, or the default rating from configuration



9
10
11
# File 'lib/elo/player.rb', line 9

def rating
  @rating ||= Elo.config.default_rating
end

#saveObject

in a database or something like that. This method will be called when a result is known.



50
51
# File 'lib/elo/player.rb', line 50

def save
end

#starter?Boolean

Is the player just starting? Provide the boundry for the amount of games played in the configuration. This is needed for calculating the K-factor.



32
33
34
# File 'lib/elo/player.rb', line 32

def starter?
  games_played < Elo.config.starter_boundry
end

#versus(other_player, options = {}) ⇒ Object

Start a game with another player. At this point, no result is known and nothing really happens.



71
72
73
# File 'lib/elo/player.rb', line 71

def versus(other_player, options = {})
  Game.new(options.merge(:one => self, :two => other_player)).calculate
end

#wins_from(other_player, options = {}) ⇒ Object

Start a game with another player and set the score immediately.



77
78
79
# File 'lib/elo/player.rb', line 77

def wins_from(other_player, options = {})
  versus(other_player, options).win
end