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/league/floodgate'
23 module ShogiServer # for a namespace
25 # MonitorObserver obserers GameResult to send messages to the monotors
29 def update(game_result)
30 game_result.game.each_monitor do |monitor|
31 monitor.write_safe("##[MONITOR][%s] %s\n" % [game_result.game.game_id, game_result.result_type])
36 # Base class for a game result
49 # Command to send monitors such as '%TORYO' etc...
50 attr_reader :result_type
52 def initialize(game, p1, p2)
55 if p1.sente && !p2.sente
56 @black, @white = p1, p2
57 elsif !p1.sente && p2.sente
58 @black, @white = p2, p1
60 raise "Never reached!"
62 @players.each do |player|
63 player.status = "connected"
71 add_observer MonitorObserver.new
73 if League::Floodgate.game_name?(@game.game_name) &&
74 @game.sente.player_id &&
75 @game.gote.player_id &&
76 $options["floodgate-history"]
77 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"
261 def initialize(game_name, player0, player1)
262 @monitors = Array::new
263 @game_name = game_name
264 if (@game_name =~ /-(\d+)-(\d+)$/)
265 @total_time = $1.to_i
270 @sente, @gote = player0, player1
272 @sente, @gote = player1, player0
274 @sente.socket_buffer.clear
275 @gote.socket_buffer.clear
276 @current_player, @next_player = @sente, @gote
283 @sente.status = "agree_waiting"
284 @gote.status = "agree_waiting"
286 @game_id = sprintf("%s+%s+%s+%s+%s",
287 $league.event, @game_name,
288 @sente.name, @gote.name, issue_current_time)
291 log_dir_name = File.join($league.dir,
295 FileUtils.mkdir_p(log_dir_name) unless File.exist?(log_dir_name)
296 @logfile = File.join(log_dir_name, @game_id + ".csa")
298 $league.games[@game_id] = self
300 log_message(sprintf("game created %s", @game_id))
305 @fh = open(@logfile, "w")
311 attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :game_id, :board, :current_player, :next_player, :fh, :monitors
312 attr_accessor :last_move, :current_turn
316 @sente.rated? && @gote.rated?
320 return player.status == "game" && @current_player == player
323 def monitoron(monitor)
324 @monitors.delete(monitor)
325 @monitors.push(monitor)
328 def monitoroff(monitor)
329 @monitors.delete(monitor)
333 @monitors.each do |monitor|
340 log_error("Failed to write to Game[%s]'s log file: %s" %
343 @fh.printf("%s\n", str)
347 @sente.write_safe(sprintf("REJECT:%s by %s\n", @game_id, rejector))
348 @gote.write_safe(sprintf("REJECT:%s by %s\n", @game_id, rejector))
353 [@sente, @gote].each do |player|
354 if ["agree_waiting", "start_waiting"].include?(player.status)
356 return # return from this method
360 if (@current_player == killer)
361 result = GameResultAbnormalWin.new(self, @next_player, @current_player)
368 log_message(sprintf("game finished %s", @game_id))
369 @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
374 @sente.status = "connected"
375 @gote.status = "connected"
377 if (@current_player.protocol == LoginCSA::PROTOCOL)
378 @current_player.finish
380 if (@next_player.protocol == LoginCSA::PROTOCOL)
383 @monitors = Array::new
386 @current_player = nil
388 $league.games.delete(@game_id)
392 def handle_one_move(str, player)
394 return false if str == :timeout
396 @fh.puts("'Deferred %s" % [str])
397 log_warning("Deferred a move [%s] scince it is not %s 's turn." %
399 player.socket_buffer << str # always in the player's thread
404 @end_time = Time::new
405 t = [(@end_time - @start_time).floor, Least_Time_Per_Move].max
408 if ((@current_player.mytime - t <= -@byoyomi) &&
409 ((@total_time > 0) || (@byoyomi > 0)))
411 elsif (str == :timeout)
412 return false # time isn't expired. players aren't swapped. continue game
414 @current_player.mytime -= t
415 if (@current_player.mytime < 0)
416 @current_player.mytime = 0
419 move_status = @board.handle_one_move(str, @sente == @current_player)
420 # log_debug("move_status: %s for %s's %s" % [move_status, @sente == @current_player ? "BLACK" : "WHITE", str])
422 if [:illegal, :uchifuzume, :oute_kaihimore].include?(move_status)
423 @fh.printf("'ILLEGAL_MOVE(%s)\n", str)
425 if [:normal, :outori, :sennichite, :oute_sennichite_sente_lose, :oute_sennichite_gote_lose].include?(move_status)
426 # Thinking time includes network traffic
427 @sente.write_safe(sprintf("%s,T%d\n", str, t))
428 @gote.write_safe(sprintf("%s,T%d\n", str, t))
429 @fh.printf("%s\nT%d\n", str, t)
430 @last_move = sprintf("%s,T%d", str, t)
434 @monitors.each do |monitor|
435 monitor.write_safe(show.gsub(/^/, "##[MONITOR][#{@game_id}] "))
436 monitor.write_safe(sprintf("##[MONITOR][%s] +OK\n", @game_id))
442 if (@next_player.status != "game") # rival is logout or disconnected
443 result = GameResultAbnormalWin.new(self, @current_player, @next_player)
444 elsif (status == :timeout)
445 # current_player losed
446 result = GameResultTimeoutWin.new(self, @next_player, @current_player)
447 elsif (move_status == :illegal)
448 result = GameResultIllegalMoveWin.new(self, @next_player, @current_player)
449 elsif (move_status == :kachi_win)
450 result = GameResultKachiWin.new(self, @current_player, @next_player)
451 elsif (move_status == :kachi_lose)
452 result = GameResultIllegalKachiWin.new(self, @next_player, @current_player)
453 elsif (move_status == :toryo)
454 result = GameResultToryoWin.new(self, @next_player, @current_player)
455 elsif (move_status == :outori)
456 # The current player captures the next player's king
457 result = GameResultOutoriWin.new(self, @current_player, @next_player)
458 elsif (move_status == :oute_sennichite_sente_lose)
459 result = GameResultOuteSennichiteWin.new(self, @gote, @sente) # Sente is checking
460 elsif (move_status == :oute_sennichite_gote_lose)
461 result = GameResultOuteSennichiteWin.new(self, @sente, @gote) # Gote is checking
462 elsif (move_status == :sennichite)
463 result = GameResultSennichiteDraw.new(self, @current_player, @next_player)
464 elsif (move_status == :uchifuzume)
465 # the current player losed
466 result = GameResultUchifuzumeWin.new(self, @next_player, @current_player)
467 elsif (move_status == :oute_kaihimore)
468 # the current player losed
469 result = GameResultOuteKaihiMoreWin.new(self, @next_player, @current_player)
473 result.process if result
474 finish() if finish_flag
475 @current_player, @next_player = @next_player, @current_player
476 @start_time = Time::new
481 log_message(sprintf("game started %s", @game_id))
482 @sente.write_safe(sprintf("START:%s\n", @game_id))
483 @gote.write_safe(sprintf("START:%s\n", @game_id))
484 @sente.mytime = @total_time
485 @gote.mytime = @total_time
486 @start_time = Time::new
491 @fh.puts("N+#{@sente.name}")
492 @fh.puts("N-#{@gote.name}")
493 @fh.puts("$EVENT:#{@game_id}")
495 @sente.write_safe(propose_message("+"))
496 @gote.write_safe(propose_message("-"))
498 now = Time::new.strftime("%Y/%m/%d %H:%M:%S")
499 @fh.puts("$START_TIME:#{now}")
501 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
502 P2 * -HI * * * * * -KA *
503 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
507 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
508 P8 * +KA * * * * * +HI *
509 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
513 black_name = @sente.rated? ? @sente.player_id : @sente.name
514 white_name = @gote.rated? ? @gote.player_id : @gote.name
515 @fh.puts("'rating:%s:%s" % [black_name, white_name])
525 Declaration:Jishogi 1.1
533 Total_Time:#{@total_time}
535 Least_Time_Per_Move:#{Least_Time_Per_Move}
536 Remaining_Time+:#{@sente.mytime}
537 Remaining_Time-:#{@gote.mytime}
538 Last_Move:#{@last_move}
539 Current_Turn:#{@current_turn}
549 return str0 + @board.to_s + str1
552 def propose_message(sg_flag)
558 Declaration:Jishogi 1.1
567 Total_Time:#{@total_time}
569 Least_Time_Per_Move:#{Least_Time_Per_Move}
572 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
573 P2 * -HI * * * * * -KA *
574 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
578 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
579 P8 * +KA * * * * * +HI *
580 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
592 def issue_current_time
593 time = Time::new.strftime("%Y%m%d%H%M%S").to_i
594 @@mutex.synchronize do
595 while time <= @@time do