Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cache winner and losers score

  • Loading branch information...
commit 957001b4344c56a83c2a4068e553de6c23a8d5df 1 parent 7e005da
@agilion authored
View
2  db/migrate/20120211205503_create_games.rb
@@ -7,6 +7,8 @@ def self.up
t.integer :loser_id
t.integer :challenger_score
t.integer :challengee_score
+ t.integer :winner_score
+ t.integer :loser_score
t.boolean :completed
t.boolean :accepted
t.timestamp :accepted_at
View
10 models/game.rb
@@ -7,6 +7,8 @@ class Game < ActiveRecord::Base
def finalize!
self.winner = find_the_winner
self.loser = find_the_loser
+ self.winner_score = find_winners_score
+ self.loser_score = find_losers_score
self.completed = true
self.completed_at = Time.now
# Player.rank_players!
@@ -25,6 +27,14 @@ def find_the_loser
challenger_score < challengee_score ? challenger : challengee
end
+ def find_winners_score
+ winner == challenger ? challenger_score : challengee_score
+ end
+
+ def find_losers_score
+ winner == challenger ? challengee_score : challenger_score
+ end
+
def its_a_tie!
challenger_score == challengee_score
end
View
10 test/models/game_test.rb
@@ -41,4 +41,14 @@ def test_a_tie_game
@game.challengee = @player_b
assert_equal true, @game.its_a_tie!
end
+
+ def test_find_the_winners_score
+ @game = Game.create(:challenger => @player_a, :challengee => @player_b, :challenger_score => 21, :challengee_score => 19, :winner => @player_a, :loser => @player_b, :completed_at => Time.now)
+ @game.finalize!
+
+ assert_equal @player_a, @game.winner
+ assert_equal @player_b, @game.loser
+ assert_equal 21, @game.winner_score
+ assert_equal 19, @game.loser_score
+ end
end
View
37 test/models/player_test.rb
@@ -39,41 +39,4 @@ def test_player_wins_a_game
assert_equal @player_b, @game.loser
end
- def test_calculate_player_rank
- 10.times do
- # Sean beats Pete 10 times by 2
- Game.create(:challenger => @player_a, :challengee => @player_b, :challenger_score => 21, :challengee_score => 19, :winner => @player_a, :loser => @player_b, :completed_at => Time.now)
- # Sean beats Tom 10 times by 6
- Game.create(:challenger => @player_a, :challengee => @player_c, :challenger_score => 21, :challengee_score => 15, :winner => @player_a, :loser => @player_c, :completed_at => Time.now)
- # Pete beats Tom 10 times by 4
- Game.create(:challenger => @player_b, :challengee => @player_c, :challenger_score => 21, :challengee_score => 17, :winner => @player_b, :loser => @player_c, :completed_at => Time.now)
- end
-
- # Pete beats Sean by 4
- 5.times do
- Game.create(:challenger => @player_b, :challengee => @player_a, :challenger_score => 21, :challengee_score => 18, :winner => @player_b, :loser => @player_a, :completed_at => Time.now)
- end
-
- 3.times do
- # Tom beats Pete by 3
- Game.create(:challenger => @player_c, :challengee => @player_b, :challenger_score => 21, :challengee_score => 18, :winner => @player_c, :loser => @player_b, :completed_at => Time.now)
- # Tom beats Sean by 1
- Game.create(:challenger => @player_c, :challengee => @player_a, :challenger_score => 21, :challengee_score => 20, :winner => @player_c, :loser => @player_a, :completed_at => Time.now)
- end
-
- Player.rank_players!
-
- # Sean, Pete, Tom
- assert_equal @player_a, Player.by_rank.first
- assert_equal @player_b, Player.by_rank.second
- assert_equal @player_c, Player.by_rank.third
-
- puts %W(Pong_Player Wins Losses Rank).join("\t")
- %W(first second third).each do |place|
- puts [Player.by_rank.send(place).full_name, Player.by_rank.send(place).wins.count, Player.by_rank.send(place).losses.count, Player.by_rank.send(place).rank].join("\t")
- end
-
- # assert false
- end
-
end
Please sign in to comment.
Something went wrong with that request. Please try again.