2 ## -*-Ruby-*- $RCSfile$ $Revision$ $Name$
4 ## Copyright (C) 2004 nanami@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)
53 def gets_safe(t = nil)
74 return self.write(str)
89 @hash[player.name] = player
92 @hash.delete(player.name)
94 def duplicated?(player)
95 if (@hash[player.name])
101 def get_player(status, game_name, sente, searcher=nil)
102 @hash.each do |name, player|
103 if ((player.status == status) &&
104 (player.game_name == game_name) &&
105 ((player.sente == nil) || (player.sente == sente)) &&
106 ((searcher == nil) || (player != searcher)))
112 def new_game(game_name, player0, player1)
113 game = Game::new(game_name, player0, player1)
121 def initialize(str, socket)
125 @status = "connected" # game_waiting -> agree_waiting -> start_waiting -> game
127 @protocol = nil # CSA or x1
128 @eol = "\m" # favorite eol code
131 @mytime = Total_Time # set in start method also
133 @watchdog_thread = nil
138 attr_accessor :name, :password, :socket, :status
139 attr_accessor :protocol, :eol, :game, :mytime, :watchdog_thread, :game_name, :sente
142 log_message(sprintf("user %s finish", @name))
143 Thread::kill(@watchdog_thread) if @watchdog_thread
144 @socket.close if (! @socket.closed?)
150 Ping.pingecho(@socket.addr[3])
158 if ((status == "game_waiting") ||
159 (status == "agree_waiting") ||
162 return sprintf("%s %s %s +", @name, @status, @game_name)
163 elsif (@sente == false)
164 return sprintf("%s %s %s -", @name, @status, @game_name)
165 elsif (@sente == nil)
166 return sprintf("%s %s %s +-", @name, @status, @game_name)
169 return sprintf("%s %s", @name, @status)
174 @socket.write_safe('##[HELP] available commands "%%WHO", "%%CHAT str", "%%GAME game_name +", "%%GAME game_name -"')
178 @socket.write_safe(str.gsub(/[\r\n]+/, @eol))
185 (login, @name, @password, ext) = str.split
191 @watchdog_thread = Thread::start do
192 watchdog(Watchdog_Time)
197 write_safe(sprintf("LOGIN:%s OK\n", @name))
198 if (@protocol != "CSA")
199 log_message(sprintf("user %s run in %s mode", @name, @protocol))
200 write_safe(sprintf("##[LOGIN] +OK %s\n", @protocol))
202 log_message(sprintf("user %s run in CSA mode", @name))
203 csa_1st_str = "%%GAME default +-"
207 while (csa_1st_str || (str = @socket.gets_safe(@mytime)))
214 str.chomp! if (str.class == String)
216 when /^[\+\-%][^%]/, :timeout
217 if (@status == "game")
218 s = @game.handle_one_move(str, self)
219 return if (s && @protocol == "CSA")
224 if (@status == "agree_waiting")
225 @status = "start_waiting"
226 if ((@game.sente.status == "start_waiting") &&
227 (@game.gote.status == "start_waiting"))
229 @game.sente.status = "game"
230 @game.gote.status = "game"
233 write_safe("##[ERROR] you are in %s status. AGREE is valid in agree_waiting status\n", @status)
238 when /^%%GAME\s+(\S+)\s+([\+\-]+)/
239 if ((@status == "connected") || (@status == "game_waiting"))
240 @status = "game_waiting"
242 write_safe(sprintf("##[ERROR] you are in %s status. GAME is valid in connected or game_waiting status\n", @status))
245 @status = "game_waiting"
248 if (sente_str == "+")
251 elsif (sente_str == "-")
258 rival = LEAGUE.get_player("game_waiting", @game_name, rival_sente, self)
259 rival = LEAGUE.get_player("game_waiting", @game_name, nil, self) if (! rival)
269 elsif (rival_sente == nil)
276 rival.sente = rival_sente
277 LEAGUE.new_game(@game_name, self, rival)
278 self.status = "agree_waiting"
279 rival.status = "agree_waiting"
281 when /^%%CHAT\s+(\S+)/
283 LEAGUE.hash.each do |name, player|
284 if (player.protocol != "CSA")
285 s = player.write_safe(sprintf("##[CHAT][%s] %s\n", @name, message))
286 player.status = "zombie" if (! s)
291 LEAGUE.hash.each do |name, player|
292 buf.push(sprintf("##[WHO] %s\n", player.to_s))
294 buf.push("##[WHO] +OK\n")
297 write_safe("LOGOUT:completed\n")
301 write_safe(sprintf("##[ERROR] unknown command %s\n", str))
314 def initialize(game_name, player0, player1)
315 @game_name = game_name
323 @current_player = @sente
328 @sente.status = "agree_waiting"
329 @gote.status = "agree_waiting"
330 @id = sprintf("%s-%s-%s-%s", @game_name, @sente.name, @gote.name, Time::new.strftime("%Y%m%d%H%M%S"))
331 log_message(sprintf("game created %s %s %s", game_name, sente.name, gote.name))
333 @logfile = @id + ".csa"
340 attr_accessor :game_name, :sente, :gote, :id, :board, :current_player, :next_player, :fh
343 log_message(sprintf("game finished %s %s %s", game_name, sente.name, gote.name))
344 @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
346 @sente.status = "connected"
347 @gote.status = "connected"
348 if (@current_player.protocol == "CSA")
349 @current_player.finish
353 def handle_one_move(str, player)
355 if (@current_player == player)
356 @end_time = Time::new
357 t = @end_time - @start_time
358 t = Least_Time_Per_Move if (t < Least_Time_Per_Move)
360 @sente.write_safe(sprintf("%s,T%d\n", str, t))
361 @gote.write_safe(sprintf("%s,T%d\n", str, t))
362 @fh.printf("%s\nT%d\n", str, t)
363 @current_player.mytime = @current_player.mytime - t
365 @current_player.mytime = 0
367 if (@current_player.mytime <= 0)
370 elsif (str =~ /%KACHI/)
373 elsif (str =~ /%TORYO/)
377 (@current_player, @next_player) = [@next_player, @current_player]
378 @start_time = Time::new
379 finish if (finish_flag)
385 @current_player.status = "connected"
386 @next_player.status = "connected"
387 @current_player.write_safe("#TIME_UP\n#LOSE\n")
388 @next_player.write_safe("#TIME_UP\n#WIN\n")
392 @current_player.status = "connected"
393 @next_player.status = "connected"
394 @current_player.write_safe("#JISHOGI\n#WIN\n")
395 @next_player.write_safe("#JISHOGI\n#LOSE\n")
399 @current_player.status = "connected"
400 @next_player.status = "connected"
401 @current_player.write_safe("#RESIGN\n#LOSE\n")
402 @next_player.write_safe("#RESIGN\n#WIN\n")
406 log_message(sprintf("game started %s %s %s", game_name, sente.name, gote.name))
407 @sente.write_safe(sprintf("START:%s\n", @id))
408 @gote.write_safe(sprintf("START:%s\n", @id))
410 @start_time = Time::new
415 @fh = open(@logfile, "w")
419 @fh.printf("N+%s\n", @sente.name)
420 @fh.printf("N-%s\n", @gote.name)
421 @fh.printf("$EVENT:%s\n", @id)
423 @sente.write_safe(propose_message("+"))
424 @gote.write_safe(propose_message("-"))
426 @fh.printf("$START_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
428 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
429 P2 * -HI * * * * * -KA *
430 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
434 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
435 P8 * +KA * * * * * +HI *
436 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
442 def propose_message(sg_flag)
454 Total_Time:#{Total_Time}
455 Least_Time_Per_Move:#{Least_Time_Per_Move}
458 Jishogi_Declaration:1.1
459 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
460 P2 * -HI * * * * * -KA *
461 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
465 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
466 P8 * +KA * * * * * +HI *
467 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
481 shogi-server - server for CSA server protocol
484 shogi-server event_name port_number
487 server for CSA server protocol
491 specify filename for logging process ID
494 this file is distributed under GPL version2 and might be compiled by Exerb
507 printf("%s message: %s\n", Time::new.to_s, str)
511 printf("%s message: %s\n", Time::new.to_s, str)
515 printf("%s error: %s\n", Time::new.to_s, str)
519 def parse_command_line
521 parser = GetoptLong.new
522 parser.ordering = GetoptLong::REQUIRE_ORDER
524 ["--pid-file", GetoptLong::REQUIRED_ARGUMENT])
528 parser.each_option do |name, arg|
530 options[name] = arg.dup
534 raise parser.error_message
542 return false if (str !~ /^LOGIN /)
544 if ((tokens.length == 3) ||
545 ((tokens.length == 4) && tokens[3] == "x1"))
553 def write_pid_file(file)
554 open(file, "w") do |fh|
555 fh.print Process::pid, "\n"
561 $options = parse_command_line
562 if (ARGV.length != 2)
569 write_pid_file($options["pid-file"]) if ($options["pid-file"])
572 Thread.abort_on_exception = true
574 server = TCPserver.open(port)
575 log_message("server started")
578 Thread::start(server.accept) do |client|
581 while (str = client.gets_timeout(Login_Time))
584 Thread::kill(Thread::current) if (! str) # disconnected
587 if (good_login?(str))
588 player = Player::new(str, client)
589 if (LEAGUE.duplicated?(player))
590 client.write_safe("LOGIN:incorrect" + eol)
591 client.write_safe(sprintf("username %s is already connected%s", player.name, eol)) if (str.split.length >= 4)
593 Thread::kill(Thread::current)
598 client.write_safe("LOGIN:incorrect" + eol)
599 client.write_safe("type 'LOGIN name password' or 'LOGIN name password x1'" + eol) if (str.split.length >= 4)
601 Thread::kill(Thread::current)
607 log_message(sprintf("user %s login", player.name))
609 LEAGUE.delete(player)
610 log_message(sprintf("user %s logout", player.name))