OSDN Git Service

* The counter of a buoy game is decremented before the game is started.
authorbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Thu, 6 Aug 2009 14:35:37 +0000 (14:35 +0000)
committerbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Thu, 6 Aug 2009 14:35:37 +0000 (14:35 +0000)
* GETBUOYCOUNT returns -1 when a non-existing game is searched.

changelog
shogi_server/buoy.rb
shogi_server/command.rb
shogi_server/game.rb
test/TC_buoy.rb
test/TC_command.rb
test/TC_config.rb

index 39131eb..45f1052 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,4 +1,4 @@
-2009-07-29 Daigo Moriwaki <daigo at debian dot org>
+2009-08-06 Daigo Moriwaki <daigo at debian dot org>
 
        * [shogi-server]
          - A experimental new feature, codenamed Buoy: it allows players to
@@ -26,7 +26,7 @@
              ex. %%DELETEBUOY buoy_foo-900-0
              - game_name is the buoy game name that was created.
            + %%GETBUOYCOUNT <game_name>
-             Show a current count of the buoy game or zero for non-existing
+             Show a current count of the buoy game or -1 for non-existing
              games.
 
 
index 7008ff1..c46c575 100644 (file)
@@ -102,28 +102,19 @@ module ShogiServer
         end
       end
     end
-  end
 
+    def decrement_count(buoy_game)
+      return if buoy_game.instance_of?(NilBuoyGame)
 
-  # Observer obserers GameResult of a buoy game
-  #
-  class BuoyObserver
-    def update(game_result)
-      game_name = game_result.game.game_name
-      unless game_result.instance_of?(GameResultWin)
-        log_message "#{game_name} was not valid. It will be retried"
-        return
-      end
-      buoy = Buoy.new
-      buoy_game = buoy.get_game(game_name)
       buoy_game.decrement_count
-      if buoy_game.count < 1
-        buoy.delete_game buoy_game
-        log_message "#{game_name} has finished."
-      else
-        buoy.update_game buoy_game
-        log_message "#{game_name} remains #{buoy_game.count} slots."
+      if buoy_game.count > 0
+        update_game buoy_game
+        log_message "Buoy #{buoy_game.game_name} remains #{buoy_game.count} slots."
+      else                
+        delete_game buoy_game
+        log_message "Buoy #{buoy_game.game_name} finished."
       end
     end
   end
+
 end # module ShogiServer
index 5910a3e..b677c5e 100644 (file)
@@ -441,7 +441,7 @@ module ShogiServer
             # When the game is set, it will be started.
           else
             buoy_game = buoy.get_game(@game_name)
-            if buoy_game.instance_of NilBuoyGame
+            if buoy_game.instance_of? NilBuoyGame
               # error. never reach
             end
             board = Board.new
@@ -452,6 +452,7 @@ module ShogiServer
               log_error "Failed to set up a buoy game: #{moves}"
               return :continue
             end
+            buoy.decrement_count(buoy_game)
             Game::new(@player.game_name, @player, rival, board)
           end
         else
@@ -653,6 +654,7 @@ module ShogiServer
       p2 = $league.get_player("game_waiting", @game_name, false, @player)
       return :continue unless p2
 
+      buoy.decrement_count(buoy_game)
       game = Game::new(@game_name, p1, p2, board)
       return :continue
     rescue WrongMoves => e
@@ -722,7 +724,7 @@ module ShogiServer
       buoy = Buoy.new
       buoy_game = buoy.get_game(@game_name)
       if buoy_game.instance_of?(NilBuoyGame)
-        @player.write_safe("##[GETBUOYCOUNT] 0\n")
+        @player.write_safe("##[GETBUOYCOUNT] -1\n")
       else
         @player.write_safe("##[GETBUOYCOUNT] %s\n" % [buoy_game.count])
       end
index ccb537e..cf6ab42 100644 (file)
@@ -77,10 +77,6 @@ class GameResult
       add_observer League::Floodgate::History.factory
     end
 
-    # TODO take observers from the game object
-    if Buoy.game_name?(@game.game_name)
-      add_observer(BuoyObserver.new)
-    end
   end
 
   def process
index 6311ff4..1c15ca0 100644 (file)
@@ -70,85 +70,3 @@ class TestBuoy < Test::Unit::TestCase
   end
 end
 
-
-class TestBuoyObserver < Test::Unit::TestCase
-  def setup
-    @dir = File.dirname(__FILE__)
-    @filename = File.join(@dir, "buoy.yaml")
-    @conf = {:topdir => @dir}
-    @buoy = ShogiServer::Buoy.new @conf
-  end
-  
-  def teardown
-    if File.exist? @filename
-      File.delete @filename
-    end
-  end
-
-  def test_update_game_result_win
-    p1 = MockPlayer.new
-    p1.sente = true
-    p2 = MockPlayer.new
-    p2.sente = false
-
-    buoy_game = ShogiServer::BuoyGame.new("buoy_1234-900-0", [], "p1", 2)
-    assert @buoy.is_new_game?(buoy_game.game_name)
-    @buoy.add_game buoy_game
-    assert !@buoy.is_new_game?(buoy_game.game_name)
-
-    game = MockGame.new
-    game.game_name = buoy_game.game_name
-    gr = ShogiServer::GameResultWin.new game, p1, p2
-    
-    observer = ShogiServer::BuoyObserver.new
-    observer.update(gr)
-
-    assert !@buoy.is_new_game?(buoy_game.game_name)
-    buoy_game2 = @buoy.get_game(buoy_game.game_name)
-    assert_equal 1, buoy_game2.count
-  end
-
-  def test_update_game_result_win_zero
-    p1 = MockPlayer.new
-    p1.sente = true
-    p2 = MockPlayer.new
-    p2.sente = false
-
-    buoy_game = ShogiServer::BuoyGame.new("buoy_1234-900-0", [], "p1", 1)
-    assert @buoy.is_new_game?(buoy_game.game_name)
-    @buoy.add_game buoy_game
-    assert !@buoy.is_new_game?(buoy_game.game_name)
-
-    game = MockGame.new
-    game.game_name = buoy_game.game_name
-    gr = ShogiServer::GameResultWin.new game, p1, p2
-    
-    observer = ShogiServer::BuoyObserver.new
-    observer.update(gr)
-
-    assert @buoy.is_new_game?(buoy_game.game_name)
-  end
-
-  def test_update_game_result_draw
-    p1 = MockPlayer.new
-    p1.sente = true
-    p2 = MockPlayer.new
-    p2.sente = false
-
-    buoy_game = ShogiServer::BuoyGame.new("buoy_1234-900-0", [], "p1", 2)
-    assert @buoy.is_new_game?(buoy_game.game_name)
-    @buoy.add_game buoy_game
-    assert !@buoy.is_new_game?(buoy_game.game_name)
-
-    game = MockGame.new
-    game.game_name = buoy_game.game_name
-    gr = ShogiServer::GameResultDraw.new game, p1, p2
-    
-    observer = ShogiServer::BuoyObserver.new
-    observer.update(gr)
-
-    assert !@buoy.is_new_game?(buoy_game.game_name)
-    buoy_game2 = @buoy.get_game(buoy_game.game_name)
-    assert_equal 2, buoy_game2.count
-  end
-end
index d343c8b..49f15aa 100644 (file)
@@ -687,9 +687,8 @@ class BaseTestBuoyCommand < Test::Unit::TestCase
   end
 
   def delete_buoy_yaml
-    if File.exist?(File.join($topdir, "buoy.yaml"))
-      File.delete File.join($topdir, "buoy.yaml")
-    end
+    file = File.join($topdir, "buoy.yaml")
+    File.delete file if File.exist? file
   end
 
   def test_dummy
@@ -733,9 +732,9 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     end
   end
 
-  def test_call
+  def test_call_2
     assert @buoy.is_new_game?("buoy_hoge-1500-0")
-    cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
+    cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 2
     rt = cmd.call
     assert :continue, rt
     assert !@buoy.is_new_game?("buoy_hoge-1500-0")
@@ -745,6 +744,16 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
     assert_equal ShogiServer::BuoyGame.new("buoy_hoge-1500-0", "+7776FU", @p.name, 1), buoy_game2
   end
 
+  def test_call_1
+    assert @buoy.is_new_game?("buoy_hoge-1500-0")
+    cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
+    rt = cmd.call
+    assert :continue, rt
+    assert @buoy.is_new_game?("buoy_hoge-1500-0")
+    assert !$p1.out.empty?
+    assert !$p2.out.empty?
+  end
+
   def test_call_error_not_buoy_game_name
     assert @buoy.is_new_game?("buoy_hoge-1500-0")
     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
@@ -852,7 +861,7 @@ class TestGetBuoyCountCommand < BaseTestBuoyCommand
     cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
     rt = cmd.call
     assert :continue, rt
-    assert_equal ["##[GETBUOYCOUNT] 0\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
+    assert_equal ["##[GETBUOYCOUNT] -1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
   end
 end
 
index 9dc3fd4..00b94e6 100644 (file)
@@ -65,19 +65,9 @@ class TestConfig < Test::Unit::TestCase
   end
 
   def test_top_dir2
-    topdir_orig = $topdir
-    $topdir = "/should_be_replaced"
+    assert !File.exist?(File.join("/", "tmp", ShogiServer::Config::FILENAME))
     conf = ShogiServer::Config.new({:topdir => "/tmp"})
     assert_equal "/tmp", conf[:topdir]
-    $topdir = topdir_orig
-  end
-
-  def test_top_dir3
-    topdir_orig = $topdir
-    $topdir = "/should_be_replaced"
-    conf = ShogiServer::Config.new({"topdir" => "/tmp"})
-    assert_equal "/tmp", conf[:topdir]
-    $topdir = topdir_orig
   end
 
   def test_braces1