OSDN Git Service

- shogi_server/command.rb: Commands specific to 81Dojo, startig with '%%%', are just...
[shogi-server/shogi-server.git] / shogi_server / command.rb
index a63cac2..f604ea9 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 "" 
@@ -96,16 +96,35 @@ module ShogiServer
         cmd = GetBuoyCountCommand.new(str, player, game_name)
       when /^\s*$/
         cmd = SpaceCommand.new(str, player)
+      when /^%%%[^%]/
+        # TODO: just ignore commands specific to 81Dojo.
+        # Need to discuss with 81Dojo people.
+        cmd = VoidCommand.new(str, player)
       else
         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
+
+  # Dummy command which does nothing.
+  #
+  class VoidCommand < Command
+    def initialize(str, player)
+      super
+    end
+
+    def call
+      return :continue
     end
   end
 
@@ -136,12 +155,17 @@ module ShogiServer
       if (@player.status == "game")
         array_str = @str.split(",")
         move = array_str.shift
+        if @player.game.last_move &&
+           @player.game.last_move.split(",").first == move
+          log_warning("Received two sequencial identical moves [#{move}] from #{@player.name}. The last one was ignored.")
+          return :continue
+        end
         additional = array_str.shift
         comment = nil
         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 +195,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
@@ -280,8 +304,8 @@ module ShogiServer
     def ==(rhs)
       return rhs != nil &&
              rhs.is_a?(MonitorHandler) &&
-             @player = rhs.player &&
-             @type   = rhs.type
+             @player == rhs.player &&
+             @type   == rhs.type
     end
 
     def write_safe(game_id, str)
@@ -366,10 +390,17 @@ module ShogiServer
     end
   end
 
-  class Monitor2OffCommand < MonitorOffCommand # same
+  class Monitor2OffCommand < MonitorOffCommand
     def initialize(str, player, game)
       super
     end
+
+    def call
+      if (@game)
+        @game.monitoroff(MonitorHandler2.new(@player))
+      end
+      return :continue
+    end
   end
 
   # Command of HELP
@@ -678,7 +709,7 @@ module ShogiServer
     end
 
     def call
-      msg = "##[ERROR] unknown command %s\n" % [@str]
+      msg = "##[ERROR] unknown command %s\n" % [@str.chomp]
       @player.write_safe(msg)
       log_error(msg)
       return :continue