OSDN Git Service

Fix some lint warnings
[shogi-server/shogi-server.git] / shogi_server / league / floodgate.rb
index 24ff68f..da46a8a 100644 (file)
@@ -12,7 +12,7 @@ class League
       # ex. "floodgate-900-0"
       #
       def game_name?(str)
-        return /^floodgate\-\d+\-\d+$/.match(str) ? true : false
+        return /^floodgate\-\d+\-\d+F?$/.match(str) ? true : false
       end
 
       def history_file_path(gamename)
@@ -36,8 +36,10 @@ class League
       # Options will be updated by NextTimeGenerator and then passed to a
       # pairing factory.
       @options = {}
-      @options[:pairing_factory] = hash[:pairing_factory] || "default_factory"
-      @options[:sacrifice]       = hash[:sacrifice] || "gps500+e293220e3f8a3e59f79f6b0efffaa931"
+      @options[:pairing_factory]     = hash[:pairing_factory] || "default_factory"
+      @options[:sacrifice]           = hash[:sacrifice] || "gps500+e293220e3f8a3e59f79f6b0efffaa931"
+      @options[:max_moves]           = hash[:max_moves] || Default_Max_Moves
+      @options[:least_time_per_move] = hash[:least_time_per_move] || Default_Least_Time_Per_Move
       charge if @next_time.nil?
     end
 
@@ -53,26 +55,44 @@ class League
       return @options[:sacrifice]
     end
 
+    def max_moves
+      return @options[:max_moves]
+    end
+
+    def least_time_per_move
+      return @options[:least_time_per_move]
+    end
+
     def charge
       ntg = NextTimeGenerator.factory(@game_name)
       if ntg
         @next_time = ntg.call(Time.now)
-        @options[:pairing_factory] = ntg.pairing_factory
-        @options[:sacrifice]       = ntg.sacrifice
+        @options[:pairing_factory]     = ntg.pairing_factory
+        @options[:sacrifice]           = ntg.sacrifice
+        @options[:max_moves]           = ntg.max_moves
+        @options[:least_time_per_move] = ntg.least_time_per_move
       else
         @next_time = nil
       end
     end
 
-    def match_game
-      log_message("Starting Floodgate games...: %s, %s" % [@game_name, @options])
+    # Returns an array of players who are allowed to participate in this
+    # Floodgate match
+    #
+    def select_players
       players = @league.find_all_players do |pl|
         pl.status == "game_waiting" &&
         game_name?(pl.game_name) &&
-        pl.sente == nil
+        pl.sente == nil &&
+        pl.rated? # Only players who have player ID can participate in Floodgate (rating match)
       end
+      return players
+    end
+
+    def match_game
+      log_message("Starting Floodgate games...: %s, %s" % [@game_name, @options])
       logics = Pairing.send(@options[:pairing_factory], @options)
-      Pairing.match(players, logics)
+      Pairing.match(select_players(), logics, @options)
     end
     
     #
@@ -102,12 +122,16 @@ class League
 
       attr_reader :pairing_factory
       attr_reader :sacrifice
+      attr_reader :max_moves
+      attr_reader :least_time_per_move
 
       # Constructor. 
       #
       def initialize
-        @pairing_factory = "default_factory"
-        @sacrifice       = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
+        @pairing_factory     = "default_factory"
+        @sacrifice           = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
+        @max_moves           = Default_Max_Moves
+        @least_time_per_move = Default_Least_Time_Per_Move
       end
     end
 
@@ -138,6 +162,12 @@ class League
     # * sacrifice:
     #   Specifies a sacrificed player.
     #   ex. set sacrifice gps500+e293220e3f8a3e59f79f6b0efffaa931
+    # * max_moves:
+    #   Sepcifies a number of max moves
+    #   ex. set max_moves 256
+    # * least_time_per_move:
+    #   Sepcifies a least time per move
+    #   ex. set least_time_per_move 0
     #
     class NextTimeGeneratorConfig < AbstructNextTimeGenerator
       
@@ -162,6 +192,10 @@ class League
             @pairing_factory = $1.chomp
           when %r!^\s*set\s+sacrifice\s+(.*)!
             @sacrifice = $1.chomp
+          when %r!^\s*set\s+max_moves\s+(\d+)!
+            @max_moves = $1.chomp.to_i
+          when %r!^\s*set\s+least_time_per_move\s+(\d+)!
+            @least_time_per_move = $1.chomp.to_i
           when %r!^\s*(\w+)\s+(\d{1,2}):(\d{1,2})!
             dow, hour, minute = $1, $2.to_i, $3.to_i
             dow_index = ::ShogiServer::parse_dow(dow)
@@ -354,12 +388,12 @@ class League
         @@mutex.synchronize do
           records = @records.reverse
         end
-        rc = records.find do |rc|
+        ret = records.find do |rc|
           rc[:winner] && 
           rc[:loser]  && 
           (rc[:black] == player_id || rc[:white] == player_id)
         end
-        return rc
+        return ret
       end
 
       def win_games(player_id)
@@ -367,10 +401,10 @@ class League
         @@mutex.synchronize do
           records = @records.reverse
         end
-        rc = records.find_all do |rc|
+        ret = records.find_all do |rc|
           rc[:winner] == player_id && rc[:loser]
         end
-        return rc
+        return ret
       end
 
       def loss_games(player_id)
@@ -378,10 +412,10 @@ class League
         @@mutex.synchronize do
           records = @records.reverse
         end
-        rc = records.find_all do |rc|
+        ret = records.find_all do |rc|
           rc[:winner] && rc[:loser] == player_id
         end
-        return rc
+        return ret
       end
     end # class History