Search in sources :

Example 56 with SOCGame

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

the class SOCServerMessageHandler method handleSETSEATLOCK.

/**
 * handle "set seat lock" message.
 *
 * @param c  the connection
 * @param mes  the message
 * @since 1.0.0
 */
private void handleSETSEATLOCK(Connection c, final SOCSetSeatLock mes) {
    final SOCGame.SeatLockState sl = mes.getLockState();
    final String gaName = mes.getGame();
    SOCGame ga = gameList.getGameData(gaName);
    if (ga == null)
        return;
    SOCPlayer player = ga.getPlayer(c.getData());
    if (player == null)
        return;
    try {
        final int pn = mes.getPlayerNumber();
        ga.setSeatLock(pn, sl);
        if ((sl != SOCGame.SeatLockState.CLEAR_ON_RESET) || (ga.clientVersionLowest >= 2000)) {
            srv.messageToGame(gaName, mes);
        } else {
            // older clients won't recognize that lock state
            srv.messageToGameForVersions(ga, 2000, Integer.MAX_VALUE, mes, true);
            srv.messageToGameForVersions(ga, -1, 1999, new SOCSetSeatLock(gaName, pn, SOCGame.SeatLockState.LOCKED), true);
        }
    } catch (IllegalStateException e) {
        // "Cannot set that lock right now."
        srv.messageToPlayerKeyed(c, gaName, "reply.lock.cannot");
    }
}
Also used : SOCPlayer(soc.game.SOCPlayer) SOCGame(soc.game.SOCGame)

Example 57 with SOCGame

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

the class TestBoardLayouts method testSingleLayout.

/**
 * Test one scenario's layout for a given number of players.
 * @param sc  Scenario to test, or {@code null} for classic games
 * @param pl  Number of players (3, 4, 6, etc)
 * @return  True if OK, false if construction failed
 */
public final boolean testSingleLayout(final SOCScenario sc, final int pl) {
    final Map<String, SOCGameOption> gaOpts = SOCGameOption.parseOptionsToMap("PL=" + pl + ((sc != null) ? ",SC=" + sc.key : ""));
    if (gaOpts != null)
        assertNull("Unexpected problems with scenario options", SOCGameOption.adjustOptionsToKnown(gaOpts, null, true));
    // this same pre-check is done by TestScenarioOpts.testAllScenarios()
    final String gaName = ((sc != null) ? sc.key : "classic") + ":" + pl;
    gl.createGame(gaName, "test", "en_US", gaOpts, sgh);
    final SOCGame ga = gl.getGameData(gaName);
    assertNotNull("Game not created", ga);
    // which has a reminder comment to keep sync'd with this test method
    try {
        ga.addPlayer("player", 1);
        // SOCBoard/SOCBoardAtServer.makeNewBoard is called here
        ga.startGame();
        gl.deleteGame(gaName);
    } catch (Exception e) {
        System.err.println("Error at board setup: " + gaName + ", " + pl + " players:");
        e.printStackTrace();
        return false;
    }
    return true;
}
Also used : SOCGameOption(soc.game.SOCGameOption) SOCGame(soc.game.SOCGame)

Example 58 with SOCGame

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

the class SOCHandPanel method actionPerformed.

/**
 * handle interaction
 */
public void actionPerformed(ActionEvent e) {
    try {
        String target = e.getActionCommand();
        SOCPlayerClient client = playerInterface.getClient();
        SOCGame game = playerInterface.getGame();
        if (target == LOCKSEAT) {
            // Seat Lock while game forming (gamestate NEW); see below for ROBOTLOCKBUT_L etc
            client.getGameManager().setSeatLock(game, playerNumber, SOCGame.SeatLockState.LOCKED);
        } else if (target == UNLOCKSEAT) {
            // Unlock while game forming
            client.getGameManager().setSeatLock(game, playerNumber, SOCGame.SeatLockState.UNLOCKED);
        } else if (target == TAKEOVER) {
            client.getGameManager().sitDown(game, playerNumber);
        } else if (target == SIT) {
            client.getGameManager().sitDown(game, playerNumber);
        } else if ((target == START) && startBut.isVisible()) {
            client.getGameManager().startGame(game);
        // checks isVisible to guard against button action from hitting spacebar
        // when hidden but has focus because startBut is the first button added to panel;
        // this bug seen on OSX 10.9.1 (1.5.0 JVM)
        } else if (target == ROBOT) {
        // cf.cc.addRobot(cf.cname, playerNum);
        } else if (target == ROLL) {
            if (autoRollTimerTask != null) {
                autoRollTimerTask.cancel();
                autoRollTimerTask = null;
            }
            clickRollButton();
        } else if (target == QUIT) {
            SOCQuitConfirmDialog.createAndShow(playerInterface.getGameDisplay(), playerInterface);
        } else if (target == DONE) {
            // sqPanel.setValues(zero, zero);
            client.getGameManager().endTurn(game);
        } else if (target == DONE_RESTART) {
            playerInterface.resetBoardRequest(game.isPractice && !game.isInitialPlacement());
        } else if (target == CLEAR) {
            // Zero the square panel numbers, unless board-reset vote in progress
            clearOffer(true);
            if (game.getGameState() == SOCGame.PLAY1) {
                client.getGameManager().clearOffer(game);
            }
        } else if (target == BANK) {
            int gstate = game.getGameState();
            if (gstate == SOCGame.PLAY1) {
                int[] give = new int[5];
                int[] get = new int[5];
                sqPanel.getValues(give, get);
                client.getGameManager().clearOffer(game);
                createSendBankTradeRequest(game, give, get);
            } else if (gstate == SOCGame.OVER) {
                String msg = game.gameOverMessageToPlayer(player);
                // msg = "The game is over; you are the winner!";
                // msg = "The game is over; <someone> won.";
                // msg = "The game is over; no one won.";
                playerInterface.print("* " + msg);
            }
        } else if (target == BANK_UNDO) {
            if ((bankGive != null) && (bankGet != null)) {
                // reverse the previous order to undo it
                client.getGameManager().bankTrade(game, bankGet, bankGive);
                bankGive = null;
                bankGet = null;
                bankUndoBut.setEnabled(false);
            }
        } else if (target == ROBOTLOCKBUT_L) {
            // Seat Lock while game in progress; see above for UNLOCKSEAT etc
            clickRobotSeatLockButton(SOCGame.SeatLockState.LOCKED);
        } else if (target == ROBOTLOCKBUT_U) {
            clickRobotSeatLockButton(SOCGame.SeatLockState.UNLOCKED);
        } else if (target == ROBOTLOCKBUT_M) {
            clickRobotSeatLockButton(SOCGame.SeatLockState.CLEAR_ON_RESET);
        } else if (target == SEND) {
            if (playerTradingDisabled)
                return;
            if (game.getGameState() == SOCGame.PLAY1) {
                int[] give = new int[5];
                int[] get = new int[5];
                int giveSum = 0;
                int getSum = 0;
                sqPanel.getValues(give, get);
                for (int i = 0; i < 5; i++) {
                    giveSum += give[i];
                    getSum += get[i];
                }
                SOCResourceSet giveSet = new SOCResourceSet(give);
                SOCResourceSet getSet = new SOCResourceSet(get);
                if (!player.getResources().contains(giveSet)) {
                    playerInterface.print("*** " + strings.get("hpan.trade.msg.donthave"));
                // "You can't offer what you don't have."
                } else if ((giveSum == 0) || (getSum == 0)) {
                    playerInterface.print("*** " + strings.get("hpan.trade.msg.eachplayer"));
                // "A trade must contain at least one resource from each player."
                } else {
                    // bool array elements begin as false
                    boolean[] to = new boolean[game.maxPlayers];
                    boolean toAny = false;
                    if (game.getCurrentPlayerNumber() == playerNumber) {
                        for (int i = 0; i < (game.maxPlayers - 1); i++) {
                            if (playerSend[i].getBoolValue() && !game.isSeatVacant(playerSendMap[i])) {
                                to[playerSendMap[i]] = true;
                                toAny = true;
                                playerSendForPrevTrade[i] = true;
                            } else {
                                playerSendForPrevTrade[i] = false;
                            }
                        }
                    } else {
                        // can only offer to current player
                        to[game.getCurrentPlayerNumber()] = true;
                        toAny = true;
                    }
                    if (!toAny) {
                        playerInterface.print("*** " + strings.get("hpan.trade.msg.chooseoppo"));
                    // "Choose at least one opponent's checkbox."
                    } else {
                        SOCTradeOffer tradeOffer = new SOCTradeOffer(game.getName(), playerNumber, to, giveSet, getSet);
                        client.getGameManager().offerTrade(game, tradeOffer);
                        disableBankUndoButton();
                    }
                }
            } else {
                getPlayerInterface().print("* " + strings.get("hpan.trade.msg.notnow") + "\n");
            // "You cannot trade at this time."
            }
        } else if ((e.getSource() == inventory) || (e.getSource() == playCardBut)) {
            clickPlayCardButton();
        }
    } catch (Throwable th) {
        playerInterface.chatPrintStackTrace(th);
    }
}
Also used : SOCTradeOffer(soc.game.SOCTradeOffer) SOCResourceSet(soc.game.SOCResourceSet) SOCGame(soc.game.SOCGame)

Example 59 with SOCGame

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

the class SOCPlayerClient method shutdownFromNetwork.

/**
 * network trouble; if possible, ask if they want to play locally (practiceServer vs. robots).
 * Otherwise, go ahead and shut down. Either way, calls
 * {@link SOCPlayerClient.GameDisplay#showErrorPanel(String, boolean)}
 * to show an error message or network exception detail.
 *<P>
 * "If possible" is determined from return value of {@link SOCPlayerClient.ClientNetwork#putLeaveAll()}.
 *<P>
 * Before v2.0.00 this method was {@code dispose()}.
 */
public void shutdownFromNetwork() {
    // Can we still start a practice game?
    final boolean canPractice = net.putLeaveAll();
    String err;
    if (canPractice) {
        // "Sorry, network trouble has occurred."
        err = strings.get("pcli.error.networktrouble");
    } else {
        // "Sorry, the client has been shut down."
        err = strings.get("pcli.error.clientshutdown");
    }
    err = err + " " + ((net.ex == null) ? strings.get("pcli.error.loadpageagain") : net.ex.toString());
    // "Load the page again."
    gameDisplay.channelsClosed(err);
    // Stop network games; continue Practice games if possible.
    for (Map.Entry<String, PlayerClientListener> e : clientListeners.entrySet()) {
        String gameName = e.getKey();
        SOCGame game = games.get(gameName);
        boolean isPractice = (game != null) && game.isPractice;
        if (!(canPractice && isPractice))
            e.getValue().gameDisconnected(false, err);
    }
    net.dispose();
    gameDisplay.showErrorPanel(err, canPractice);
}
Also used : SOCGame(soc.game.SOCGame) Map(java.util.Map) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap)

Example 60 with SOCGame

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

the class SOCPlayerClient method updateGameEndStats.

/**
 * If we're playing in a game that's just finished, update the scores.
 *  This is used to show the true scores, including hidden
 *  victory-point cards, at the game's end.
 *  @since 1.1.00
 */
public void updateGameEndStats(String game, final int[] scores) {
    SOCGame ga = games.get(game);
    if (ga == null)
        // Not playing in that game
        return;
    if (ga.getGameState() != SOCGame.OVER) {
        System.err.println("L4044: pcli.updateGameEndStats called at state " + ga.getGameState());
        // Should not have been sent; game is not yet over.
        return;
    }
    PlayerClientListener pcl = clientListeners.get(game);
    if (pcl == null)
        return;
    Map<SOCPlayer, Integer> scoresMap = new HashMap<SOCPlayer, Integer>();
    for (int i = 0; i < scores.length; ++i) {
        scoresMap.put(ga.getPlayer(i), Integer.valueOf(scores[i]));
    }
    pcl.gameEnded(scoresMap);
}
Also used : HashMap(java.util.HashMap) 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