Search in sources :

Example 1 with SOCMoveRobberResult

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

the class SOCGameMessageHandler method handleMOVEROBBER.

// / Robber/pirate robbery ///
/**
 * handle "move robber" message (move the robber or the pirate).
 *
 * @param c  the connection that sent the message
 * @param mes  the message
 * @since 1.0.0
 */
private void handleMOVEROBBER(SOCGame ga, Connection c, SOCMoveRobber mes) {
    ga.takeMonitor();
    try {
        SOCPlayer player = ga.getPlayer(c.getData());
        /**
         * make sure the player can do it
         */
        final String gaName = ga.getName();
        final boolean isPirate = ga.getRobberyPirateFlag();
        final int pn = player.getPlayerNumber();
        // negative for pirate
        int coord = mes.getCoordinates();
        final boolean canDo = (isPirate == (coord < 0)) && (isPirate ? ga.canMovePirate(pn, -coord) : ga.canMoveRobber(pn, coord));
        if (canDo) {
            SOCMoveRobberResult result;
            SOCMoveRobber moveMsg;
            if (isPirate) {
                result = ga.movePirate(pn, -coord);
                moveMsg = new SOCMoveRobber(gaName, pn, coord);
            } else {
                result = ga.moveRobber(pn, coord);
                moveMsg = new SOCMoveRobber(gaName, pn, coord);
            }
            srv.messageToGame(gaName, moveMsg);
            final List<SOCPlayer> victims = result.getVictims();
            /**
             * only one possible victim
             */
            if ((victims.size() == 1) && (ga.getGameState() != SOCGame.WAITING_FOR_ROB_CLOTH_OR_RESOURCE)) {
                /**
                 * report what was stolen
                 */
                SOCPlayer victim = victims.get(0);
                handler.reportRobbery(ga, player, victim, result.getLoot());
            } else {
                final String msgKey;
                /**
                 * no victim
                 */
                if (victims.size() == 0) {
                    /**
                     * just say it was moved; nothing is stolen
                     */
                    // "{0} moved the robber" or "{0} moved the pirate"
                    msgKey = "robberpirate.moved";
                } else if (ga.getGameState() == SOCGame.WAITING_FOR_ROB_CLOTH_OR_RESOURCE) {
                    /**
                     * only one possible victim, they have both clay and resources
                     */
                    msgKey = "robberpirate.moved.choose.cloth.rsrcs";
                // "{0} moved the robber/pirate. Must choose to steal cloth or steal resources."
                } else {
                    /**
                     * else, the player needs to choose a victim
                     */
                    msgKey = "robberpirate.moved.choose.victim";
                // "{0} moved the robber/pirate. Must choose a victim."
                }
                srv.messageToGameKeyed(ga, true, msgKey, player.getName(), ((isPirate) ? 2 : 1));
            }
            handler.sendGameState(ga);
            // victims there, just send the prompt from here:
            if (ga.getGameState() == SOCGame.WAITING_FOR_ROB_CLOTH_OR_RESOURCE) {
                final int vpn = victims.get(0).getPlayerNumber();
                srv.messageToPlayer(c, new SOCChoosePlayer(gaName, vpn));
            }
        } else {
            srv.messageToPlayerKeyed(c, gaName, ((coord < 0) ? "robber.cantmove.pirate" : "robber.cantmove"));
        // "You can't move the pirate" / "You can't move the robber"
        }
    } catch (Exception e) {
        D.ebugPrintStackTrace(e, "Exception caught");
    }
    ga.releaseMonitor();
}
Also used : SOCPlayer(soc.game.SOCPlayer) SOCMoveRobberResult(soc.game.SOCMoveRobberResult)

Aggregations

SOCMoveRobberResult (soc.game.SOCMoveRobberResult)1 SOCPlayer (soc.game.SOCPlayer)1