Class: Elo::Game

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

Overview

A Game is a collection of two Elo::Player objects and a result. Once the result is known, it propagates the new ratings to the players.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

included, #initialize

Instance Attribute Details

#oneObject (readonly)

The first Elo::Player. The result is in perspecive of this player.


15
16
17
# File 'lib/elo/game.rb', line 15

def one
  @one
end

#resultObject (readonly)

The result is the result of the match. It's a nubmer from 0 to 1 from the perspective of player :one.


11
12
13
# File 'lib/elo/game.rb', line 11

def result
  @result
end

#twoObject (readonly)

The second Elo::Player.


18
19
20
# File 'lib/elo/game.rb', line 18

def two
  @two
end

Instance Method Details

#calculateObject


28
29
30
31
32
33
34
35
# File 'lib/elo/game.rb', line 28

def calculate
  if result
    one.send(:played, self)
    two.send(:played, self)
    save
  end
  self
end

#drawObject

It was a draw. This is a shortcut method for setting the score to 0.5


51
52
53
# File 'lib/elo/game.rb', line 51

def draw
  process_result 0.5
end

#inspectObject


76
77
78
# File 'lib/elo/game.rb', line 76

def inspect
  'game'
end

#loseObject

Player :one has lost! This is a shortcut method for setting the score to 0


45
46
47
# File 'lib/elo/game.rb', line 45

def lose
  process_result 0.0
end

#loser=(player) ⇒ Object

Set the loser. Provide it with a Elo::Player.


67
68
69
# File 'lib/elo/game.rb', line 67

def loser=(player)
  process_result(player == one ? 0.0 : 1.0)
end

#process_result(result) ⇒ Object Also known as: result=

Every time a result is set, it tells the Elo::Player objects to update their scores.


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

def process_result(result)
  @result = result
  calculate
end

#ratingsObject

Access the Elo::Rating objects for both players.


72
73
74
# File 'lib/elo/game.rb', line 72

def ratings
  @ratings ||= { one => rating_one, two => rating_two }
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.


58
59
# File 'lib/elo/game.rb', line 58

def save
end

#winObject

Player :one has won! This is a shortcut method for setting the score to 1


39
40
41
# File 'lib/elo/game.rb', line 39

def win
  process_result 1.0
end

#winner=(player) ⇒ Object

Set the winner. Provide it with a Elo::Player.


62
63
64
# File 'lib/elo/game.rb', line 62

def winner=(player)
  process_result(player == one ? 1.0 : 0.0)
end