Skip to content
Browse files

Add options to versus method

  • Loading branch information...
1 parent 2343ce1 commit 2fff5b2177c6f8f079d4f1a97039e9dab005b1bc @iain committed Mar 18, 2010
Showing with 39 additions and 21 deletions.
  1. +6 −4 README.rdoc
  2. +14 −4 lib/elo/game.rb
  3. +12 −8 lib/elo/player.rb
  4. +7 −5 spec/elo_spec.rb
View
10 README.rdoc
@@ -43,18 +43,20 @@ There is more than one way to do it, choose whatever works from your other code:
game7 = bob.versus(jane)
game7.result = 1 # result is in perspective of bob, so bob wins
+ game8 = bob.versus(jane, :result => 0) # jane wins
+
You can get all kinds of info from a player:
- bob.rating # => 1084
+ bob.rating # => 1080
bob.pro? # => false
bob.starter? # => true
- bob.games_played # => 7
- bob.games # => [ game1, game2, ... game7 ]
+ bob.games_played # => 8
+ bob.games # => [ game1, game2, ... game8 ]
Or get a list of players:
Elo::Player.all # => [ bob, jane ]
- Elo::Game.all # => [ game1, game2, ... game7 ]
+ Elo::Game.all # => [ game1, game2, ... game8 ]
== Configuration
View
18 lib/elo/game.rb
@@ -23,13 +23,19 @@ class Game
# objects to update their scores.
def process_result(result)
@result = result
- one.send(:played, self)
- two.send(:played, self)
- save
- self
+ calculate
end
alias result= process_result
+ def calculate
+ if result
+ one.send(:played, self)
+ two.send(:played, self)
+ save
+ end
+ self
+ end
+
# Player +:one+ has won!
# This is a shortcut method for setting the score to 1
def win
@@ -69,6 +75,10 @@ def ratings
@ratings ||= { one => rating_one, two => rating_two }
end
+ def inspect
+ "game"
+ end
+
private
# Create an Elo::Rating object for player one
View
20 lib/elo/player.rb
@@ -68,26 +68,30 @@ def k_factor
# Start a game with another player. At this point, no
# result is known and nothing really happens.
- def versus(other_player)
- Game.new(:one => self, :two => other_player)
+ def versus(other_player, options = {})
+ Game.new(options.merge(:one => self, :two => other_player)).calculate
end
# Start a game with another player and set the score
# immediately.
- def wins_from(other_player)
- versus(other_player).win
+ def wins_from(other_player, options = {})
+ versus(other_player, options).win
end
# Start a game with another player and set the score
# immediately.
- def plays_draw(other_player)
- versus(other_player).draw
+ def plays_draw(other_player, options = {})
+ versus(other_player, options).draw
end
# Start a game with another player and set the score
# immediately.
- def loses_from(other_player)
- versus(other_player).lose
+ def loses_from(other_player, options = {})
+ versus(other_player, options).lose
+ end
+
+ def inspect
+ "player"
end
private
View
12 spec/elo_spec.rb
@@ -27,15 +27,17 @@
game7 = bob.versus(jane)
game7.result = 1
- bob.rating.should == 1084
- jane.rating.should == 1409
+ game8 = bob.versus(jane, :result => 0)
+
+ bob.rating.should == 1080
+ jane.rating.should == 1412
bob.should_not be_pro
bob.should be_starter
- bob.games_played.should == 7
- bob.games.should == [ game1, game2, game3, game4, game5, game6, game7 ]
+ bob.games_played.should == 8
+ bob.games.should == [ game1, game2, game3, game4, game5, game6, game7, game8 ]
Elo::Player.all.should == [ bob, jane ]
- Elo::Game.all.should == [ game1, game2, game3, game4, game5, game6, game7 ]
+ Elo::Game.all.should == [ game1, game2, game3, game4, game5, game6, game7, game8 ]
end

0 comments on commit 2fff5b2

Please sign in to comment.
Something went wrong with that request. Please try again.