OSDN Git Service

Refactored Player#set_sente_from_str().
[shogi-server/shogi-server.git] / shogi_server / command.rb
index c8b00c0..fe0ddf4 100644 (file)
@@ -96,6 +96,10 @@ 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
@@ -112,6 +116,18 @@ module ShogiServer
     attr_accessor :time
   end
 
+  # Dummy command which does nothing.
+  #
+  class VoidCommand < Command
+    def initialize(str, player)
+      super
+    end
+
+    def call
+      return :continue
+    end
+  end
+
   # Application-level protocol for Keep-Alive.
   # If the server receives an LF, it sends back an LF.  Note that the 30 sec
   # rule (client may not send LF again within 30 sec) is not implemented
@@ -374,7 +390,7 @@ module ShogiServer
     end
   end
 
-  class Monitor2OffCommand < MonitorOffCommand # same
+  class Monitor2OffCommand < MonitorOffCommand
     def initialize(str, player, game)
       super
     end
@@ -474,14 +490,6 @@ module ShogiServer
       end
 
       rival = nil
-      if (Buoy.game_name?(@game_name))
-        if (@my_sente_str != "*")
-          @player.write_safe(sprintf("##[ERROR] You are not allowed to specify TEBAN %s for the game %s\n", @my_sente_str, @game_name))
-          return :continue
-        end
-        @player.sente = nil
-      end # really end
-
       if (League::Floodgate.game_name?(@game_name))
         if (@my_sente_str != "*")
           @player.write_safe(sprintf("##[ERROR] You are not allowed to specify TEBAN %s for the game %s\n", @my_sente_str, @game_name))
@@ -489,42 +497,18 @@ module ShogiServer
         end
         @player.sente = nil
       else
-        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
-          @player.write_safe(sprintf("##[ERROR] bad game option\n"))
-          return :continue
+        rival = $league.find_rival(@player, @my_sente_str, @game_name)
+        if rival.instance_of?(Symbol)  
+          # An error happened. rival is not a player instance, but an error
+          # symobl that must be returned to the main routine immediately.
+          return rival
         end
       end
 
       if (rival)
         @player.game_name = @game_name
-        
-        if ((@my_sente_str == "*") && (rival.sente == nil))
-          if (rand(2) == 0)
-            @player.sente = true
-            rival.sente = false
-          else
-            @player.sente = false
-            rival.sente = true
-          end
-        elsif (rival.sente == true) # rival has higher priority
-          @player.sente = false
-        elsif (rival.sente == false)
-          @player.sente = true
-        elsif (@my_sente_str == "+")
-          @player.sente = true
-          rival.sente = false
-        elsif (@my_sente_str == "-")
-          @player.sente = false
-          rival.sente = true
-        else
-          ## never reached
-        end
+        Game::decide_turns(@player, @my_sente_str, rival)
+
         if (Buoy.game_name?(@game_name))
           buoy = Buoy.new # TODO config
           if buoy.is_new_game?(@game_name)
@@ -559,13 +543,7 @@ module ShogiServer
         if (@command_name == "GAME")
           @player.status = "game_waiting"
           @player.game_name = @game_name
-          if (@my_sente_str == "+")
-            @player.sente = true
-          elsif (@my_sente_str == "-")
-            @player.sente = false
-          else
-            @player.sente = nil
-          end
+          @player.set_sente_from_str(@my_sente_str)
         else                # challenge
           @player.write_safe(sprintf("##[ERROR] can't find rival for %s\n", @game_name))
           @player.status = "connected"
@@ -690,12 +668,17 @@ module ShogiServer
   class ErrorCommand < Command
     def initialize(str, player)
       super
+      @msg = nil
     end
+    attr_reader :msg
 
     def call
-      msg = "##[ERROR] unknown command %s\n" % [@str]
-      @player.write_safe(msg)
-      log_error(msg)
+      cmd = @str.chomp
+      # Aim to hide a possible password
+      cmd.gsub!(/LOGIN\s*(\w+)\s+.*/i, 'LOGIN \1...')
+      @msg = "##[ERROR] unknown command %s\n" % [cmd]
+      @player.write_safe(@msg)
+      log_error(@msg)
       return :continue
     end
   end