OSDN Git Service

Merge branch '201312-usiToCsa'
authorDaigo Moriwaki <daigo@debian.org>
Sat, 22 Feb 2014 12:54:41 +0000 (21:54 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Sat, 22 Feb 2014 12:54:41 +0000 (21:54 +0900)
Conflicts:
changelog

12 files changed:
changelog
shogi-server
shogi_server.rb
shogi_server/league/floodgate.rb
shogi_server/league/floodgate_thread.rb
shogi_server/pairing.rb
shogi_server/player.rb
test/TC_floodgate_next_time_generator.rb
test/TC_floodgate_thread.rb
test/TC_fork.rb
test/TC_pairing.rb
webserver

index c498f62..f167a90 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,26 @@
+2013-12-29  Daigo Moriwaki <daigo at debian dot org>
+
+       * [shogi-server]
+         - Previously, parameters in Floodgate time configuration file were
+           not applied to actual instances. This issue has been fixed.
+
+2013-12-21  Daigo Moriwaki <daigo at debian dot org>
+
+       * [shogi-server]
+         - shogi_server/pairing.rb: Impose penalty on matches between
+           likely kin players.
+
+2013-12-15  Daigo Moriwaki <daigo at debian dot org>
+
+       * [webserver]
+         - Listen on only IPv4 explicitly. Under ruby 1.9.3p194, without
+           any bind addresses, ruby attempts to listen on both "0.0.0.0"
+           and "::". Specifying "::" on Linux leads to listen on both IPv4
+           and IPv6, which results in a warning message saying "Address
+           already in use - bind(2)".
+       * [shogi-server]
+         - Released: Revision "20131215"
+
 2013-12-14  Daigo Moriwaki <daigo at debian dot org>
 
        * [usiToCsa]
@@ -8,8 +31,40 @@
 2013-12-13  Daigo Moriwaki <daigo at debian dot org>
 
        * [shogi-server]
+         - shogi_server/league/floodgate_thread.rb:
+           - Added a log message.
+
+2013-12-08  Daigo Moriwaki <daigo at debian dot org>
+
+       * [shogi-server]
+         - shogi-server/shogi_server/pairing.rb:
+           - Modified comment for LeastDiff match.
+           - Changed log messages and levels. 
+
+2013-12-07  Daigo Moriwaki <daigo at debian dot org>
+
+       * [shogi-server]
+         - shogi_server/pairing.rb, player.rb:
+           Simplify estimated rate of unrated players (less memory).
+         - Enhanced syntax of Floodgate time configuration file.
+           Now it supports "set sacrifice <player_id>"; it sets a
+           sacrificed player for a specific Floodgate game.
+           ex. set sacrifice gps500+e293220e3f8a3e59f79f6b0efffaa931
+
+2013-12-05  Daigo Moriwaki <daigo at debian dot org>
+
+       * [shogi-server]
+         - Added a new pairing method, ShogiServer::ExcludeUnratedPlayers,
+           which filters out unrated players.
+         - Enhanced syntax of Floodgate time configuration file.
+           Now it supports "set pairing_factory <function_name>"; it sets a
+           factory function name generating a pairing method which will be
+           used in a specific Floodgate game.
+           ex. set pairing_factory floodgate_zyunisen 
+=======
          - Implemented conversion of move representation between CSA format
            and USI one.
+>>>>>>> 201312-usiToCsa
 
 2013-11-24  Daigo Moriwaki <daigo at debian dot org>
 
index c20dba8..33a2d70 100755 (executable)
@@ -138,6 +138,20 @@ FLOODGATE SCHEDULE CONFIGURATIONS
                Sat 22:00
                Sun 13:00
 
+            PAREMETER SETTING
+
+            In addition, this configuration file allows to set parameters
+            for the specific Floodaget group. A list of parameters is the
+            following:
+
+            * 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
+
 LICENSE
         GPL versoin 2 or later
 
index 4ddf963..f1161c2 100644 (file)
@@ -51,7 +51,7 @@ Default_Game_Name = "default-1500-0"
 One_Time = 10
 Least_Time_Per_Move = 1
 Login_Time = 300                # time for LOGIN
-Revision = "20131104"
+Revision = "20131215"
 
 RELOAD_FILES = ["shogi_server/league/floodgate.rb",
                 "shogi_server/league/persistent.rb",
index b308454..24ff68f 100644 (file)
@@ -27,11 +27,17 @@ class League
     #
     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"
+      @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
 
@@ -39,22 +45,34 @@ class League
       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
     
     #
@@ -80,10 +98,24 @@ class League
       end
     end
 
+    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
@@ -97,12 +129,23 @@ class League
     #   Sat 22:00
     #   Sun 13:00
     #
-    class NextTimeGeneratorConfig
+    # 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
 
@@ -114,7 +157,12 @@ class League
         # now.cweek 1-53
         # now.cwday 1(Monday)-7
         @lines.each do |line|
-          if %r!^\s*(\w+)\s+(\d{1,2}):(\d{1,2})! =~ 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?
@@ -123,6 +171,12 @@ class League
             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)}
@@ -132,7 +186,14 @@ class League
 
     # Schedule the next time for floodgate-900-0: each 30 minutes
     #
-    class NextTimeGenerator_Floodgate_900_0
+    class NextTimeGenerator_Floodgate_900_0 < AbstructNextTimeGenerator
+
+      # Constructor. 
+      #
+      def initialize
+        super
+      end
+
       def call(now)
         if now.min < 30
           return Time.mktime(now.year, now.month, now.day, now.hour, 30)
@@ -144,7 +205,14 @@ class League
 
     # Schedule the next time for floodgate-3600-0: each 2 hours (odd hour)
     #
-    class NextTimeGenerator_Floodgate_3600_0
+    class NextTimeGenerator_Floodgate_3600_0 < AbstructNextTimeGenerator
+
+      # Constructor. 
+      #
+      def initialize
+        super
+      end
+
       def call(now)
         return Time.mktime(now.year, now.month, now.day, now.hour) + ((now.hour%2)+1)*3600
       end
@@ -152,7 +220,14 @@ class League
 
     # Schedule the next time for debug: each 30 seconds.
     #
-    class NextTimeGenerator_Debug
+    class NextTimeGenerator_Debug < AbstructNextTimeGenerator
+
+      # Constructor. 
+      #
+      def initialize
+        super
+      end
+
       def call(now)
         if now.sec < 30
           return Time.mktime(now.year, now.month, now.day, now.hour, now.min, 30)
index 4cd9be1..e45f60a 100644 (file)
@@ -64,19 +64,20 @@ module ShogiServer
     end
 
     def start_games(floodgate)
-      log_message("Starting Floodgate games...: %s" % [floodgate.game_name])
       $league.reload
       floodgate.match_game
     end
 
-    # Regenerate floodgate instances from next_array for the next matches.
-    # @param next_array array of [game_name, next_time]
+    # Regenerate floodgate instances from next_instances for the next matches.
+    # @param next_instances array of [game_name, next_time]
     #
-    def regenerate_leagues(next_array)
-      leagues = next_array.collect do |game_name, next_time|
+    def regenerate_leagues(next_instances)
+      leagues = next_instances.collect do |prev|
+        log_message("Regenerating a floodgate league...: %s %s %s %s" %
+                    [prev.game_name, prev.next_time, prev.pairing_factory, prev.sacrifice])
         floodgate = ShogiServer::League::Floodgate.new($league, 
-                                                       {:game_name => game_name,
-                                                        :next_time => next_time})
+                      {:game_name       => prev.game_name,       :next_time => prev.next_time,
+                       :pairing_factory => prev.pairing_factory, :sacrifice => prev.sacrifice})
       end
       floodgate_reload_log(leagues)
       return leagues
@@ -98,20 +99,18 @@ module ShogiServer
             floodgate = next_league(leagues)
             next if wait_next_floodgate(floodgate)
 
-            next_array = leagues.collect do |floodgate|
-              if (floodgate.next_time - Time.now) > 0
-                [floodgate.game_name, floodgate.next_time]
-              else
+            next_instances = leagues.collect do |floodgate|
+              unless (floodgate.next_time - Time.now) > 0
                 start_games(floodgate)
                 floodgate.charge # updates next_time
-                [floodgate.game_name, floodgate.next_time] 
               end
+              floodgate
             end
 
             reload_shogi_server
 
             # Regenerate floodgate instances after ShogiServer.realod
-            leagues = regenerate_leagues(next_array)
+            leagues = regenerate_leagues(next_instances)
           rescue Exception => ex 
             # ignore errors
             log_error("[in Floodgate's thread] #{ex} #{ex.backtrace}")
index 11f7665..fec47af 100644 (file)
@@ -24,44 +24,52 @@ module ShogiServer
   class Pairing
 
     class << self
-      def default_factory
-        return least_diff_pairing
+      def default_factory(options)
+        return least_diff_pairing(options)
       end
 
-      def sort_by_rate_with_randomness
+      def sort_by_rate_with_randomness(options)
         return [LogPlayers.new,
-                ExcludeSacrificeGps500.new,
+                ExcludeSacrifice.new(options[:sacrifice]),
                 MakeEven.new,
                 SortByRateWithRandomness.new(1200, 2400),
                 StartGameWithoutHumans.new]
       end
 
-      def random_pairing
+      def random_pairing(options)
         return [LogPlayers.new,
-                ExcludeSacrificeGps500.new,
+                ExcludeSacrifice.new(options[:sacrifice]),
                 MakeEven.new,
                 Randomize.new,
                 StartGameWithoutHumans.new]
       end
 
-      def swiss_pairing
+      def swiss_pairing(options)
         return [LogPlayers.new,
-                ExcludeSacrificeGps500.new,
+                ExcludeSacrifice.new(options[:sacrifice]),
                 MakeEven.new,
                 Swiss.new,
                 StartGameWithoutHumans.new]
       end
 
-      def least_diff_pairing
+      def least_diff_pairing(options)
         return [LogPlayers.new,
-                ExcludeSacrificeGps500.new,
+                ExcludeSacrifice.new(options[:sacrifice]),
                 MakeEven.new,
                 LeastDiff.new,
                 StartGameWithoutHumans.new]
       end
 
-      def match(players)
-        logics = default_factory
+      def floodgate_zyunisen(options)
+        return [LogPlayers.new,
+                ExcludeUnratedPlayers.new,
+                ExcludeSacrifice.new(options[:sacrifice]),
+                MakeEven.new,
+                LeastDiff.new,
+                StartGameWithoutHumans.new]
+      end
+
+      def match(players, logics)
         logics.inject(players) do |result, item|
           item.match(result)
           result
@@ -136,7 +144,7 @@ module ShogiServer
     def match(players)
       super
       if players.size < 2
-        log_warning("Floodgate: There should be more than one player (%d)." % [players.size])
+        log_message("Floodgate: There are less than two players: %d" % [players.size])
         return
       end
       if players.size.odd?
@@ -159,7 +167,7 @@ module ShogiServer
       super
       log_players(players)
       if players.size < 2
-        log_warning("Floodgate: There should be more than one player (%d)." % [players.size])
+        log_message("Floodgate: There are less than two players: %d" % [players.size])
         return
       elsif players.size == 2
         start_game_shuffle(players)
@@ -346,7 +354,7 @@ module ShogiServer
     # @sacrifice a player id to be eliminated
     def initialize(sacrifice)
       super()
-      @sacrifice = sacrifice
+      @sacrifice = sacrifice || "gps500+e293220e3f8a3e59f79f6b0efffaa931"
     end
 
     def match(players)
@@ -388,43 +396,74 @@ module ShogiServer
       players.shuffle
     end
 
-    # Returns a player's rate value.
+    # Update estimated rate of a player.
     # 1. If it has a valid rate, return the rate.
-    # 2. If it has no valid rate, return average of the following values:
-    #   a. For games it won, the opponent's rate + 100
-    #   b. For games it lost, the opponent's rate - 100
-    #   (if the opponent has no valid rate, count out the game)
-    #   (if there are not such games, return 2150 (default value)
+    # 2. If it has no valid rate, return:
+    #   a. If it won the last game, the opponent's rate + 200
+    #   b. If it lost the last game, the opponent's rate - 200
+    #   c. otherwise, return 2150 (default value)
     #
-    def get_player_rate(player, history)
-      return player.rate if player.rate != 0
-      return 2150 unless history
-
-      count = 0
-      sum = 0
-
-      history.win_games(player.player_id).each do |g|
-        next unless g[:loser]
-        name = g[:loser].split("+")[0]
-        p = $league.find(name)
-        if p && p.rate != 0
-          count += 1
-          sum += p.rate + 100
-        end
+    def estimate_rate(player, history)
+      player.estimated_rate = 2150 # default value
+
+      unless history
+        log_message("Floodgate: Without game history, estimated %s's rate: %d" % [player.name, player.estimated_rate])
+        return
       end
-      history.loss_games(player.player_id).each do |g|
-        next unless g[:winner]
-        name = g[:winner].split("+")[0]
-        p = $league.find(name)
-        if p && p.rate != 0
-          count += 1
-          sum += p.rate - 100
-        end
+
+      g = history.last_valid_game(player.player_id)
+      unless g
+        log_message("Floodgate: Without any valid games in history, estimated %s's rate: %d" % [player.name, player.estimated_rate])
+        return
+      end
+
+      opponent_id = nil
+      win         = true
+      case player.player_id
+      when g[:winner]
+        opponent_id = g[:loser]
+        win = true
+      when g[:loser]
+        opponent_id = g[:winner]
+        win = false
+      else
+        log_warning("Floodgate: The last valid game is invalid for %s!" % [player.name])
+        log_message("Floodgate: Estimated %s's rate: %d" % [player.name, player.estimated_rate])
+        return
+      end
+
+      opponent_name = opponent_id.split("+")[0]
+      p = $league.find(opponent_name)
+      unless p
+        log_message("Floodgate: No active opponent found. Estimated %s's rate: %d" % [player.name, player.estimated_rate])
+        return
+      end
+
+      opponent_rate = 0
+      if p.rate != 0
+        opponent_rate = p.rate
+      elsif p.estimated_rate != 0
+        opponent_rate = p.estimated_rate
       end
 
-      estimate = (count == 0 ? 2150 : sum/count)
-      log_message("Floodgate: Estimated rate of %s is %d" % [player.name, estimate])
-      return estimate
+      if opponent_rate != 0
+        player.estimated_rate = opponent_rate + (win ? 200 : -200)
+      end
+
+      log_message("Floodgate: Estimated %s's rate: %d" % [player.name, player.estimated_rate])
+    end
+
+    # Return a player's rate based on its actual rate or estimated rate.
+    #
+    def get_player_rate(player, history)
+      if player.rate != 0
+        return player.rate
+      elsif player.estimated_rate != 0
+        return player.estimated_rate 
+      else
+        estimate_rate(player, history)
+        return player.estimated_rate
+      end
     end
 
     def calculate_diff_with_penalty(players, history)
@@ -455,6 +494,13 @@ module ShogiServer
         if p1.is_human? && p2.is_human?
           ret += 800
         end
+
+        # 2.3 a match with likely kin players
+        if (p1.player_id[0..6] == p2.player_id[0..6])
+          ret += 800
+        elsif (p1.player_id[0..3] == p2.player_id[0..3])
+          ret += 400
+        end
       end
 
       ret
@@ -467,6 +513,9 @@ module ShogiServer
         return players
       end
 
+      # Reset estimated rate
+      players.each {|p| p.estimated_rate = 0}
+
       # 10 trials
       matches = []
       scores  = []
@@ -493,10 +542,23 @@ module ShogiServer
           min_score = s
         end
       end
-      log_message("Floodgate: the least score %d (%d per player) [%s]" % [min_score, min_score/players.size, scores.join(" ")])
+      log_message("Floodgate: the least score %d (%d per game) [%s]" % [min_score, min_score/players.size*2, scores.join(" ")])
 
       players.replace(matches[min_index])
     end
   end
 
+  # This pairing method excludes unrated players
+  #
+  class ExcludeUnratedPlayers < Pairing
+
+    def match(players)
+      super
+
+      log_message("Floodgate: Deleting unrated players...")
+      players.delete_if{|a| a.rate == 0}
+      log_players(players)
+    end
+  end # class ExcludeUnratedPlayers
+
 end # ShogiServer
index b6b60f5..471119b 100644 (file)
@@ -27,6 +27,7 @@ class BasicPlayer
     @name = nil
     @password = nil
     @rate = 0
+    @estimated_rate = 0
     @win  = 0
     @loss = 0
     @last_game_win = false
@@ -48,6 +49,10 @@ class BasicPlayer
   # Score in the rating sysem
   attr_accessor :rate
 
+  # Estimated rate for unrated player (rate == 0)
+  # But this value is not persisted and cleared when player logs off.
+  attr_accessor :estimated_rate
+
   # Number of games for win and loss in the rating system
   attr_accessor :win, :loss
   
index 2388634..297d63b 100644 (file)
@@ -3,6 +3,7 @@ require 'test/unit'
 require 'shogi_server'
 require 'shogi_server/league/floodgate'
 require 'fileutils'
+require 'test/mock_log_message'
 
 $topdir = File.expand_path File.dirname(__FILE__)
 
@@ -142,6 +143,20 @@ class TestNextTimeGeneratorConfig < Test::Unit::TestCase
   def setup
   end
 
+  def test_comment
+    now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
+    lines = %w(#\ comment1 Thu\ 22:00 #\ comment2)
+    ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
+    assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
+  end
+
+  def test_empty_line
+    now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
+    lines = %w(\  Thu\ 22:00 \  hoge)
+    ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
+    assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
+  end
+
   def test_read
     now = DateTime.new(2010, 6, 10, 21, 20, 15) # Thu
     assert_equal DateTime.parse("10-06-2010 21:20:15"), now
@@ -202,4 +217,36 @@ class TestNextTimeGeneratorConfig < Test::Unit::TestCase
     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new ["Thu 22:00"]
     assert_equal Time.parse("17-06-2010 22:00"), ntc.call(now)
   end
+
+  def test_default_pairing_factory
+    now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
+    lines = %w(Thu\ 22:00)
+    ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
+    assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
+    assert_equal("default_factory", ntc.pairing_factory)
+  end
+
+  def test_read_pairing_factory
+    now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
+    lines = %w(set\ pairing_factory\ least_diff_pairing Thu\ 22:00)
+    ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
+    assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
+    assert_equal("least_diff_pairing", ntc.pairing_factory)
+  end
+
+  def test_default_sacrifice
+    now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
+    lines = %w(Thu\ 22:00)
+    ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
+    assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
+    assert_equal("gps500+e293220e3f8a3e59f79f6b0efffaa931", ntc.sacrifice)
+  end
+
+  def test_read_sacrifice
+    now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
+    lines = %w(set\ sacrifice\ yowai_gps+95908f6c18338f5340371f71523fc5e3 Thu\ 22:00)
+    ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new lines
+    assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
+    assert_equal("yowai_gps+95908f6c18338f5340371f71523fc5e3", ntc.sacrifice)
+  end
 end
index 434f2b8..f1fb034 100644 (file)
@@ -65,7 +65,13 @@ class TestSetupFloodgate < Test::Unit::TestCase
   def test_regenerate_leagues
     game_names = %w(floodgate-900-0 floodgate-3600-0)
     now = Time.now
-    next_array = [["floodgate-900-0", now+100], ["floodgate-3600-0", now+200]]
+    next_array = []
+    next_array << ShogiServer::League::Floodgate.new($league, 
+                    {:game_name => "floodgate-900-0",
+                     :next_time => (now+100)})
+    next_array << ShogiServer::League::Floodgate.new($league, 
+                    {:game_name => "floodgate-3600-0",
+                     :next_time => (now+200)})
     objs = @sf.regenerate_leagues(next_array)
     assert_equal 2, objs.size
     assert_instance_of ShogiServer::League::Floodgate, objs[0]
index 7e2b6f3..b788fd1 100644 (file)
@@ -1,3 +1,4 @@
+$:.unshift File.dirname(__FILE__)
 $:.unshift File.join(File.dirname(__FILE__), "..")
 $topdir = File.expand_path File.dirname(__FILE__)
 require "baseclient"
index 90cea15..94274fa 100644 (file)
@@ -411,6 +411,19 @@ class TestLeastDiff < Test::Unit::TestCase
     @x.player_id = "x"
     @x.name = "x"
 
+    @abcdefg1 = ShogiServer::BasicPlayer.new
+    @abcdefg1.player_id = "abcdefg1"
+    @abcdefg1.name = "abcdefg1"
+    @abcdefg1.rate = 2100
+    @abcdefg2 = ShogiServer::BasicPlayer.new
+    @abcdefg2.player_id = "abcdefg2"
+    @abcdefg2.name = "abcdefg2"
+    @abcdefg2.rate = 2200
+    @abcdxyz = ShogiServer::BasicPlayer.new
+    @abcdxyz.player_id = "abcdxyz"
+    @abcdxyz.name = "abcdxyz"
+    @abcdxyz.rate = 2300
+
     $league.add(@a)
     $league.add(@b)
     $league.add(@c)
@@ -420,6 +433,9 @@ class TestLeastDiff < Test::Unit::TestCase
     $league.add(@g)
     $league.add(@h)
     $league.add(@x)
+    $league.add(@abcdefg1)
+    $league.add(@abcdefg2)
+    $league.add(@abcdxyz)
   end
 
   def teardown
@@ -548,9 +564,20 @@ class TestLeastDiff < Test::Unit::TestCase
     assert_equal(@b.rate-@a.rate+400+@h.rate-@g.rate+400, @pairing.calculate_diff_with_penalty(players, @history))
   end
 
+  def test_calculate_diff_with_kin_4_players
+    players = [@abcdefg1, @abcdxyz]
+    assert_equal(@abcdxyz.rate - @abcdefg1.rate + 400, @pairing.calculate_diff_with_penalty(players,nil))
+  end
+
+  def test_calculate_diff_with_kin_7_players
+    players = [@abcdefg1, @abcdefg2]
+    assert_equal(@abcdefg2.rate - @abcdefg1.rate + 800, @pairing.calculate_diff_with_penalty(players,nil))
+  end
+
   def test_get_player_rate_0
     assert_equal(2150, @pairing.get_player_rate(@x, @history))
 
+    @x.estimated_rate = 0
     dummy = nil
     def @history.make_record(game_result)
       {:game_id => "wdoor+floodgate-900-0-x-a-1", 
@@ -558,8 +585,9 @@ class TestLeastDiff < Test::Unit::TestCase
        :winner => "x", :loser => "a"}
     end
     @history.update(dummy)
-    assert_equal(@a.rate+100, @pairing.get_player_rate(@x, @history))
+    assert_equal(@a.rate+200, @pairing.get_player_rate(@x, @history))
 
+    @x.estimated_rate = 0
     def @history.make_record(game_result)
       {:game_id => "wdoor+floodgate-900-0-x-b-1", 
        :black => "x",  :white => "b",
@@ -567,7 +595,62 @@ class TestLeastDiff < Test::Unit::TestCase
     end
     @history.update(dummy)
 
-    assert_equal((@a.rate+100+@b.rate-100)/2, @pairing.get_player_rate(@x, @history))
+    assert_equal(@b.rate-200, @pairing.get_player_rate(@x, @history))
   end
 end
 
+class TestExcludeUnratedPlayers < Test::Unit::TestCase
+  def setup
+    @pairing= ShogiServer::ExcludeUnratedPlayers.new
+    @a = ShogiServer::BasicPlayer.new
+    @a.name = "a"
+    @a.win  = 1
+    @a.loss = 2
+    @a.rate = 0
+    @b = ShogiServer::BasicPlayer.new
+    @b.name = "b"
+    @b.win  = 10
+    @b.loss = 20
+    @b.rate = 1500
+    @c = ShogiServer::BasicPlayer.new
+    @c.name = "c"
+    @c.win  = 100
+    @c.loss = 200
+    @c.rate = 1000
+    @d = ShogiServer::BasicPlayer.new
+    @d.name = "d"
+    @d.win  = 1000
+    @d.loss = 2000
+    @d.rate = 2000
+  end
+
+  def test_match_without_any_players
+    players = []
+    @pairing.match(players)
+    assert_equal([], players)
+  end
+
+  def test_match_without_unrated_player_1
+    players = [@b, @c, @d]
+    @pairing.match(players)
+    assert_equal([@b, @c, @d], players)
+  end
+
+  def test_match_without_unrated_player_2
+    players = [@b]
+    @pairing.match(players)
+    assert_equal([@b], players)
+  end
+
+  def test_match_with_unrated_player_1
+    players = [@a, @b, @c, @d]
+    @pairing.match(players)
+    assert_equal([@b, @c, @d], players)
+  end
+
+  def test_match_with_unrated_player_2
+    players = [@a]
+    @pairing.match(players)
+    assert_equal([], players)
+  end
+end
index 97f8059..1c5acbd 100755 (executable)
--- a/webserver
+++ b/webserver
@@ -151,6 +151,7 @@ def main
   http_log_file            = File.join($options["dir"], "shogi-server-httpd.log")
   http_access_log_file     = File.join($options["dir"], "shogi-server-access.log")
   http_config = {}
+  http_config[:BindAddress] = "0.0.0.0"
   http_config[:Port]       = $options["port"]
   http_config[:ServerType] = WEBrick::Daemon if $options["daemon"]
   http_config[:Logger]     = WEBrick::Log.new(http_log_file)