Search in sources :

Example 41 with SOCPlayer

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

the class SOCPlayerInterface method updateAtPutPiece.

/**
 * Handle updates after putting a piece on the board,
 * or moving a ship that was already placed.
 * Place or move the piece within our {@link SOCGame}
 * and visually on our {@link SOCBoardPanel}.
 *
 * @param mesPn  The piece's player number
 * @param coord  The piece's coordinate.  If <tt>isMove</tt>, the coordinate to move <em>from</em>.
 * @param pieceType  Piece type, like {@link SOCPlayingPiece#CITY}
 * @param isMove   If true, it's a move, not a new placement; valid only for ships.
 * @param moveToCoord  If <tt>isMove</tt>, the coordinate to move <em>to</em>.  Otherwise ignored.
 *
 * @see #updateAtPiecesChanged()
 * @since 2.0.00
 */
public void updateAtPutPiece(final int mesPn, final int coord, final int pieceType, final boolean isMove, final int moveToCoord) {
    // TODO consider more effic way for flushBoardLayoutAndRepaint, without the =null
    final SOCPlayer pl = (pieceType != SOCPlayingPiece.VILLAGE) ? game.getPlayer(mesPn) : null;
    final SOCPlayer oldLongestRoadPlayer = game.getPlayerWithLongestRoad();
    final SOCHandPanel mesHp = (pieceType != SOCPlayingPiece.VILLAGE) ? getPlayerHandPanel(mesPn) : null;
    final boolean[] debugShowPotentials = boardPanel.debugShowPotentials;
    final SOCPlayingPiece pp;
    switch(pieceType) {
        case SOCPlayingPiece.ROAD:
            pp = new SOCRoad(pl, coord, null);
            game.putPiece(pp);
            mesHp.updateValue(PlayerClientListener.UpdateType.Road);
            if (debugShowPotentials[4] || debugShowPotentials[5] || debugShowPotentials[7])
                boardPanel.flushBoardLayoutAndRepaint();
            break;
        case SOCPlayingPiece.SETTLEMENT:
            pp = new SOCSettlement(pl, coord, null);
            game.putPiece(pp);
            mesHp.updateValue(PlayerClientListener.UpdateType.Settlement);
            /**
             * if this is the second initial settlement, then update the resource display
             */
            mesHp.updateValue(PlayerClientListener.UpdateType.ResourceTotalAndDetails);
            if (debugShowPotentials[4] || debugShowPotentials[5] || debugShowPotentials[7] || debugShowPotentials[6])
                boardPanel.flushBoardLayoutAndRepaint();
            break;
        case SOCPlayingPiece.CITY:
            pp = new SOCCity(pl, coord, null);
            game.putPiece(pp);
            mesHp.updateValue(PlayerClientListener.UpdateType.Settlement);
            mesHp.updateValue(PlayerClientListener.UpdateType.City);
            if (debugShowPotentials[4] || debugShowPotentials[5] || debugShowPotentials[7] || debugShowPotentials[6])
                boardPanel.flushBoardLayoutAndRepaint();
            break;
        case SOCPlayingPiece.SHIP:
            pp = new SOCShip(pl, coord, null);
            if (!isMove) {
                game.putPiece(pp);
                mesHp.updateValue(PlayerClientListener.UpdateType.Ship);
            } else {
                game.moveShip((SOCShip) pp, moveToCoord);
                if (mesHp == clientHand)
                    // just in case; it probably wasn't enabled
                    mesHp.disableBankUndoButton();
            }
            if (debugShowPotentials[4] || debugShowPotentials[5] || debugShowPotentials[7])
                boardPanel.flushBoardLayoutAndRepaint();
            break;
        case SOCPlayingPiece.VILLAGE:
            // no need to refresh boardPanel after receiving each village
            pp = new SOCVillage(coord, game.getBoard());
            game.putPiece(pp);
            // <--- Early return: Piece is part of board initial layout, not player info ---
            return;
        case SOCPlayingPiece.FORTRESS:
            pp = new SOCFortress(pl, coord, game.getBoard());
            game.putPiece(pp);
            // <--- Early return: Piece is part of board initial layout, not added during game ---
            return;
        default:
            chatPrintDebug("* Unknown piece type " + pieceType + " at coord 0x" + Integer.toHexString(coord));
            // <--- Early return ---
            return;
    }
    mesHp.updateValue(PlayerClientListener.UpdateType.VictoryPoints);
    boardPanel.repaint();
    buildingPanel.updateButtonStatus();
    if (game.isDebugFreePlacement() && game.isInitialPlacement())
        // update here, since gamestate doesn't change to trigger update
        boardPanel.updateMode();
    if (hasCalledBegan && (game.getGameState() >= SOCGame.START1A))
        playSound(SOUND_PUT_PIECE);
    /**
     * Check for and announce change in longest road; update all players' victory points.
     */
    SOCPlayer newLongestRoadPlayer = game.getPlayerWithLongestRoad();
    if (newLongestRoadPlayer != oldLongestRoadPlayer) {
        updateLongestLargest(true, oldLongestRoadPlayer, newLongestRoadPlayer);
    }
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCVillage(soc.game.SOCVillage) SOCCity(soc.game.SOCCity) SOCPlayingPiece(soc.game.SOCPlayingPiece) SOCShip(soc.game.SOCShip) SOCPlayer(soc.game.SOCPlayer) SOCFortress(soc.game.SOCFortress) SOCRoad(soc.game.SOCRoad)

Example 42 with SOCPlayer

use of soc.game.SOCPlayer 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)

Example 43 with SOCPlayer

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

the class SOCDisplaylessPlayerClient method handleSETSPECIALITEM.

/**
 * Handle the "set special item" message.
 * This method handles only {@link SOCSetSpecialItem#OP_SET OP_SET} and {@link SOCSetSpecialItem#OP_CLEAR OP_CLEAR}
 * and ignores other operations, such as {@link SOCSetSpecialItem#OP_PICK OP_PICK}.  If your client needs to react
 * to those other operations, override this method.
 *
 * @param games  Games the client is playing, for method reuse by SOCPlayerClient
 * @param mes  the message
 * @since 2.0.00
 */
public static final void handleSETSPECIALITEM(final Map<String, SOCGame> games, SOCSetSpecialItem mes) {
    final SOCGame ga = games.get(mes.getGame());
    if (ga == null)
        return;
    final String typeKey = mes.typeKey;
    final int gi = mes.gameItemIndex, pi = mes.playerItemIndex, pn = mes.playerNumber;
    switch(mes.op) {
        case SOCSetSpecialItem.OP_CLEAR:
            {
                if (gi != -1)
                    ga.setSpecialItem(typeKey, gi, null);
                if ((pn != -1) && (pi != -1)) {
                    SOCPlayer pl = ga.getPlayer(pn);
                    if (pl != null)
                        pl.setSpecialItem(typeKey, pi, null);
                }
            }
            break;
        case SOCSetSpecialItem.OP_SET:
            {
                if ((gi == -1) && ((pi == -1) || (pn == -1))) {
                    // malformed message
                    return;
                }
                SOCSpecialItem item = ga.getSpecialItem(typeKey, gi, pi, pn);
                final SOCPlayer pl = (pn != -1) ? ga.getPlayer(pn) : null;
                if (item != null) {
                    item.setPlayer(pl);
                    item.setCoordinates(mes.coord);
                    item.setLevel(mes.level);
                    item.setStringValue(mes.sv);
                } else {
                    item = new SOCSpecialItem(pl, mes.coord, mes.level, mes.sv, null, null);
                }
                if (gi != -1) {
                    item.setGameIndex(gi);
                    ga.setSpecialItem(typeKey, gi, item);
                }
                if ((pi != -1) && (pl != null))
                    pl.setSpecialItem(typeKey, pi, item);
            }
            break;
    }
}
Also used : SOCPlayer(soc.game.SOCPlayer) SOCGame(soc.game.SOCGame) SOCSpecialItem(soc.game.SOCSpecialItem)

Example 44 with SOCPlayer

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

the class SOCDisplaylessPlayerClient method handlePLAYERELEMENTS.

/**
 * Handle the PlayerElements message: Finds game by name, and loops calling
 * {@link #handlePLAYERELEMENT(SOCGame, SOCPlayer, int, int, int, int)}.
 * @param mes  the message
 * @since 2.0.00
 */
protected void handlePLAYERELEMENTS(final SOCPlayerElements mes) {
    final SOCGame ga = games.get(mes.getGame());
    if (ga == null)
        return;
    final int pn = mes.getPlayerNumber();
    final SOCPlayer pl = (pn != -1) ? ga.getPlayer(pn) : null;
    final int action = mes.getAction();
    final int[] etypes = mes.getElementTypes(), amounts = mes.getAmounts();
    for (int i = 0; i < etypes.length; ++i) handlePLAYERELEMENT(ga, pl, pn, action, etypes[i], amounts[i], nickname);
}
Also used : SOCPlayer(soc.game.SOCPlayer) SOCGame(soc.game.SOCGame)

Example 45 with SOCPlayer

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

the class SOCDisplaylessPlayerClient method handleLEAVEGAME.

/**
 * handle the "leave game" message
 * @param mes  the message
 */
protected void handleLEAVEGAME(SOCLeaveGame mes) {
    String gn = (mes.getGame());
    SOCGame ga = games.get(gn);
    if (ga != null) {
        SOCPlayer player = ga.getPlayer(mes.getNickname());
        if (player != null) {
            // 
            // This user was not a spectator
            // 
            ga.removePlayer(mes.getNickname());
        }
    }
}
Also used : SOCPlayer(soc.game.SOCPlayer) SOCGame(soc.game.SOCGame)

Aggregations

SOCPlayer (soc.game.SOCPlayer)61 SOCGame (soc.game.SOCGame)17 SOCShip (soc.game.SOCShip)12 SOCSettlement (soc.game.SOCSettlement)11 SOCRoad (soc.game.SOCRoad)9 SOCResourceSet (soc.game.SOCResourceSet)8 SOCCity (soc.game.SOCCity)7 SOCBoardLarge (soc.game.SOCBoardLarge)6 SOCFortress (soc.game.SOCFortress)5 ArrayList (java.util.ArrayList)4 SOCBoard (soc.game.SOCBoard)4 Connection (soc.server.genericServer.Connection)4 SOCInventoryItem (soc.game.SOCInventoryItem)3 SOCVillage (soc.game.SOCVillage)3 SQLException (java.sql.SQLException)2 Iterator (java.util.Iterator)2 Stack (java.util.Stack)2 SOCLRPathData (soc.game.SOCLRPathData)2 SOCPlayingPiece (soc.game.SOCPlayingPiece)2 SOCSpecialItem (soc.game.SOCSpecialItem)2