Search in sources :

Example 36 with SOCGame

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

the class SOCRobotClient method handleDELETEGAME.

/**
 * handle the "delete game" message
 * @param mes  the message
 */
@Override
protected void handleDELETEGAME(SOCDeleteGame mes) {
    SOCRobotBrain brain = robotBrains.get(mes.getGame());
    if (brain != null) {
        SOCGame ga = games.get(mes.getGame());
        if (ga != null) {
            if (ga.getGameState() == SOCGame.OVER) {
                gamesFinished++;
                if (ga.getPlayer(nickname).getTotalVP() >= ga.vp_winner) {
                    gamesWon++;
                // TODO: should check actual winning player number (getCurrentPlayerNumber?)
                }
            }
            brain.kill();
            robotBrains.remove(mes.getGame());
            brainQs.remove(mes.getGame());
            games.remove(mes.getGame());
        }
    }
}
Also used : SOCGame(soc.game.SOCGame)

Example 37 with SOCGame

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

the class SOCRobotClient method handleGAMETEXTMSG_debug.

/**
 * Handle debug text messages from players to the robot, which start with
 * the robot's nickname + ":".
 * @since 1.1.12
 */
private final void handleGAMETEXTMSG_debug(SOCGameTextMsg mes) {
    final int nL = nickname.length();
    try {
        if (mes.getText().charAt(nL) != ':')
            return;
    } catch (IndexOutOfBoundsException e) {
        return;
    }
    final String dcmd = mes.getText().substring(nL);
    if (dcmd.startsWith(":debug-off")) {
        SOCGame ga = games.get(mes.getGame());
        SOCRobotBrain brain = robotBrains.get(mes.getGame());
        if (brain != null) {
            brain.turnOffDRecorder();
            sendText(ga, "Debug mode OFF");
        }
    } else if (dcmd.startsWith(":debug-on")) {
        SOCGame ga = games.get(mes.getGame());
        SOCRobotBrain brain = robotBrains.get(mes.getGame());
        if (brain != null) {
            brain.turnOnDRecorder();
            sendText(ga, "Debug mode ON");
        }
    } else if (dcmd.startsWith(":current-plans") || dcmd.startsWith(":cp")) {
        SOCGame ga = games.get(mes.getGame());
        SOCRobotBrain brain = robotBrains.get(mes.getGame());
        if ((brain != null) && (brain.getDRecorder().isOn())) {
            sendRecordsText(ga, brain.getDRecorder().getRecord(CURRENT_PLANS));
        }
    } else if (dcmd.startsWith(":current-resources") || dcmd.startsWith(":cr")) {
        SOCGame ga = games.get(mes.getGame());
        SOCRobotBrain brain = robotBrains.get(mes.getGame());
        if ((brain != null) && (brain.getDRecorder().isOn())) {
            sendRecordsText(ga, brain.getDRecorder().getRecord(CURRENT_RESOURCES));
        }
    } else if (dcmd.startsWith(":last-plans") || dcmd.startsWith(":lp")) {
        SOCRobotBrain brain = robotBrains.get(mes.getGame());
        if ((brain != null) && (brain.getDRecorder().isOn())) {
            Vector<String> record = brain.getOldDRecorder().getRecord(CURRENT_PLANS);
            if (record != null) {
                SOCGame ga = games.get(mes.getGame());
                sendRecordsText(ga, record);
            }
        }
    } else if (dcmd.startsWith(":last-resources") || dcmd.startsWith(":lr")) {
        SOCRobotBrain brain = robotBrains.get(mes.getGame());
        if ((brain != null) && (brain.getDRecorder().isOn())) {
            Vector<String> record = brain.getOldDRecorder().getRecord(CURRENT_RESOURCES);
            if (record != null) {
                SOCGame ga = games.get(mes.getGame());
                sendRecordsText(ga, record);
            }
        }
    } else if (dcmd.startsWith(":last-move") || dcmd.startsWith(":lm")) {
        SOCRobotBrain brain = robotBrains.get(mes.getGame());
        if ((brain != null) && (brain.getOldDRecorder().isOn())) {
            SOCPossiblePiece lastMove = brain.getLastMove();
            if (lastMove != null) {
                String key = null;
                switch(lastMove.getType()) {
                    case SOCPossiblePiece.CARD:
                        key = "DEVCARD";
                        break;
                    case SOCPossiblePiece.ROAD:
                        key = "ROAD" + lastMove.getCoordinates();
                        break;
                    case SOCPossiblePiece.SETTLEMENT:
                        key = "SETTLEMENT" + lastMove.getCoordinates();
                        break;
                    case SOCPossiblePiece.CITY:
                        key = "CITY" + lastMove.getCoordinates();
                        break;
                    case SOCPossiblePiece.SHIP:
                        key = "SHIP" + lastMove.getCoordinates();
                        break;
                }
                Vector<String> record = brain.getOldDRecorder().getRecord(key);
                if (record != null) {
                    SOCGame ga = games.get(mes.getGame());
                    sendRecordsText(ga, record);
                }
            }
        }
    } else if (dcmd.startsWith(":consider-move ") || dcmd.startsWith(":cm ")) {
        SOCRobotBrain brain = robotBrains.get(mes.getGame());
        if ((brain != null) && (brain.getOldDRecorder().isOn())) {
            String[] tokens = mes.getText().split(" ");
            String key = null;
            if (tokens[1].trim().equals("card")) {
                key = "DEVCARD";
            } else if (tokens[1].equals("road")) {
                key = "ROAD" + tokens[2].trim();
            } else if (tokens[1].equals("settlement")) {
                key = "SETTLEMENT" + tokens[2].trim();
            } else if (tokens[1].equals("city")) {
                key = "CITY" + tokens[2].trim();
            }
            Vector<String> record = brain.getOldDRecorder().getRecord(key);
            if (record != null) {
                SOCGame ga = games.get(mes.getGame());
                sendRecordsText(ga, record);
            }
        }
    } else if (dcmd.startsWith(":last-target") || dcmd.startsWith(":lt")) {
        SOCRobotBrain brain = robotBrains.get(mes.getGame());
        if ((brain != null) && (brain.getDRecorder().isOn())) {
            SOCPossiblePiece lastTarget = brain.getLastTarget();
            if (lastTarget != null) {
                String key = null;
                switch(lastTarget.getType()) {
                    case SOCPossiblePiece.CARD:
                        key = "DEVCARD";
                        break;
                    case SOCPossiblePiece.ROAD:
                        key = "ROAD" + lastTarget.getCoordinates();
                        break;
                    case SOCPossiblePiece.SETTLEMENT:
                        key = "SETTLEMENT" + lastTarget.getCoordinates();
                        break;
                    case SOCPossiblePiece.CITY:
                        key = "CITY" + lastTarget.getCoordinates();
                        break;
                    case SOCPossiblePiece.SHIP:
                        key = "SHIP" + lastTarget.getCoordinates();
                        break;
                }
                Vector<String> record = brain.getDRecorder().getRecord(key);
                if (record != null) {
                    SOCGame ga = games.get(mes.getGame());
                    sendRecordsText(ga, record);
                }
            }
        }
    } else if (dcmd.startsWith(":consider-target ") || dcmd.startsWith(":ct ")) {
        SOCRobotBrain brain = robotBrains.get(mes.getGame());
        if ((brain != null) && (brain.getDRecorder().isOn())) {
            String[] tokens = mes.getText().split(" ");
            String key = null;
            if (tokens[1].trim().equals("card")) {
                key = "DEVCARD";
            } else if (tokens[1].equals("road")) {
                key = "ROAD" + tokens[2].trim();
            } else if (tokens[1].equals("settlement")) {
                key = "SETTLEMENT" + tokens[2].trim();
            } else if (tokens[1].equals("city")) {
                key = "CITY" + tokens[2].trim();
            }
            Vector<String> record = brain.getDRecorder().getRecord(key);
            if (record != null) {
                SOCGame ga = games.get(mes.getGame());
                sendRecordsText(ga, record);
            }
        }
    } else if (dcmd.startsWith(":print-vars") || dcmd.startsWith(":pv")) {
        // "prints" the results as series of SOCGameTextMsg to game
        debugPrintBrainStatus(mes.getGame(), true);
    } else if (dcmd.startsWith(":stats")) {
        SOCGame ga = games.get(mes.getGame());
        sendText(ga, "Games played:" + gamesPlayed);
        sendText(ga, "Games finished:" + gamesFinished);
        sendText(ga, "Games won:" + gamesWon);
        sendText(ga, "Clean brain kills:" + cleanBrainKills);
        sendText(ga, "Brains running: " + robotBrains.size());
        Runtime rt = Runtime.getRuntime();
        sendText(ga, "Total Memory:" + rt.totalMemory());
        sendText(ga, "Free Memory:" + rt.freeMemory());
    } else if (dcmd.startsWith(":gc")) {
        SOCGame ga = games.get(mes.getGame());
        Runtime rt = Runtime.getRuntime();
        rt.gc();
        sendText(ga, "Free Memory:" + rt.freeMemory());
    }
}
Also used : SOCGame(soc.game.SOCGame) Vector(java.util.Vector)

Example 38 with SOCGame

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

the class SOCSpecialItemDialog method actionPerformed.

/**
 * A button was clicked to choose a special item such as a Wonder.
 * Find the right {@link #buttons}[i] and send the server a pick-item command.
 *
 * @param e AWT event, from a button source
 */
public void actionPerformed(ActionEvent e) {
    try {
        final Object src = e.getSource();
        if (src == null)
            return;
        if (src == bClose) {
            nbddListenerCalled = true;
            dispose();
            if (nbddListener != null)
                EventQueue.invokeLater(new Runnable() {

                    public void run() {
                        nbddListener.dialogDismissed(SOCSpecialItemDialog.this, true);
                    }
                });
            return;
        }
        int i;
        for (i = 0; i < buttons.length; ++i) {
            if (src == buttons[i])
                break;
        }
        if (i < buttons.length) {
            // assumes typeKey == _SC_WOND -> always sends PICK
            // or (6-player) asks for Special Building Phase on
            // other players' turns. Eventually other typeKeys
            // may allow other actions besides PICK, or actions
            // during other players' turns.
            final SOCGame ga = pi.getGame();
            final SOCPlayerClient.GameManager gm = pi.getClient().getGameManager();
            boolean askedSBP = false;
            if (!pi.clientIsCurrentPlayer()) {
                final int cpn = pi.getClientPlayerNumber();
                if ((cpn != -1) && ga.canAskSpecialBuild(cpn, false)) {
                    // Can't build on other players' turns, but can request SBP.
                    // Consistent with what happens when clicking Buy for a road,
                    // city, etc on another player's turn in 6-player game.
                    gm.buildRequest(ga, -1);
                    askedSBP = true;
                }
            // else: Fall through, send PICK request, server will
            // send feedback it can't be built right now: That way
            // this dialog's feedback is consistently delivered.
            }
            if (!askedSBP)
                gm.pickSpecialItem(ga, typeKey, 1 + i, 0);
            nbddListenerCalled = true;
            dispose();
            if (nbddListener != null)
                EventQueue.invokeLater(new Runnable() {

                    public void run() {
                        nbddListener.dialogDismissed(SOCSpecialItemDialog.this, false);
                    }
                });
        }
    } catch (Exception ex) {
        pi.chatPrintStackTrace(ex);
    }
}
Also used : SOCGame(soc.game.SOCGame) MissingResourceException(java.util.MissingResourceException)

Example 39 with SOCGame

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

the class SOCDisplaylessPlayerClient method handleTURN.

/**
 * handle the "turn" message
 * @param mes  the message
 */
protected void handleTURN(SOCTurn mes) {
    SOCGame ga = games.get(mes.getGame());
    if (ga == null)
        return;
    handleGAMESTATE(ga, mes.getGameState());
    ga.setCurrentPlayerNumber(mes.getPlayerNumber());
    ga.updateAtTurn();
}
Also used : SOCGame(soc.game.SOCGame)

Example 40 with SOCGame

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

the class SOCDisplaylessPlayerClient method handleCANCELBUILDREQUEST.

/**
 * handle the rare "cancel build request" message; usually not sent from
 * server to client.
 *<P>
 * - When sent from client to server, CANCELBUILDREQUEST means the player has changed
 *   their mind about spending resources to build a piece.  Only allowed during normal
 *   game play (PLACING_ROAD, PLACING_SETTLEMENT, or PLACING_CITY).
 *<P>
 *  When sent from server to client:
 *<P>
 * - During game startup (START1B or START2B): <BR>
 *       Sent from server, CANCELBUILDREQUEST means the current player
 *       wants to undo the placement of their initial settlement.
 *       This handler method calls <tt>{@link SOCGame#undoPutInitSettlement(SOCPlayingPiece) ga.undoPutInitSettlement}
 *       (new SOCSettlement {@link SOCPlayer#getLastSettlementCoord() (currPlayer.getLastSettlementCoord())})</tt>.
 *<P>
 * - During piece placement (PLACING_ROAD, PLACING_CITY, PLACING_SETTLEMENT,
 *                           PLACING_FREE_ROAD1 or PLACING_FREE_ROAD2):
 *<P>
 *      Sent from server, CANCELBUILDREQUEST means the player has sent
 *      an illegal PUTPIECE (bad building location). Humans can probably
 *      decide a better place to put their road, but robots must cancel
 *      the build request and decide on a new plan.
 *<P>
 *      Our client can ignore this case, because the server also sends a text
 *      message that the human player is capable of reading and acting on.
 *
 * @param mes  the message
 * @since 1.1.00
 */
protected void handleCANCELBUILDREQUEST(SOCCancelBuildRequest mes) {
    SOCGame ga = games.get(mes.getGame());
    if (ga == null)
        return;
    final int sta = ga.getGameState();
    if ((sta != SOCGame.START1B) && (sta != SOCGame.START2B) && (sta != SOCGame.START3B)) {
        // The robot player will override this method and react.
        return;
    }
    if (mes.getPieceType() != SOCPlayingPiece.SETTLEMENT)
        return;
    SOCPlayer pl = ga.getPlayer(ga.getCurrentPlayerNumber());
    SOCSettlement pp = new SOCSettlement(pl, pl.getLastSettlementCoord(), null);
    ga.undoPutInitSettlement(pp);
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCPlayer(soc.game.SOCPlayer) 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