OSDN Git Service

Renewed year of copyright notice in each file.
[shogi-server/shogi-server.git] / shogi_server / pairing.rb
index 4e0b6d6..6a660ec 100644 (file)
@@ -1,7 +1,7 @@
 ## $Id$
 
 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
 ## $Id$
 
 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
-## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
+## Copyright (C) 2007-2012 Daigo Moriwaki (daigo at debian dot org)
 ##
 ## This program is free software; you can redistribute it and/or modify
 ## it under the terms of the GNU General Public License as published by
 ##
 ## This program is free software; you can redistribute it and/or modify
 ## it under the terms of the GNU General Public License as published by
@@ -45,11 +45,10 @@ module ShogiServer
       end
 
       def swiss_pairing
       end
 
       def swiss_pairing
-        history = ShogiServer::League::Floodgate::History.factory
         return [LogPlayers.new,
                 ExcludeSacrificeGps500.new,
                 MakeEven.new,
         return [LogPlayers.new,
                 ExcludeSacrificeGps500.new,
                 MakeEven.new,
-                Swiss.new(history),
+                Swiss.new,
                 StartGameWithoutHumans.new]
       end
 
                 StartGameWithoutHumans.new]
       end
 
@@ -251,15 +250,21 @@ module ShogiServer
   end
 
   class Swiss < Pairing
   end
 
   class Swiss < Pairing
-    def initialize(history)
-      super()
-      @history = history
-    end
-
     def match(players)
       super
     def match(players)
       super
-      winners = players.find_all {|pl| @history.last_win?(pl.player_id)}
-      rest    = players - winners
+      if players.size < 3
+        log_message("Floodgate: players are small enough to skip Swiss pairing: %d" % [players.size])
+        return
+      end
+
+      path = ShogiServer::League::Floodgate.history_file_path(players.first.game_name)
+      history = ShogiServer::League::Floodgate::History.factory(path)
+
+      winners = []
+      if history
+        winners = players.find_all {|pl| history.last_win?(pl.player_id)}
+      end
+      rest = players - winners
 
       log_message("Floodgate: Ordering %d winners..." % [winners.size])
       sbrwr_winners = SortByRateWithRandomness.new(800, 2500)
 
       log_message("Floodgate: Ordering %d winners..." % [winners.size])
       sbrwr_winners = SortByRateWithRandomness.new(800, 2500)
@@ -280,7 +285,7 @@ module ShogiServer
     def match(players)
       super
       return if less_than_one?(players)
     def match(players)
       super
       return if less_than_one?(players)
-      one = players.choice
+      one = players.sample
       log_message("Floodgate: Deleted %s at random" % [one.name])
       players.delete(one)
       log_players(players)
       log_message("Floodgate: Deleted %s at random" % [one.name])
       players.delete(one)
       log_players(players)