OSDN Git Service

Merge branch '201312-fix-sacrifice'
authorDaigo Moriwaki <daigo@debian.org>
Sun, 29 Dec 2013 07:33:19 +0000 (16:33 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Sun, 29 Dec 2013 07:33:19 +0000 (16:33 +0900)
changelog
shogi_server/league/floodgate.rb
shogi_server/league/floodgate_thread.rb
test/TC_floodgate_thread.rb
test/TC_fork.rb

index 3d114b7..f55c27c 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,9 @@
+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]
index 5d6ccaa..24ff68f 100644 (file)
@@ -27,16 +27,17 @@ class League
     #
     attr_reader :next_time
     attr_reader :league, :game_name
-    attr_reader :pairing_factory
     attr_reader :options
 
     def initialize(league, hash={})
       @league = league
-      @next_time = hash[:next_time] || nil
-      @game_name = hash[:game_name] || "floodgate-900-0"
-      @pairing_factory = "default_factory" # will be updated by NextTimeGenerator
-      # Options will be updated by NextTimeGenerator
-      @options = {:sacrifice => "gps500+e293220e3f8a3e59f79f6b0efffaa931"}
+      @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
 
@@ -44,25 +45,33 @@ 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)
-      @pairing_factory = ntg.pairing_factory
-      @options[:sacrifice] = ntg.sacrifice
       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, @pairing_factory])
+      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
-      logics = Pairing.send(@pairing_factory, @options)
+      logics = Pairing.send(@options[:pairing_factory], @options)
       Pairing.match(players, logics)
     end
     
@@ -150,9 +159,9 @@ class League
         @lines.each do |line|
           case line
           when %r!^\s*set\s+pairing_factory\s+(\w+)!
-            @pairing_factory = $1
+            @pairing_factory = $1.chomp
           when %r!^\s*set\s+sacrifice\s+(.*)!
-            @sacrifice = $1
+            @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)
index 231c8ce..e45f60a 100644 (file)
@@ -68,15 +68,16 @@ module ShogiServer
       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|
-        log_message("Regenerating a floodgate league...: %s %s" % [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 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"