- case str
- when ""
- # Application-level protocol for Keep-Alive
- # If the server gets LF, it sends back LF.
- # 30 sec rule (client may not send LF again within 30 sec) is not implemented yet.
- write_safe("\n")
- when /^[\+\-][^%]/
- if (@status == "game")
- array_str = str.split(",")
- move = array_str.shift
- additional = array_str.shift
- if /^'(.*)/ =~ additional
- comment = array_str.unshift("'*#{$1.toeuc}")
- end
- s = @game.handle_one_move(move, self)
- @game.fh.print("#{Kconv.toeuc(comment.first)}\n") if (comment && comment.first && !s)
- return if (s && @protocol == LoginCSA::PROTOCOL)
- end
- when /^%[^%]/, :timeout
- if (@status == "game")
- s = @game.handle_one_move(str, self)
- return if (s && @protocol == LoginCSA::PROTOCOL)
- end
- when :exception
- log_error("Failed to receive a message from #{@name}.")
- return
- when /^REJECT/
- if (@status == "agree_waiting")
- @game.reject(@name)
- return if (@protocol == LoginCSA::PROTOCOL)
- else
- write_safe(sprintf("##[ERROR] you are in %s status. AGREE is valid in agree_waiting status\n", @status))
- end
- when /^AGREE/
- if (@status == "agree_waiting")
- @status = "start_waiting"
- if ((@game.sente.status == "start_waiting") &&
- (@game.gote.status == "start_waiting"))
- @game.start
- @game.sente.status = "game"
- @game.gote.status = "game"
- end
- else
- write_safe(sprintf("##[ERROR] you are in %s status. AGREE is valid in agree_waiting status\n", @status))
- end
- when /^%%SHOW\s+(\S+)/
- game_id = $1
- if ($league.games[game_id])
- write_safe($league.games[game_id].show.gsub(/^/, '##[SHOW] '))
- end
- write_safe("##[SHOW] +OK\n")
- when /^%%MONITORON\s+(\S+)/
- game_id = $1
- if ($league.games[game_id])
- $league.games[game_id].monitoron(self)
- write_safe($league.games[game_id].show.gsub(/^/, "##[MONITOR][#{game_id}] "))
- write_safe("##[MONITOR][#{game_id}] +OK\n")
- end
- when /^%%MONITOROFF\s+(\S+)/
- game_id = $1
- if ($league.games[game_id])
- $league.games[game_id].monitoroff(self)
- end
- when /^%%HELP/
- write_safe(
- %!##[HELP] available commands "%%WHO", "%%CHAT str", "%%GAME game_name +", "%%GAME game_name -"\n!)
- when /^%%RATING/
- players = $league.rated_players
- players.sort {|a,b| b.rate <=> a.rate}.each do |p|
- write_safe("##[RATING] %s \t %4d @%s\n" %
- [p.simple_player_id, p.rate, p.modified_at.strftime("%Y-%m-%d")])
- end
- write_safe("##[RATING] +OK\n")
- when /^%%VERSION/
- write_safe "##[VERSION] Shogi Server revision #{Revision}\n"
- write_safe("##[VERSION] +OK\n")
- when /^%%GAME\s*$/
- if ((@status == "connected") || (@status == "game_waiting"))
- @status = "connected"
- @game_name = ""
- else
- write_safe(sprintf("##[ERROR] you are in %s status. GAME is valid in connected or game_waiting status\n", @status))
- end
- when /^%%(GAME|CHALLENGE)\s+(\S+)\s+([\+\-\*])\s*$/
- command_name = $1
- game_name = $2
- my_sente_str = $3
- if (! Login::good_game_name?(game_name))
- write_safe(sprintf("##[ERROR] bad game name\n"))
- next
- elsif ((@status == "connected") || (@status == "game_waiting"))
- ## continue
- else
- write_safe(sprintf("##[ERROR] you are in %s status. GAME is valid in connected or game_waiting status\n", @status))
- next
- end