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 module ShogiServer # for a namespace
23 attr_reader :players, :black, :white
25 def initialize(game, p1, p2)
28 if p1.sente && !p2.sente
29 @black, @white = p1, p2
30 elsif !p1.sente && p2.sente
31 @black, @white = p2, p1
33 raise "Never reached!"
35 @players.each do |player|
36 player.status = "connected"
50 log(@game.board.to_s.gsub(/^/, "\'"))
54 log("'rating:%s\n" % [self.to_s]) if @game.rated?
58 black_name = @black.rated? ? @black.player_id : @black.name
59 white_name = @white.rated? ? @white.player_id : @white.name
60 return "%s:%s" % [black_name, white_name]
63 def notify_monitor(type)
64 @game.each_monitor do |monitor|
65 monitor.write_safe(sprintf("##[MONITOR][%s] %s\n", @game.game_id, type))
70 class GameResultWin < GameResult
71 attr_reader :winner, :loser
73 def initialize(game, winner, loser)
75 @winner, @loser = winner, loser
76 @winner.last_game_win = true
77 @loser.last_game_win = false
83 black_result = white_result = ""
91 log("'summary:%s:%s %s:%s %s\n" % [type,
92 @black.name, black_result,
93 @white.name, white_result])
99 class GameResultAbnormalWin < GameResultWin
101 @winner.write_safe("%TORYO\n#RESIGN\n#WIN\n")
102 @loser.write_safe( "%TORYO\n#RESIGN\n#LOSE\n")
104 log_summary("abnormal")
105 notify_monitor("%%TORYO")
109 class GameResultTimeoutWin < GameResultWin
111 @winner.write_safe("#TIME_UP\n#WIN\n")
112 @loser.write_safe( "#TIME_UP\n#LOSE\n")
113 log_summary("time up")
114 notify_monitor("#TIME_UP")
118 # A player declares (successful) Kachi
119 class GameResultKachiWin < GameResultWin
121 @winner.write_safe("%KACHI\n#JISHOGI\n#WIN\n")
122 @loser.write_safe( "%KACHI\n#JISHOGI\n#LOSE\n")
125 notify_monitor("%%KACHI")
129 # A player declares wrong Kachi
130 class GameResultIllegalKachiWin < GameResultWin
132 @winner.write_safe("%KACHI\n#ILLEGAL_MOVE\n#WIN\n")
133 @loser.write_safe( "%KACHI\n#ILLEGAL_MOVE\n#LOSE\n")
135 log_summary("illegal kachi")
136 notify_monitor("%%KACHI")
140 class GameResultIllegalWin < GameResultWin
141 def initialize(game, winner, loser, cause)
142 super(game, winner, loser)
147 @winner.write_safe("#ILLEGAL_MOVE\n#WIN\n")
148 @loser.write_safe( "#ILLEGAL_MOVE\n#LOSE\n")
150 notify_monitor("#ILLEGAL_MOVE")
154 class GameResultIllegalMoveWin < GameResultIllegalWin
155 def initialize(game, winner, loser)
156 super(game, winner, loser, "illegal move")
160 class GameResultUchifuzumeWin < GameResultIllegalWin
161 def initialize(game, winner, loser)
162 super(game, winner, loser, "uchifuzume")
166 class GameResultOuteKaihiMoreWin < GameResultWin
167 def initialize(game, winner, loser)
168 super(game, winner, loser, "oute_kaihimore")
172 class GameResultOutoriWin < GameResultWin
173 def initialize(game, winner, loser)
174 super(game, winner, loser, "outori")
178 class GameReulstToryoWin < GameResultWin
180 @winner.write_safe("%TORYO\n#RESIGN\n#WIN\n")
181 @loser.write_safe( "%TORYO\n#RESIGN\n#LOSE\n")
184 notify_monitor("%%TORYO")
188 class GameResultOuteSennichiteWin < GameResultWin
190 @winner.write_safe("#OUTE_SENNICHITE\n#WIN\n")
191 @loser.write_safe( "#OUTE_SENNICHITE\n#LOSE\n")
192 log_summary("oute_sennichite")
193 notify_monitor("#OUTE_SENNICHITE")
197 class GameResultDraw < GameResult
198 def initialize(game, p1, p2)
200 p1.last_game_win = false
201 p2.last_game_win = false
204 def log_summary(type)
206 log("'summary:%s:%s draw:%s draw\n", type, @black.name, @white.name)
211 class GameResultSennichiteDraw < GameResultDraw
213 @players.each do |player|
214 player.write_safe("#SENNICHITE\n#DRAW\n")
216 log_summary("sennichite")
217 notify_monitor("#SENNICHITE")
225 def initialize(game_name, player0, player1)
226 @monitors = Array::new
227 @game_name = game_name
228 if (@game_name =~ /-(\d+)-(\d+)$/)
229 @total_time = $1.to_i
234 @sente, @gote = player0, player1
236 @sente, @gote = player1, player0
238 @sente.socket_buffer.clear
239 @gote.socket_buffer.clear
240 @current_player, @next_player = @sente, @gote
247 @sente.status = "agree_waiting"
248 @gote.status = "agree_waiting"
250 @game_id = sprintf("%s+%s+%s+%s+%s",
251 LEAGUE.event, @game_name,
252 @sente.name, @gote.name, issue_current_time)
255 log_dir_name = File.join(LEAGUE.dir,
259 FileUtils.mkdir_p(log_dir_name) unless File.exist?(log_dir_name)
260 @logfile = File.join(log_dir_name, @game_id + ".csa")
262 LEAGUE.games[@game_id] = self
264 log_message(sprintf("game created %s", @game_id))
269 @fh = open(@logfile, "w")
275 attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :game_id, :board, :current_player, :next_player, :fh, :monitors
276 attr_accessor :last_move, :current_turn
280 @sente.rated? && @gote.rated?
284 return player.status == "game" && @current_player == player
287 def monitoron(monitor)
288 @monitors.delete(monitor)
289 @monitors.push(monitor)
292 def monitoroff(monitor)
293 @monitors.delete(monitor)
297 @monitors.each do |monitor|
304 log_error("Failed to write to Game[%s]'s log file: %s" %
307 @fh.printf("%s\n", str)
311 @sente.write_safe(sprintf("REJECT:%s by %s\n", @game_id, rejector))
312 @gote.write_safe(sprintf("REJECT:%s by %s\n", @game_id, rejector))
317 if ["agree_waiting", "start_waiting"].include?(@sente.status)
319 elsif (@current_player == killer)
320 result = GameResultAbnormalWin.new(self, @next_player, @current_player)
327 log_message(sprintf("game finished %s", @game_id))
328 @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
333 @sente.status = "connected"
334 @gote.status = "connected"
336 if (@current_player.protocol == LoginCSA::PROTOCOL)
337 @current_player.finish
339 if (@next_player.protocol == LoginCSA::PROTOCOL)
342 @monitors = Array::new
345 @current_player = nil
347 LEAGUE.games.delete(@game_id)
351 def handle_one_move(str, player)
353 return false if str == :timeout
355 @fh.puts("'Deferred %s" % [str])
356 log_warning("Deferred a move [%s] scince it is not %s 's turn." %
358 player.socket_buffer << str # always in the player's thread
363 @end_time = Time::new
364 t = [(@end_time - @start_time).floor, Least_Time_Per_Move].max
367 if ((@current_player.mytime - t <= -@byoyomi) &&
368 ((@total_time > 0) || (@byoyomi > 0)))
370 elsif (str == :timeout)
371 return false # time isn't expired. players aren't swapped. continue game
373 @current_player.mytime -= t
374 if (@current_player.mytime < 0)
375 @current_player.mytime = 0
378 move_status = @board.handle_one_move(str, @sente == @current_player)
380 if [:illegal, :uchifuzume, :oute_kaihimore].include?(move_status)
381 @fh.printf("'ILLEGAL_MOVE(%s)\n", str)
383 if [:normal, :outori, :sennichite, :oute_sennichite_sente_lose, :oute_sennichite_gote_lose].include?(move_status)
384 # Thinking time includes network traffic
385 @sente.write_safe(sprintf("%s,T%d\n", str, t))
386 @gote.write_safe(sprintf("%s,T%d\n", str, t))
387 @fh.printf("%s\nT%d\n", str, t)
388 @last_move = sprintf("%s,T%d", str, t)
392 @monitors.each do |monitor|
393 monitor.write_safe(show.gsub(/^/, "##[MONITOR][#{@game_id}] "))
394 monitor.write_safe(sprintf("##[MONITOR][%s] +OK\n", @game_id))
400 if (@next_player.status != "game") # rival is logout or disconnected
401 result = GameResultAbnormalWin.new(self, @current_player, @next_player)
402 elsif (status == :timeout)
403 # current_player losed
404 result = GameResultTimeoutWin.new(self, @next_player, @current_player)
405 elsif (move_status == :illegal)
406 result = GameResultIllegalMoveWin.new(self, @next_player, @current_player)
407 elsif (move_status == :kachi_win)
408 result = GameResultKachiWin.new(self, @current_player, @next_player)
409 elsif (move_status == :kachi_lose)
410 result = GameResultIllegalKachiWin.new(self, @next_player, @current_player)
411 elsif (move_status == :toryo)
412 result = GameReulstToryoWin.new(self, @next_player, @current_player)
413 elsif (move_status == :outori)
414 # The current player captures the next player's king
415 result = GameResultOutoriWin.new(self, @current_player, @next_player)
416 elsif (move_status == :oute_sennichite_sente_lose)
417 result = GameResultOuteSennichiteWin.new(self, @gote, @sente) # Sente is checking
418 elsif (move_status == :oute_sennichite_gote_lose)
419 result = GameResultOuteSennichiteWin.new(self, @sente, @gote) # Gote is checking
420 elsif (move_status == :sennichite)
421 result = GameResultSennichiteDraw.new(self, @current_player, @next_player)
422 elsif (move_status == :uchifuzume)
423 # the current player losed
424 result = GameResultUchifuzumeWin.new(self, @next_player, @current_player)
425 elsif (move_status == :oute_kaihimore)
426 # the current player losed
427 result = GameResultOuteKaihiMoreWin.new(self, @next_player, @current_player)
431 result.process if result
432 finish() if finish_flag
433 @current_player, @next_player = @next_player, @current_player
434 @start_time = Time::new
439 log_message(sprintf("game started %s", @game_id))
440 @sente.write_safe(sprintf("START:%s\n", @game_id))
441 @gote.write_safe(sprintf("START:%s\n", @game_id))
442 @sente.mytime = @total_time
443 @gote.mytime = @total_time
444 @start_time = Time::new
449 @fh.puts("N+#{@sente.name}")
450 @fh.puts("N-#{@gote.name}")
451 @fh.puts("$EVENT:#{@game_id}")
453 @sente.write_safe(propose_message("+"))
454 @gote.write_safe(propose_message("-"))
456 now = Time::new.strftime("%Y/%m/%d %H:%M:%S")
457 @fh.puts("$START_TIME:#{now}")
459 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
460 P2 * -HI * * * * * -KA *
461 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
465 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
466 P8 * +KA * * * * * +HI *
467 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
478 Declaration:Jishogi 1.1
486 Total_Time:#{@total_time}
488 Least_Time_Per_Move:#{Least_Time_Per_Move}
489 Remaining_Time+:#{@sente.mytime}
490 Remaining_Time-:#{@gote.mytime}
491 Last_Move:#{@last_move}
492 Current_Turn:#{@current_turn}
502 return str0 + @board.to_s + str1
505 def propose_message(sg_flag)
511 Declaration:Jishogi 1.1
520 Total_Time:#{@total_time}
522 Least_Time_Per_Move:#{Least_Time_Per_Move}
525 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
526 P2 * -HI * * * * * -KA *
527 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
531 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
532 P8 * +KA * * * * * +HI *
533 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
545 def issue_current_time
546 time = Time::new.strftime("%Y%m%d%H%M%S").to_i
547 @@mutex.synchronize do
548 while time <= @@time do