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(/^/, "\'"))
53 def notify_monitor(type)
54 @game.each_monitor do |monitor|
55 monitor.write_safe(sprintf("##[MONITOR][%s] %s\n", @game.game_id, type))
60 class GameResultWin < GameResult
61 attr_reader :winner, :loser
63 def initialize(game, winner, loser)
65 @winner, @loser = winner, loser
66 @winner.last_game_win = true
67 @loser.last_game_win = false
73 black_result = white_result = ""
81 log("'summary:%s:%s %s:%s %s\n" % [type,
82 @black.name, black_result,
83 @white.name, white_result])
88 class GameResultAbnormalWin < GameResultWin
90 @winner.write_safe("%TORYO\n#RESIGN\n#WIN\n")
91 @loser.write_safe( "%TORYO\n#RESIGN\n#LOSE\n")
93 log_summary("abnormal")
94 notify_monitor("%%TORYO")
98 class GameResultTimeoutWin < GameResultWin
100 @winner.write_safe("#TIME_UP\n#WIN\n")
101 @loser.write_safe( "#TIME_UP\n#LOSE\n")
102 log_summary("time up")
103 notify_monitor("#TIME_UP")
107 # A player declares (successful) Kachi
108 class GameResultKachiWin < GameResultWin
110 @winner.write_safe("%KACHI\n#JISHOGI\n#WIN\n")
111 @loser.write_safe( "%KACHI\n#JISHOGI\n#LOSE\n")
114 notify_monitor("%%KACHI")
118 # A player declares wrong Kachi
119 class GameResultIllegalKachiWin < GameResultWin
121 @winner.write_safe("%KACHI\n#ILLEGAL_MOVE\n#WIN\n")
122 @loser.write_safe( "%KACHI\n#ILLEGAL_MOVE\n#LOSE\n")
124 log_summary("illegal kachi")
125 notify_monitor("%%KACHI")
129 class GameResultIllegalWin < GameResultWin
130 def initialize(game, winner, loser, cause)
131 super(game, winner, loser)
136 @winner.write_safe("#ILLEGAL_MOVE\n#WIN\n")
137 @loser.write_safe( "#ILLEGAL_MOVE\n#LOSE\n")
139 notify_monitor("#ILLEGAL_MOVE")
143 class GameResultIllegalMoveWin < GameResultIllegalWin
144 def initialize(game, winner, loser)
145 super(game, winner, loser, "illegal move")
149 class GameResultUchifuzumeWin < GameResultIllegalWin
150 def initialize(game, winner, loser)
151 super(game, winner, loser, "uchifuzume")
155 class GameResultOuteKaihiMoreWin < GameResultWin
156 def initialize(game, winner, loser)
157 super(game, winner, loser, "oute_kaihimore")
161 class GameResultOutoriWin < GameResultWin
162 def initialize(game, winner, loser)
163 super(game, winner, loser, "outori")
167 class GameReulstToryoWin < GameResultWin
169 @winner.write_safe("%TORYO\n#RESIGN\n#WIN\n")
170 @loser.write_safe( "%TORYO\n#RESIGN\n#LOSE\n")
173 notify_monitor("%%TORYO")
177 class GameResultOuteSennichiteWin < GameResultWin
179 @winner.write_safe("#OUTE_SENNICHITE\n#WIN\n")
180 @loser.write_safe( "#OUTE_SENNICHITE\n#LOSE\n")
181 log_summary("oute_sennichite")
182 notify_monitor("#OUTE_SENNICHITE")
186 class GameResultDraw < GameResult
187 def initialize(game, p1, p2)
189 p1.last_game_win = false
190 p2.last_game_win = false
193 def log_summary(type)
195 log("'summary:%s:%s draw:%s draw\n", type, @black.name, @white.name)
199 class GameResultSennichiteDraw < GameResultDraw
201 @players.each do |player|
202 player.write_safe("#SENNICHITE\n#DRAW\n")
204 log_summary("sennichite")
205 notify_monitor("#SENNICHITE")
213 def initialize(game_name, player0, player1)
214 @monitors = Array::new
215 @game_name = game_name
216 if (@game_name =~ /-(\d+)-(\d+)$/)
217 @total_time = $1.to_i
222 @sente, @gote = player0, player1
224 @sente, @gote = player1, player0
226 @sente.socket_buffer.clear
227 @gote.socket_buffer.clear
228 @current_player, @next_player = @sente, @gote
235 @sente.status = "agree_waiting"
236 @gote.status = "agree_waiting"
238 @game_id = sprintf("%s+%s+%s+%s+%s",
239 LEAGUE.event, @game_name,
240 @sente.name, @gote.name, issue_current_time)
243 log_dir_name = File.join(LEAGUE.dir,
247 FileUtils.mkdir_p(log_dir_name) unless File.exist?(log_dir_name)
248 @logfile = File.join(log_dir_name, @game_id + ".csa")
250 LEAGUE.games[@game_id] = self
252 log_message(sprintf("game created %s", @game_id))
257 @fh = open(@logfile, "w")
263 attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :game_id, :board, :current_player, :next_player, :fh, :monitors
264 attr_accessor :last_move, :current_turn
268 @sente.rated? && @gote.rated?
272 return player.status == "game" && @current_player == player
275 def monitoron(monitor)
276 @monitors.delete(monitor)
277 @monitors.push(monitor)
280 def monitoroff(monitor)
281 @monitors.delete(monitor)
285 @monitors.each do |monitor|
292 log_error("Failed to write to Game[%s]'s log file: %s" %
295 @fh.printf("%s\n", str)
299 @sente.write_safe(sprintf("REJECT:%s by %s\n", @game_id, rejector))
300 @gote.write_safe(sprintf("REJECT:%s by %s\n", @game_id, rejector))
305 if ["agree_waiting", "start_waiting"].include?(@sente.status)
307 elsif (@current_player == killer)
308 result = GameResultAbnormalWin.new(self, @next_player, @current_player)
315 log_message(sprintf("game finished %s", @game_id))
316 @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
321 @sente.status = "connected"
322 @gote.status = "connected"
324 if (@current_player.protocol == LoginCSA::PROTOCOL)
325 @current_player.finish
327 if (@next_player.protocol == LoginCSA::PROTOCOL)
330 @monitors = Array::new
333 @current_player = nil
335 LEAGUE.games.delete(@game_id)
339 def handle_one_move(str, player)
341 return false if str == :timeout
343 @fh.puts("'Deferred %s" % [str])
344 log_warning("Deferred a move [%s] scince it is not %s 's turn." %
346 player.socket_buffer << str # always in the player's thread
351 @end_time = Time::new
352 t = [(@end_time - @start_time).floor, Least_Time_Per_Move].max
355 if ((@current_player.mytime - t <= -@byoyomi) &&
356 ((@total_time > 0) || (@byoyomi > 0)))
358 elsif (str == :timeout)
359 return false # time isn't expired. players aren't swapped. continue game
361 @current_player.mytime -= t
362 if (@current_player.mytime < 0)
363 @current_player.mytime = 0
366 move_status = @board.handle_one_move(str, @sente == @current_player)
367 # log_debug("move_status: %s for %s's %s" % [move_status, @sente == @current_player ? "BLACK" : "WHITE", str])
369 if [:illegal, :uchifuzume, :oute_kaihimore].include?(move_status)
370 @fh.printf("'ILLEGAL_MOVE(%s)\n", str)
372 if [:normal, :outori, :sennichite, :oute_sennichite_sente_lose, :oute_sennichite_gote_lose].include?(move_status)
373 # Thinking time includes network traffic
374 @sente.write_safe(sprintf("%s,T%d\n", str, t))
375 @gote.write_safe(sprintf("%s,T%d\n", str, t))
376 @fh.printf("%s\nT%d\n", str, t)
377 @last_move = sprintf("%s,T%d", str, t)
381 @monitors.each do |monitor|
382 monitor.write_safe(show.gsub(/^/, "##[MONITOR][#{@game_id}] "))
383 monitor.write_safe(sprintf("##[MONITOR][%s] +OK\n", @game_id))
389 if (@next_player.status != "game") # rival is logout or disconnected
390 result = GameResultAbnormalWin.new(self, @current_player, @next_player)
391 elsif (status == :timeout)
392 # current_player losed
393 result = GameResultTimeoutWin.new(self, @next_player, @current_player)
394 elsif (move_status == :illegal)
395 result = GameResultIllegalMoveWin.new(self, @next_player, @current_player)
396 elsif (move_status == :kachi_win)
397 result = GameResultKachiWin.new(self, @current_player, @next_player)
398 elsif (move_status == :kachi_lose)
399 result = GameResultIllegalKachiWin.new(self, @next_player, @current_player)
400 elsif (move_status == :toryo)
401 result = GameReulstToryoWin.new(self, @next_player, @current_player)
402 elsif (move_status == :outori)
403 # The current player captures the next player's king
404 result = GameResultOutoriWin.new(self, @current_player, @next_player)
405 elsif (move_status == :oute_sennichite_sente_lose)
406 result = GameResultOuteSennichiteWin.new(self, @gote, @sente) # Sente is checking
407 elsif (move_status == :oute_sennichite_gote_lose)
408 result = GameResultOuteSennichiteWin.new(self, @sente, @gote) # Gote is checking
409 elsif (move_status == :sennichite)
410 result = GameResultSennichiteDraw.new(self, @current_player, @next_player)
411 elsif (move_status == :uchifuzume)
412 # the current player losed
413 result = GameResultUchifuzumeWin.new(self, @next_player, @current_player)
414 elsif (move_status == :oute_kaihimore)
415 # the current player losed
416 result = GameResultOuteKaihiMoreWin.new(self, @next_player, @current_player)
420 result.process if result
421 finish() if finish_flag
422 @current_player, @next_player = @next_player, @current_player
423 @start_time = Time::new
428 log_message(sprintf("game started %s", @game_id))
429 @sente.write_safe(sprintf("START:%s\n", @game_id))
430 @gote.write_safe(sprintf("START:%s\n", @game_id))
431 @sente.mytime = @total_time
432 @gote.mytime = @total_time
433 @start_time = Time::new
438 @fh.puts("N+#{@sente.name}")
439 @fh.puts("N-#{@gote.name}")
440 @fh.puts("$EVENT:#{@game_id}")
442 @sente.write_safe(propose_message("+"))
443 @gote.write_safe(propose_message("-"))
445 now = Time::new.strftime("%Y/%m/%d %H:%M:%S")
446 @fh.puts("$START_TIME:#{now}")
448 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
449 P2 * -HI * * * * * -KA *
450 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
454 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
455 P8 * +KA * * * * * +HI *
456 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
460 black_name = @sente.rated? ? @sente.player_id : @sente.name
461 white_name = @gote.rated? ? @gote.player_id : @gote.name
462 @fh.puts("'rating:%s:%s" % [black_name, white_name])
472 Declaration:Jishogi 1.1
480 Total_Time:#{@total_time}
482 Least_Time_Per_Move:#{Least_Time_Per_Move}
483 Remaining_Time+:#{@sente.mytime}
484 Remaining_Time-:#{@gote.mytime}
485 Last_Move:#{@last_move}
486 Current_Turn:#{@current_turn}
496 return str0 + @board.to_s + str1
499 def propose_message(sg_flag)
505 Declaration:Jishogi 1.1
514 Total_Time:#{@total_time}
516 Least_Time_Per_Move:#{Least_Time_Per_Move}
519 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
520 P2 * -HI * * * * * -KA *
521 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
525 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
526 P8 * +KA * * * * * +HI *
527 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
539 def issue_current_time
540 time = Time::new.strftime("%Y%m%d%H%M%S").to_i
541 @@mutex.synchronize do
542 while time <= @@time do