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)
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