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 Max_Identifier_Length = 32
21 Default_Timeout = 60 # for single socket operation
23 Default_Game_Name = "default:1500:0"
26 Least_Time_Per_Move = 1
27 Watchdog_Time = 30 # time for ping
28 Login_Time = 300 # time for LOGIN
30 Release = "$Name$".split[1].sub(/\A[^\d]*/, '').gsub(/_/, '.')
31 Release.concat("-") if (Release == "")
32 Revision = "$Revision$".gsub(/[^\.\d]/, '')
43 TCPSocket.do_not_reverse_lookup = true
46 def gets_timeout(t = Default_Timeout)
57 def gets_safe(t = nil)
78 return self.write(str)
92 attr_accessor :players, :games, :event
95 @players[player.name] = player
98 @players.delete(player.name)
100 def get_player(status, game_name, sente, searcher=nil)
101 @players.each do |name, player|
102 if ((player.status == status) &&
103 (player.game_name == game_name) &&
104 ((player.sente == nil) || (player.sente == sente)) &&
105 ((searcher == nil) || (player != searcher)))
114 def initialize(str, socket)
118 @status = "connected" # game_waiting -> agree_waiting -> start_waiting -> game -> finished
120 @protocol = nil # CSA or x1
121 @eol = "\m" # favorite eol code
124 @mytime = 0 # set in start method also
126 @watchdog_thread = nil
131 attr_accessor :name, :password, :socket, :status
132 attr_accessor :protocol, :eol, :game, :mytime, :main_thread, :watchdog_thread, :game_name, :sente
135 Thread::kill(@main_thread) if @main_thread
139 if (@status != "finished")
141 log_message(sprintf("user %s finish", @name))
142 Thread::kill(@watchdog_thread) if @watchdog_thread
143 @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 %s +", @name, @protocol, @status, @game_name)
163 elsif (@sente == false)
164 return sprintf("%s %s %s %s -", @name, @protocol, @status, @game_name)
165 elsif (@sente == nil)
166 return sprintf("%s %s %s %s +-", @name, @protocol, @status, @game_name)
169 return sprintf("%s %s %s", @name, @protocol, @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 @main_thread = Thread::current
192 @watchdog_thread = Thread::start do
193 watchdog(Watchdog_Time)
198 write_safe(sprintf("LOGIN:%s OK\n", @name))
199 if (@protocol != "CSA")
200 log_message(sprintf("user %s run in %s mode", @name, @protocol))
201 write_safe(sprintf("##[LOGIN] +OK %s\n", @protocol))
203 log_message(sprintf("user %s run in CSA mode", @name))
204 csa_1st_str = "%%GAME #{Default_Game_Name} +-"
207 while (csa_1st_str || (str = @socket.gets_safe(Default_Timeout)))
214 if (@status == "finished")
217 str.chomp! if (str.class == String)
219 when /^[\+\-%][^%]/, :timeout
220 if (@status == "game")
221 s = @game.handle_one_move(str, self)
222 return if (s && @protocol == "CSA")
225 if (@status == "agree_waiting")
227 return if (@protocol == "CSA")
229 write_safe(sprintf("##[ERROR] you are in %s status. AGREE is valid in agree_waiting status\n", @status))
232 if (@status == "agree_waiting")
233 @status = "start_waiting"
234 if ((@game.sente.status == "start_waiting") &&
235 (@game.gote.status == "start_waiting"))
237 @game.sente.status = "game"
238 @game.gote.status = "game"
241 write_safe(sprintf("##[ERROR] you are in %s status. AGREE is valid in agree_waiting status\n", @status))
245 when /^%%GAME\s+(\S+)\s+([\+\-]+)$/
248 if (! good_game_name?(game_name))
249 write_safe(sprintf("##[ERROR] bad game name\n"))
250 elsif ((@status == "connected") || (@status == "game_waiting"))
251 @status = "game_waiting"
253 write_safe(sprintf("##[ERROR] you are in %s status. GAME is valid in connected or game_waiting status\n", @status))
255 @status = "game_waiting"
257 if (sente_str == "+")
260 elsif (sente_str == "-")
267 rival = LEAGUE.get_player("game_waiting", @game_name, rival_sente, self)
268 rival = LEAGUE.get_player("game_waiting", @game_name, nil, self) if (! rival)
278 elsif (rival_sente == nil)
285 rival.sente = rival_sente
286 Game::new(@game_name, self, rival)
287 self.status = "agree_waiting"
288 rival.status = "agree_waiting"
290 when /^%%CHAT\s+(.+)/
292 LEAGUE.players.each do |name, player|
293 if (player.protocol != "CSA")
294 s = player.write_safe(sprintf("##[CHAT][%s] %s\n", @name, message))
295 player.status = "zombie" if (! s)
300 LEAGUE.games.each do |id, game|
301 buf.push(sprintf("##[LIST] %s\n", id))
303 buf.push("##[LIST] +OK\n")
305 when /^%%SHOW\s+(\S+)/
307 if (LEAGUE.games[id])
308 write_safe(LEAGUE.games[id].board.to_s.gsub(/^/, '##[SHOW] '))
310 write_safe("##[SHOW] +OK\n")
313 LEAGUE.players.each do |name, player|
314 buf.push(sprintf("##[WHO] %s\n", player.to_s))
316 buf.push("##[WHO] +OK\n")
319 @status = "connected"
320 write_safe("LOGOUT:completed\n")
323 write_safe(sprintf("##[ERROR] unknown command %s\n", str))
333 PROMOTE = {"FU" => "TO", "KY" => "NY", "KE" => "NK", "GI" => "NG", "KA" => "UM", "HI" => "RY"}
334 def initialize(name, sente)
339 attr_accessor :name, :promoted, :sente
364 @sente_hands = Array::new
365 @gote_hands = Array::new
366 @array = [[], [], [], [], [], [], [], [], [], []]
368 attr_accessor :array, :sente_hands, :gote_hands
371 @array[1][1] = Piece::new("KY", false)
372 @array[2][1] = Piece::new("KE", false)
373 @array[3][1] = Piece::new("GI", false)
374 @array[4][1] = Piece::new("KI", false)
375 @array[5][1] = Piece::new("OU", false)
376 @array[6][1] = Piece::new("KI", false)
377 @array[7][1] = Piece::new("GI", false)
378 @array[8][1] = Piece::new("KE", false)
379 @array[9][1] = Piece::new("KY", false)
380 @array[2][2] = Piece::new("KA", false)
381 @array[8][2] = Piece::new("HI", false)
382 @array[1][3] = Piece::new("FU", false)
383 @array[2][3] = Piece::new("FU", false)
384 @array[3][3] = Piece::new("FU", false)
385 @array[4][3] = Piece::new("FU", false)
386 @array[5][3] = Piece::new("FU", false)
387 @array[6][3] = Piece::new("FU", false)
388 @array[7][3] = Piece::new("FU", false)
389 @array[8][3] = Piece::new("FU", false)
390 @array[9][3] = Piece::new("FU", false)
392 @array[1][9] = Piece::new("KY", true)
393 @array[2][9] = Piece::new("KE", true)
394 @array[3][9] = Piece::new("GI", true)
395 @array[4][9] = Piece::new("KI", true)
396 @array[5][9] = Piece::new("OU", true)
397 @array[6][9] = Piece::new("KI", true)
398 @array[7][9] = Piece::new("GI", true)
399 @array[8][9] = Piece::new("KE", true)
400 @array[9][9] = Piece::new("KY", true)
401 @array[2][8] = Piece::new("HI", true)
402 @array[8][8] = Piece::new("KA", true)
403 @array[1][7] = Piece::new("FU", true)
404 @array[2][7] = Piece::new("FU", true)
405 @array[3][7] = Piece::new("FU", true)
406 @array[4][7] = Piece::new("FU", true)
407 @array[5][7] = Piece::new("FU", true)
408 @array[6][7] = Piece::new("FU", true)
409 @array[7][7] = Piece::new("FU", true)
410 @array[8][7] = Piece::new("FU", true)
411 @array[9][7] = Piece::new("FU", true)
414 def get_piece_from_hands(hands, name)
424 def handle_one_move(str)
425 if (str =~ /^([\+\-])(\d)(\d)(\d)(\d)([A-Z]{2})/)
432 elsif (str =~ /^%KACHI/)
434 elsif (str =~ /^%TORYO/)
446 if (@array[x1][y1] == sente) # this is mine
448 elsif (@array[x1][y1].name == "OU")
451 hands.push(@array[x1][y1])
454 if ((x0 == 0) && (y0 == 0))
455 p = get_piece_from_hands(hands, name)
456 return "illegal" if (! p) # i don't have this one
461 @array[x1][y1] = @array[x0][y0]
463 if (@array[x1][y1].name != name) # promoted ?
464 return "illegal" if (@array[x1][y1].promoted_name != name) # can't promote
465 @array[x1][y1].promoted = true
468 return "normal" # legal move
475 a.push(sprintf("P%d", y))
487 a.push(sprintf("\n"))
490 if (! sente_hands.empty?)
492 sente_hands.each do |p|
493 a.push("00" + p.name)
497 if (! gote_hands.empty?)
499 gote_hands.each do |p|
500 a.push("00" + p.name)
509 def initialize(game_name, player0, player1)
510 @game_name = game_name
511 if (@game_name =~ /:(\d+):(\d+)/)
512 @total_time = $1.to_i
523 @current_player = @sente
529 @sente.status = "agree_waiting"
530 @gote.status = "agree_waiting"
531 @id = sprintf("%s+%s+%s+%s+%s",
532 LEAGUE.event, @game_name, @sente.name, @gote.name,
533 Time::new.strftime("%Y%m%d%H%M%S"))
535 LEAGUE.games[@id] = self
538 log_message(sprintf("game created %s %s %s", game_name, sente.name, gote.name))
540 @logfile = @id + ".csa"
548 attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :id, :board, :current_player, :next_player, :fh
551 @sente.write_safe(sprintf("REJECT:%s by %s\n", @id, rejector))
552 @gote.write_safe(sprintf("REJECT:%s by %s\n", @id, rejector))
557 if ((@sente.status == "agree_waiting") || (@sente.status == "start_waiting"))
559 elsif (@current_player == killer)
565 log_message(sprintf("game finished %s %s %s", game_name, sente.name, gote.name))
566 @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
571 @sente.status = "connected"
572 @gote.status = "connected"
574 if (@current_player.protocol == "CSA")
575 @current_player.finish
577 if (@next_player.protocol == "CSA")
580 LEAGUE.games.delete(@id)
583 def handle_one_move(str, player)
585 if (@current_player == player)
586 @end_time = Time::new
587 t = @end_time - @start_time
588 t = Least_Time_Per_Move if (t < Least_Time_Per_Move)
591 if (@current_player.mytime - t <= 0)
593 elsif (str == :timeout)
594 return false # time isn't expired. players aren't swapped. continue game
596 move_status = @board.handle_one_move(str)
597 if (move_status == "normal")
598 @sente.write_safe(sprintf("%s,T%d\n", str, t))
599 @gote.write_safe(sprintf("%s,T%d\n", str, t))
600 @fh.printf("%s\nT%d\n", str, t)
602 @fh.printf("'ILLEGAL_MOVE(%s)\n", str)
606 if (@current_player.mytime - t < @byoyomi)
607 @current_player.mytime = @byoyomi
609 @current_player.mytime = @current_player.mytime - t
612 if (@next_player.status != "game") # rival is logout or disconnected
614 elsif (status == :timeout)
616 elsif (move_status == "illegal")
618 elsif (move_status == "kachi")
620 elsif (move_status == "toryo")
622 elsif (move_status == "ootori")
625 (@current_player, @next_player) = [@next_player, @current_player]
626 @start_time = Time::new
632 @current_player.status = "connected"
633 @next_player.status = "connected"
634 @current_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
635 @next_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
639 @current_player.status = "connected"
640 @next_player.status = "connected"
641 @current_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
642 @next_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
646 @current_player.status = "connected"
647 @next_player.status = "connected"
648 @current_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
649 @next_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
653 @current_player.status = "connected"
654 @next_player.status = "connected"
655 @current_player.write_safe("#TIME_UP\n#LOSE\n")
656 @next_player.write_safe("#TIME_UP\n#WIN\n")
660 @current_player.status = "connected"
661 @next_player.status = "connected"
662 @current_player.write_safe("%KACHI\n#JISHOGI\n#WIN\n")
663 @next_player.write_safe("%KACHI\n#JISHOGI\n#LOSE\n")
667 @current_player.status = "connected"
668 @next_player.status = "connected"
669 @current_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
670 @next_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
674 @current_player.status = "connected"
675 @next_player.status = "connected"
676 @current_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
677 @next_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
681 log_message(sprintf("game started %s %s %s", game_name, sente.name, gote.name))
682 @sente.write_safe(sprintf("START:%s\n", @id))
683 @gote.write_safe(sprintf("START:%s\n", @id))
684 @sente.mytime = @total_time
685 @gote.mytime = @total_time
686 @start_time = Time::new
691 @fh = open(@logfile, "w")
695 @fh.printf("N+%s\n", @sente.name)
696 @fh.printf("N-%s\n", @gote.name)
697 @fh.printf("$EVENT:%s\n", @id)
699 @sente.write_safe(propose_message("+"))
700 @gote.write_safe(propose_message("-"))
702 @fh.printf("$START_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
704 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
705 P2 * -HI * * * * * -KA *
706 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
710 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
711 P8 * +KA * * * * * +HI *
712 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
718 def propose_message(sg_flag)
732 Total_Time:#{@total_time}
734 Least_Time_Per_Move:#{Least_Time_Per_Move}
737 Jishogi_Declaration:1.1
738 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
739 P2 * -HI * * * * * -KA *
740 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
744 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
745 P8 * +KA * * * * * +HI *
746 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
760 shogi-server - server for CSA server protocol
763 shogi-server event_name port_number
766 server for CSA server protocol
770 specify filename for logging process ID
773 this file is distributed under GPL version2 and might be compiled by Exerb
786 printf("%s message: %s\n", Time::new.to_s, str)
790 printf("%s message: %s\n", Time::new.to_s, str)
794 printf("%s error: %s\n", Time::new.to_s, str)
798 def parse_command_line
800 parser = GetoptLong.new
801 parser.ordering = GetoptLong::REQUIRE_ORDER
803 ["--pid-file", GetoptLong::REQUIRED_ARGUMENT])
807 parser.each_option do |name, arg|
809 options[name] = arg.dup
813 raise parser.error_message
820 def good_game_name?(str)
821 if ((str =~ /^(.+):\d+:\d+$/) &&
822 (good_identifier?($1)))
829 def good_identifier?(str)
830 if ((str =~ /\A[\w\d_@\-\.]+\z/) &&
831 (str.length < Max_Identifier_Length))
840 if (((tokens.length == 3) || ((tokens.length == 4) && tokens[3] == "x1")) &&
841 (tokens[0] == "LOGIN") &&
842 (good_identifier?(tokens[1])))
849 def write_pid_file(file)
850 open(file, "w") do |fh|
851 fh.print Process::pid, "\n"
857 $options = parse_command_line
858 if (ARGV.length != 2)
863 LEAGUE.event = ARGV.shift
866 write_pid_file($options["pid-file"]) if ($options["pid-file"])
869 Thread.abort_on_exception = true
871 server = TCPserver.open(port)
872 log_message("server started")
875 Thread::start(server.accept) do |client|
878 while (str = client.gets_timeout(Login_Time))
881 Thread::kill(Thread::current) if (! str) # disconnected
884 if (good_login?(str))
885 player = Player::new(str, client)
886 if (LEAGUE.players[player.name])
887 if ((LEAGUE.players[player.name].password == player.password) &&
888 (LEAGUE.players[player.name].status != "game"))
889 log_message(sprintf("user %s login forcely", player.name))
890 LEAGUE.players[player.name].kill
892 client.write_safe("LOGIN:incorrect" + eol)
893 client.write_safe(sprintf("username %s is already connected%s", player.name, eol)) if (str.split.length >= 4)
895 Thread::kill(Thread::current)
901 client.write_safe("LOGIN:incorrect" + eol)
902 client.write_safe("type 'LOGIN name password' or 'LOGIN name password x1'" + eol) if (str.split.length >= 4)
910 Thread::kill(Thread::current)
912 log_message(sprintf("user %s login", player.name))
917 player.game.kill(player)
920 LEAGUE.delete(player)
921 log_message(sprintf("user %s logout", player.name))