Search in sources :

Example 1 with StringConnection

use of soc.server.genericServer.StringConnection in project JSettlers2 by jdmonin.

the class SOCServerMessageHandler method handleGAMETEXTMSG.

/**
 * Handle game text messages, including debug commands.
 * Was part of SOCServer.processCommand before 1.1.07.
 *<P>
 * Some commands are unprivileged and can be run by any client:
 *<UL>
 * <LI> *ADDTIME*
 * <LI> *CHECKTIME*
 * <LI> *VERSION*
 * <LI> *STATS*
 * <LI> *WHO*
 *</UL>
 * These commands are processed in this method.
 * Others can be run only by certain users or when certain server flags are set.
 * Those are processed in {@link SOCServer#processDebugCommand(Connection, String, String, String)}.
 *
 * @since 1.1.07
 */
void handleGAMETEXTMSG(Connection c, SOCGameTextMsg gameTextMsgMes) {
    // createNewGameEventRecord();
    // currentGameEventRecord.setMessageIn(new SOCMessageRecord(mes, c.getData(), "SERVER"));
    final String gaName = gameTextMsgMes.getGame();
    srv.recordGameEvent(gaName, gameTextMsgMes);
    SOCGame ga = gameList.getGameData(gaName);
    if (ga == null)
        // <---- early return: no game by that name ----
        return;
    final String plName = c.getData();
    if (null == ga.getPlayer(plName)) {
        // c isn't a seated player in that game; have they joined it?
        // To avoid disruptions by game observers, only players can chat after initial placement.
        // To help form the game, non-seated members can also participate in the chat until then.
        final boolean canChat = (ga.getGameState() < SOCGame.ROLL_OR_CARD) && gameList.isMember(c, gaName);
        if (!canChat) {
            // "Observers can't chat during the game."
            srv.messageToPlayerKeyed(c, gaName, "member.chat.not_observers");
            // <---- early return: not a player in that game ----
            return;
        }
    }
    // currentGameEventRecord.setSnapshot(ga);
    final String cmdText = gameTextMsgMes.getText();
    final String cmdTxtUC = cmdText.toUpperCase();
    // /
    if (cmdTxtUC.startsWith("*ADDTIME*") || cmdTxtUC.startsWith("ADDTIME")) {
        if (ga.isPractice) {
            // ">>> Practice games never expire."
            srv.messageToPlayerKeyed(c, gaName, "reply.addtime.practice.never");
        } else if (ga.getGameState() >= SOCGame.OVER) {
            // "This game is over, cannot extend its time."
            srv.messageToPlayerKeyed(c, gaName, "reply.addtime.game_over");
        } else {
            // check game time currently remaining: if already more than
            // the original GAME_TIME_EXPIRE_MINUTES + GAME_TIME_EXPIRE_ADDTIME_MINUTES,
            // don't add more now.
            final long now = System.currentTimeMillis();
            long exp = ga.getExpiration();
            int minRemain = (int) ((exp - now) / (60 * 1000));
            final int gameMaxMins = SOCGameListAtServer.GAME_TIME_EXPIRE_MINUTES + SOCServer.GAME_TIME_EXPIRE_ADDTIME_MINUTES;
            if (minRemain > gameMaxMins - 4) {
                srv.messageToPlayerKeyed(c, gaName, "reply.addtime.not_expire_soon", Integer.valueOf(minRemain));
            // "Ask again later: This game does not expire soon, it has {0} minutes remaining."
            // This check time subtracts 4 minutes to keep too-frequent addtime requests
            // from spamming all game members with announcements
            } else {
                int minAdd = SOCServer.GAME_TIME_EXPIRE_ADDTIME_MINUTES;
                if (minRemain + minAdd > gameMaxMins)
                    minAdd = gameMaxMins - minRemain;
                exp += (minAdd * 60 * 1000);
                minRemain += minAdd;
                ga.setExpiration(exp);
                // ">>> Game time has been extended."
                srv.messageToGameKeyed(ga, true, "reply.addtime.extended");
                srv.messageToGameKeyed(ga, true, "stats.game.willexpire.urgent", Integer.valueOf(minRemain));
            // ">>> This game will expire in 45 minutes."
            }
        }
    } else // /
    if (cmdTxtUC.startsWith("*CHECKTIME*")) {
        processDebugCommand_gameStats(c, gaName, ga, true);
    } else if (cmdTxtUC.startsWith("*VERSION*")) {
        srv.messageToPlayer(c, gaName, "Java Settlers Server " + Version.versionNumber() + " (" + Version.version() + ") build " + Version.buildnum());
    } else if (cmdTxtUC.startsWith("*STATS*")) {
        srv.processDebugCommand_serverStats(c, ga);
    } else if (cmdTxtUC.startsWith("*WHO*")) {
        processDebugCommand_who(c, ga, cmdText);
    } else if (cmdTxtUC.startsWith("*DBSETTINGS*")) {
        processDebugCommand_dbSettings(c, ga);
    } else // 
    // check for admin/debugging commands
    // 
    // 1.1.07: all practice games are debug mode, for ease of debugging;
    // not much use for a chat window in a practice game anyway.
    // 
    {
        final boolean userIsDebug = ((srv.isDebugUserEnabled() && plName.equals("debug")) || (c instanceof StringConnection));
        if (cmdTxtUC.startsWith("*HELP")) {
            for (int i = 0; i < SOCServer.GENERAL_COMMANDS_HELP.length; ++i) srv.messageToPlayer(c, gaName, SOCServer.GENERAL_COMMANDS_HELP[i]);
            if (// no user admins in practice games
            (userIsDebug && !(c instanceof StringConnection)) || srv.isUserDBUserAdmin(plName)) {
                srv.messageToPlayer(c, gaName, SOCServer.ADMIN_COMMANDS_HEADING);
                for (int i = 0; i < SOCServer.ADMIN_USER_COMMANDS_HELP.length; ++i) srv.messageToPlayer(c, gaName, SOCServer.ADMIN_USER_COMMANDS_HELP[i]);
            }
            if (userIsDebug) {
                for (int i = 0; i < SOCServer.DEBUG_COMMANDS_HELP.length; ++i) srv.messageToPlayer(c, gaName, SOCServer.DEBUG_COMMANDS_HELP[i]);
                GameHandler hand = gameList.getGameTypeHandler(gaName);
                if (hand != null) {
                    final String[] GAMETYPE_DEBUG_HELP = hand.getDebugCommandsHelp();
                    if (GAMETYPE_DEBUG_HELP != null)
                        for (int i = 0; i < GAMETYPE_DEBUG_HELP.length; ++i) srv.messageToPlayer(c, gaName, GAMETYPE_DEBUG_HELP[i]);
                }
            }
        } else {
            boolean isCmd = userIsDebug && srv.processDebugCommand(c, ga.getName(), cmdText, cmdTxtUC);
            if (!isCmd)
                // 
                // Send the message to the members of the game
                // 
                srv.messageToGame(gaName, new SOCGameTextMsg(gaName, plName, cmdText));
        }
    }
// saveCurrentGameEventRecord(gameTextMsgMes.getGame());
}
Also used : StringConnection(soc.server.genericServer.StringConnection) SOCGame(soc.game.SOCGame)

Aggregations

SOCGame (soc.game.SOCGame)1 StringConnection (soc.server.genericServer.StringConnection)1