2 ## -*-Ruby-*- $RCSfile$ $Revision$ $Name$
4 ## Copyright (C) 2004 773@2ch
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 DEFAULT_TIMEOUT = 10 # for single socket operation
22 Least_Time_Per_Move = 1
23 Watchdog_Time = 30 # time for ping
24 Login_Time = 300 # time for LOGIN
26 Release = "$Name$".split[1].sub(/\A[^\d]*/, '').gsub(/_/, '.')
27 Release.concat("-") if (Release == "")
28 Revision = "$Revision$".gsub(/[^\.\d]/, '')
39 TCPSocket.do_not_reverse_lookup = true
42 def gets_timeout(t = DEFAULT_TIMEOUT)
62 return self.write(str)
77 @hash[player.name] = player
80 @hash.delete(player.name)
82 def duplicated?(player)
83 if (@hash[player.name])
89 def get_player(status, game_name, sente, searcher=nil)
90 @hash.each do |name, player|
91 if ((player.status == status) &&
92 (player.game_name == game_name) &&
93 ((player.sente == nil) || (player.sente == sente)) &&
94 ((searcher == nil) || (player != searcher)))
100 def new_game(game_name, player0, player1)
101 game = Game::new(game_name, player0, player1)
109 def initialize(str, socket)
113 @status = "connected" # game_waiting -> agree_waiting -> start_waiting -> game
115 @protocol = nil # CSA or x1
116 @eol = "\m" # favorite eol code
121 @watchdog_thread = nil
126 attr_accessor :name, :password, :socket, :status
127 attr_accessor :protocol, :eol, :game, :mytime, :watchdog_thread, :game_name, :sente
130 log_message(sprintf("user %s finish", @name))
131 Thread::kill(@watchdog_thread) if @watchdog_thread
132 @socket.close if (! @socket.closed?)
138 Ping.pingecho(@socket.addr[3])
146 if ((status == "game_waiting") ||
147 (status == "agree_waiting") ||
150 return sprintf("%s %s %s +", @name, @status, @game_name)
151 elsif (@sente == false)
152 return sprintf("%s %s %s -", @name, @status, @game_name)
153 elsif (@sente == nil)
154 return sprintf("%s %s %s +-", @name, @status, @game_name)
157 return sprintf("%s %s", @name, @status)
162 @socket.write_safe('##[HELP] available commands "%%WHO", "%%CHAT str", "%%GAME game_name +", "%%GAME game_name -"')
166 @socket.write_safe(str.gsub(/[\r\n]+/, @eol))
173 (login, @name, @password, ext) = str.split
179 @watchdog_thread = Thread::start do
180 watchdog(Watchdog_Time)
185 if (@protocol != "CSA")
186 log_message(sprintf("user %s run in %s mode", @name, @protocol))
187 write_safe(sprintf("##[LOGIN] +OK %s\n", @protocol))
189 log_message(sprintf("user %s run in CSA mode", @name))
190 csa_1st_str = "%%GAME default +-"
194 while (csa_1st_str || (str = @socket.gets_safe))
204 if (@status == "game")
205 s = @game.handle_one_move(str, self)
206 return if (s && @protocol == "CSA")
211 if (@status == "agree_waiting")
212 @status = "start_waiting"
213 if ((@game.sente.status == "start_waiting") &&
214 (@game.gote.status == "start_waiting"))
216 @game.sente.status = "game"
217 @game.gote.status = "game"
220 write_safe("## you are in %s status. AGREE is valid in agree_waiting status\n", @status)
225 when /^%%GAME\s+(\S+)\s+([\+\-]+)/
226 if ((@status == "connected") || (@status == "game_waiting"))
227 @status = "game_waiting"
229 write_safe(sprintf("## you are in %s status. GAME is valid in connected or game_waiting status\n", @status))
232 @status = "game_waiting"
235 if (sente_str == "+")
238 elsif (sente_str == "-")
245 rival = LEAGUE.get_player("game_waiting", @game_name, rival_sente, self)
246 rival = LEAGUE.get_player("game_waiting", @game_name, nil, self) if (! rival)
256 elsif (rival_sente == nil)
263 rival.sente = rival_sente
264 LEAGUE.new_game(@game_name, self, rival)
265 self.status = "agree_waiting"
266 rival.status = "agree_waiting"
268 when /^%%CHAT\s+(\S+)/
270 LEAGUE.hash.each do |name, player|
271 if (player.protocol != "CSA")
272 s = player.write_safe(sprintf("##[CHAT][%s] %s\n", @name, message))
273 player.status = "zombie" if (! s)
278 LEAGUE.hash.each do |name, player|
279 buf.push(sprintf("##[WHO] %s\n", player.to_s))
281 buf.push("##[WHO] +OK\n")
287 write_safe(sprintf("## unknown command %s\n", str))
300 def initialize(game_name, player0, player1)
301 @game_name = game_name
309 @current_player = @sente
314 @sente.status = "agree_waiting"
315 @gote.status = "agree_waiting"
316 @id = sprintf("%s-%s-%s-%s", @game_name, @sente.name, @gote.name, Time::new.strftime("%Y%m%d%H%M%S"))
317 log_message(sprintf("game created %s %s %s", game_name, sente.name, gote.name))
319 @logfile = @id + ".csa"
326 attr_accessor :game_name, :sente, :gote, :id, :board, :current_player, :next_player, :fh
329 log_message(sprintf("game finished %s %s %s", game_name, sente.name, gote.name))
330 @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
332 @sente.status = "connected"
333 @gote.status = "connected"
334 if (@current_player.protocol == "CSA")
335 @current_player.finish
339 def handle_one_move(str, player)
341 if (@current_player == player)
342 @end_time = Time::new
343 t = @end_time - @start_time
344 t = Least_Time_Per_Move if (t < Least_Time_Per_Move)
345 @sente.write_safe(sprintf("%s,T%d\n", str, t))
346 @gote.write_safe(sprintf("%s,T%d\n", str, t))
347 @fh.printf("%s\nT%d\n", str, t)
348 @current_player.mytime = @current_player.mytime - t
349 if (@current_player.mytime < 0)
352 elsif (str =~ /%KACHI/)
355 elsif (str =~ /%TORYO/)
359 (@current_player, @next_player) = [@next_player, @current_player]
360 @start_time = Time::new
361 finish if (finish_flag)
367 @current_player.status = "connected"
368 @next_player.status = "connected"
369 @current_player.write_safe("#TIME_UP\n#LOSE\n")
370 @next_player.write_safe("#TIME_UP\n#WIN\n")
374 @current_player.status = "connected"
375 @next_player.status = "connected"
376 @current_player.write_safe("#JISHOGI\n#WIN\n")
377 @next_player.write_safe("#JISHOGI\n#LOSE\n")
381 @current_player.status = "connected"
382 @next_player.status = "connected"
383 @current_player.write_safe("#RESIGN\n#LOSE\n")
384 @next_player.write_safe("#RESIGN\n#WIN\n")
388 log_message(sprintf("game started %s %s %s", game_name, sente.name, gote.name))
389 @sente.write_safe(sprintf("START:%s\n", @id))
390 @gote.write_safe(sprintf("START:%s\n", @id))
391 @start_time = Time::new
396 @fh = open(@logfile, "w")
400 @fh.printf("N+%s\n", @sente.name)
401 @fh.printf("N-%s\n", @gote.name)
402 @fh.printf("$EVENT:%s\n", @id)
404 @sente.write_safe(propose_message("+"))
405 @gote.write_safe(propose_message("-"))
407 @fh.printf("$START_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
409 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
410 P2 * -HI * * * * * -KA *
411 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
415 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
416 P8 * +KA * * * * * +HI *
417 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
423 def propose_message(sg_flag)
435 Total_Time:#{Total_Time}
436 Least_Time_Per_Move:#{Least_Time_Per_Move}
439 Jishogi_Declaration:1.1
440 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
441 P2 * -HI * * * * * -KA *
442 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
446 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
447 P8 * +KA * * * * * +HI *
448 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
462 shogi-server - server for CSA server protocol
465 shogi-server event_name port_number
468 server for CSA server protocol
472 specify filename for logging process ID
475 this file is distributed under GPL version2 and might be compiled by Exerb
488 printf("%s message: %s\n", Time::new.to_s, str)
492 printf("%s message: %s\n", Time::new.to_s, str)
496 printf("%s error: %s\n", Time::new.to_s, str)
500 def parse_command_line
502 parser = GetoptLong.new
503 parser.ordering = GetoptLong::REQUIRE_ORDER
505 ["--pid-file", GetoptLong::REQUIRED_ARGUMENT])
509 parser.each_option do |name, arg|
511 options[name] = arg.dup
515 raise parser.error_message
523 return false if (str !~ /^LOGIN /)
525 if ((tokens.length == 3) ||
526 ((tokens.length == 4) && tokens[3] == "x1"))
534 def write_pid_file(file)
535 open(file, "w") do |fh|
536 fh.print Process::pid, "\n"
542 $options = parse_command_line
543 if (ARGV.length != 2)
550 write_pid_file($options["pid-file"]) if ($options["pid-file"])
553 Thread.abort_on_exception = true
555 server = TCPserver.open(port)
556 log_message("server started")
559 Thread::start(server.accept) do |client|
562 while (str = client.gets_timeout(Login_Time))
565 Thread::kill(Thread::current) if (! str) # disconnected
568 if (good_login?(str))
569 player = Player::new(str, client)
570 if (LEAGUE.duplicated?(player))
571 client.write_safe(sprintf("username %s is already connected%s", player.name, eol))
573 Thread::kill(Thread::current)
578 client.write_safe("type 'LOGIN name password' or 'LOGIN name password x1'" + eol)
580 Thread::kill(Thread::current)
586 log_message(sprintf("user %s login", player.name))
588 LEAGUE.delete(player)
589 log_message(sprintf("user %s logout", player.name))