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
22 module ShogiServer # for a namespace
24 # MonitorObserver observes GameResult to send messages to the monitors
28 def update(game_result)
29 game_result.game.each_monitor do |monitor|
30 monitor.write_safe("##[MONITOR][%s] %s\n" % [game_result.game.game_id, game_result.result_type])
35 # Base class for a game result
48 # Command to send monitors such as '%TORYO' etc...
49 attr_reader :result_type
51 def initialize(game, p1, p2)
54 if p1.sente && !p2.sente
55 @black, @white = p1, p2
56 elsif !p1.sente && p2.sente
57 @black, @white = p2, p1
59 raise "Never reached!"
61 @players.each do |player|
62 player.status = "connected"
70 add_observer MonitorObserver.new
72 if League::Floodgate.game_name?(@game.game_name) &&
73 @game.sente.player_id &&
74 @game.gote.player_id &&
75 $options["floodgate-history"]
76 add_observer League::Floodgate::History.factory
87 notify_observers(self)
95 log(@game.board.to_s.gsub(/^/, "\'").chomp)
100 class GameResultWin < GameResult
101 attr_reader :winner, :loser
103 def initialize(game, winner, loser)
105 @winner, @loser = winner, loser
106 @winner.last_game_win = true
107 @loser.last_game_win = false
110 def log_summary(type)
113 black_result = white_result = ""
116 white_result = "lose"
118 black_result = "lose"
121 log("'summary:%s:%s %s:%s %s" % [type,
122 @black.name, black_result,
123 @white.name, white_result])
128 class GameResultAbnormalWin < GameResultWin
130 @winner.write_safe("%TORYO\n#RESIGN\n#WIN\n")
131 @loser.write_safe( "%TORYO\n#RESIGN\n#LOSE\n")
133 log_summary("abnormal")
134 @result_type = "%TORYO"
139 class GameResultTimeoutWin < GameResultWin
141 @winner.write_safe("#TIME_UP\n#WIN\n")
142 @loser.write_safe( "#TIME_UP\n#LOSE\n")
143 log_summary("time up")
144 @result_type = "#TIME_UP"
149 # A player declares (successful) Kachi
150 class GameResultKachiWin < GameResultWin
152 @winner.write_safe("%KACHI\n#JISHOGI\n#WIN\n")
153 @loser.write_safe( "%KACHI\n#JISHOGI\n#LOSE\n")
156 @result_type = "%KACHI"
161 # A player declares wrong Kachi
162 class GameResultIllegalKachiWin < GameResultWin
164 @winner.write_safe("%KACHI\n#ILLEGAL_MOVE\n#WIN\n")
165 @loser.write_safe( "%KACHI\n#ILLEGAL_MOVE\n#LOSE\n")
167 log_summary("illegal kachi")
168 @result_type = "%KACHI"
173 class GameResultIllegalWin < GameResultWin
174 def initialize(game, winner, loser, cause)
175 super(game, winner, loser)
180 @winner.write_safe("#ILLEGAL_MOVE\n#WIN\n")
181 @loser.write_safe( "#ILLEGAL_MOVE\n#LOSE\n")
183 @result_type = "#ILLEGAL_MOVE"
188 class GameResultIllegalMoveWin < GameResultIllegalWin
189 def initialize(game, winner, loser)
190 super(game, winner, loser, "illegal move")
194 class GameResultUchifuzumeWin < GameResultIllegalWin
195 def initialize(game, winner, loser)
196 super(game, winner, loser, "uchifuzume")
200 class GameResultOuteKaihiMoreWin < GameResultIllegalWin
201 def initialize(game, winner, loser)
202 super(game, winner, loser, "oute_kaihimore")
206 class GameResultOutoriWin < GameResultWin
207 def initialize(game, winner, loser)
208 super(game, winner, loser)
212 class GameResultToryoWin < GameResultWin
214 @winner.write_safe("%TORYO\n#RESIGN\n#WIN\n")
215 @loser.write_safe( "%TORYO\n#RESIGN\n#LOSE\n")
218 @result_type = "%TORYO"
223 class GameResultOuteSennichiteWin < GameResultWin
225 @winner.write_safe("#OUTE_SENNICHITE\n#WIN\n")
226 @loser.write_safe( "#OUTE_SENNICHITE\n#LOSE\n")
227 log_summary("oute_sennichite")
228 @result_type = "#OUTE_SENNICHITE"
233 class GameResultDraw < GameResult
234 def initialize(game, p1, p2)
236 p1.last_game_win = false
237 p2.last_game_win = false
240 def log_summary(type)
242 log("'summary:%s:%s draw:%s draw" % [type, @black.name, @white.name])
246 class GameResultSennichiteDraw < GameResultDraw
248 @players.each do |player|
249 player.write_safe("#SENNICHITE\n#DRAW\n")
251 log_summary("sennichite")
252 @result_type = "#SENNICHITE"