OSDN Git Service

Debugged History
authorbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Fri, 7 Nov 2008 07:14:51 +0000 (07:14 +0000)
committerbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Fri, 7 Nov 2008 07:14:51 +0000 (07:14 +0000)
shogi_server/league/floodgate.rb
test/TC_floodgate.rb

index 1d7d9e1..f568e56 100644 (file)
@@ -61,6 +61,8 @@ class League
         end
       end
 
+      attr_reader :records
+
       # file_path_name is a Pathname object for this storage
       #
       def initialize(file_path_name)
@@ -122,26 +124,26 @@ class League
       end
       
       def last_win?(player_id)
-        rc = last_valid_record(player_id)
+        rc = last_valid_game(player_id)
         return false unless rc
         return rc[:winner] == player_id
       end
       
       def last_lose?(player_id)
-        rc = last_valid_record(player_id)
+        rc = last_valid_game(player_id)
         return false unless rc
         return rc[:loser] == player_id
       end
 
-      def last_valid_record(player_id)
+      def last_valid_game(player_id)
         records = nil
-        @mutex.synchronize do
+        @@mutex.synchronize do
           records = @records.reverse
         end
         rc = records.find do |rc|
           rc[:winner] && 
           rc[:loser]  && 
-          (rc[:black] == player_id || rc[:while] == player_id)
+          (rc[:black] == player_id || rc[:white] == player_id)
         end
         return rc
       end
index f9ac2a7..aa499ef 100644 (file)
@@ -370,7 +370,7 @@ class TestFloodgateHistory < Test::Unit::TestCase
     @history = ShogiServer::League::Floodgate::History.new @file
   end
 
-  def teaup
+  def teardown
     @file.delete if @file.exist?
   end
 
@@ -381,4 +381,45 @@ class TestFloodgateHistory < Test::Unit::TestCase
     assert file.exist?
     file.delete if file.exist?
   end
+
+  def test_update
+    dummy = nil
+    def @history.make_record(game_result)
+      {:game_id => "wdoor+floodgate-900-0-hoge-foo-1", 
+       :black => "hoge",  :white => "foo",
+       :winner => "foo", :loser => "hoge"}
+    end
+    @history.update(dummy)
+
+    def @history.make_record(game_result)
+      {:game_id => "wdoor+floodgate-900-0-hoge-foo-2", 
+       :black => "hoge",  :white => "foo",
+       :winner => "hoge", :loser => "foo"}
+    end
+    @history.update(dummy)
+
+    def @history.make_record(game_result)
+      {:game_id => "wdoor+floodgate-900-0-hoge-foo-3", 
+       :black => "hoge",  :white => "foo",
+       :winner => nil, :loser => nil}
+    end
+    @history.update(dummy)
+
+    @history.load
+    assert_equal 3, @history.records.size
+    assert_equal "wdoor+floodgate-900-0-hoge-foo-1", @history.records[0][:game_id]
+    assert_equal "wdoor+floodgate-900-0-hoge-foo-2", @history.records[1][:game_id]
+    assert_equal "wdoor+floodgate-900-0-hoge-foo-3", @history.records[2][:game_id]
+    assert_equal "hoge", @history.records[1][:black]
+    assert_equal "foo",  @history.records[1][:white]
+    assert_equal "hoge", @history.records[1][:winner]
+    assert_equal "foo",  @history.records[1][:loser]
+
+    assert @history.last_win? "hoge"
+    assert !@history.last_win?("foo")
+    assert !@history.last_lose?("hoge")
+    assert @history.last_lose?("foo")
+  end
 end
+
+