Class: Elo::Game
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
-
#one ⇒ Object
readonly
The first Elo::Player.
-
#result ⇒ Object
readonly
The result is the result of the match.
-
#two ⇒ Object
readonly
The second Elo::Player.
Instance Method Summary collapse
- #calculate ⇒ Object
-
#draw ⇒ Object
It was a draw.
- #inspect ⇒ Object
-
#lose ⇒ Object
Player
:onehas lost! This is a shortcut method for setting the score to 0. -
#loser=(player) ⇒ Object
Set the loser.
-
#process_result(result) ⇒ Object
(also: #result=)
objects to update their scores.
-
#ratings ⇒ Object
Access the Elo::Rating objects for both players.
-
#save ⇒ Object
in a database or something like that.
-
#win ⇒ Object
Player
:onehas won! This is a shortcut method for setting the score to 1. -
#winner=(player) ⇒ Object
Set the winner.
Methods included from Helper
Instance Attribute Details
#one ⇒ Object (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 |
#result ⇒ Object (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 |
#two ⇒ Object (readonly)
The second Elo::Player.
20 21 22 |
# File 'lib/elo/game.rb', line 20 def two @two end |
Instance Method Details
#calculate ⇒ Object
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 |
#draw ⇒ Object
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 |
#inspect ⇒ Object
78 79 80 |
# File 'lib/elo/game.rb', line 78 def inspect "game" end |
#lose ⇒ Object
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 |
#ratings ⇒ Object
Access the Elo::Rating objects for both players.
74 75 76 |
# File 'lib/elo/game.rb', line 74 def @ratings ||= { one => , two => } end |
#save ⇒ Object
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 |
#win ⇒ Object
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 |