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'
28 return sort_by_rate_with_randomness
31 def sort_by_rate_with_randomness
32 return [ExcludeSacrificeGps500.new,
34 SortByRateWithRandomness.new(1200, 2400),
39 return [ExcludeSacrificeGps500.new,
46 logics = default_factory
47 logics.inject(players) do |result, item|
58 def include_newbie?(players)
59 return players.find{|a| a.rate == 0} == nil ? false : true
62 def less_than_one?(players)
64 log_warning("Floodgate: At least one player is required")
71 def log_players(players)
72 str_array = players.map do |one|
79 log_message("Floodgate: [Players] %s" % [str_array.join(", ")])
83 class StartGame < Pairing
87 log_warning("There should be more than one player: %d" % [players.size])
91 log_warning("There are odd players: %d" % [players.size])
95 while (players.size >= 2) do
96 pair = players.shift(2)
98 start_game(pair.first, pair.last)
103 log_message("Floodgate: BLACK %s; WHITE %s" % [p1.name, p2.name])
106 Game.new(p1.game_name, p1, p2)
110 class Randomize < Pairing
113 log_message("Floodgate: Randomize... before")
116 log_message("Floodgate: Randomized after")
121 class SortByRate < Pairing
124 log_message("Floodgate: Ordered by rate")
125 players.sort! {|a,b| a.rate <=> b.rate} # decendent order
130 class SortByRateWithRandomness < Pairing
131 def initialize(rand1, rand2)
133 @rand1, @rand2 = rand1, rand2
139 players.each{|a| cur_rate[a] = a.rate ? a.rate + rand(@rand1) : rand(@rand2)}
140 players.sort!{|a,b| cur_rate[a] <=> cur_rate[b]}
141 log_players(players) do |one|
142 "%s %d (randomness %d)" % [one.name, one.rate, cur_rate[one] - one.rate]
147 class DeletePlayerAtRandom < Pairing
150 return if less_than_one?(players)
152 log_message("Floodgate: Deleted %s at random" % [one.name])
158 class DeletePlayerAtRandomExcept < Pairing
159 def initialize(except)
166 log_message("Floodgate: Deleting a player at rondom except %s" % [@except.name])
167 players.delete(@except)
168 DeletePlayerAtRandom.new.match(players)
169 players.push(@except)
173 class DeleteMostPlayingPlayer < Pairing
176 one = players.max_by {|a| a.win + a.loss}
177 log_message("Floodgate: Deleted the most playing player: %s (%d)" % [one.name, one.win + one.loss])
183 class DeleteLeastRatePlayer < Pairing
186 one = players.min_by {|a| a.rate}
187 log_message("Floodgate: Deleted the least rate player %s (%d)" % [one.name, one.rate])
193 class ExcludeSacrifice < Pairing
194 attr_reader :sacrifice
196 # @sacrifice a player id to be eliminated
197 def initialize(sacrifice)
199 @sacrifice = sacrifice
206 players.find{|a| a.player_id == @sacrifice}
207 log_message("Floodgate: Deleting the sacrifice %s" % [@sacrifice])
208 players.delete_if{|a| a.player_id == @sacrifice}
212 end # class ExcludeSacrifice
214 class ExcludeSacrificeGps500 < ExcludeSacrifice
216 super("gps500+e293220e3f8a3e59f79f6b0efffaa931")
220 class MakeEven < Pairing
223 return if players.even?
224 log_message("Floodgate: there are odd players: %d. Deleting one..." %
226 DeletePlayerAtRandom.new.match(players)