OSDN Git Service

* [shogi-server] The server now provides more accurate time control.
authorDaigo Moriwaki <beatles@users.sourceforge.jp>
Sat, 27 Feb 2010 14:52:14 +0000 (23:52 +0900)
committerDaigo Moriwaki <beatles@users.sourceforge.jp>
Sat, 27 Feb 2010 14:52:14 +0000 (23:52 +0900)
Previouslly, a player's thinking time included a time waiting to get th
e giant lock. This may have caused games to time up, especially, during
byo-yomi etc.

changelog
shogi_server/command.rb
shogi_server/game.rb
shogi_server/player.rb
test/mock_game.rb

index fb8617c..76b79b9 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,11 @@
+2010-02-27  Daigo Moriwaki <daigo at debian dot org>
+
+       * [shogi-server]
+         - The server now provides more accurate time control. Previouslly,
+           a player's thinking time included a time waiting to get the giant
+           lock. This may have caused games to time up, especially, during
+           byo-yomi etc.
+
 2010-01-22  Daigo Moriwaki <daigo at debian dot org>
 
        * [shogi-server]
index a63cac2..ee81965 100644 (file)
@@ -25,7 +25,7 @@ module ShogiServer
   class Command
     # Factory method
     #
-    def Command.factory(str, player)
+    def Command.factory(str, player, time=Time.now)
       cmd = nil
       case str 
       when "" 
@@ -100,13 +100,16 @@ module ShogiServer
         cmd = ErrorCommand.new(str, player)
       end
 
+      cmd.time = time
       return cmd
     end
 
     def initialize(str, player)
       @str    = str
       @player = player
+      @time   = Time.now # this should be replaced later with a real time
     end
+    attr_accessor :time
   end
 
   # Application-level protocol for Keep-Alive.
@@ -141,7 +144,7 @@ module ShogiServer
         if /^'(.*)/ =~ additional
           comment = array_str.unshift("'*#{$1.toeuc}")
         end
-        s = @player.game.handle_one_move(move, @player)
+        s = @player.game.handle_one_move(move, @player, @time)
         @player.game.log_game(Kconv.toeuc(comment.first)) if (comment && comment.first && !s)
         return :return if (s && @player.protocol == LoginCSA::PROTOCOL)
       end
@@ -171,7 +174,7 @@ module ShogiServer
     def in_game_status
       rc = :continue
 
-      s = @player.game.handle_one_move(@str, @player)
+      s = @player.game.handle_one_move(@str, @player, @time)
       rc = :return if (s && @player.protocol == LoginCSA::PROTOCOL)
 
       return rc
index 9e8f71b..e920105 100644 (file)
@@ -174,7 +174,7 @@ class Game
   end
 
   # class Game
-  def handle_one_move(str, player)
+  def handle_one_move(str, player, end_time)
     unless turn?(player)
       return false if str == :timeout
 
@@ -186,7 +186,7 @@ class Game
     end
 
     finish_flag = true
-    @end_time = Time::new
+    @end_time = end_time
     t = [(@end_time - @start_time).floor, Least_Time_Per_Move].max
     
     move_status = nil
index 02b75f3..3302aa7 100644 (file)
@@ -258,6 +258,7 @@ class Player < BasicPlayer
   def run(csa_1st_str=nil)
     while ( csa_1st_str || 
             str = gets_safe(@socket, (@socket_buffer.empty? ? Default_Timeout : 1)) )
+      time = Time.now
       log(:info, :in, str) if str && str.instance_of?(String) 
       $mutex.lock
       begin
@@ -281,7 +282,11 @@ class Player < BasicPlayer
         end
         str.chomp! if (str.class == String) # may be strip! ?
 
-        cmd = ShogiServer::Command.factory(str, self)
+        delay = Time.now - time
+        if delay > 5
+          log_warning("Detected a long delay: %.2f sec" % [delay])
+        end
+        cmd = ShogiServer::Command.factory(str, self, time)
         case cmd.call
         when :return
           return
index 80263df..a30b326 100644 (file)
@@ -21,7 +21,7 @@ class MockGame
     @monitoroff_called = false
   end
 
-  def handle_one_move(move, player)
+  def handle_one_move(move, player, time)
     return @finish_flag
   end