Search in sources :

Example 6 with SOCGame

use of soc.game.SOCGame in project JSettlers2 by jdmonin.

the class SOCRobotClient method handleJOINGAMEAUTH.

/**
 * handle the "join game authorization" message
 * @param mes  the message
 * @param isPractice Is the server local for practice, or remote?
 */
@Override
protected void handleJOINGAMEAUTH(SOCJoinGameAuth mes, final boolean isPractice) {
    gamesPlayed++;
    final String gaName = mes.getGame();
    SOCGame ga = new SOCGame(gaName, true, gameOptions.get(gaName));
    ga.isPractice = isPractice;
    games.put(gaName, ga);
    CappedQueue<SOCMessage> brainQ = new CappedQueue<SOCMessage>();
    brainQs.put(gaName, brainQ);
    SOCRobotBrain rb = createBrain(currentRobotParameters, ga, brainQ);
    robotBrains.put(gaName, rb);
}
Also used : SOCGame(soc.game.SOCGame) CappedQueue(soc.util.CappedQueue)

Example 7 with SOCGame

use of soc.game.SOCGame in project JSettlers2 by jdmonin.

the class SOCRobotClient method handleADMINPING.

/**
 * handle the admin ping message
 * @param mes  the message
 */
protected void handleADMINPING(SOCAdminPing mes) {
    D.ebugPrintln("*** Admin Ping message = " + mes);
    SOCGame ga = games.get(mes.getGame());
    // 
    if (ga != null) {
        sendText(ga, "OK");
    } else {
        put(SOCJoinGame.toCmd(nickname, password, host, mes.getGame()));
    }
}
Also used : SOCGame(soc.game.SOCGame)

Example 8 with SOCGame

use of soc.game.SOCGame in project JSettlers2 by jdmonin.

the class SOCRobotClient method treat.

/**
 * Treat the incoming messages.
 * Messages of unknown type are ignored. All {@link SOCGameServerText} are ignored.
 * ({@code mes} will be null from {@link SOCMessage#toMsg(String)}).
 *<P>
 *<B>Note:</B> Currently, does not call {@link SOCDisplaylessPlayerClient#treat(SOCMessage)}.
 * New message types should be added to both methods if both displayless and robot should handle them.
 * The robot treat's switch case can call super.treat before or after any robot-specific handling.
 *
 * @param mes    the message
 */
@Override
public void treat(SOCMessage mes) {
    if (mes == null)
        // Message syntax error or unknown type
        return;
    // Using debugRandomPause?
    if (debugRandomPause && (!robotBrains.isEmpty()) && (mes instanceof SOCMessageForGame) && !(mes instanceof SOCGameTextMsg) && !(mes instanceof SOCGameServerText) && !(mes instanceof SOCTurn)) {
        final String ga = ((SOCMessageForGame) mes).getGame();
        if (ga != null) {
            SOCRobotBrain brain = robotBrains.get(ga);
            if (brain != null) {
                if (!debugRandomPauseActive) {
                    // random chance of doing so
                    if ((Math.random() < DEBUGRANDOMPAUSE_FREQ) && ((debugRandomPauseQueue == null) || (debugRandomPauseQueue.isEmpty()))) {
                        SOCGame gm = games.get(ga);
                        final int cpn = gm.getCurrentPlayerNumber();
                        SOCPlayer rpl = gm.getPlayer(nickname);
                        if ((rpl != null) && (cpn == rpl.getPlayerNumber()) && (gm.getGameState() >= SOCGame.ROLL_OR_CARD)) {
                            // we're current player, pause us
                            debugRandomPauseActive = true;
                            debugRandomPauseUntil = System.currentTimeMillis() + (1000L * DEBUGRANDOMPAUSE_SECONDS);
                            if (debugRandomPauseQueue == null)
                                debugRandomPauseQueue = new Vector<SOCMessage>();
                            System.err.println("L379 -> do random pause: " + nickname);
                            sendText(gm, "debugRandomPauseActive for " + DEBUGRANDOMPAUSE_SECONDS + " seconds");
                        }
                    }
                }
            }
        }
    }
    if (debugRandomPause && debugRandomPauseActive) {
        if ((System.currentTimeMillis() < debugRandomPauseUntil) && !(mes instanceof SOCTurn)) {
            // time hasn't arrived yet, and still our turn:
            // Add message to queue (even non-game and SOCGameTextMsg)
            debugRandomPauseQueue.addElement(mes);
            // <--- Early return: debugRandomPauseActive ---
            return;
        }
        // time to resume the queue
        debugRandomPauseActive = false;
        while (!debugRandomPauseQueue.isEmpty()) {
            // calling ourself is safe, because
            // ! queue.isEmpty; thus won't decide
            // to set debugRandomPauseActive=true again.
            treat(debugRandomPauseQueue.firstElement());
            debugRandomPauseQueue.removeElementAt(0);
        }
    // Don't return from this method yet,
    // we still need to process mes.
    }
    if ((debugTraffic || D.ebugIsEnabled()) && !((mes instanceof SOCServerPing) && (nextServerPingExpectedAt != 0) && (Math.abs(System.currentTimeMillis() - nextServerPingExpectedAt) <= 66000))) // within 66 seconds of the expected time; see displaylesscli.handleSERVERPING
    {
        soc.debug.D.ebugPrintln("IN - " + nickname + " - " + mes);
    }
    try {
        switch(mes.getType()) {
            /**
             * status message
             */
            case SOCMessage.STATUSMESSAGE:
                handleSTATUSMESSAGE((SOCStatusMessage) mes);
                break;
            /**
             * admin ping
             */
            case SOCMessage.ADMINPING:
                handleADMINPING((SOCAdminPing) mes);
                break;
            /**
             * admin reset
             */
            case SOCMessage.ADMINRESET:
                handleADMINRESET((SOCAdminReset) mes);
                break;
            /**
             * update the current robot parameters
             */
            case SOCMessage.UPDATEROBOTPARAMS:
                handleUPDATEROBOTPARAMS((SOCUpdateRobotParams) mes);
                break;
            /**
             * join game authorization
             */
            case SOCMessage.JOINGAMEAUTH:
                handleJOINGAMEAUTH((SOCJoinGameAuth) mes, (sLocal != null));
                break;
            /**
             * game has been destroyed
             */
            case SOCMessage.DELETEGAME:
                handleDELETEGAME((SOCDeleteGame) mes);
                break;
            /**
             * list of game members
             */
            case SOCMessage.GAMEMEMBERS:
                handleGAMEMEMBERS((SOCGameMembers) mes);
                break;
            /**
             * game text message (bot debug commands)
             */
            case SOCMessage.GAMETEXTMSG:
                handleGAMETEXTMSG((SOCGameTextMsg) mes);
                break;
            /**
             * someone is sitting down
             */
            case SOCMessage.SITDOWN:
                handleSITDOWN((SOCSitDown) mes);
                break;
            /**
             * update the state of the game
             */
            case SOCMessage.GAMESTATE:
                handleGAMESTATE((SOCGameState) mes);
                break;
            /**
             * a player built something
             */
            case SOCMessage.PUTPIECE:
                handlePUTPIECE((SOCPutPiece) mes);
                break;
            /**
             * the server is requesting that we join a game
             */
            case SOCMessage.BOTJOINGAMEREQUEST:
                handleBOTJOINGAMEREQUEST((SOCBotJoinGameRequest) mes);
                break;
            /**
             * message that means the server wants us to leave the game
             */
            case SOCMessage.ROBOTDISMISS:
                handleROBOTDISMISS((SOCRobotDismiss) mes);
                break;
            /**
             * handle board reset (new game with same players, same game name, new layout).
             */
            case SOCMessage.RESETBOARDAUTH:
                handleRESETBOARDAUTH((SOCResetBoardAuth) mes);
                break;
            /**
             * generic "simple request" responses or announcements from the server.
             * Message type added 2013-02-17 for v1.1.18,
             * bot ignored these until 2015-10-10 for v2.0.00 SC_PIRI
             * and for PROMPT_PICK_RESOURCES from gold hex.
             */
            case SOCMessage.SIMPLEREQUEST:
                super.handleSIMPLEREQUEST(games, (SOCSimpleRequest) mes);
                handlePutBrainQ((SOCSimpleRequest) mes);
                break;
            /**
             * generic "simple action" announcements from the server.
             * Added 2013-09-04 for v1.1.19.
             */
            case SOCMessage.SIMPLEACTION:
                super.handleSIMPLEACTION(games, (SOCSimpleAction) mes);
                handlePutBrainQ((SOCSimpleAction) mes);
                break;
            /**
             * a special inventory item action: either add or remove,
             * or we cannot play our requested item.
             * Added 2013-11-26 for v2.0.00.
             */
            case SOCMessage.INVENTORYITEMACTION:
                {
                    final boolean isReject = super.handleINVENTORYITEMACTION(games, (SOCInventoryItemAction) mes);
                    if (isReject)
                        handlePutBrainQ((SOCInventoryItemAction) mes);
                }
                break;
            /**
             * Special Item change announcements.
             * Added 2014-04-16 for v2.0.00.
             */
            case SOCMessage.SETSPECIALITEM:
                super.handleSETSPECIALITEM(games, (SOCSetSpecialItem) mes);
                handlePutBrainQ((SOCSetSpecialItem) mes);
                break;
            case SOCMessage.ACCEPTOFFER:
            // current player has cancelled an initial settlement
            case SOCMessage.CANCELBUILDREQUEST:
            // server wants our player to choose to rob cloth or rob resources from victim
            case SOCMessage.CHOOSEPLAYER:
            case SOCMessage.CHOOSEPLAYERREQUEST:
            case SOCMessage.CLEAROFFER:
            // either draw, play, or add to hand, or cannot play our requested dev card
            case SOCMessage.DEVCARDACTION:
            case SOCMessage.DICERESULT:
            case SOCMessage.DISCARDREQUEST:
            case SOCMessage.BANKTRADE:
            case SOCMessage.MAKEOFFER:
            // move a previously placed ship; will update game data and player trackers
            case SOCMessage.MOVEPIECE:
            case SOCMessage.MOVEROBBER:
            case SOCMessage.PLAYERELEMENT:
            // apply multiple PLAYERELEMENT updates; added 2017-12-10 for v2.0.00
            case SOCMessage.PLAYERELEMENTS:
            case SOCMessage.REJECTOFFER:
            case SOCMessage.RESOURCECOUNT:
            // added 2017-12-18 for v2.0.00 when gameState became a field of this message
            case SOCMessage.STARTGAME:
            // server's 1x/second timing ping
            case SOCMessage.TIMINGPING:
            case SOCMessage.TURN:
                handlePutBrainQ((SOCMessageForGame) mes);
                break;
            case SOCMessage.BCASTTEXTMSG:
            case SOCMessage.CHANGEFACE:
            case SOCMessage.CHANNELMEMBERS:
            // If bot ever uses CHANNELS, update SOCChannels class javadoc
            case SOCMessage.CHANNELS:
            case SOCMessage.CHANNELTEXTMSG:
            case SOCMessage.DELETECHANNEL:
            case SOCMessage.GAMES:
            // SOCGameServerText contents are ignored by bots
            case SOCMessage.GAMESERVERTEXT:
            // (but not SOCGameTextMsg, which is used solely for debug commands)
            case SOCMessage.GAMESTATS:
            case SOCMessage.JOINCHANNEL:
            case SOCMessage.JOINCHANNELAUTH:
            case SOCMessage.LEAVECHANNEL:
            case SOCMessage.NEWCHANNEL:
            case SOCMessage.NEWGAME:
            case SOCMessage.SETSEATLOCK:
                // ignore this message type
                break;
            /**
             * Call SOCDisplaylessClient.treat for all other message types.
             * For types relevant to robots, it will update data from the message contents.
             * Other message types will be ignored.
             */
            default:
                super.treat(mes, true);
        }
    } catch (Throwable e) {
        System.err.println("SOCRobotClient treat ERROR - " + e + " " + e.getMessage());
        e.printStackTrace();
        while (e.getCause() != null) {
            e = e.getCause();
            System.err.println(" -> nested: " + e.getClass());
            e.printStackTrace();
        }
        System.err.println("-- end stacktrace --");
        System.out.println("  For message: " + mes);
    }
}
Also used : SOCPlayer(soc.game.SOCPlayer) SOCGame(soc.game.SOCGame) Vector(java.util.Vector)

Example 9 with SOCGame

use of soc.game.SOCGame in project JSettlers2 by jdmonin.

the class SOCRobotClient method handleSITDOWN.

/**
 * handle the "someone is sitting down" message
 * @param mes  the message
 */
@Override
protected void handleSITDOWN(SOCSitDown mes) {
    /**
     * tell the game that a player is sitting
     */
    SOCGame ga = games.get(mes.getGame());
    if (ga != null) {
        ga.addPlayer(mes.getNickname(), mes.getPlayerNumber());
        /**
         * set the robot flag
         */
        ga.getPlayer(mes.getPlayerNumber()).setRobotFlag(mes.isRobot(), false);
        /**
         * let the robot brain find our player object if we sat down
         */
        if (nickname.equals(mes.getNickname())) {
            SOCRobotBrain brain = robotBrains.get(mes.getGame());
            /**
             * retrieve the proper face for our strategy
             */
            int faceId;
            switch(brain.getRobotParameters().getStrategyType()) {
                case SOCRobotDM.SMART_STRATEGY:
                    // smarter robot face
                    faceId = -1;
                    break;
                default:
                    // default robot face
                    faceId = 0;
            }
            brain.setOurPlayerData();
            brain.start();
            /**
             * change our face to the robot face
             */
            put(SOCChangeFace.toCmd(ga.getName(), mes.getPlayerNumber(), faceId));
        } else {
            /**
             * add tracker for player in previously vacant seat
             */
            SOCRobotBrain brain = robotBrains.get(mes.getGame());
            if (brain != null) {
                brain.addPlayerTracker(mes.getPlayerNumber());
            }
        }
    }
}
Also used : SOCGame(soc.game.SOCGame)

Example 10 with SOCGame

use of soc.game.SOCGame in project JSettlers2 by jdmonin.

the class SOCRobotClient method handleROBOTDISMISS.

/**
 * handle the "dismiss robot" message
 * @param mes  the message
 */
protected void handleROBOTDISMISS(SOCRobotDismiss mes) {
    SOCGame ga = games.get(mes.getGame());
    CappedQueue<SOCMessage> brainQ = brainQs.get(mes.getGame());
    if ((ga != null) && (brainQ != null)) {
        try {
            brainQ.put(mes);
        } catch (CutoffExceededException exc) {
            D.ebugPrintln("CutoffExceededException" + exc);
        }
        /**
         * if the brain isn't alive, then we need to leave
         * the game here, instead of having the brain leave it
         */
        SOCRobotBrain brain = robotBrains.get(mes.getGame());
        if ((brain == null) || (!brain.isAlive())) {
            leaveGame(games.get(mes.getGame()), "brain not alive in handleROBOTDISMISS", true, false);
        }
    }
}
Also used : CutoffExceededException(soc.util.CutoffExceededException) SOCGame(soc.game.SOCGame)

Aggregations

SOCGame (soc.game.SOCGame)60 SOCPlayer (soc.game.SOCPlayer)17 Connection (soc.server.genericServer.Connection)7 SOCBoardLarge (soc.game.SOCBoardLarge)5 MissingResourceException (java.util.MissingResourceException)3 Vector (java.util.Vector)3 SOCBoard (soc.game.SOCBoard)3 StringConnection (soc.server.genericServer.StringConnection)3 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 SOCResourceSet (soc.game.SOCResourceSet)2 SOCShip (soc.game.SOCShip)2 SOCTradeOffer (soc.game.SOCTradeOffer)2 SOCVillage (soc.game.SOCVillage)2 SOCGameBoardReset (soc.util.SOCGameBoardReset)2 Color (java.awt.Color)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1