OSDN Git Service

Improted %%FORK command.
authorDaigo Moriwaki <daigo@debian.org>
Sun, 31 Mar 2013 04:44:13 +0000 (13:44 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Sun, 31 Mar 2013 04:44:13 +0000 (13:44 +0900)
%%FORK command: %%FORK <source_game> [<new_buoy_game>] [<nth-move>]
The new_buoy_game parameter is now optional. If it is not
supplied, Shogi-server generates a new buoy game name from
source_game.

changelog
shogi_server/command.rb
test/TC_command.rb
test/TC_fork.rb

index 1e4b366..8b31f4b 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,11 @@
+2013-03-31  Daigo Moriwaki <daigo at debian dot org>
+
+       * [shogi-server]
+         - %%FORK command: %%FORK <source_game> [<new_buoy_game>] [<nth-move>]
+           The new_buoy_game parameter is now optional. If it is not
+           supplied, Shogi-server generates a new buoy game name from
+           source_game.
+
 2013-02-23  Daigo Moriwaki <daigo at debian dot org>
 
        * [shogi-server]
index 99c2b41..9ce0d6b 100644 (file)
@@ -102,6 +102,11 @@ module ShogiServer
           nth_move = $3.to_i
         end
         cmd = ForkCommand.new(str, player, source_game, new_buoy_game, nth_move)
+      when /^%%FORK\s+(\S+)$/
+        source_game   = $1
+        new_buoy_game = nil
+        nth_move      = nil
+        cmd = ForkCommand.new(str, player, source_game, new_buoy_game, nth_move)
       when /^\s*$/
         cmd = SpaceCommand.new(str, player)
       when /^%%%[^%]/
@@ -830,6 +835,28 @@ module ShogiServer
       @new_buoy_game = new_buoy_game
       @nth_move      = nth_move # may be nil
     end
+    attr_reader :new_buoy_game
+
+    def decide_new_buoy_game_name
+      name       = nil
+      total_time = nil
+      byo_time   = nil
+
+      if @source_game.split("+").size >= 2 &&
+         /^([^-]+)-(\d+)-(\d+)/ =~ @source_game.split("+")[1]
+        name       = $1
+        total_time = $2
+        byo_time   = $3
+      end
+      if name == nil || total_time == nil || byo_time == nil
+        @player.write_safe(sprintf("##[ERROR] wrong source game name to make a new buoy game name: %s\n", @source_game))
+        log_error "Received a wrong source game name to make a new buoy game name: %s from %s." % [@source_game, @player.name]
+        return :continue
+      end
+      @new_buoy_game = "buoy_%s_%d-%s-%s" % [name, @nth_move, total_time, byo_time]
+      @player.write_safe(sprintf("##[FORK]: new buoy game name: %s\n", @new_buoy_game))
+      @player.write_safe("##[FORK] +OK\n")
+    end
 
     def call
       game = $league.games[@source_game]
@@ -850,6 +877,11 @@ module ShogiServer
       moves[0...@nth_move].each do |m|
         new_moves_str << m.join(",")
       end
+
+      unless @new_buoy_game
+        decide_new_buoy_game_name
+      end
+
       buoy_cmd = SetBuoyCommand.new(@str, @player, @new_buoy_game, new_moves_str, 1)
       return buoy_cmd.call
     end
index 83d11a9..8620296 100644 (file)
@@ -233,6 +233,11 @@ class TestFactoryMethod < Test::Unit::TestCase
     assert_instance_of(ShogiServer::ForkCommand, cmd)
   end
 
+  def test_fork_command2
+    cmd = ShogiServer::Command.factory("%%FORK server-denou-14400-60+p1+p2+20130223185013", @p)
+    assert_instance_of(ShogiServer::ForkCommand, cmd)
+  end
+
   def test_void_command
     cmd = ShogiServer::Command.factory("%%%HOGE", @p)
     assert_instance_of(ShogiServer::VoidCommand, cmd)
@@ -944,6 +949,21 @@ end
 
 #
 #
+class TestForkCommand < Test::Unit::TestCase
+  def setup
+    @player = MockPlayer.new
+  end
+
+  def test_new_buoy_game_name
+    src = "%%FORK server+denou-14400-60+p1+p2+20130223185013"
+    c = ShogiServer::ForkCommand.new src, @player, "server+denou-14400-60+p1+p2+20130223185013", nil, 13
+    c.decide_new_buoy_game_name
+    assert_equal "buoy_denou_13-14400-60", c.new_buoy_game
+  end
+end
+
+#
+#
 class TestGetBuoyCountCommand < BaseTestBuoyCommand
   def test_call
     buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
@@ -1056,4 +1076,3 @@ class TestMonitorHandler2 < Test::Unit::TestCase
                  @player.out.join)
   end
 end
-
index 64f5f0c..7e2b6f3 100644 (file)
@@ -85,4 +85,47 @@ class TestFork < BaseClient
 
     @admin.logout
   end
+
+  def test_fork2
+    buoy = ShogiServer::Buoy.new
+    
+    @admin = SocketPlayer.new "dummy", "admin", "*"
+    @admin.connect
+    @admin.reader
+    @admin.login
+
+    result, result2 = handshake do
+      source_game = parse_game_name(@admin)
+      @admin.puts "%%FORK #{source_game}" # nil for new_buoy_game name
+      sleep 1
+      assert /##\[FORK\]: new buoy game name: buoy_TestFork_1-1500-0/ =~ @admin.message
+    end
+
+    assert buoy.is_new_game?("buoy_TestFork_1-1500-0")
+    @p1 = SocketPlayer.new "buoy_TestFork_1", "p1", true
+    @p2 = SocketPlayer.new "buoy_TestFork_1", "p2", false
+    @p1.connect
+    @p2.connect
+    @p1.reader
+    @p2.reader
+    @p1.login
+    @p2.login
+    sleep 1
+    @p1.game
+    @p2.game
+    sleep 1
+    @p1.agree
+    @p2.agree
+    sleep 1
+    assert /^Total_Time:1500/ =~ @p1.message
+    assert /^Total_Time:1500/ =~ @p2.message
+    @p2.move("-3334FU")
+    sleep 1
+    @p1.toryo
+    sleep 1
+    @p2.logout
+    @p1.logout
+
+    @admin.logout
+  end
 end