OSDN Git Service

Added a new command line to specify floodgate_history.yaml file.
[shogi-server/shogi-server.git] / shogi_server / pairing.rb
index 01f4fe0..26d0f9f 100644 (file)
@@ -25,27 +25,39 @@ module ShogiServer
 
     class << self
       def default_factory
-        return sort_by_rate_with_randomness
+        return swiss_pairing
       end
 
       def sort_by_rate_with_randomness
-        return [ExcludeSacrificeGps500.new,
+        return [LogPlayers.new,
+                ExcludeSacrificeGps500.new,
                 MakeEven.new,
                 SortByRateWithRandomness.new(1200, 2400),
                 StartGame.new]
       end
 
       def random_pairing
-        return [ExcludeSacrificeGps500.new,
+        return [LogPlayers.new,
+                ExcludeSacrificeGps500.new,
                 MakeEven.new,
                 Randomize.new,
                 StartGame.new]
       end
 
+      def swiss_pairing
+        history = ShogiServer::League::Floodgate::History.factory
+        return [LogPlayers.new,
+                ExcludeSacrificeGps500.new,
+                MakeEven.new,
+                Swiss.new(history),
+                StartGame.new]
+      end
+
       def match(players)
         logics = default_factory
         logics.inject(players) do |result, item|
           item.match(result)
+          result
         end
       end
     end # class << self
@@ -84,6 +96,13 @@ module ShogiServer
     end
   end # Pairing
 
+
+  class LogPlayers < Pairing
+    def match(players)
+      log_players(players)
+    end
+  end
+
   class StartGame < Pairing
     def match(players)
       super
@@ -104,7 +123,7 @@ module ShogiServer
       end
     end
 
-    def start_game
+    def start_game(p1, p2)
       log_message("Floodgate: BLACK %s; WHITE %s" % [p1.name, p2.name])
       p1.sente = true
       p2.sente = false
@@ -138,13 +157,42 @@ module ShogiServer
       @rand1, @rand2 = rand1, rand2
     end
 
-    def match(players)
-      super
+    def match(players, desc=false)
+      super(players)
       cur_rate = Hash.new
       players.each{|a| cur_rate[a] = a.rate ? a.rate + rand(@rand1) : rand(@rand2)}
       players.sort!{|a,b| cur_rate[a] <=> cur_rate[b]}
+      players.reverse! if desc
       log_players(players) do |one|
-        "%s %d (randomness %d)" % [one.name, one.rate, cur_rate[one] - one.rate]
+        "%s %d (+ randomness %d)" % [one.name, one.rate, cur_rate[one] - one.rate]
+      end
+    end
+  end
+
+  class Swiss < Pairing
+    def initialize(history)
+      super()
+      @history = history
+    end
+
+    def match(players)
+      super
+      winners = players.find_all {|pl| @history.last_win?(pl.player_id)}
+      rest    = players - winners
+
+      log_message("Floodgate: %d winners" % [winners.size])
+      sbrwr_winners = SortByRateWithRandomness.new(800, 2500)
+      sbrwr_winners.match(winners, true)
+      log_players(winners)
+
+      log_message("Floodgate: and the rest: %d" % [rest.size])
+      sbrwr_losers = SortByRateWithRandomness.new(200, 400)
+      sbrwr_losers.match(rest, true)
+      log_players(rest)
+
+      players.clear
+      [winners, rest].each do |group|
+        group.each {|pl| players << pl}
       end
     end
   end