OSDN Git Service

* [shogi-server] The server now provides more accurate time control.
[shogi-server/shogi-server.git] / shogi_server / player.rb
index f893c6f..3302aa7 100644 (file)
@@ -31,6 +31,7 @@ class BasicPlayer
     @loss = 0
     @last_game_win = false
     @sente = nil
+    @game_name = ""
   end
 
   # Idetifier of the player in the rating system
@@ -60,6 +61,19 @@ class BasicPlayer
   # true for Sente; false for Gote
   attr_accessor :sente
 
+  # game name
+  attr_accessor :game_name
+
+  def is_human?
+    return [%r!_human$!, %r!_human@!].any? do |re|
+      re.match(@name)
+    end
+  end
+
+  def is_computer?
+    return !is_human?
+  end
+
   def modified_at
     @modified_at || Time.now
   end
@@ -112,7 +126,6 @@ class Player < BasicPlayer
     @protocol = nil             # CSA or x1
     @eol = eol || "\m"          # favorite eol code
     @game = nil
-    @game_name = ""
     @mytime = 0                 # set in start method also
     @socket_buffer = []
     @main_thread = Thread::current
@@ -122,7 +135,7 @@ class Player < BasicPlayer
   end
 
   attr_accessor :socket, :status
-  attr_accessor :protocol, :eol, :game, :mytime, :game_name
+  attr_accessor :protocol, :eol, :game, :mytime
   attr_accessor :main_thread
   attr_reader :socket_buffer
   
@@ -245,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
@@ -268,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