OSDN Git Service

* Return [...] +OK after %%SETBUOY and %%DELETEBUOY commands
[shogi-server/shogi-server.git] / shogi_server / pairing.rb
1 ## $Id$
2
3 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
4 ## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
5 ##
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.
10 ##
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.
15 ##
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
19
20 require 'shogi_server/util'
21
22 module ShogiServer
23
24   class Pairing
25
26     class << self
27       def default_factory
28         return swiss_pairing
29       end
30
31       def sort_by_rate_with_randomness
32         return [LogPlayers.new,
33                 ExcludeSacrificeGps500.new,
34                 MakeEven.new,
35                 SortByRateWithRandomness.new(1200, 2400),
36                 StartGame.new]
37       end
38
39       def random_pairing
40         return [LogPlayers.new,
41                 ExcludeSacrificeGps500.new,
42                 MakeEven.new,
43                 Randomize.new,
44                 StartGame.new]
45       end
46
47       def swiss_pairing
48         history = ShogiServer::League::Floodgate::History.factory
49         return [LogPlayers.new,
50                 ExcludeSacrificeGps500.new,
51                 MakeEven.new,
52                 Swiss.new(history),
53                 StartGame.new]
54       end
55
56       def match(players)
57         logics = default_factory
58         logics.inject(players) do |result, item|
59           item.match(result)
60           result
61         end
62       end
63     end # class << self
64
65
66     def match(players)
67       # to be implemented
68       log_message("Floodgate: %s" % [self.class.to_s])
69     end
70
71     def include_newbie?(players)
72       return players.find{|a| a.rate == 0} == nil ? false : true
73     end
74
75     def less_than_one?(players)
76       if players.size < 1
77         log_warning("Floodgate: There should be at least one player.")
78         return true
79       else
80         return false
81       end
82     end
83
84     def log_players(players)
85       str_array = players.map do |one|
86         if block_given?
87           yield one
88         else
89           one.name
90         end
91       end
92       if str_array.empty?
93         log_message("Floodgate: [Players] Nobody found.")
94       else
95         log_message("Floodgate: [Players] %s." % [str_array.join(", ")])
96       end
97     end
98   end # Pairing
99
100
101   class LogPlayers < Pairing
102     def match(players)
103       super
104       log_players(players)
105     end
106   end
107
108   class StartGame < Pairing
109     def match(players)
110       super
111       if players.size < 2
112         log_warning("Floodgate: There should be more than one player (%d)." % [players.size])
113         return
114       end
115       if players.size.odd?
116         log_warning("Floodgate: There are odd players (%d). %s will not be matched." % 
117                     [players.size, players.last.name])
118       end
119
120       log_players(players)
121       while (players.size >= 2) do
122         pair = players.shift(2)
123         pair.shuffle!
124         start_game(pair.first, pair.last)
125       end
126     end
127
128     def start_game(p1, p2)
129       log_message("Floodgate: Starting a game: BLACK %s vs WHITE %s" % [p1.name, p2.name])
130       p1.sente = true
131       p2.sente = false
132       board = Board.new
133       board.initial
134       Game.new(p1.game_name, p1, p2, board)
135     end
136   end
137
138   class Randomize < Pairing
139     def match(players)
140       super
141       log_message("Floodgate: Randomize... before")
142       log_players(players)
143       players.shuffle!
144       log_message("Floodgate: Randomized after")
145       log_players(players)
146     end
147   end # RadomPairing
148
149   class SortByRate < Pairing
150     def match(players)
151       super
152       log_message("Floodgate: Ordered by rate")
153       players.sort! {|a,b| a.rate <=> b.rate} # decendent order
154       log_players(players)
155     end
156   end
157
158   class SortByRateWithRandomness < Pairing
159     def initialize(rand1, rand2)
160       super()
161       @rand1, @rand2 = rand1, rand2
162     end
163
164     def match(players, desc=false)
165       super(players)
166       cur_rate = Hash.new
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]
172       end
173     end
174   end
175
176   class Swiss < Pairing
177     def initialize(history)
178       super()
179       @history = history
180     end
181
182     def match(players)
183       super
184       winners = players.find_all {|pl| @history.last_win?(pl.player_id)}
185       rest    = players - winners
186
187       log_message("Floodgate: Ordering %d winners..." % [winners.size])
188       sbrwr_winners = SortByRateWithRandomness.new(800, 2500)
189       sbrwr_winners.match(winners, true)
190
191       log_message("Floodgate: Ordering the rest (%d)..." % [rest.size])
192       sbrwr_losers = SortByRateWithRandomness.new(200, 400)
193       sbrwr_losers.match(rest, true)
194
195       players.clear
196       [winners, rest].each do |group|
197         group.each {|pl| players << pl}
198       end
199     end
200   end
201
202   class DeletePlayerAtRandom < Pairing
203     def match(players)
204       super
205       return if less_than_one?(players)
206       one = players.choice
207       log_message("Floodgate: Deleted %s at random" % [one.name])
208       players.delete(one)
209       log_players(players)
210     end
211   end
212
213   class DeletePlayerAtRandomExcept < Pairing
214     def initialize(except)
215       super()
216       @except = except
217     end
218
219     def match(players)
220       super
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)
225     end
226   end
227   
228   class DeleteMostPlayingPlayer < Pairing
229     def match(players)
230       super
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])
233       players.delete(one)
234       log_players(players)
235     end
236   end
237
238   class DeleteLeastRatePlayer < Pairing
239     def match(players)
240       super
241       one = players.min_by {|a| a.rate}
242       log_message("Floodgate: Deleted the least rate player %s (%d)" % [one.name, one.rate])
243       players.delete(one)
244       log_players(players)
245     end
246   end
247
248   class ExcludeSacrifice < Pairing
249     attr_reader :sacrifice
250
251     # @sacrifice a player id to be eliminated
252     def initialize(sacrifice)
253       super()
254       @sacrifice = sacrifice
255     end
256
257     def match(players)
258       super
259       if @sacrifice && 
260          players.size.odd? && 
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}
264          log_players(players)
265       end
266     end
267   end # class ExcludeSacrifice
268
269   class ExcludeSacrificeGps500 < ExcludeSacrifice
270     def initialize
271       super("gps500+e293220e3f8a3e59f79f6b0efffaa931")
272     end
273   end
274
275   class MakeEven < Pairing
276     def match(players)
277       super
278       return if players.size.even?
279       log_message("Floodgate: There are odd players (%d). Deleting one of them..." % 
280                   [players.size])
281       DeletePlayerAtRandom.new.match(players)
282     end
283   end
284
285 end # ShogiServer