4 ## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
5 ## Copyright (C) 2007 Daigo Moriwaki (daigo at debian dot org)
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
12 ## This program is distributed in the hope that it will be useful,
13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ## GNU General Public License for more details.
17 ## You should have received a copy of the GNU General Public License
18 ## along with this program; if not, write to the Free Software
19 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 def gets_timeout(t = Default_Timeout)
44 def gets_safe(t = nil)
52 rescue Exception => ex
53 log_error("#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}")
66 return self.write(str)
74 module ShogiServer # for a namespace
76 Max_Write_Queue_Size = 1000
77 Max_Identifier_Length = 32
78 Default_Timeout = 60 # for single socket operation
80 Default_Game_Name = "default-1500-0"
83 Least_Time_Per_Move = 1
84 Login_Time = 300 # time for LOGIN
86 Release = "$Name$".split[1].sub(/\A[^\d]*/, '').gsub(/_/, '.')
87 Release.concat("-") if (Release == "")
88 Revision = "$Revision$".gsub(/[^\.\d]/, '')
96 @dir = File.dirname(__FILE__)
98 attr_accessor :players, :games, :event, :dir
100 # this should be called just after instanciating a League object.
101 def setup_players_database
102 @db = YAML::Store.new(File.join(@dir, "players.yaml"))
106 self.load(player) if player.id
107 @players[player.name] = player
111 @players.delete(player.name)
114 def get_player(status, game_name, sente, searcher=nil)
115 @players.each do |name, player|
116 if ((player.status == status) &&
117 (player.game_name == game_name) &&
118 ((sente == nil) || (player.sente == nil) || (player.sente == sente)) &&
119 ((searcher == nil) || (player != searcher)))
127 hash = search(player.id)
130 player.name = hash['name']
131 player.rate = hash['rate']
132 player.modified_at = hash['last_modified']
133 player.rating_group = hash['rating_group']
140 break unless @db["players"]
141 @db["players"].each do |group, players|
151 @db.transaction(true) do
152 break unless @db["players"]
153 @db["players"].each do |group, players_hash|
154 players << players_hash.keys
157 return players.flatten.collect do |id|
167 ######################################################
168 # Processes the LOGIN command.
171 def Login.good_login?(str)
173 if (((tokens.length == 3) || ((tokens.length == 4) && tokens[3] == "x1")) &&
174 (tokens[0] == "LOGIN") &&
175 (good_identifier?(tokens[1])))
182 def Login.good_game_name?(str)
183 if ((str =~ /^(.+)-\d+-\d+$/) && (good_identifier?($1)))
190 def Login.good_identifier?(str)
191 if str =~ /\A[\w\d_@\-\.]{1,#{Max_Identifier_Length}}\z/
198 def Login.factory(str, player)
199 (login, player.name, password, ext) = str.chomp.split
201 return Loginx1.new(player, password)
203 return LoginCSA.new(player, password)
209 # the first command that will be executed just after LOGIN.
210 # If it is nil, the default process will be started.
211 attr_reader :csa_1st_str
213 def initialize(player, password)
216 parse_password(password)
220 @player.write_safe(sprintf("LOGIN:%s OK\n", @player.name))
221 log_message(sprintf("user %s run in %s mode", @player.name, @player.protocol))
224 def incorrect_duplicated_player(str)
225 @player.write_safe("LOGIN:incorrect\n")
226 @player.write_safe(sprintf("username %s is already connected\n", @player.name)) if (str.split.length >= 4)
227 sleep 3 # wait for sending the above messages.
228 @player.name = "%s [duplicated]" % [@player.name]
233 ######################################################
234 # Processes LOGIN for the CSA standard mode.
236 class LoginCSA < Login
239 def initialize(player, password)
242 @player.protocol = PROTOCOL
245 def parse_password(password)
246 if Login.good_game_name?(password)
248 @player.set_password(nil)
249 elsif password.split(",").size > 1
250 @gamename, *trip = password.split(",")
251 @player.set_password(trip.join(","))
253 @player.set_password(password)
254 @gamename = Default_Game_Name
256 @gamename = self.class.good_game_name?(@gamename) ? @gamename : Default_Game_Name
261 @csa_1st_str = "%%GAME #{@gamename} *"
265 ######################################################
266 # Processes LOGIN for the extented mode.
268 class Loginx1 < Login
271 def initialize(player, password)
273 @player.protocol = PROTOCOL
276 def parse_password(password)
277 @player.set_password(password)
282 @player.write_safe(sprintf("##[LOGIN] +OK %s\n", PROTOCOL))
288 # Idetifier of the player in the rating system
294 # Password of the player, which does not include a trip
295 attr_accessor :password
297 # Score in the rating sysem
300 # Group in the rating system
301 attr_accessor :rating_group
303 # Last timestamp when the rate was modified
304 attr_accessor :modified_at
312 @modified_at || Time.now
318 @modified_at = Time.now
328 simple_name = @name.gsub(/@.*?$/, '')
329 "%s+%s" % [simple_name, @trip[0..8]]
336 # Parses str in the LOGIN command, sets up @id and @trip
338 def set_password(str)
339 if str && !str.empty?
340 @password = str.strip
341 @id = "%s+%s" % [@name, Digest::MD5.hexdigest(@password)]
343 @id = @password = nil
349 class Player < BasicPlayer
350 def initialize(str, socket)
353 @status = "connected" # game_waiting -> agree_waiting -> start_waiting -> game -> finished
355 @protocol = nil # CSA or x1
356 @eol = "\m" # favorite eol code
359 @mytime = 0 # set in start method also
361 @write_queue = Queue::new
362 @main_thread = Thread::current
363 @writer_thread = Thread::start do
369 attr_accessor :socket, :status
370 attr_accessor :protocol, :eol, :game, :mytime, :game_name, :sente
371 attr_accessor :main_thread, :writer_thread, :write_queue
374 log_message(sprintf("user %s killed", @name))
379 Thread::kill(@main_thread) if @main_thread
383 if (@status != "finished")
385 log_message(sprintf("user %s finish", @name))
386 # TODO you should confirm that there is no message in the queue.
387 Thread::kill(@writer_thread) if @writer_thread
389 # @socket.close if (! @socket.closed?)
391 log_message(sprintf("user %s finish failed", @name))
397 @write_queue.push(str.gsub(/[\r\n]+/, @eol))
401 while (str = @write_queue.pop)
404 rescue Exception => ex
405 log_error("Failed to send a message to #{@name}.")
406 log_error("#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}")
413 if ((status == "game_waiting") ||
414 (status == "start_waiting") ||
415 (status == "agree_waiting") ||
418 return sprintf("%s %s %s %s +", @name, @protocol, @status, @game_name)
419 elsif (@sente == false)
420 return sprintf("%s %s %s %s -", @name, @protocol, @status, @game_name)
421 elsif (@sente == nil)
422 return sprintf("%s %s %s %s *", @name, @protocol, @status, @game_name)
425 return sprintf("%s %s %s", @name, @protocol, @status)
429 def run(csa_1st_str=nil)
430 while (csa_1st_str || (str = @socket.gets_safe(Default_Timeout)))
434 if (@writer_thread == nil || @writer_thread.status == false)
435 # The writer_thread has been killed because of socket errors.
443 if (@write_queue.size > Max_Write_Queue_Size)
444 log_warning(sprintf("write_queue of %s is %d", @name, @write_queue.size))
448 if (@status == "finished")
451 str.chomp! if (str.class == String) # may be strip! ?
452 log_message(str) if $DEBUG
455 # Application-level protocol for Keep-Alive
456 # If the server gets LF, it sends back LF.
457 # 30 sec rule (client may not send LF again within 30 sec) is not implemented yet.
460 if (@status == "game")
461 array_str = str.split(",")
462 move = array_str.shift
463 additional = array_str.shift
464 if /^'(.*)/ =~ additional
465 comment = array_str.unshift("'*#{$1}")
467 s = @game.handle_one_move(move, self)
468 @game.fh.print("#{comment}\n") if (comment && !s)
469 return if (s && @protocol == LoginCSA::PROTOCOL)
471 when /^%[^%]/, :timeout
472 if (@status == "game")
473 s = @game.handle_one_move(str, self)
474 return if (s && @protocol == LoginCSA::PROTOCOL)
477 # @socket.write("##[KEEPALIVE] #{Time.now}\n")
478 # rescue Exception => ex
479 # log_error("Failed to send a keepalive to #{@name}.")
480 # log_error("#{ex.class}: #{ex.message}\n\t#{ex.backtrace[0]}")
485 log_error("Failed to receive a message from #{@name}.")
488 if (@status == "agree_waiting")
490 return if (@protocol == LoginCSA::PROTOCOL)
492 write_safe(sprintf("##[ERROR] you are in %s status. AGREE is valid in agree_waiting status\n", @status))
495 if (@status == "agree_waiting")
496 @status = "start_waiting"
497 if ((@game.sente.status == "start_waiting") &&
498 (@game.gote.status == "start_waiting"))
500 @game.sente.status = "game"
501 @game.gote.status = "game"
504 write_safe(sprintf("##[ERROR] you are in %s status. AGREE is valid in agree_waiting status\n", @status))
506 when /^%%SHOW\s+(\S+)/
508 if (LEAGUE.games[game_id])
509 write_safe(LEAGUE.games[game_id].show.gsub(/^/, '##[SHOW] '))
511 write_safe("##[SHOW] +OK\n")
512 when /^%%MONITORON\s+(\S+)/
514 if (LEAGUE.games[game_id])
515 LEAGUE.games[game_id].monitoron(self)
516 write_safe(LEAGUE.games[game_id].show.gsub(/^/, "##[MONITOR][#{game_id}] "))
517 write_safe("##[MONITOR][#{game_id}] +OK\n")
519 when /^%%MONITOROFF\s+(\S+)/
521 if (LEAGUE.games[game_id])
522 LEAGUE.games[game_id].monitoroff(self)
526 %!##[HELP] available commands "%%WHO", "%%CHAT str", "%%GAME game_name +", "%%GAME game_name -"\n!)
528 players = LEAGUE.rated_players
529 players.sort {|a,b| b.rate <=> a.rate}.each do |p|
530 write_safe("##[RATING] %s \t %4d @%s\n" %
531 [p.simple_id, p.rate, p.modified_at.strftime("%Y-%m-%d")])
533 write_safe("##[RATING] +OK\n")
535 write_safe "##[VERSION] Shogi Server revision #{Revision}\n"
536 write_safe("##[VERSION] +OK\n")
538 if ((@status == "connected") || (@status == "game_waiting"))
539 @status = "connected"
542 write_safe(sprintf("##[ERROR] you are in %s status. GAME is valid in connected or game_waiting status\n", @status))
544 when /^%%(GAME|CHALLENGE)\s+(\S+)\s+([\+\-\*])\s*$/
548 if (! Login::good_game_name?(game_name))
549 write_safe(sprintf("##[ERROR] bad game name\n"))
551 elsif ((@status == "connected") || (@status == "game_waiting"))
554 write_safe(sprintf("##[ERROR] you are in %s status. GAME is valid in connected or game_waiting status\n", @status))
557 if ((my_sente_str == "*") ||
558 (my_sente_str == "+") ||
559 (my_sente_str == "-"))
562 write_safe(sprintf("##[ERROR] bad game option\n"))
566 if (my_sente_str == "*")
567 rival = LEAGUE.get_player("game_waiting", game_name, nil, self) # no preference
568 elsif (my_sente_str == "+")
569 rival = LEAGUE.get_player("game_waiting", game_name, false, self) # rival must be gote
570 elsif (my_sente_str == "-")
571 rival = LEAGUE.get_player("game_waiting", game_name, true, self) # rival must be sente
576 @game_name = game_name
577 if ((my_sente_str == "*") && (rival.sente == nil))
585 elsif (rival.sente == true) # rival has higher priority
587 elsif (rival.sente == false)
589 elsif (my_sente_str == "+")
592 elsif (my_sente_str == "-")
598 Game::new(@game_name, self, rival)
599 self.status = "agree_waiting"
600 rival.status = "agree_waiting"
601 else # rival not found
602 if (command_name == "GAME")
603 @status = "game_waiting"
604 @game_name = game_name
605 if (my_sente_str == "+")
607 elsif (my_sente_str == "-")
613 write_safe(sprintf("##[ERROR] can't find rival for %s\n", game_name))
614 @status = "connected"
619 when /^%%CHAT\s+(.+)/
621 LEAGUE.players.each do |name, player|
622 if (player.protocol != LoginCSA::PROTOCOL)
623 player.write_safe(sprintf("##[CHAT][%s] %s\n", @name, message))
628 LEAGUE.games.each do |id, game|
629 buf.push(sprintf("##[LIST] %s\n", id))
631 buf.push("##[LIST] +OK\n")
635 LEAGUE.players.each do |name, player|
636 buf.push(sprintf("##[WHO] %s\n", player.to_s))
638 buf.push("##[WHO] +OK\n")
641 @status = "connected"
642 write_safe("LOGOUT:completed\n")
645 # This command is only available for CSA's official testing server.
646 # So, this means nothing for this program.
647 write_safe("CHALLENGE ACCEPTED\n")
649 ## ignore null string
651 msg = "##[ERROR] unknown command %s\n" % [str]
663 PROMOTE = {"FU" => "TO", "KY" => "NY", "KE" => "NK", "GI" => "NG", "KA" => "UM", "HI" => "RY"}
664 def initialize(board, x, y, sente, promoted=false)
671 if ((x == 0) || (y == 0))
673 hands = board.sente_hands
675 hands = board.gote_hands
682 @board.array[x][y] = self
685 attr_accessor :promoted, :sente, :x, :y, :board
687 def room_of_head?(x, y, name)
692 return adjacent_movable_grids + far_movable_grids
695 def far_movable_grids
700 if ((1 <= x) && (x <= 9) && (1 <= y) && (y <= 9))
701 if ((@board.array[x][y] == nil) || # dst is empty
702 (@board.array[x][y].sente != @sente)) # dst is enemy
710 if ((1 <= x) && (x <= 9) && (1 <= y) && (y <= 9))
711 if (@board.array[x][y] == nil) # dst is empty?
718 def adjacent_movable_grids
721 moves = @promoted_moves
723 moves = @normal_moves
725 moves.each do |(dx, dy)|
732 if (jump_to?(cand_x, cand_y))
733 grids.push([cand_x, cand_y])
739 def move_to?(x, y, name)
740 return false if (! room_of_head?(x, y, name))
741 return false if ((name != @name) && (name != @promoted_name))
742 return false if (@promoted && (name != @promoted_name)) # can't un-promote
745 return false if (((@x == 0) || (@y == 0)) && (name != @name)) # can't put promoted piece
747 return false if ((4 <= @y) && (4 <= y) && (name != @name)) # can't promote
749 return false if ((6 >= @y) && (6 >= y) && (name != @name))
753 if ((@x == 0) || (@y == 0))
754 return jump_to?(x, y)
756 return movable_grids.include?([x, y])
761 if ((@x == 0) || (@y == 0))
763 @board.sente_hands.delete(self)
765 @board.gote_hands.delete(self)
767 @board.array[x][y] = self
768 elsif ((x == 0) || (y == 0))
769 @promoted = false # clear promoted flag before moving to hands
771 @board.sente_hands.push(self)
773 @board.gote_hands.push(self)
775 @board.array[@x][@y] = nil
777 @board.array[@x][@y] = nil
778 @board.array[x][y] = self
811 class PieceFU < Piece
814 @normal_moves = [[0, +1]]
815 @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
817 @promoted_name = "TO"
820 def room_of_head?(x, y, name)
823 return false if (y == 1)
825 return false if (y == 9)
831 if ((iy != @y) && # not source position
832 @board.array[x][iy] &&
833 (@board.array[x][iy].sente == @sente) && # mine
834 (@board.array[x][iy].name == "FU") &&
835 (@board.array[x][iy].promoted == false))
845 class PieceKY < Piece
849 @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
851 @promoted_name = "NY"
854 def room_of_head?(x, y, name)
857 return false if (y == 1)
859 return false if (y == 9)
864 def far_movable_grids
872 while (jump_to?(cand_x, cand_y))
873 grids.push([cand_x, cand_y])
874 break if (! put_to?(cand_x, cand_y))
880 while (jump_to?(cand_x, cand_y))
881 grids.push([cand_x, cand_y])
882 break if (! put_to?(cand_x, cand_y))
890 class PieceKE < Piece
893 @normal_moves = [[+1, +2], [-1, +2]]
894 @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
896 @promoted_name = "NK"
899 def room_of_head?(x, y, name)
902 return false if ((y == 1) || (y == 2))
904 return false if ((y == 9) || (y == 8))
910 class PieceGI < Piece
913 @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, -1], [-1, -1]]
914 @promoted_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
916 @promoted_name = "NG"
920 class PieceKI < Piece
923 @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1]]
930 class PieceKA < Piece
934 @promoted_moves = [[0, +1], [+1, 0], [-1, 0], [0, -1]]
936 @promoted_name = "UM"
939 def far_movable_grids
944 while (jump_to?(cand_x, cand_y))
945 grids.push([cand_x, cand_y])
946 break if (! put_to?(cand_x, cand_y))
953 while (jump_to?(cand_x, cand_y))
954 grids.push([cand_x, cand_y])
955 break if (! put_to?(cand_x, cand_y))
962 while (jump_to?(cand_x, cand_y))
963 grids.push([cand_x, cand_y])
964 break if (! put_to?(cand_x, cand_y))
971 while (jump_to?(cand_x, cand_y))
972 grids.push([cand_x, cand_y])
973 break if (! put_to?(cand_x, cand_y))
980 class PieceHI < Piece
984 @promoted_moves = [[+1, +1], [-1, +1], [+1, -1], [-1, -1]]
986 @promoted_name = "RY"
989 def far_movable_grids
994 while (jump_to?(cand_x, cand_y))
995 grids.push([cand_x, cand_y])
996 break if (! put_to?(cand_x, cand_y))
1002 while (jump_to?(cand_x, cand_y))
1003 grids.push([cand_x, cand_y])
1004 break if (! put_to?(cand_x, cand_y))
1010 while (jump_to?(cand_x, cand_y))
1011 grids.push([cand_x, cand_y])
1012 break if (! put_to?(cand_x, cand_y))
1018 while (jump_to?(cand_x, cand_y))
1019 grids.push([cand_x, cand_y])
1020 break if (! put_to?(cand_x, cand_y))
1026 class PieceOU < Piece
1027 def initialize(*arg)
1029 @normal_moves = [[0, +1], [+1, +1], [-1, +1], [+1, +0], [-1, +0], [0, -1], [+1, -1], [-1, -1]]
1030 @promoted_moves = []
1032 @promoted_name = nil
1039 @sente_hands = Array::new
1040 @gote_hands = Array::new
1041 @history = Hash::new(0)
1042 @sente_history = Hash::new(0)
1043 @gote_history = Hash::new(0)
1044 @array = [[], [], [], [], [], [], [], [], [], []]
1047 attr_accessor :array, :sente_hands, :gote_hands, :history, :sente_history, :gote_history
1048 attr_reader :move_count
1051 PieceKY::new(self, 1, 1, false)
1052 PieceKE::new(self, 2, 1, false)
1053 PieceGI::new(self, 3, 1, false)
1054 PieceKI::new(self, 4, 1, false)
1055 PieceOU::new(self, 5, 1, false)
1056 PieceKI::new(self, 6, 1, false)
1057 PieceGI::new(self, 7, 1, false)
1058 PieceKE::new(self, 8, 1, false)
1059 PieceKY::new(self, 9, 1, false)
1060 PieceKA::new(self, 2, 2, false)
1061 PieceHI::new(self, 8, 2, false)
1063 PieceFU::new(self, i, 3, false)
1066 PieceKY::new(self, 1, 9, true)
1067 PieceKE::new(self, 2, 9, true)
1068 PieceGI::new(self, 3, 9, true)
1069 PieceKI::new(self, 4, 9, true)
1070 PieceOU::new(self, 5, 9, true)
1071 PieceKI::new(self, 6, 9, true)
1072 PieceGI::new(self, 7, 9, true)
1073 PieceKE::new(self, 8, 9, true)
1074 PieceKY::new(self, 9, 9, true)
1075 PieceKA::new(self, 8, 8, true)
1076 PieceHI::new(self, 2, 8, true)
1078 PieceFU::new(self, i, 7, true)
1082 def have_piece?(hands, name)
1083 piece = hands.find { |i|
1089 def move_to(x0, y0, x1, y1, name, sente)
1091 hands = @sente_hands
1096 if ((x0 == 0) || (y0 == 0))
1097 piece = have_piece?(hands, name)
1098 return :illegal if (! piece.move_to?(x1, y1, name)) # TODO null check for the piece?
1099 piece.move_to(x1, y1)
1101 return :illegal if (! @array[x0][y0].move_to?(x1, y1, name)) # TODO null check?
1102 if (@array[x0][y0].name != name) # promoted ?
1103 @array[x0][y0].promoted = true
1105 if (@array[x1][y1]) # capture
1106 if (@array[x1][y1].name == "OU")
1107 return :outori # return board update
1109 @array[x1][y1].sente = @array[x0][y0].sente
1110 @array[x1][y1].move_to(0, 0)
1111 hands.sort! {|a, b| # TODO refactor. Move to Piece class
1115 @array[x0][y0].move_to(x1, y1)
1121 def look_for_ou(sente)
1127 (@array[x][y].name == "OU") &&
1128 (@array[x][y].sente == sente))
1135 raise "can't find ou"
1138 # note checkmate, but check. sente is checked.
1139 def checkmated?(sente) # sente is loosing
1140 ou = look_for_ou(sente)
1146 (@array[x][y].sente != sente))
1147 if (@array[x][y].movable_grids.include?([ou.x, ou.y]))
1158 def uchifuzume?(sente)
1159 rival_ou = look_for_ou(! sente) # rival's ou
1160 if (sente) # rival is gote
1161 if ((rival_ou.y != 9) &&
1162 (@array[rival_ou.x][rival_ou.y + 1]) &&
1163 (@array[rival_ou.x][rival_ou.y + 1].name == "FU") &&
1164 (@array[rival_ou.x][rival_ou.y + 1].sente == sente)) # uchifu true
1166 fu_y = rival_ou.y + 1
1171 if ((rival_ou.y != 0) &&
1172 (@array[rival_ou.x][rival_ou.y - 1]) &&
1173 (@array[rival_ou.x][rival_ou.y - 1].name == "FU") &&
1174 (@array[rival_ou.x][rival_ou.y - 1].sente == sente)) # uchifu true
1176 fu_y = rival_ou.y - 1
1182 ## case: rival_ou is moving
1184 rival_ou.movable_grids.each do |(cand_x, cand_y)|
1185 tmp_board = Marshal.load(Marshal.dump(self))
1186 s = tmp_board.move_to(rival_ou.x, rival_ou.y, cand_x, cand_y, "OU", ! sente)
1187 raise "internal error" if (s != true)
1188 if (! tmp_board.checkmated?(! sente)) # good move
1193 ## case: rival is capturing fu
1199 (@array[x][y].sente != sente) &&
1200 @array[x][y].movable_grids.include?([fu_x, fu_y])) # capturable
1201 if (@array[x][y].promoted)
1202 name = @array[x][y].promoted_name
1204 name = @array[x][y].name
1206 tmp_board = Marshal.load(Marshal.dump(self))
1207 s = tmp_board.move_to(x, y, fu_x, fu_y, name, ! sente)
1208 raise "internal error" if (s != true)
1209 if (! tmp_board.checkmated?(! sente)) # good move
1220 # @[sente|gote]_history has at least one item while the player is checking the other or
1221 # the other escapes.
1222 def update_sennichite(player)
1225 if checkmated?(!player)
1227 @sente_history["dummy"] = 1 # flag to see Sente player is checking Gote player
1229 @gote_history["dummy"] = 1 # flag to see Gote player is checking Sente player
1233 @sente_history.clear # no more continuous check
1235 @gote_history.clear # no more continuous check
1238 if @sente_history.size > 0 # possible for Sente's or Gote's turn
1239 @sente_history[str] += 1
1241 if @gote_history.size > 0 # possible for Sente's or Gote's turn
1242 @gote_history[str] += 1
1246 def oute_sennichite?(player)
1247 if (@sente_history[to_s] >= 4)
1248 return :oute_sennichite_sente_lose
1249 elsif (@gote_history[to_s] >= 4)
1250 return :oute_sennichite_gote_lose
1256 def sennichite?(sente)
1257 if (@history[to_s] >= 4) # already 3 times
1263 def good_kachi?(sente)
1264 if (checkmated?(sente))
1265 puts "'NG: Checkmating." if $DEBUG
1269 ou = look_for_ou(sente)
1270 if (sente && (ou.y >= 4))
1271 puts "'NG: Black's OU does not enter yet." if $DEBUG
1274 if (! sente && (ou.y <= 6))
1275 puts "'NG: White's OU does not enter yet." if $DEBUG
1283 hands = @sente_hands
1293 (@array[x][y].sente == sente) &&
1294 (@array[x][y].point > 0))
1295 point = point + @array[x][y].point
1301 hands.each do |piece|
1302 point = point + piece.point
1306 puts "'NG: Piece#[%d] is too small." % [number] if $DEBUG
1311 puts "'NG: Black's point#[%d] is too small." % [point] if $DEBUG
1316 puts "'NG: White's point#[%d] is too small." % [point] if $DEBUG
1321 puts "'Good: Piece#[%d], Point[%d]." % [number, point] if $DEBUG
1325 # sente is nil only if tests in test_board run
1326 def handle_one_move(str, sente=nil)
1327 if (str =~ /^([\+\-])(\d)(\d)(\d)(\d)([A-Z]{2})/)
1334 elsif (str =~ /^%KACHI/)
1335 raise ArgumentError, "sente is null", caller if sente == nil
1336 if (good_kachi?(sente))
1341 elsif (str =~ /^%TORYO/)
1347 if (((x0 == 0) || (y0 == 0)) && # source is not from hand
1348 ((x0 != 0) || (y0 != 0)))
1350 elsif ((x1 == 0) || (y1 == 0)) # destination is out of board
1356 sente = true if sente == nil # deprecated
1357 return :illegal unless sente == true # black player's move must be black
1358 hands = @sente_hands
1360 sente = false if sente == nil # deprecated
1361 return :illegal unless sente == false # white player's move must be white
1366 if ((x0 == 0) && (y0 == 0))
1367 return :illegal if (! have_piece?(hands, name))
1368 elsif (! @array[x0][y0])
1369 return :illegal # no piece
1370 elsif (@array[x0][y0].sente != sente)
1371 return :illegal # this is not mine
1372 elsif (@array[x0][y0].name != name)
1373 return :illegal if (@array[x0][y0].promoted_name != name) # can't promote
1376 ## destination check
1377 if (@array[x1][y1] &&
1378 (@array[x1][y1].sente == sente)) # can't capture mine
1380 elsif ((x0 == 0) && (y0 == 0) && @array[x1][y1])
1381 return :illegal # can't put on existing piece
1384 tmp_board = Marshal.load(Marshal.dump(self))
1385 return :illegal if (tmp_board.move_to(x0, y0, x1, y1, name, sente) == :illegal)
1386 return :oute_kaihimore if (tmp_board.checkmated?(sente))
1387 tmp_board.update_sennichite(sente)
1388 os_result = tmp_board.oute_sennichite?(sente)
1389 return os_result if os_result # :oute_sennichite_sente_lose or :oute_sennichite_gote_lose
1390 return :sennichite if tmp_board.sennichite?(sente)
1392 if ((x0 == 0) && (y0 == 0) && (name == "FU") && tmp_board.uchifuzume?(sente))
1396 move_to(x0, y0, x1, y1, name, sente)
1399 update_sennichite(sente)
1407 a.push(sprintf("P%d", y))
1410 piece = @array[x][y]
1419 a.push(sprintf("\n"))
1422 if (! sente_hands.empty?)
1424 sente_hands.each do |p|
1425 a.push("00" + p.name)
1429 if (! gote_hands.empty?)
1431 gote_hands.each do |p|
1432 a.push("00" + p.name)
1442 attr_reader :players, :black, :white
1444 def initialize(p1, p2)
1448 if p1.sente && !p2.sente
1449 @black, @white = p1, p2
1450 elsif !p1.sente && p2.sente
1451 @black, @white = p2, p1
1453 raise "Never reached!"
1458 class GameResultWin < GameResult
1459 attr_reader :winner, :loser
1461 def initialize(winner, loser)
1463 @winner, @loser = winner, loser
1467 black_name = @black.id || @black.name
1468 white_name = @white.id || @white.name
1469 "%s:%s" % [black_name, white_name]
1473 class GameResultDraw < GameResult
1481 def initialize(game_name, player0, player1)
1482 @monitors = Array::new
1483 @game_name = game_name
1484 if (@game_name =~ /-(\d+)-(\d+)$/)
1485 @total_time = $1.to_i
1496 @current_player = @sente
1497 @next_player = @gote
1505 @sente.status = "agree_waiting"
1506 @gote.status = "agree_waiting"
1508 @id = sprintf("%s+%s+%s+%s+%s",
1509 LEAGUE.event, @game_name, @sente.name, @gote.name, issue_current_time)
1510 @logfile = File.join(LEAGUE.dir, @id + ".csa")
1512 LEAGUE.games[@id] = self
1514 log_message(sprintf("game created %s", @id))
1524 attr_accessor :game_name, :total_time, :byoyomi, :sente, :gote, :id, :board, :current_player, :next_player, :fh, :monitors
1525 attr_accessor :last_move, :current_turn
1529 @sente.rated? && @gote.rated?
1532 def monitoron(monitor)
1533 @monitors.delete(monitor)
1534 @monitors.push(monitor)
1537 def monitoroff(monitor)
1538 @monitors.delete(monitor)
1541 def reject(rejector)
1542 @sente.write_safe(sprintf("REJECT:%s by %s\n", @id, rejector))
1543 @gote.write_safe(sprintf("REJECT:%s by %s\n", @id, rejector))
1548 if ((@sente.status == "agree_waiting") || (@sente.status == "start_waiting"))
1550 elsif (@current_player == killer)
1557 log_message(sprintf("game finished %s", @id))
1558 @fh.printf("'$END_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
1563 @sente.status = "connected"
1564 @gote.status = "connected"
1566 if (@current_player.protocol == LoginCSA::PROTOCOL)
1567 @current_player.finish
1569 if (@next_player.protocol == LoginCSA::PROTOCOL)
1572 @monitors = Array::new
1575 @current_player = nil
1577 LEAGUE.games.delete(@id)
1580 def handle_one_move(str, player)
1582 if (@current_player == player)
1583 @end_time = Time::new
1584 t = (@end_time - @start_time).floor
1585 t = Least_Time_Per_Move if (t < Least_Time_Per_Move)
1588 if ((@current_player.mytime - t <= -@byoyomi) && ((@total_time > 0) || (@byoyomi > 0)))
1590 elsif (str == :timeout)
1591 return false # time isn't expired. players aren't swapped. continue game
1593 @current_player.mytime = @current_player.mytime - t
1594 if (@current_player.mytime < 0)
1595 @current_player.mytime = 0
1599 move_status = @board.handle_one_move(str, @sente == @current_player)
1601 # log_error("handle_one_move raise exception for #{str}")
1602 # move_status = :illegal
1605 if ((move_status == :illegal) || (move_status == :uchifuzme) || (move_status == :oute_kaihimore))
1606 @fh.printf("'ILLEGAL_MOVE(%s)\n", str)
1608 if ((move_status == :normal) || (move_status == :outori) || (move_status == :sennichite) || (move_status == :oute_sennichite_sente_lose) || (move_status == :oute_sennichite_gote_lose))
1609 @sente.write_safe(sprintf("%s,T%d\n", str, t))
1610 @gote.write_safe(sprintf("%s,T%d\n", str, t))
1611 @fh.printf("%s\nT%d\n", str, t)
1612 @last_move = sprintf("%s,T%d", str, t)
1613 @current_turn = @current_turn + 1
1616 @monitors.each do |monitor|
1617 monitor.write_safe(show.gsub(/^/, "##[MONITOR][#{@id}] "))
1618 monitor.write_safe(sprintf("##[MONITOR][%s] +OK\n", @id))
1623 if (@next_player.status != "game") # rival is logout or disconnected
1625 elsif (status == :timeout)
1627 elsif (move_status == :illegal)
1629 elsif (move_status == :kachi_win)
1631 elsif (move_status == :kachi_lose)
1633 elsif (move_status == :toryo)
1635 elsif (move_status == :outori)
1637 elsif (move_status == :oute_sennichite_sente_lose)
1638 oute_sennichite_win_lose(@gote, @sente) # Sente is checking
1639 elsif (move_status == :oute_sennichite_gote_lose)
1640 oute_sennichite_win_lose(@sente, @gote) # Gote is checking
1641 elsif (move_status == :sennichite)
1643 elsif (move_status == :uchifuzume)
1645 elsif (move_status == :oute_kaihimore)
1646 oute_kaihimore_lose()
1650 finish() if finish_flag
1651 (@current_player, @next_player) = [@next_player, @current_player]
1652 @start_time = Time::new
1658 @current_player.status = "connected"
1659 @next_player.status = "connected"
1660 @current_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
1661 @next_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
1662 @fh.printf("%%TORYO\n")
1663 @fh.print(@board.to_s.gsub(/^/, "\'"))
1664 @fh.printf("'summary:abnormal:%s win:%s lose\n", @current_player.name, @next_player.name)
1665 @result = GameResultWin.new(@current_player, @next_player)
1666 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1667 @monitors.each do |monitor|
1668 monitor.write_safe(sprintf("##[MONITOR][%s] %%TORYO\n", @id))
1673 @current_player.status = "connected"
1674 @next_player.status = "connected"
1675 @current_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
1676 @next_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
1677 @fh.printf("%%TORYO\n")
1678 @fh.print(@board.to_s.gsub(/^/, "\'"))
1679 @fh.printf("'summary:abnormal:%s lose:%s win\n", @current_player.name, @next_player.name)
1680 @result = GameResultWin.new(@next_player, @current_player)
1681 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1682 @monitors.each do |monitor|
1683 monitor.write_safe(sprintf("##[MONITOR][%s] %%TORYO\n", @id))
1688 @current_player.status = "connected"
1689 @next_player.status = "connected"
1690 @current_player.write_safe("#SENNICHITE\n#DRAW\n")
1691 @next_player.write_safe("#SENNICHITE\n#DRAW\n")
1692 @fh.print(@board.to_s.gsub(/^/, "\'"))
1693 @fh.printf("'summary:sennichite:%s draw:%s draw\n", @current_player.name, @next_player.name)
1694 @result = GameResultDraw.new(@current_player, @next_player)
1695 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1696 @monitors.each do |monitor|
1697 monitor.write_safe(sprintf("##[MONITOR][%s] #SENNICHITE\n", @id))
1701 def oute_sennichite_win_lose(winner, loser)
1702 @current_player.status = "connected"
1703 @next_player.status = "connected"
1704 loser.write_safe("#OUTE_SENNICHITE\n#LOSE\n")
1705 winner.write_safe("#OUTE_SENNICHITE\n#WIN\n")
1706 @fh.print(@board.to_s.gsub(/^/, "\'"))
1707 if loser == @current_player
1708 @fh.printf("'summary:oute_sennichite:%s lose:%s win\n", @current_player.name, @next_player.name)
1710 @fh.printf("'summary:oute_sennichite:%s win:%s lose\n", @current_player.name, @next_player.name)
1712 @result = GameResultWin.new(winner, loser)
1713 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1714 @monitors.each do |monitor|
1715 monitor.write_safe(sprintf("##[MONITOR][%s] #OUTE_SENNICHITE\n", @id))
1720 @current_player.status = "connected"
1721 @next_player.status = "connected"
1722 @current_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
1723 @next_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
1724 @fh.print(@board.to_s.gsub(/^/, "\'"))
1725 @fh.printf("'summary:illegal move:%s lose:%s win\n", @current_player.name, @next_player.name)
1726 @result = GameResultWin.new(@next_player, @current_player)
1727 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1728 @monitors.each do |monitor|
1729 monitor.write_safe(sprintf("##[MONITOR][%s] #ILLEGAL_MOVE\n", @id))
1734 @current_player.status = "connected"
1735 @next_player.status = "connected"
1736 @current_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
1737 @next_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
1738 @fh.print(@board.to_s.gsub(/^/, "\'"))
1739 @fh.printf("'summary:uchifuzume:%s lose:%s win\n", @current_player.name, @next_player.name)
1740 @result = GameResultWin.new(@next_player, @current_player)
1741 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1742 @monitors.each do |monitor|
1743 monitor.write_safe(sprintf("##[MONITOR][%s] #ILLEGAL_MOVE\n", @id))
1747 def oute_kaihimore_lose
1748 @current_player.status = "connected"
1749 @next_player.status = "connected"
1750 @current_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
1751 @next_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
1752 @fh.print(@board.to_s.gsub(/^/, "\'"))
1753 @fh.printf("'summary:oute_kaihimore:%s lose:%s win\n", @current_player.name, @next_player.name)
1754 @result = GameResultWin.new(@next_player, @current_player)
1755 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1756 @monitors.each do |monitor|
1757 monitor.write_safe(sprintf("##[MONITOR][%s] #ILLEGAL_MOVE\n", @id))
1762 @current_player.status = "connected"
1763 @next_player.status = "connected"
1764 @current_player.write_safe("#TIME_UP\n#LOSE\n")
1765 @next_player.write_safe("#TIME_UP\n#WIN\n")
1766 @fh.print(@board.to_s.gsub(/^/, "\'"))
1767 @fh.printf("'summary:time up:%s lose:%s win\n", @current_player.name, @next_player.name)
1768 @result = GameResultWin.new(@next_player, @current_player)
1769 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1770 @monitors.each do |monitor|
1771 monitor.write_safe(sprintf("##[MONITOR][%s] #TIME_UP\n", @id))
1776 @current_player.status = "connected"
1777 @next_player.status = "connected"
1778 @current_player.write_safe("%KACHI\n#JISHOGI\n#WIN\n")
1779 @next_player.write_safe("%KACHI\n#JISHOGI\n#LOSE\n")
1780 @fh.printf("%%KACHI\n")
1781 @fh.print(@board.to_s.gsub(/^/, "\'"))
1782 @fh.printf("'summary:kachi:%s win:%s lose\n", @current_player.name, @next_player.name)
1783 @result = GameResultWin.new(@current_player, @next_player)
1784 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1785 @monitors.each do |monitor|
1786 monitor.write_safe(sprintf("##[MONITOR][%s] %%KACHI\n", @id))
1791 @current_player.status = "connected"
1792 @next_player.status = "connected"
1793 @current_player.write_safe("%KACHI\n#ILLEGAL_MOVE\n#LOSE\n")
1794 @next_player.write_safe("%KACHI\n#ILLEGAL_MOVE\n#WIN\n")
1795 @fh.printf("%%KACHI\n")
1796 @fh.print(@board.to_s.gsub(/^/, "\'"))
1797 @fh.printf("'summary:illegal kachi:%s lose:%s win\n", @current_player.name, @next_player.name)
1798 @result = GameResultWin.new(@next_player, @current_player)
1799 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1800 @monitors.each do |monitor|
1801 monitor.write_safe(sprintf("##[MONITOR][%s] %%KACHI\n", @id))
1806 @current_player.status = "connected"
1807 @next_player.status = "connected"
1808 @current_player.write_safe("%TORYO\n#RESIGN\n#LOSE\n")
1809 @next_player.write_safe("%TORYO\n#RESIGN\n#WIN\n")
1810 @fh.printf("%%TORYO\n")
1811 @fh.print(@board.to_s.gsub(/^/, "\'"))
1812 @fh.printf("'summary:toryo:%s lose:%s win\n", @current_player.name, @next_player.name)
1813 @result = GameResultWin.new(@next_player, @current_player)
1814 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1815 @monitors.each do |monitor|
1816 monitor.write_safe(sprintf("##[MONITOR][%s] %%TORYO\n", @id))
1821 @current_player.status = "connected"
1822 @next_player.status = "connected"
1823 @current_player.write_safe("#ILLEGAL_MOVE\n#WIN\n")
1824 @next_player.write_safe("#ILLEGAL_MOVE\n#LOSE\n")
1825 @fh.print(@board.to_s.gsub(/^/, "\'"))
1826 @fh.printf("'summary:outori:%s win:%s lose\n", @current_player.name, @next_player.name)
1827 @result = GameResultWin.new(@current_player, @next_player)
1828 @fh.printf("'rating:#{@result.to_s}\n") if rated?
1829 @monitors.each do |monitor|
1830 monitor.write_safe(sprintf("##[MONITOR][%s] #ILLEGAL_MOVE\n", @id))
1835 log_message(sprintf("game started %s", @id))
1836 @sente.write_safe(sprintf("START:%s\n", @id))
1837 @gote.write_safe(sprintf("START:%s\n", @id))
1838 @sente.mytime = @total_time
1839 @gote.mytime = @total_time
1840 @start_time = Time::new
1845 @fh = open(@logfile, "w")
1849 @fh.printf("N+%s\n", @sente.name)
1850 @fh.printf("N-%s\n", @gote.name)
1851 @fh.printf("$EVENT:%s\n", @id)
1853 @sente.write_safe(propose_message("+"))
1854 @gote.write_safe(propose_message("-"))
1856 @fh.printf("$START_TIME:%s\n", Time::new.strftime("%Y/%m/%d %H:%M:%S"))
1858 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
1859 P2 * -HI * * * * * -KA *
1860 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
1861 P4 * * * * * * * * *
1862 P5 * * * * * * * * *
1863 P6 * * * * * * * * *
1864 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
1865 P8 * +KA * * * * * +HI *
1866 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
1875 Protocol_Version:1.1
1876 Protocol_Mode:Server
1878 Declaration:Jishogi 1.1
1880 Name+:#{@sente.name}
1886 Total_Time:#{@total_time}
1888 Least_Time_Per_Move:#{Least_Time_Per_Move}
1889 Remaining_Time+:#{@sente.mytime}
1890 Remaining_Time-:#{@gote.mytime}
1891 Last_Move:#{@last_move}
1892 Current_Turn:#{@current_turn}
1902 return str0 + @board.to_s + str1
1905 def propose_message(sg_flag)
1908 Protocol_Version:1.1
1909 Protocol_Mode:Server
1911 Declaration:Jishogi 1.1
1913 Name+:#{@sente.name}
1915 Your_Turn:#{sg_flag}
1920 Total_Time:#{@total_time}
1922 Least_Time_Per_Move:#{Least_Time_Per_Move}
1925 P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
1926 P2 * -HI * * * * * -KA *
1927 P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
1928 P4 * * * * * * * * *
1929 P5 * * * * * * * * *
1930 P6 * * * * * * * * *
1931 P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
1932 P8 * +KA * * * * * +HI *
1933 P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
1945 def issue_current_time
1946 time = Time::new.strftime("%Y%m%d%H%M%S").to_i
1947 @@mutex.synchronize do
1948 while time <= @@time do
1955 end # module ShogiServer
1957 #################################################
1964 shogi-server - server for CSA server protocol
1967 shogi-server [OPTIONS] event_name port_number
1970 server for CSA server protocol
1974 specify filename for logging process ID
1976 run as a daemon. Log files will be put in dir.
1979 this file is distributed under GPL version2 and might be compiled by Exerb
1984 #{ShogiServer::Release}
1987 #{ShogiServer::Revision}
1995 def log_message(str)
1999 def log_warning(str)
2008 def parse_command_line
2010 parser = GetoptLong.new( ["--daemon", GetoptLong::REQUIRED_ARGUMENT],
2011 ["--pid-file", GetoptLong::REQUIRED_ARGUMENT]
2015 parser.each_option do |name, arg|
2016 name.sub!(/^--/, '')
2017 options[name] = arg.dup
2021 raise parser.error_message
2026 def write_pid_file(file)
2027 open(file, "w") do |fh|
2028 fh.print Process::pid, "\n"
2032 def mutex_watchdog(mutex, sec)
2044 log_error("mutex watchdog timeout")
2055 mutex_watchdog($mutex, 10)
2058 $options = parse_command_line
2059 if (ARGV.length != 2)
2064 LEAGUE.event = ARGV.shift
2067 write_pid_file($options["pid-file"]) if ($options["pid-file"])
2069 dir = $options["daemon"] || nil
2070 if dir && ! File.exist?(dir)
2071 FileUtils.mkdir(dir)
2073 log_file = dir ? File.join(dir, "shogi-server.log") : STDOUT
2074 $logger = WEBrick::Log.new(log_file)
2076 LEAGUE.dir = dir || File.dirname(__FILE__)
2077 LEAGUE.setup_players_database
2080 config[:Port] = port
2081 config[:ServerType] = WEBrick::Daemon if $options["daemon"]
2082 config[:Logger] = $logger
2084 server = WEBrick::GenericServer.new(config)
2085 ["INT", "TERM"].each {|signal| trap(signal){ server.shutdown } }
2086 $stderr.puts("server started as a deamon [Revision: #{ShogiServer::Revision}]") if $options["daemon"]
2087 log_message("server started [Revision: #{ShogiServer::Revision}]")
2089 server.start do |client|
2090 # client.sync = true # this is already set in WEBrick
2091 client.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
2092 # Keepalive time can be set by /proc/sys/net/ipv4/tcp_keepalive_time
2095 while (str = client.gets_timeout(ShogiServer::Login_Time))
2100 if (ShogiServer::Login::good_login?(str))
2101 player = ShogiServer::Player::new(str, client)
2103 login = ShogiServer::Login::factory(str, player)
2104 if (LEAGUE.players[player.name])
2105 if ((LEAGUE.players[player.name].password == player.password) &&
2106 (LEAGUE.players[player.name].status != "game"))
2107 log_message(sprintf("user %s login forcely", player.name))
2108 LEAGUE.players[player.name].kill
2110 login.incorrect_duplicated_player(str)
2121 client.write_safe("LOGIN:incorrect" + eol)
2122 client.write_safe("type 'LOGIN name password' or 'LOGIN name password x1'" + eol) if (str.split.length >= 4)
2134 log_message(sprintf("user %s login", player.name))
2136 player.run(login.csa_1st_str)
2140 player.game.kill(player)
2142 player.finish # socket has been closed
2143 LEAGUE.delete(player)
2144 log_message(sprintf("user %s logout", player.name))
2155 TCPSocket.do_not_reverse_lookup = true
2156 Thread.abort_on_exception = true
2158 LEAGUE = ShogiServer::League::new