OSDN Git Service

Enhanced the CSA Login mode to accept a turn preference.
authordaigo <beatles@users.sourceforge.jp>
Sun, 5 Sep 2010 14:27:23 +0000 (23:27 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Sun, 5 Sep 2010 14:27:23 +0000 (23:27 +0900)
Logging in the server with the CSA mode, users are now allowed to
specify a turn preference in an enhanced gamename string which
looks like <gamename>-<time>-<time>-<turn>. The first three
parts are a regular game definition. The last "-<turn>"
part is optional.
+ Without -<turn> (i.e. same as the previous behavior), there
  is no turn preference. A user's turn will be defined randomly.
+ <turn> is either "B" for black or "W" for white.

changelog
shogi_server/login.rb
test/TC_login.rb

index 48bffe9..a38bc08 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,16 @@
+2010-09-05  Daigo Moriwaki <daigo at debian dot org>
+
+       * [shogi-server]
+         - shogi_server/login.rb: Enhanced the CSA Login mode.
+           Logging in the server with the CSA mode, users are now allowed to
+           specify a turn preference in an enhanced gamename string which
+           looks like <gamename>-<time>-<time>-<turn>. The first three 
+           parts are a regular game definition. The last "-<turn>"
+           part is optional. 
+           + Without -<turn> (i.e. same as the previous behavior), there
+           is no turn preference. A user's turn will be defined randomly. 
+           + <turn> is either "B" for black or "W" for white.
+
 2010-09-04  Daigo Moriwaki <daigo at debian dot org>
 
        * [shogi-server]
index 086e14f..4cee31c 100644 (file)
@@ -127,14 +127,40 @@ end
 class LoginCSA < Login
   PROTOCOL = "CSA"
 
+  attr_reader :gamename
+
+  # A turn preference string: "+", "-" or default "*"
+  attr_reader :turn_preference
+
   def initialize(player, password)
     @gamename = nil
+    @turn_preference = "*"
     super
     @player.protocol = PROTOCOL
   end
 
+  # Parse a gamename str and see if it includes an optional turn 
+  # preference. 
+  # ex. foo-1500-0-B for black
+  # ex. foo-1500-0-W for white
+  #
+  # Return an array of a valid gamename without an turn preference and a
+  # turn character "+" or "-"; false otherwise
+  #
+  def parse_gamename_turn(str)
+    if str =~ /^(.+)-\d+-\d+-(\w)$/
+      case $2
+      when "b","B"
+        return [str[0, str.length-2], "+"]
+      when "w","W"
+        return [str[0, str.length-2], "-"]
+      end
+    end
+    return false
+  end
+
   def parse_password(password)
-    if Login.good_game_name?(password)
+    if Login.good_game_name?(password) || parse_gamename_turn(password)
       @gamename = password
       @player.set_password(nil)
     elsif password.split(",").size > 1
@@ -144,12 +170,17 @@ class LoginCSA < Login
       @player.set_password(password)
       @gamename = Default_Game_Name
     end
-    @gamename = self.class.good_game_name?(@gamename) ? @gamename : Default_Game_Name
+    array = parse_gamename_turn(@gamename)
+    if array
+      @gamename = array.first
+      @turn_preference = array.last
+    end
+    @gamename = Login.good_game_name?(@gamename) ? @gamename : Default_Game_Name
   end
 
   def process
     super
-    @csa_1st_str = "%%GAME #{@gamename} *"
+    @csa_1st_str = "%%GAME #{@gamename} #{@turn_preference}"
   end
 end
 
index e79bf37..ad4cc93 100644 (file)
@@ -40,15 +40,73 @@ class TestLogin < Test::Unit::TestCase
     assert_instance_of(ShogiServer::LoginCSA, login)
     assert_equal("xyz", player.password)
     assert_equal(@p_csa.player_id, player.player_id)
+    assert_equal("*", login.turn_preference)
+  end
+
+  def test_login_factory_csa_no_gamename
+    player = ShogiServer::BasicPlayer.new
+    player.name = "hoge"
+    login = ShogiServer::Login::factory("LOGIN hoge xyz", player)
+    assert_instance_of(ShogiServer::LoginCSA, login)
+    assert_equal("xyz", player.password)
+    assert_equal(@p_csa.player_id, player.player_id)
+    assert_equal("*", login.turn_preference)
+    assert_equal(ShogiServer::Default_Game_Name, login.gamename)
+  end
+
+  def test_login_factory_csa_with_black
+    player = ShogiServer::BasicPlayer.new
+    player.name = "hoge"
+    login = ShogiServer::Login::factory("LOGIN hoge floodgate-900-0-B,xyz", player)
+    assert_instance_of(ShogiServer::LoginCSA, login)
+    assert_equal("xyz", player.password)
+    assert_equal(@p_csa.player_id, player.player_id)
+    assert_equal("+", login.turn_preference)
+    assert_equal("floodgate-900-0", login.gamename)
+  end
+
+  def test_login_factory_csa_with_white
+    player = ShogiServer::BasicPlayer.new
+    player.name = "hoge"
+    login = ShogiServer::Login::factory("LOGIN hoge floodgate-900-0-W,xyz", player)
+    assert_instance_of(ShogiServer::LoginCSA, login)
+    assert_equal("xyz", player.password)
+    assert_equal(@p_csa.player_id, player.player_id)
+    assert_equal("-", login.turn_preference)
+    assert_equal("floodgate-900-0", login.gamename)
   end
 
   def test_login_factory_csa_without_trip
     player = ShogiServer::BasicPlayer.new
     player.name = "hoge"
-    login = ShogiServer::Login::factory("LOGIN hoge floodagate-900-0", player)
+    login = ShogiServer::Login::factory("LOGIN hoge floodgate-900-0", player)
+    assert_instance_of(ShogiServer::LoginCSA, login)
+    assert_nil(player.password)
+    assert_equal(nil, player.player_id)
+    assert_equal("*", login.turn_preference)
+    assert_equal("floodgate-900-0", login.gamename)
+  end
+
+  def test_login_factory_csa_without_trip_with_black
+    player = ShogiServer::BasicPlayer.new
+    player.name = "hoge"
+    login = ShogiServer::Login::factory("LOGIN hoge floodgate-900-0-B", player)
+    assert_instance_of(ShogiServer::LoginCSA, login)
+    assert_nil(player.password)
+    assert_equal(nil, player.player_id)
+    assert_equal("+", login.turn_preference)
+    assert_equal("floodgate-900-0", login.gamename)
+  end
+
+  def test_login_factory_csa_without_trip_with_white
+    player = ShogiServer::BasicPlayer.new
+    player.name = "hoge"
+    login = ShogiServer::Login::factory("LOGIN hoge floodgate-900-0-W", player)
     assert_instance_of(ShogiServer::LoginCSA, login)
     assert_nil(player.password)
     assert_equal(nil, player.player_id)
+    assert_equal("-", login.turn_preference)
+    assert_equal("floodgate-900-0", login.gamename)
   end
 end