3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 require 'shogi_server/util'
31 def sort_by_rate_with_randomness
32 return [LogPlayers.new,
33 ExcludeSacrificeGps500.new,
35 SortByRateWithRandomness.new(1200, 2400),
40 return [LogPlayers.new,
41 ExcludeSacrificeGps500.new,
48 history = ShogiServer::League::Floodgate::History.factory
49 return [LogPlayers.new,
50 ExcludeSacrificeGps500.new,
57 logics = default_factory
58 logics.inject(players) do |result, item|
68 log_message("Floodgate: %s" % [self.class.to_s])
71 def include_newbie?(players)
72 return players.find{|a| a.rate == 0} == nil ? false : true
75 def less_than_one?(players)
77 log_warning("Floodgate: There should be at least one player.")
84 def log_players(players)
85 str_array = players.map do |one|
93 log_message("Floodgate: [Players] Nobody found.")
95 log_message("Floodgate: [Players] %s." % [str_array.join(", ")])
101 class LogPlayers < Pairing
108 class StartGame < Pairing
112 log_warning("Floodgate: There should be more than one player (%d)." % [players.size])
116 log_warning("Floodgate: There are odd players (%d). %s will not be matched." %
117 [players.size, players.last.name])
121 while (players.size >= 2) do
122 pair = players.shift(2)
124 start_game(pair.first, pair.last)
128 def start_game(p1, p2)
129 log_message("Floodgate: Starting a game: BLACK %s vs WHITE %s" % [p1.name, p2.name])
134 Game.new(p1.game_name, p1, p2, board)
138 class Randomize < Pairing
141 log_message("Floodgate: Randomize... before")
144 log_message("Floodgate: Randomized after")
149 class SortByRate < Pairing
152 log_message("Floodgate: Ordered by rate")
153 players.sort! {|a,b| a.rate <=> b.rate} # decendent order
158 class SortByRateWithRandomness < Pairing
159 def initialize(rand1, rand2)
161 @rand1, @rand2 = rand1, rand2
164 def match(players, desc=false)
167 players.each{|a| cur_rate[a] = a.rate ? a.rate + rand(@rand1) : rand(@rand2)}
168 players.sort!{|a,b| cur_rate[a] <=> cur_rate[b]}
169 players.reverse! if desc
170 log_players(players) do |one|
171 "%s %d (+ randomness %d)" % [one.name, one.rate, cur_rate[one] - one.rate]
176 class Swiss < Pairing
177 def initialize(history)
184 winners = players.find_all {|pl| @history.last_win?(pl.player_id)}
185 rest = players - winners
187 log_message("Floodgate: Ordering %d winners..." % [winners.size])
188 sbrwr_winners = SortByRateWithRandomness.new(800, 2500)
189 sbrwr_winners.match(winners, true)
191 log_message("Floodgate: Ordering the rest (%d)..." % [rest.size])
192 sbrwr_losers = SortByRateWithRandomness.new(200, 400)
193 sbrwr_losers.match(rest, true)
196 [winners, rest].each do |group|
197 group.each {|pl| players << pl}
202 class DeletePlayerAtRandom < Pairing
205 return if less_than_one?(players)
207 log_message("Floodgate: Deleted %s at random" % [one.name])
213 class DeletePlayerAtRandomExcept < Pairing
214 def initialize(except)
221 log_message("Floodgate: Deleting a player at rondom except %s" % [@except.name])
222 players.delete(@except)
223 DeletePlayerAtRandom.new.match(players)
224 players.push(@except)
228 class DeleteMostPlayingPlayer < Pairing
231 one = players.max_by {|a| a.win + a.loss}
232 log_message("Floodgate: Deleted the most playing player: %s (%d)" % [one.name, one.win + one.loss])
238 class DeleteLeastRatePlayer < Pairing
241 one = players.min_by {|a| a.rate}
242 log_message("Floodgate: Deleted the least rate player %s (%d)" % [one.name, one.rate])
248 class ExcludeSacrifice < Pairing
249 attr_reader :sacrifice
251 # @sacrifice a player id to be eliminated
252 def initialize(sacrifice)
254 @sacrifice = sacrifice
261 players.find{|a| a.player_id == @sacrifice}
262 log_message("Floodgate: Deleting the sacrifice %s" % [@sacrifice])
263 players.delete_if{|a| a.player_id == @sacrifice}
267 end # class ExcludeSacrifice
269 class ExcludeSacrificeGps500 < ExcludeSacrifice
271 super("gps500+e293220e3f8a3e59f79f6b0efffaa931")
275 class MakeEven < Pairing
278 return if players.size.even?
279 log_message("Floodgate: There are odd players (%d). Deleting one of them..." %
281 DeletePlayerAtRandom.new.match(players)