OSDN Git Service

* Return [...] +OK after %%SETBUOY and %%DELETEBUOY commands
[shogi-server/shogi-server.git] / shogi_server / command.rb
index 5910a3e..0cf0025 100644 (file)
@@ -439,19 +439,23 @@ module ShogiServer
           if buoy.is_new_game?(@game_name)
             # The buoy game is not ready yet.
             # When the game is set, it will be started.
+            @player.status = "game_waiting"
           else
             buoy_game = buoy.get_game(@game_name)
-            if buoy_game.instance_of NilBuoyGame
+            if buoy_game.instance_of? NilBuoyGame
               # error. never reach
             end
+
+            moves_array = Board::split_moves(buoy_game.moves)
             board = Board.new
             begin
-              board.set_from_moves(buoy_game.moves)
+              board.set_from_moves(moves_array)
             rescue => err
               # it will never happen since moves have already been checked
               log_error "Failed to set up a buoy game: #{moves}"
               return :continue
             end
+            buoy.decrement_count(buoy_game)
             Game::new(@player.game_name, @player, rival, board)
           end
         else
@@ -607,7 +611,6 @@ module ShogiServer
   #
   #
   class SetBuoyCommand < Command
-    class WrongMoves < ArgumentError; end
 
     def initialize(str, player, game_name, moves, count)
       super(str, player)
@@ -635,8 +638,7 @@ module ShogiServer
       end
 
       # check moves
-      moves_array = split_moves @moves
-
+      moves_array = Board::split_moves(@moves)
       board = Board.new
       begin
         board.set_from_moves(moves_array)
@@ -646,13 +648,17 @@ module ShogiServer
 
       buoy_game = BuoyGame.new(@game_name, @moves, @player.name, @count)
       buoy.add_game(buoy_game)
+      @player.write_safe(sprintf("##[SETBUOY] +OK\n"))
+      log_info("A buoy game was created: %s by %s" % [@game_name, @player.name])
 
       # if two players, who are not @player, are waiting for a new game, start it
+      log_error $league.inspect
       p1 = $league.get_player("game_waiting", @game_name, true, @player)
       return :continue unless p1
       p2 = $league.get_player("game_waiting", @game_name, false, @player)
       return :continue unless p2
 
+      buoy.decrement_count(buoy_game)
       game = Game::new(@game_name, p1, p2, board)
       return :continue
     rescue WrongMoves => e
@@ -660,25 +666,6 @@ module ShogiServer
       log_error "Received wrong moves: %s from %s. [%s]" % [@moves, @player.name, e.message]
       return :continue
     end
-
-    private
-    
-    # Split a moves line into an array of a move string.
-    # If it fails to parse the moves, it raises WrongMoves.
-    # @param moves a moves line. Ex. "+776FU-3334Fu"
-    # @return an array of a move string. Ex. ["+7776FU", "-3334FU"]
-    #
-    def split_moves(moves)
-      ret = []
-
-      rs = moves.gsub %r{[\+\-]\d{4}\w{2}} do |s|
-             ret << s
-             ""
-           end
-      raise WrongMoves, rs unless rs.empty?
-
-      return ret
-    end
   end
 
   #
@@ -705,6 +692,7 @@ module ShogiServer
       end
 
       buoy.delete_game(buoy_game)
+      @player.write_safe(sprintf("##[DELETEBUOY] +OK\n"))
       log_info("A buoy game was deleted: %s" % [@game_name])
       return :continue
     end
@@ -722,7 +710,7 @@ module ShogiServer
       buoy = Buoy.new
       buoy_game = buoy.get_game(@game_name)
       if buoy_game.instance_of?(NilBuoyGame)
-        @player.write_safe("##[GETBUOYCOUNT] 0\n")
+        @player.write_safe("##[GETBUOYCOUNT] -1\n")
       else
         @player.write_safe("##[GETBUOYCOUNT] %s\n" % [buoy_game.count])
       end