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"
49 log(@game.board.to_s.gsub(/^/, "\'"))
52 def notify_monitor(type)
53 @game.each_monitor do |monitor|
54 monitor.write_safe(sprintf("##[MONITOR][%s] %s\n", @game.game_id, type))
59 class GameResultWin < GameResult
60 attr_reader :winner, :loser
62 def initialize(game, winner, loser)
64 @winner, @loser = winner, loser
65 @winner.last_game_win = true
66 @loser.last_game_win = false
72 black_result = white_result = ""
80 log("'summary:%s:%s %s:%s %s\n" % [type,
81 @black.name, black_result,
82 @white.name, white_result])
87 class GameResultAbnormalWin < GameResultWin
89 @winner.write_safe("%TORYO\n#RESIGN\n#WIN\n")
90 @loser.write_safe( "%TORYO\n#RESIGN\n#LOSE\n")
92 log_summary("abnormal")
93 notify_monitor("%%TORYO")
97 class GameResultTimeoutWin < GameResultWin
99 @winner.write_safe("#TIME_UP\n#WIN\n")
100 @loser.write_safe( "#TIME_UP\n#LOSE\n")
101 log_summary("time up")
102 notify_monitor("#TIME_UP")
106 # A player declares (successful) Kachi
107 class GameResultKachiWin < GameResultWin
109 @winner.write_safe("%KACHI\n#JISHOGI\n#WIN\n")
110 @loser.write_safe( "%KACHI\n#JISHOGI\n#LOSE\n")
113 notify_monitor("%%KACHI")
117 # A player declares wrong Kachi
118 class GameResultIllegalKachiWin < GameResultWin
120 @winner.write_safe("%KACHI\n#ILLEGAL_MOVE\n#WIN\n")
121 @loser.write_safe( "%KACHI\n#ILLEGAL_MOVE\n#LOSE\n")
123 log_summary("illegal kachi")
124 notify_monitor("%%KACHI")
128 class GameResultIllegalWin < GameResultWin
129 def initialize(game, winner, loser, cause)
130 super(game, winner, loser)
135 @winner.write_safe("#ILLEGAL_MOVE\n#WIN\n")
136 @loser.write_safe( "#ILLEGAL_MOVE\n#LOSE\n")
138 notify_monitor("#ILLEGAL_MOVE")
142 class GameResultIllegalMoveWin < GameResultIllegalWin
143 def initialize(game, winner, loser)
144 super(game, winner, loser, "illegal move")
148 class GameResultUchifuzumeWin < GameResultIllegalWin
149 def initialize(game, winner, loser)
150 super(game, winner, loser, "uchifuzume")
154 class GameResultOuteKaihiMoreWin < GameResultWin
155 def initialize(game, winner, loser)
156 super(game, winner, loser, "oute_kaihimore")
160 class GameResultOutoriWin < GameResultWin
161 def initialize(game, winner, loser)
162 super(game, winner, loser, "outori")
166 class GameReulstToryoWin < GameResultWin
168 @winner.write_safe("%TORYO\n#RESIGN\n#WIN\n")
169 @loser.write_safe( "%TORYO\n#RESIGN\n#LOSE\n")
172 notify_monitor("%%TORYO")
176 class GameResultOuteSennichiteWin < GameResultWin
178 @winner.write_safe("#OUTE_SENNICHITE\n#WIN\n")
179 @loser.write_safe( "#OUTE_SENNICHITE\n#LOSE\n")
180 log_summary("oute_sennichite")
181 notify_monitor("#OUTE_SENNICHITE")
185 class GameResultDraw < GameResult
186 def initialize(game, p1, p2)
188 p1.last_game_win = false
189 p2.last_game_win = false
192 def log_summary(type)
194 log("'summary:%s:%s draw:%s draw\n" % [type, @black.name, @white.name])
198 class GameResultSennichiteDraw < GameResultDraw
200 @players.each do |player|
201 player.write_safe("#SENNICHITE\n#DRAW\n")
203 log_summary("sennichite")
204 notify_monitor("#SENNICHITE")
212 def initialize(game_name, player0, player1)
213 @monitors = Array::new
214 @game_name = game_name
215 if (@game_name =~ /-(\d+)-(\d+)$/)
216 @total_time = $1.to_i
221 @sente, @gote = player0, player1
223 @sente, @gote = player1, player0
225 @sente.socket_buffer.clear
226 @gote.socket_buffer.clear
227 @current_player, @next_player = @sente, @gote
234 @sente.status = "agree_waiting"
235 @gote.status = "agree_waiting"
237 @game_id = sprintf("%s+%s+%s+%s+%s",
238 LEAGUE.event, @game_name,
239 @sente.name, @gote.name, issue_current_time)
242 log_dir_name = File.join(LEAGUE.dir,
246 FileUtils.mkdir_p(log_dir_name) unless File.exist?(log_dir_name)
247 @logfile = File.join(log_dir_name, @game_id + ".csa")
249 LEAGUE.games[@game_id] = self
251 log_message(sprintf("game created %s", @game_id))
256 @fh = open(@logfile, "w")
262 attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :game_id, :board, :current_player, :next_player, :fh, :monitors
263 attr_accessor :last_move, :current_turn
267 @sente.rated? && @gote.rated?
271 return player.status == "game" && @current_player == player
274 def monitoron(monitor)
275 @monitors.delete(monitor)
276 @monitors.push(monitor)
279 def monitoroff(monitor)
280 @monitors.delete(monitor)
284 @monitors.each do |monitor|
291 log_error("Failed to write to Game[%s]'s log file: %s" %
294 @fh.printf("%s\n", str)
298 @sente.write_safe(sprintf("REJECT:%s by %s\n", @game_id, rejector))
299 @gote.write_safe(sprintf("REJECT:%s by %s\n", @game_id, rejector))
304 if ["agree_waiting", "start_waiting"].include?(@sente.status)
306 elsif (@current_player == killer)
307 result = GameResultAbnormalWin.new(self, @next_player, @current_player)
314 log_message(sprintf("game finished %s", @game_id))
315 @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
320 @sente.status = "connected"
321 @gote.status = "connected"
323 if (@current_player.protocol == LoginCSA::PROTOCOL)
324 @current_player.finish
326 if (@next_player.protocol == LoginCSA::PROTOCOL)
329 @monitors = Array::new
332 @current_player = nil
334 LEAGUE.games.delete(@game_id)
338 def handle_one_move(str, player)
340 return false if str == :timeout
342 @fh.puts("'Deferred %s" % [str])
343 log_warning("Deferred a move [%s] scince it is not %s 's turn." %
345 player.socket_buffer << str # always in the player's thread
350 @end_time = Time::new
351 t = [(@end_time - @start_time).floor, Least_Time_Per_Move].max
354 if ((@current_player.mytime - t <= -@byoyomi) &&
355 ((@total_time > 0) || (@byoyomi > 0)))
357 elsif (str == :timeout)
358 return false # time isn't expired. players aren't swapped. continue game
360 @current_player.mytime -= t
361 if (@current_player.mytime < 0)
362 @current_player.mytime = 0
365 move_status = @board.handle_one_move(str, @sente == @current_player)
366 # log_debug("move_status: %s for %s's %s" % [move_status, @sente == @current_player ? "BLACK" : "WHITE", str])
368 if [:illegal, :uchifuzume, :oute_kaihimore].include?(move_status)
369 @fh.printf("'ILLEGAL_MOVE(%s)\n", str)
371 if [:normal, :outori, :sennichite, :oute_sennichite_sente_lose, :oute_sennichite_gote_lose].include?(move_status)
372 # Thinking time includes network traffic
373 @sente.write_safe(sprintf("%s,T%d\n", str, t))
374 @gote.write_safe(sprintf("%s,T%d\n", str, t))
375 @fh.printf("%s\nT%d\n", str, t)
376 @last_move = sprintf("%s,T%d", str, t)
380 @monitors.each do |monitor|
381 monitor.write_safe(show.gsub(/^/, "##[MONITOR][#{@game_id}] "))
382 monitor.write_safe(sprintf("##[MONITOR][%s] +OK\n", @game_id))
388 if (@next_player.status != "game") # rival is logout or disconnected
389 result = GameResultAbnormalWin.new(self, @current_player, @next_player)
390 elsif (status == :timeout)
391 # current_player losed
392 result = GameResultTimeoutWin.new(self, @next_player, @current_player)
393 elsif (move_status == :illegal)
394 result = GameResultIllegalMoveWin.new(self, @next_player, @current_player)
395 elsif (move_status == :kachi_win)
396 result = GameResultKachiWin.new(self, @current_player, @next_player)
397 elsif (move_status == :kachi_lose)
398 result = GameResultIllegalKachiWin.new(self, @next_player, @current_player)
399 elsif (move_status == :toryo)
400 result = GameReulstToryoWin.new(self, @next_player, @current_player)
401 elsif (move_status == :outori)
402 # The current player captures the next player's king
403 result = GameResultOutoriWin.new(self, @current_player, @next_player)
404 elsif (move_status == :oute_sennichite_sente_lose)
405 result = GameResultOuteSennichiteWin.new(self, @gote, @sente) # Sente is checking
406 elsif (move_status == :oute_sennichite_gote_lose)
407 result = GameResultOuteSennichiteWin.new(self, @sente, @gote) # Gote is checking
408 elsif (move_status == :sennichite)
409 result = GameResultSennichiteDraw.new(self, @current_player, @next_player)
410 elsif (move_status == :uchifuzume)
411 # the current player losed
412 result = GameResultUchifuzumeWin.new(self, @next_player, @current_player)
413 elsif (move_status == :oute_kaihimore)
414 # the current player losed
415 result = GameResultOuteKaihiMoreWin.new(self, @next_player, @current_player)
419 result.process if result
420 finish() if finish_flag
421 @current_player, @next_player = @next_player, @current_player
422 @start_time = Time::new
427 log_message(sprintf("game started %s", @game_id))
428 @sente.write_safe(sprintf("START:%s\n", @game_id))
429 @gote.write_safe(sprintf("START:%s\n", @game_id))
430 @sente.mytime = @total_time
431 @gote.mytime = @total_time
432 @start_time = Time::new
437 @fh.puts("N+#{@sente.name}")
438 @fh.puts("N-#{@gote.name}")
439 @fh.puts("$EVENT:#{@game_id}")
441 @sente.write_safe(propose_message("+"))
442 @gote.write_safe(propose_message("-"))
444 now = Time::new.strftime("%Y/%m/%d %H:%M:%S")
445 @fh.puts("$START_TIME:#{now}")
447 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
448 P2 * -HI * * * * * -KA *
449 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
453 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
454 P8 * +KA * * * * * +HI *
455 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
459 black_name = @sente.rated? ? @sente.player_id : @sente.name
460 white_name = @gote.rated? ? @gote.player_id : @gote.name
461 @fh.puts("'rating:%s:%s" % [black_name, white_name])
471 Declaration:Jishogi 1.1
479 Total_Time:#{@total_time}
481 Least_Time_Per_Move:#{Least_Time_Per_Move}
482 Remaining_Time+:#{@sente.mytime}
483 Remaining_Time-:#{@gote.mytime}
484 Last_Move:#{@last_move}
485 Current_Turn:#{@current_turn}
495 return str0 + @board.to_s + str1
498 def propose_message(sg_flag)
504 Declaration:Jishogi 1.1
513 Total_Time:#{@total_time}
515 Least_Time_Per_Move:#{Least_Time_Per_Move}
518 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
519 P2 * -HI * * * * * -KA *
520 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
524 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
525 P8 * +KA * * * * * +HI *
526 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
538 def issue_current_time
539 time = Time::new.strftime("%Y%m%d%H%M%S").to_i
540 @@mutex.synchronize do
541 while time <= @@time do