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.



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

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.



13
14
15
# File 'lib/elo/game.rb', line 13

def result
  @result
end

#twoObject (readonly)

The second Elo::Player.



20
21
22
# File 'lib/elo/game.rb', line 20

def two
  @two
end

Instance Method Details

#calculateObject



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

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



53
54
55
# File 'lib/elo/game.rb', line 53

def draw
  process_result 0.5
end

#inspectObject



78
79
80
# File 'lib/elo/game.rb', line 78

def inspect
  "game"
end

#loseObject

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



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

def lose
  process_result 0.0
end

#loser=(player) ⇒ Object

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



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

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

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

objects to update their scores.



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

def process_result(result)
  @result = result
  calculate
end

#ratingsObject

Access the Elo::Rating objects for both players.



74
75
76
# File 'lib/elo/game.rb', line 74

def ratings
  @ratings ||= { one => rating_one, two => rating_two }
end

#saveObject

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



60
61
# File 'lib/elo/game.rb', line 60

def save
end

#winObject

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



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

def win
  process_result 1.0
end

#winner=(player) ⇒ Object

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



64
65
66
# File 'lib/elo/game.rb', line 64

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