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))
243 when /^%%MONITORON\s+(\S+)/
245 if (LEAGUE.games[game_name])
246 LEAGUE.games[game_name].monitoron(self)
248 when /^%%MONITOROFF\s+(\S+)/
250 if (LEAGUE.games[game_name])
251 LEAGUE.games[game_name].monitoroff(self)
255 when /^%%GAME\s+(\S+)\s+([\+\-]+)$/
258 if (! good_game_name?(game_name))
259 write_safe(sprintf("##[ERROR] bad game name\n"))
260 elsif ((@status == "connected") || (@status == "game_waiting"))
261 @status = "game_waiting"
263 write_safe(sprintf("##[ERROR] you are in %s status. GAME is valid in connected or game_waiting status\n", @status))
265 @status = "game_waiting"
267 if (sente_str == "+")
270 elsif (sente_str == "-")
277 rival = LEAGUE.get_player("game_waiting", @game_name, rival_sente, self)
278 rival = LEAGUE.get_player("game_waiting", @game_name, nil, self) if (! rival)
288 elsif (rival_sente == nil)
295 rival.sente = rival_sente
296 Game::new(@game_name, self, rival)
297 self.status = "agree_waiting"
298 rival.status = "agree_waiting"
300 when /^%%CHAT\s+(.+)/
302 LEAGUE.players.each do |name, player|
303 if (player.protocol != "CSA")
304 s = player.write_safe(sprintf("##[CHAT][%s] %s\n", @name, message))
305 player.status = "zombie" if (! s)
310 LEAGUE.games.each do |id, game|
311 buf.push(sprintf("##[LIST] %s\n", id))
313 buf.push("##[LIST] +OK\n")
315 when /^%%SHOW\s+(\S+)/
317 if (LEAGUE.games[id])
318 write_safe(LEAGUE.games[id].board.to_s.gsub(/^/, '##[SHOW] '))
320 write_safe("##[SHOW] +OK\n")
323 LEAGUE.players.each do |name, player|
324 buf.push(sprintf("##[WHO] %s\n", player.to_s))
326 buf.push("##[WHO] +OK\n")
329 @status = "connected"
330 write_safe("LOGOUT:completed\n")
333 write_safe(sprintf("##[ERROR] unknown command %s\n", str))
343 PROMOTE = {"FU" => "TO", "KY" => "NY", "KE" => "NK", "GI" => "NG", "KA" => "UM", "HI" => "RY"}
344 def initialize(name, sente)
349 attr_accessor :name, :promoted, :sente
374 @sente_hands = Array::new
375 @gote_hands = Array::new
376 @array = [[], [], [], [], [], [], [], [], [], []]
378 attr_accessor :array, :sente_hands, :gote_hands
381 @array[1][1] = Piece::new("KY", false)
382 @array[2][1] = Piece::new("KE", false)
383 @array[3][1] = Piece::new("GI", false)
384 @array[4][1] = Piece::new("KI", false)
385 @array[5][1] = Piece::new("OU", false)
386 @array[6][1] = Piece::new("KI", false)
387 @array[7][1] = Piece::new("GI", false)
388 @array[8][1] = Piece::new("KE", false)
389 @array[9][1] = Piece::new("KY", false)
390 @array[2][2] = Piece::new("KA", false)
391 @array[8][2] = Piece::new("HI", false)
392 @array[1][3] = Piece::new("FU", false)
393 @array[2][3] = Piece::new("FU", false)
394 @array[3][3] = Piece::new("FU", false)
395 @array[4][3] = Piece::new("FU", false)
396 @array[5][3] = Piece::new("FU", false)
397 @array[6][3] = Piece::new("FU", false)
398 @array[7][3] = Piece::new("FU", false)
399 @array[8][3] = Piece::new("FU", false)
400 @array[9][3] = Piece::new("FU", false)
402 @array[1][9] = Piece::new("KY", true)
403 @array[2][9] = Piece::new("KE", true)
404 @array[3][9] = Piece::new("GI", true)
405 @array[4][9] = Piece::new("KI", true)
406 @array[5][9] = Piece::new("OU", true)
407 @array[6][9] = Piece::new("KI", true)
408 @array[7][9] = Piece::new("GI", true)
409 @array[8][9] = Piece::new("KE", true)
410 @array[9][9] = Piece::new("KY", true)
411 @array[2][8] = Piece::new("HI", true)
412 @array[8][8] = Piece::new("KA", true)
413 @array[1][7] = Piece::new("FU", true)
414 @array[2][7] = Piece::new("FU", true)
415 @array[3][7] = Piece::new("FU", true)
416 @array[4][7] = Piece::new("FU", true)
417 @array[5][7] = Piece::new("FU", true)
418 @array[6][7] = Piece::new("FU", true)
419 @array[7][7] = Piece::new("FU", true)
420 @array[8][7] = Piece::new("FU", true)
421 @array[9][7] = Piece::new("FU", true)
424 def get_piece_from_hands(hands, name)
434 def handle_one_move(str)
435 if (str =~ /^([\+\-])(\d)(\d)(\d)(\d)([A-Z]{2})/)
442 elsif (str =~ /^%KACHI/)
444 elsif (str =~ /^%TORYO/)
456 if (@array[x1][y1] == sente) # this is mine
458 elsif (@array[x1][y1].name == "OU")
461 hands.push(@array[x1][y1])
464 if ((x0 == 0) && (y0 == 0))
465 p = get_piece_from_hands(hands, name)
466 return "illegal" if (! p) # i don't have this one
471 @array[x1][y1] = @array[x0][y0]
473 if (@array[x1][y1].name != name) # promoted ?
474 return "illegal" if (@array[x1][y1].promoted_name != name) # can't promote
475 @array[x1][y1].promoted = true
478 return "normal" # legal move
485 a.push(sprintf("P%d", y))
497 a.push(sprintf("\n"))
500 if (! sente_hands.empty?)
502 sente_hands.each do |p|
503 a.push("00" + p.name)
507 if (! gote_hands.empty?)
509 gote_hands.each do |p|
510 a.push("00" + p.name)
519 def initialize(game_name, player0, player1)
520 @monitors = Array::new
521 @game_name = game_name
522 if (@game_name =~ /:(\d+):(\d+)/)
523 @total_time = $1.to_i
534 @current_player = @sente
540 @sente.status = "agree_waiting"
541 @gote.status = "agree_waiting"
542 @id = sprintf("%s+%s+%s+%s+%s",
543 LEAGUE.event, @game_name, @sente.name, @gote.name,
544 Time::new.strftime("%Y%m%d%H%M%S"))
546 LEAGUE.games[@id] = self
549 log_message(sprintf("game created %s %s %s", game_name, sente.name, gote.name))
551 @logfile = @id + ".csa"
559 attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :id, :board, :current_player, :next_player, :fh, :monitors
561 def monitoron(monitor)
562 @monitors.delete(monitor)
563 @monitors.push(monitor)
564 monitor.write_safe(@board.to_s.gsub(/^/, "##[MONITOR][#{@id}] "))
565 monitor.write_safe(sprintf("##[MONITOR][%s] +OK\n", @id))
568 def monitoroff(monitor)
569 @monitors.delete(monitor)
573 @sente.write_safe(sprintf("REJECT:%s by %s\n", @id, rejector))
574 @gote.write_safe(sprintf("REJECT:%s by %s\n", @id, rejector))
579 if ((@sente.status == "agree_waiting") || (@sente.status == "start_waiting"))
581 elsif (@current_player == killer)
587 log_message(sprintf("game finished %s %s %s", game_name, sente.name, gote.name))
588 @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
593 @sente.status = "connected"
594 @gote.status = "connected"
596 if (@current_player.protocol == "CSA")
597 @current_player.finish
599 if (@next_player.protocol == "CSA")
602 @monitors = Array::new
605 @current_player = nil
607 LEAGUE.games.delete(@id)
610 def handle_one_move(str, player)
612 if (@current_player == player)
613 @end_time = Time::new
614 t = @end_time - @start_time
615 t = Least_Time_Per_Move if (t < Least_Time_Per_Move)
618 if ((@current_player.mytime - t <= 0) && (@total_time > 0))
620 elsif (str == :timeout)
621 return false # time isn't expired. players aren't swapped. continue game
623 move_status = @board.handle_one_move(str)
624 if (move_status == "normal")
625 @sente.write_safe(sprintf("%s,T%d\n", str, t))
626 @gote.write_safe(sprintf("%s,T%d\n", str, t))
627 @fh.printf("%s\nT%d\n", str, t)
629 @monitors.each do |monitor|
630 monitor.write_safe(sprintf("##[MONITOR][%s] %s\n", @id, str))
631 monitor.write_safe(@board.to_s.gsub(/^/, "##[MONITOR][#{@id}] "))
632 monitor.write_safe(sprintf("##[MONITOR][%s] +OK\n", @id))
634 elsif (move_status == "illegal")
635 @fh.printf("'ILLEGAL_MOVE(%s)\n", str)
639 if (@current_player.mytime - t < @byoyomi)
640 @current_player.mytime = @byoyomi
642 @current_player.mytime = @current_player.mytime - t
645 if (@next_player.status != "game") # rival is logout or disconnected
647 elsif (status == :timeout)
649 elsif (move_status == "illegal")
651 elsif (move_status == "kachi")
653 elsif (move_status == "toryo")
655 elsif (move_status == "outori")
660 finish() if finish_flag
661 (@current_player, @next_player) = [@next_player, @current_player]
662 @start_time = Time::new
668 @current_player.status = "connected"
669 @next_player.status = "connected"
670 @current_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
671 @next_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
672 @monitors.each do |monitor|
673 monitor.write_safe(sprintf("##[MONITOR][%s] %%TORYO\n", @id))
678 @current_player.status = "connected"
679 @next_player.status = "connected"
680 @current_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
681 @next_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
682 @monitors.each do |monitor|
683 monitor.write_safe(sprintf("##[MONITOR][%s] %%TORYO\n", @id))
688 @current_player.status = "connected"
689 @next_player.status = "connected"
690 @current_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
691 @next_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
692 @monitors.each do |monitor|
693 monitor.write_safe(sprintf("##[MONITOR][%s] #ILLEGAL_MOVE\n", @id))
698 @current_player.status = "connected"
699 @next_player.status = "connected"
700 @current_player.write_safe("#TIME_UP\n#LOSE\n")
701 @next_player.write_safe("#TIME_UP\n#WIN\n")
702 @monitors.each do |monitor|
703 monitor.write_safe(sprintf("##[MONITOR][%s] #TIME_UP\n", @id))
708 @current_player.status = "connected"
709 @next_player.status = "connected"
710 @current_player.write_safe("%KACHI\n#JISHOGI\n#WIN\n")
711 @next_player.write_safe("%KACHI\n#JISHOGI\n#LOSE\n")
712 @monitors.each do |monitor|
713 monitor.write_safe(sprintf("##[MONITOR][%s] %%KACHI\n", @id))
718 @current_player.status = "connected"
719 @next_player.status = "connected"
720 @current_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
721 @next_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
722 @monitors.each do |monitor|
723 monitor.write_safe(sprintf("##[MONITOR][%s] %%TORYO\n", @id))
728 @current_player.status = "connected"
729 @next_player.status = "connected"
730 @current_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
731 @next_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
732 @monitors.each do |monitor|
733 monitor.write_safe(sprintf("##[MONITOR][%s] #ILLEGAL_MOVE\n", @id))
738 log_message(sprintf("game started %s %s %s", game_name, sente.name, gote.name))
739 @sente.write_safe(sprintf("START:%s\n", @id))
740 @gote.write_safe(sprintf("START:%s\n", @id))
741 @sente.mytime = @total_time
742 @gote.mytime = @total_time
743 @start_time = Time::new
748 @fh = open(@logfile, "w")
752 @fh.printf("N+%s\n", @sente.name)
753 @fh.printf("N-%s\n", @gote.name)
754 @fh.printf("$EVENT:%s\n", @id)
756 @sente.write_safe(propose_message("+"))
757 @gote.write_safe(propose_message("-"))
759 @fh.printf("$START_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
761 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
762 P2 * -HI * * * * * -KA *
763 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
767 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
768 P8 * +KA * * * * * +HI *
769 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
775 def propose_message(sg_flag)
789 Total_Time:#{@total_time}
791 Least_Time_Per_Move:#{Least_Time_Per_Move}
794 Jishogi_Declaration:1.1
795 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
796 P2 * -HI * * * * * -KA *
797 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
801 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
802 P8 * +KA * * * * * +HI *
803 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
817 shogi-server - server for CSA server protocol
820 shogi-server event_name port_number
823 server for CSA server protocol
827 specify filename for logging process ID
830 this file is distributed under GPL version2 and might be compiled by Exerb
843 printf("%s message: %s\n", Time::new.to_s, str)
847 printf("%s message: %s\n", Time::new.to_s, str)
851 printf("%s error: %s\n", Time::new.to_s, str)
855 def parse_command_line
857 parser = GetoptLong.new
858 parser.ordering = GetoptLong::REQUIRE_ORDER
860 ["--pid-file", GetoptLong::REQUIRED_ARGUMENT])
864 parser.each_option do |name, arg|
866 options[name] = arg.dup
870 raise parser.error_message
877 def good_game_name?(str)
878 if ((str =~ /^(.+):\d+:\d+$/) &&
879 (good_identifier?($1)))
886 def good_identifier?(str)
887 if ((str =~ /\A[\w\d_@\-\.]+\z/) &&
888 (str.length < Max_Identifier_Length))
897 if (((tokens.length == 3) || ((tokens.length == 4) && tokens[3] == "x1")) &&
898 (tokens[0] == "LOGIN") &&
899 (good_identifier?(tokens[1])))
906 def write_pid_file(file)
907 open(file, "w") do |fh|
908 fh.print Process::pid, "\n"
914 $options = parse_command_line
915 if (ARGV.length != 2)
920 LEAGUE.event = ARGV.shift
923 write_pid_file($options["pid-file"]) if ($options["pid-file"])
926 Thread.abort_on_exception = true
928 server = TCPserver.open(port)
929 log_message("server started")
932 Thread::start(server.accept) do |client|
935 while (str = client.gets_timeout(Login_Time))
938 Thread::kill(Thread::current) if (! str) # disconnected
941 if (good_login?(str))
942 player = Player::new(str, client)
943 if (LEAGUE.players[player.name])
944 if ((LEAGUE.players[player.name].password == player.password) &&
945 (LEAGUE.players[player.name].status != "game"))
946 log_message(sprintf("user %s login forcely", player.name))
947 LEAGUE.players[player.name].kill
949 client.write_safe("LOGIN:incorrect" + eol)
950 client.write_safe(sprintf("username %s is already connected%s", player.name, eol)) if (str.split.length >= 4)
952 Thread::kill(Thread::current)
958 client.write_safe("LOGIN:incorrect" + eol)
959 client.write_safe("type 'LOGIN name password' or 'LOGIN name password x1'" + eol) if (str.split.length >= 4)
967 Thread::kill(Thread::current)
969 log_message(sprintf("user %s login", player.name))
974 player.game.kill(player)
977 LEAGUE.delete(player)
978 log_message(sprintf("user %s logout", player.name))