From e4a0423d429600312f940c4acd6a37be70218620 Mon Sep 17 00:00:00 2001 From: Daigo Moriwaki Date: Sun, 31 Mar 2013 13:44:13 +0900 Subject: [PATCH] Improted %%FORK command. %%FORK command: %%FORK [] [] 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 | 8 ++++++++ shogi_server/command.rb | 32 ++++++++++++++++++++++++++++++++ test/TC_command.rb | 21 ++++++++++++++++++++- test/TC_fork.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/changelog b/changelog index 1e4b366..8b31f4b 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,11 @@ +2013-03-31 Daigo Moriwaki + + * [shogi-server] + - %%FORK command: %%FORK [] [] + 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 * [shogi-server] diff --git a/shogi_server/command.rb b/shogi_server/command.rb index 99c2b41..9ce0d6b 100644 --- a/shogi_server/command.rb +++ b/shogi_server/command.rb @@ -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 diff --git a/test/TC_command.rb b/test/TC_command.rb index 83d11a9..8620296 100644 --- a/test/TC_command.rb +++ b/test/TC_command.rb @@ -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 - diff --git a/test/TC_fork.rb b/test/TC_fork.rb index 64f5f0c..7e2b6f3 100644 --- a/test/TC_fork.rb +++ b/test/TC_fork.rb @@ -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 -- 2.11.0