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 write_safe(sprintf("LOGIN:%s OK\n", @name))
186 if (@protocol != "CSA")
187 log_message(sprintf("user %s run in %s mode", @name, @protocol))
188 write_safe(sprintf("##[LOGIN] +OK %s\n", @protocol))
190 log_message(sprintf("user %s run in CSA mode", @name))
191 csa_1st_str = "%%GAME default +-"
195 while (csa_1st_str || (str = @socket.gets_safe))
205 if (@status == "game")
206 s = @game.handle_one_move(str, self)
207 return if (s && @protocol == "CSA")
212 if (@status == "agree_waiting")
213 @status = "start_waiting"
214 if ((@game.sente.status == "start_waiting") &&
215 (@game.gote.status == "start_waiting"))
217 @game.sente.status = "game"
218 @game.gote.status = "game"
221 write_safe("## you are in %s status. AGREE is valid in agree_waiting status\n", @status)
226 when /^%%GAME\s+(\S+)\s+([\+\-]+)/
227 if ((@status == "connected") || (@status == "game_waiting"))
228 @status = "game_waiting"
230 write_safe(sprintf("## you are in %s status. GAME is valid in connected or game_waiting status\n", @status))
233 @status = "game_waiting"
236 if (sente_str == "+")
239 elsif (sente_str == "-")
246 rival = LEAGUE.get_player("game_waiting", @game_name, rival_sente, self)
247 rival = LEAGUE.get_player("game_waiting", @game_name, nil, self) if (! rival)
257 elsif (rival_sente == nil)
264 rival.sente = rival_sente
265 LEAGUE.new_game(@game_name, self, rival)
266 self.status = "agree_waiting"
267 rival.status = "agree_waiting"
269 when /^%%CHAT\s+(\S+)/
271 LEAGUE.hash.each do |name, player|
272 if (player.protocol != "CSA")
273 s = player.write_safe(sprintf("##[CHAT][%s] %s\n", @name, message))
274 player.status = "zombie" if (! s)
279 LEAGUE.hash.each do |name, player|
280 buf.push(sprintf("##[WHO] %s\n", player.to_s))
282 buf.push("##[WHO] +OK\n")
285 write_safe("LOGOUT:completed\n")
289 write_safe(sprintf("## unknown command %s\n", str))
302 def initialize(game_name, player0, player1)
303 @game_name = game_name
311 @current_player = @sente
316 @sente.status = "agree_waiting"
317 @gote.status = "agree_waiting"
318 @id = sprintf("%s-%s-%s-%s", @game_name, @sente.name, @gote.name, Time::new.strftime("%Y%m%d%H%M%S"))
319 log_message(sprintf("game created %s %s %s", game_name, sente.name, gote.name))
321 @logfile = @id + ".csa"
328 attr_accessor :game_name, :sente, :gote, :id, :board, :current_player, :next_player, :fh
331 log_message(sprintf("game finished %s %s %s", game_name, sente.name, gote.name))
332 @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
334 @sente.status = "connected"
335 @gote.status = "connected"
336 if (@current_player.protocol == "CSA")
337 @current_player.finish
341 def handle_one_move(str, player)
343 if (@current_player == player)
344 @end_time = Time::new
345 t = @end_time - @start_time
346 t = Least_Time_Per_Move if (t < Least_Time_Per_Move)
347 @sente.write_safe(sprintf("%s,T%d\n", str, t))
348 @gote.write_safe(sprintf("%s,T%d\n", str, t))
349 @fh.printf("%s\nT%d\n", str, t)
350 @current_player.mytime = @current_player.mytime - t
351 if (@current_player.mytime < 0)
354 elsif (str =~ /%KACHI/)
357 elsif (str =~ /%TORYO/)
361 (@current_player, @next_player) = [@next_player, @current_player]
362 @start_time = Time::new
363 finish if (finish_flag)
369 @current_player.status = "connected"
370 @next_player.status = "connected"
371 @current_player.write_safe("#TIME_UP\n#LOSE\n")
372 @next_player.write_safe("#TIME_UP\n#WIN\n")
376 @current_player.status = "connected"
377 @next_player.status = "connected"
378 @current_player.write_safe("#JISHOGI\n#WIN\n")
379 @next_player.write_safe("#JISHOGI\n#LOSE\n")
383 @current_player.status = "connected"
384 @next_player.status = "connected"
385 @current_player.write_safe("#RESIGN\n#LOSE\n")
386 @next_player.write_safe("#RESIGN\n#WIN\n")
390 log_message(sprintf("game started %s %s %s", game_name, sente.name, gote.name))
391 @sente.write_safe(sprintf("START:%s\n", @id))
392 @gote.write_safe(sprintf("START:%s\n", @id))
393 @start_time = Time::new
398 @fh = open(@logfile, "w")
402 @fh.printf("N+%s\n", @sente.name)
403 @fh.printf("N-%s\n", @gote.name)
404 @fh.printf("$EVENT:%s\n", @id)
406 @sente.write_safe(propose_message("+"))
407 @gote.write_safe(propose_message("-"))
409 @fh.printf("$START_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
411 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
412 P2 * -HI * * * * * -KA *
413 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
417 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
418 P8 * +KA * * * * * +HI *
419 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
425 def propose_message(sg_flag)
437 Total_Time:#{Total_Time}
438 Least_Time_Per_Move:#{Least_Time_Per_Move}
441 Jishogi_Declaration:1.1
442 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
443 P2 * -HI * * * * * -KA *
444 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
448 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
449 P8 * +KA * * * * * +HI *
450 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
464 shogi-server - server for CSA server protocol
467 shogi-server event_name port_number
470 server for CSA server protocol
474 specify filename for logging process ID
477 this file is distributed under GPL version2 and might be compiled by Exerb
490 printf("%s message: %s\n", Time::new.to_s, str)
494 printf("%s message: %s\n", Time::new.to_s, str)
498 printf("%s error: %s\n", Time::new.to_s, str)
502 def parse_command_line
504 parser = GetoptLong.new
505 parser.ordering = GetoptLong::REQUIRE_ORDER
507 ["--pid-file", GetoptLong::REQUIRED_ARGUMENT])
511 parser.each_option do |name, arg|
513 options[name] = arg.dup
517 raise parser.error_message
525 return false if (str !~ /^LOGIN /)
527 if ((tokens.length == 3) ||
528 ((tokens.length == 4) && tokens[3] == "x1"))
536 def write_pid_file(file)
537 open(file, "w") do |fh|
538 fh.print Process::pid, "\n"
544 $options = parse_command_line
545 if (ARGV.length != 2)
552 write_pid_file($options["pid-file"]) if ($options["pid-file"])
555 Thread.abort_on_exception = true
557 server = TCPserver.open(port)
558 log_message("server started")
561 Thread::start(server.accept) do |client|
564 while (str = client.gets_timeout(Login_Time))
567 Thread::kill(Thread::current) if (! str) # disconnected
570 if (good_login?(str))
571 player = Player::new(str, client)
572 if (LEAGUE.duplicated?(player))
573 client.write_safe("LOGIN:incorrect" + eol)
574 client.write_safe(sprintf("username %s is already connected%s", player.name, eol))
576 Thread::kill(Thread::current)
581 client.write_safe("LOGIN:incorrect" + eol)
582 client.write_safe("type 'LOGIN name password' or 'LOGIN name password x1'" + eol)
584 Thread::kill(Thread::current)
590 log_message(sprintf("user %s login", player.name))
592 LEAGUE.delete(player)
593 log_message(sprintf("user %s logout", player.name))