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.


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

def games
  @games ||= []
end

#games_playedObject

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


12
13
14
# File 'lib/elo/player.rb', line 12

def games_played
  @games_played ||= games.size
end

#inspectObject


91
92
93
# File 'lib/elo/player.rb', line 91

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.


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

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.


87
88
89
# File 'lib/elo/player.rb', line 87

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.


81
82
83
# File 'lib/elo/player.rb', line 81

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)

Returns:

  • (Boolean)

41
42
43
# File 'lib/elo/player.rb', line 41

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.

Returns:

  • (Boolean)

23
24
25
# File 'lib/elo/player.rb', line 23

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

#ratingObject

The rating you provided, or the default rating from configuration


7
8
9
# File 'lib/elo/player.rb', line 7

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

#saveObject

You can override this method if you store each game in a database or something like that. This method will be called when a result is known.


48
49
# File 'lib/elo/player.rb', line 48

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.

Returns:

  • (Boolean)

30
31
32
# File 'lib/elo/player.rb', line 30

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.


69
70
71
# File 'lib/elo/player.rb', line 69

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.


75
76
77
# File 'lib/elo/player.rb', line 75

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