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'
21 require 'shogi_server/game_result'
23 module ShogiServer # for a namespace
26 # When this duration passes after this object instanciated (i.e.
27 # the agree_waiting or start_waiting state lasts too long),
28 # the game will be rejected by the Server.
29 WAITING_EXPIRATION = 120 # seconds
33 def initialize(game_name, player0, player1, board)
34 @monitors = Array::new # array of MonitorHandler*
35 @game_name = game_name
36 if (@game_name =~ /-(\d+)-(\d+)$/)
42 @sente, @gote = player0, player1
44 @sente, @gote = player1, player0
46 @sente.socket_buffer.clear
47 @gote.socket_buffer.clear
50 @current_player, @next_player = @sente, @gote
52 @current_player, @next_player = @gote, @sente
57 @last_move = @board.initial_moves.empty? ? "" : "%s,T1" % [@board.initial_moves.last]
58 @current_turn = @board.initial_moves.size
60 @sente.status = "agree_waiting"
61 @gote.status = "agree_waiting"
63 @game_id = sprintf("%s+%s+%s+%s+%s",
64 $league.event, @game_name,
65 @sente.name, @gote.name, issue_current_time)
67 # The time when this Game instance was created.
68 # Don't be confused with @start_time when the game was started to play.
69 @prepared_time = Time.now
70 log_dir_name = File.join($league.dir,
71 @prepared_time.strftime("%Y"),
72 @prepared_time.strftime("%m"),
73 @prepared_time.strftime("%d"))
74 FileUtils.mkdir_p(log_dir_name) unless File.exist?(log_dir_name)
75 @logfile = File.join(log_dir_name, @game_id + ".csa")
77 $league.games[@game_id] = self
79 log_message(sprintf("game created %s", @game_id))
82 @fh = open(@logfile, "w")
88 attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :game_id, :board, :current_player, :next_player, :fh, :monitors
89 attr_accessor :last_move, :current_turn
90 attr_reader :result, :prepared_time
92 # Path of a log file for this game.
96 @sente.rated? && @gote.rated?
100 return player.status == "game" && @current_player == player
103 def monitoron(monitor_handler)
104 monitoroff(monitor_handler)
105 @monitors.push(monitor_handler)
108 def monitoroff(monitor_handler)
109 @monitors.delete_if {|mon| mon == monitor_handler}
113 @monitors.each do |monitor_handler|
114 yield monitor_handler
120 log_error("Failed to write to Game[%s]'s log file: %s" %
123 @fh.printf("%s\n", str)
127 @sente.write_safe(sprintf("REJECT:%s by %s\n", @game_id, rejector))
128 @gote.write_safe(sprintf("REJECT:%s by %s\n", @game_id, rejector))
133 [@sente, @gote].each do |player|
134 if ["agree_waiting", "start_waiting"].include?(player.status)
136 return # return from this method
140 if (@current_player == killer)
141 @result = GameResultAbnormalWin.new(self, @next_player, @current_player)
148 log_message(sprintf("game finished %s", @game_id))
150 # In a case where a player in agree_waiting or start_waiting status is
151 # rejected, a GameResult object is not yet instanciated.
152 # See test/TC_before_agree.rb.
153 end_time = @result ? @result.end_time : Time.now
154 @fh.printf("'$END_TIME:%s\n", end_time.strftime("%Y/%m/%d %H:%M:%S"))
159 @sente.status = "connected"
160 @gote.status = "connected"
162 if (@current_player.protocol == LoginCSA::PROTOCOL)
163 @current_player.finish
165 if (@next_player.protocol == LoginCSA::PROTOCOL)
168 @monitors = Array::new
171 @current_player = nil
173 $league.games.delete(@game_id)
177 def handle_one_move(str, player)
179 return false if str == :timeout
181 @fh.puts("'Deferred %s" % [str])
182 log_warning("Deferred a move [%s] scince it is not %s 's turn." %
184 player.socket_buffer << str # always in the player's thread
189 @end_time = Time::new
190 t = [(@end_time - @start_time).floor, Least_Time_Per_Move].max
193 if ((@current_player.mytime - t <= -@byoyomi) &&
194 ((@total_time > 0) || (@byoyomi > 0)))
196 elsif (str == :timeout)
197 return false # time isn't expired. players aren't swapped. continue game
199 @current_player.mytime -= t
200 if (@current_player.mytime < 0)
201 @current_player.mytime = 0
204 move_status = @board.handle_one_move(str, @sente == @current_player)
205 # log_debug("move_status: %s for %s's %s" % [move_status, @sente == @current_player ? "BLACK" : "WHITE", str])
207 if [:illegal, :uchifuzume, :oute_kaihimore].include?(move_status)
208 @fh.printf("'ILLEGAL_MOVE(%s)\n", str)
210 if :toryo != move_status
211 # Thinking time includes network traffic
212 @sente.write_safe(sprintf("%s,T%d\n", str, t))
213 @gote.write_safe(sprintf("%s,T%d\n", str, t))
214 @fh.printf("%s\nT%d\n", str, t)
215 @last_move = sprintf("%s,T%d", str, t)
218 @monitors.each do |monitor_handler|
219 monitor_handler.write_one_move(@game_id, self)
222 # if move_status is :toryo then a GameResult message will be sent to monitors
227 if (@next_player.status != "game") # rival is logout or disconnected
228 @result = GameResultAbnormalWin.new(self, @current_player, @next_player)
229 elsif (status == :timeout)
230 # current_player losed
231 @result = GameResultTimeoutWin.new(self, @next_player, @current_player)
232 elsif (move_status == :illegal)
233 @result = GameResultIllegalMoveWin.new(self, @next_player, @current_player)
234 elsif (move_status == :kachi_win)
235 @result = GameResultKachiWin.new(self, @current_player, @next_player)
236 elsif (move_status == :kachi_lose)
237 @result = GameResultIllegalKachiWin.new(self, @next_player, @current_player)
238 elsif (move_status == :toryo)
239 @result = GameResultToryoWin.new(self, @next_player, @current_player)
240 elsif (move_status == :outori)
241 # The current player captures the next player's king
242 @result = GameResultOutoriWin.new(self, @current_player, @next_player)
243 elsif (move_status == :oute_sennichite_sente_lose)
244 @result = GameResultOuteSennichiteWin.new(self, @gote, @sente) # Sente is checking
245 elsif (move_status == :oute_sennichite_gote_lose)
246 @result = GameResultOuteSennichiteWin.new(self, @sente, @gote) # Gote is checking
247 elsif (move_status == :sennichite)
248 @result = GameResultSennichiteDraw.new(self, @current_player, @next_player)
249 elsif (move_status == :uchifuzume)
250 # the current player losed
251 @result = GameResultUchifuzumeWin.new(self, @next_player, @current_player)
252 elsif (move_status == :oute_kaihimore)
253 # the current player losed
254 @result = GameResultOuteKaihiMoreWin.new(self, @next_player, @current_player)
258 @result.process if @result
259 finish() if finish_flag
260 @current_player, @next_player = @next_player, @current_player
261 @start_time = Time::new
265 def is_startable_status?
266 return (@sente && @gote &&
267 (@sente.status == "start_waiting") &&
268 (@gote.status == "start_waiting"))
272 log_message(sprintf("game started %s", @game_id))
273 @sente.status = "game"
274 @gote.status = "game"
275 @sente.write_safe(sprintf("START:%s\n", @game_id))
276 @gote.write_safe(sprintf("START:%s\n", @game_id))
277 @sente.mytime = @total_time
278 @gote.mytime = @total_time
279 @start_time = Time::new
284 @fh.puts("N+#{@sente.name}")
285 @fh.puts("N-#{@gote.name}")
286 @fh.puts("$EVENT:#{@game_id}")
288 @sente.write_safe(propose_message("+"))
289 @gote.write_safe(propose_message("-"))
291 now = Time::new.strftime("%Y/%m/%d %H:%M:%S")
292 @fh.puts("$START_TIME:#{now}")
294 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
295 P2 * -HI * * * * * -KA *
296 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
300 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
301 P8 * +KA * * * * * +HI *
302 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
306 black_name = @sente.rated? ? @sente.player_id : @sente.name
307 white_name = @gote.rated? ? @gote.player_id : @gote.name
308 @fh.puts("'rating:%s:%s" % [black_name, white_name])
310 unless @board.initial_moves.empty?
311 @fh.puts "'buoy game starting with %d moves" % [@board.initial_moves.size]
312 @board.initial_moves.each do |move|
325 Declaration:Jishogi 1.1
333 Total_Time:#{@total_time}
335 Least_Time_Per_Move:#{Least_Time_Per_Move}
336 Remaining_Time+:#{@sente.mytime}
337 Remaining_Time-:#{@gote.mytime}
338 Last_Move:#{@last_move}
339 Current_Turn:#{@current_turn}
349 return str0 + @board.to_s + str1
352 def propose_message(sg_flag)
358 Declaration:Jishogi 1.1
364 To_Move:#{@board.teban ? "+" : "-"}
367 Total_Time:#{@total_time}
369 Least_Time_Per_Move:#{Least_Time_Per_Move}
380 if @prepared_time && (@prepared_time + WAITING_EXPIRATION < Time.now)
389 def issue_current_time
390 time = Time::new.strftime("%Y%m%d%H%M%S").to_i
391 @@mutex.synchronize do
392 while time <= @@time do