OSDN Git Service

Merge branch '201312-fix-sacrifice'
[shogi-server/shogi-server.git] / shogi_server / league / floodgate.rb
index e3902de..24ff68f 100644 (file)
@@ -1,3 +1,5 @@
+require 'shogi_server/util'
+require 'date'
 require 'thread'
 require 'ostruct'
 require 'pathname'
@@ -21,35 +23,56 @@ class League
       end
     end # class method
 
-    attr_reader :next_time, :league, :game_name
+    # @next_time is updated  if and only if charge() was called
+    #
+    attr_reader :next_time
+    attr_reader :league, :game_name
+    attr_reader :options
 
     def initialize(league, hash={})
       @league = league
-      @next_time = hash[:next_time] || nil
-      @game_name = hash[:game_name] || "floodgate-900-0"
-      charge
+      @next_time       = hash[:next_time] || nil
+      @game_name       = hash[:game_name] || "floodgate-900-0"
+      # 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"
+      charge if @next_time.nil?
     end
 
     def game_name?(str)
       return Regexp.new(@game_name).match(str) ? true : false
     end
 
+    def pairing_factory
+      return @options[:pairing_factory]
+    end
+
+    def sacrifice
+      return @options[:sacrifice]
+    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
       else
         @next_time = nil
       end
     end
 
     def match_game
+      log_message("Starting Floodgate games...: %s, %s" % [@game_name, @options])
       players = @league.find_all_players do |pl|
         pl.status == "game_waiting" &&
         game_name?(pl.game_name) &&
         pl.sente == nil
       end
-      Pairing.match(players)
+      logics = Pairing.send(@options[:pairing_factory], @options)
+      Pairing.match(players, logics)
     end
     
     #
@@ -58,8 +81,13 @@ class League
       class << self
         def factory(game_name)
           ret = nil
+          conf_file_name = File.join($topdir, "#{game_name}.conf")
+
           if $DEBUG
             ret = NextTimeGenerator_Debug.new
+          elsif File.exists?(conf_file_name) 
+            lines = IO.readlines(conf_file_name)
+            ret =  NextTimeGeneratorConfig.new(lines)
           elsif game_name == "floodgate-900-0"
             ret = NextTimeGenerator_Floodgate_900_0.new
           elsif game_name == "floodgate-3600-0"
@@ -70,9 +98,103 @@ class League
       end
     end
 
-    class NextTimeGenerator_Floodgate_900_0
+    class AbstructNextTimeGenerator
+
+      attr_reader :pairing_factory
+      attr_reader :sacrifice
+
+      # Constructor. 
+      #
+      def initialize
+        @pairing_factory = "default_factory"
+        @sacrifice       = "gps500+e293220e3f8a3e59f79f6b0efffaa931"
+      end
+    end
+
+    # Schedule the next time from configuration files.
+    #
+    # Line format: 
+    #   # This is a comment line
+    #   set <parameter_name> <value>
+    #   DoW Time
+    #   ...
+    # where
+    #   DoW := "Sun" | "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" |
+    #          "Sunday" | "Monday" | "Tuesday" | "Wednesday" | "Thursday" |
+    #          "Friday" | "Saturday" 
+    #   Time := HH:MM
+    #
+    # For example,
+    #   Sat 13:00
+    #   Sat 22:00
+    #   Sun 13:00
+    #
+    # Set parameters:
+    #
+    # * pairing_factory:
+    #   Specifies a factory function name generating a pairing
+    #   method which will be used in a specific Floodgate game.
+    #   ex. set pairing_factory floodgate_zyunisen
+    # * sacrifice:
+    #   Specifies a sacrificed player.
+    #   ex. set sacrifice gps500+e293220e3f8a3e59f79f6b0efffaa931
+    #
+    class NextTimeGeneratorConfig < AbstructNextTimeGenerator
+      
+      # Constructor. 
+      # Read configuration contents.
+      #
+      def initialize(lines)
+        super()
+        @lines = lines
+      end
+
+      def call(now=Time.now)
+        if now.kind_of?(Time)
+          now = ::ShogiServer::time2datetime(now)
+        end
+        candidates = []
+        # now.cweek 1-53
+        # now.cwday 1(Monday)-7
+        @lines.each do |line|
+          case line
+          when %r!^\s*set\s+pairing_factory\s+(\w+)!
+            @pairing_factory = $1.chomp
+          when %r!^\s*set\s+sacrifice\s+(.*)!
+            @sacrifice = $1.chomp
+          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)
+            next if dow_index.nil?
+            next unless (0..23).include?(hour)
+            next unless (0..59).include?(minute)
+            time = DateTime::commercial(now.cwyear, now.cweek, dow_index, hour, minute) rescue next
+            time += 7 if time <= now 
+            candidates << time
+          when %r!^\s*#!
+            # Skip comment line
+          when %r!^\s*$!
+            # Skip empty line
+          else
+            log_warning("Floodgate: Unsupported syntax in a next time generator config file: %s" % [line]) 
+          end
+        end
+        candidates.map! {|dt| ::ShogiServer::datetime2time(dt)}
+        return candidates.empty? ? nil : candidates.min
+      end
+    end
+
+    # Schedule the next time for floodgate-900-0: each 30 minutes
+    #
+    class NextTimeGenerator_Floodgate_900_0 < AbstructNextTimeGenerator
+
+      # Constructor. 
+      #
+      def initialize
+        super
+      end
+
       def call(now)
-        # each 30 minutes
         if now.min < 30
           return Time.mktime(now.year, now.month, now.day, now.hour, 30)
         else
@@ -81,16 +203,32 @@ class League
       end
     end
 
-    class NextTimeGenerator_Floodgate_3600_0
+    # Schedule the next time for floodgate-3600-0: each 2 hours (odd hour)
+    #
+    class NextTimeGenerator_Floodgate_3600_0 < AbstructNextTimeGenerator
+
+      # Constructor. 
+      #
+      def initialize
+        super
+      end
+
       def call(now)
-        # each 2 hours (odd hour)
         return Time.mktime(now.year, now.month, now.day, now.hour) + ((now.hour%2)+1)*3600
       end
     end
 
-    class NextTimeGenerator_Debug
+    # Schedule the next time for debug: each 30 seconds.
+    #
+    class NextTimeGenerator_Debug < AbstructNextTimeGenerator
+
+      # Constructor. 
+      #
+      def initialize
+        super
+      end
+
       def call(now)
-        # for test, each 30 seconds
         if now.sec < 30
           return Time.mktime(now.year, now.month, now.day, now.hour, now.min, 30)
         else
@@ -153,8 +291,13 @@ class League
       def load
         return unless @file.exist?
 
-        @records = YAML.load_file(@file)
-        unless @records && @records.instance_of?(Array)
+        begin
+          @records = YAML.load_file(@file)
+          unless @records && @records.instance_of?(Array)
+            $logger.error "%s is not a valid yaml file. Instead, an empty array will be used and updated." % [@file]
+            @records = []
+          end
+        rescue
           $logger.error "%s is not a valid yaml file. Instead, an empty array will be used and updated." % [@file]
           @records = []
         end
@@ -194,6 +337,18 @@ class League
         return rc[:loser] == player_id
       end
 
+      def last_opponent(player_id)
+        rc = last_valid_game(player_id)
+        return nil unless rc
+        if rc[:black] == player_id
+          return rc[:white]
+        elsif rc[:white] == player_id
+          return rc[:black]
+        else
+          return nil
+        end
+      end
+
       def last_valid_game(player_id)
         records = nil
         @@mutex.synchronize do
@@ -206,6 +361,28 @@ class League
         end
         return rc
       end
+
+      def win_games(player_id)
+        records = nil
+        @@mutex.synchronize do
+          records = @records.reverse
+        end
+        rc = records.find_all do |rc|
+          rc[:winner] == player_id && rc[:loser]
+        end
+        return rc
+      end
+
+      def loss_games(player_id)
+        records = nil
+        @@mutex.synchronize do
+          records = @records.reverse
+        end
+        rc = records.find_all do |rc|
+          rc[:winner] && rc[:loser] == player_id
+        end
+        return rc
+      end
     end # class History