OSDN Git Service

- shogi_server/command.rb: For an unknown error command log, an empty line is no...
[shogi-server/shogi-server.git] / shogi_server / command.rb
index 897413c..daf501f 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 "" 
@@ -49,6 +49,12 @@ module ShogiServer
       when /^%%MONITOROFF\s+(\S+)/
         game_id = $1
         cmd = MonitorOffCommand.new(str, player, $league.games[game_id])
+      when /^%%MONITOR2ON\s+(\S+)/
+        game_id = $1
+        cmd = Monitor2OnCommand.new(str, player, $league.games[game_id])
+      when /^%%MONITOR2OFF\s+(\S+)/
+        game_id = $1
+        cmd = Monitor2OffCommand.new(str, player, $league.games[game_id])
       when /^%%HELP/
         cmd = HelpCommand.new(str, player)
       when /^%%RATING/
@@ -94,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.
@@ -130,12 +139,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
@@ -165,7 +179,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
@@ -263,6 +277,53 @@ module ShogiServer
     end
   end
 
+  class MonitorHandler
+    def initialize(player)
+      @player = player
+      @type = nil
+      @header = nil
+    end
+    attr_reader :player, :type, :header
+
+    def ==(rhs)
+      return rhs != nil &&
+             rhs.is_a?(MonitorHandler) &&
+             @player == rhs.player &&
+             @type   == rhs.type
+    end
+
+    def write_safe(game_id, str)
+      str.chomp.split("\n").each do |line|
+        @player.write_safe("##[%s][%s] %s\n" % [@header, game_id, line.chomp])
+      end
+      @player.write_safe("##[%s][%s] %s\n" % [@header, game_id, "+OK"])
+    end
+  end
+
+  class MonitorHandler1 < MonitorHandler
+    def initialize(player)
+      super
+      @type = 1
+      @header = "MONITOR"
+    end
+
+    def write_one_move(game_id, game)
+      write_safe(game_id, game.show.chomp)
+    end
+  end
+
+  class MonitorHandler2 < MonitorHandler
+    def initialize(player)
+      super
+      @type = 2
+      @header = "MONITOR2"
+    end
+
+    def write_one_move(game_id, game)
+      write_safe(game_id, game.last_move.gsub(",", "\n"))
+    end
+  end
+
   # Command of MONITORON
   #
   class MonitorOnCommand < BaseCommandForGame
@@ -272,9 +333,9 @@ module ShogiServer
 
     def call
       if (@game)
-        @game.monitoron(@player)
-        @player.write_safe(@game.show.gsub(/^/, "##[MONITOR][#{@game_id}] "))
-        @player.write_safe("##[MONITOR][#{@game_id}] +OK\n")
+        monitor_handler = MonitorHandler1.new(@player)
+        @game.monitoron(monitor_handler)
+        monitor_handler.write_safe(@game_id, @game.show)
       end
       return :continue
     end
@@ -289,7 +350,38 @@ module ShogiServer
 
     def call
       if (@game)
-        @game.monitoroff(@player)
+        @game.monitoroff(MonitorHandler1.new(@player))
+      end
+      return :continue
+    end
+  end
+
+  # Command of MONITOR2ON
+  #
+  class Monitor2OnCommand < BaseCommandForGame
+    def initialize(str, player, game)
+      super
+    end
+
+    def call
+      if (@game)
+        monitor_handler = MonitorHandler2.new(@player)
+        @game.monitoron(monitor_handler)
+        lines = IO::readlines(@game.logfile).join("")
+        monitor_handler.write_safe(@game_id, lines)
+      end
+      return :continue
+    end
+  end
+
+  class Monitor2OffCommand < MonitorOffCommand
+    def initialize(str, player, game)
+      super
+    end
+
+    def call
+      if (@game)
+        @game.monitoroff(MonitorHandler2.new(@player))
       end
       return :continue
     end
@@ -397,14 +489,13 @@ module ShogiServer
         end
         @player.sente = nil
       else
-        if (@my_sente_str == "*")
+        if (@my_sente_str == "*") && !Login.handicapped_game_name?(@game_name)
           rival = $league.get_player("game_waiting", @game_name, nil, @player) # no preference
         elsif (@my_sente_str == "+")
           rival = $league.get_player("game_waiting", @game_name, false, @player) # rival must be gote
         elsif (@my_sente_str == "-")
           rival = $league.get_player("game_waiting", @game_name, true, @player) # rival must be sente
         else
-          ## never reached
           @player.write_safe(sprintf("##[ERROR] bad game option\n"))
           return :continue
         end
@@ -459,7 +550,8 @@ module ShogiServer
             Game::new(@player.game_name, @player, rival, board)
           end
         else
-          board = Board.new
+          klass = Login.handicapped_game_name?(@game_name) || Board
+          board = klass.new
           board.initial
           Game::new(@player.game_name, @player, rival, board)
         end
@@ -601,7 +693,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