Search in sources :

Example 1 with SOCDevCard

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

the class SOCHandPanel method clickPlayCardButton.

/**
 * Handle a click on the "play card" button, or double-click
 * on an item in the inventory/list of cards held.
 *<P>
 * Inventory items are almost always {@link SOCDevCard}s.
 * Some scenarios may place other items in the player's inventory,
 * such as a "gift" port being moved in {@link SOCGameOption#K_SC_FTRI _SC_FTRI}.
 * If one of these is chosen, this method calls {@link #clickPlayInventorySpecialItem(SOCInventoryItem)}.
 *<P>
 * Called from actionPerformed()
 */
public void clickPlayCardButton() {
    // Check first for "Cancel"
    if (game.getGameState() == SOCGame.PLACING_INV_ITEM) {
        client.getGameManager().cancelBuildRequest(game, SOCCancelBuildRequest.INV_ITEM_PLACE_CANCEL);
        return;
    }
    String itemText;
    // Which one to play from list?
    int itemNum;
    // SOCDevCard or special item
    SOCInventoryItem itemObj = null;
    // Clear prompt if Play Card clicked (instead of Roll clicked)
    setRollPrompt(null, false);
    itemText = inventory.getSelectedItem();
    itemNum = inventory.getSelectedIndex();
    if ((itemText == null) || (itemText.length() == 0)) {
        if (inventory.getItemCount() == 1) {
            // No card selected, but only one to choose from
            itemText = inventory.getItem(0);
            itemNum = 0;
            if (itemText.length() == 0)
                return;
            itemObj = inventoryItems.get(0);
        } else {
            /**
             * No card selected, multiple are in the list.
             * See if only one card isn't a "(VP)" card, isn't new.
             * If more than one, but they're all same type (ex.
             * unplayed Robbers), pretend there's only one.
             */
            // Nothing yet
            itemNum = -1;
            String itemNumText = null;
            for (int i = inventory.getItemCount() - 1; i >= 0; --i) {
                itemText = inventory.getItem(i);
                if ((itemText != null) && (itemText.length() > 0)) {
                    SOCInventoryItem item = inventoryItems.get(i);
                    if (item.isPlayable()) {
                        // Playable (not VP card, not new) item found
                        if (itemObj == null) {
                            itemNum = i;
                            itemNumText = itemText;
                            itemObj = item;
                        } else if (itemObj.itype != item.itype) {
                            // More than one found, and they aren't the same type;
                            itemNum = -1;
                            // we can't auto-pick among them, so stop looking through the list.
                            break;
                        }
                    }
                }
            }
            if ((itemNum == -1) || (itemObj == null)) {
                // * "Please click a card first to select it."
                playerInterface.printKeyed("hpan.devcards.clickfirst");
                return;
            }
            itemText = itemNumText;
        }
    } else {
        // get selected item's Card object
        if (itemNum < inventoryItems.size())
            itemObj = inventoryItems.get(itemNum);
    }
    if ((!playerIsCurrent) || (itemObj == null)) {
        // <--- Early Return: Not current player ---
        return;
    }
    if (itemObj.isVPItem()) {
        playerInterface.print("*** " + strings.get("hpan.devcards.vp.secretlyplayed"));
        // "You secretly played this VP card when you bought it."
        itemNum = inventory.getSelectedIndex();
        if (itemNum >= 0)
            inventory.deselect(itemNum);
        // <--- Early Return: Can't play a VP card ---
        return;
    }
    if (itemObj.isNew()) {
        // "Wait a turn before playing new cards."
        playerInterface.print("*** " + strings.get("hpan.devcards.wait"));
        // <--- Early Return: Card is new ---
        return;
    }
    if (!(itemObj instanceof SOCDevCard)) {
        clickPlayInventorySpecialItem(itemObj);
        // <--- Early Return: Special item, not a dev card ---
        return;
    }
    if (player.hasPlayedDevCard()) {
        // "You may play only one card per turn."
        playerInterface.print("*** " + strings.get("hpan.devcards.oneperturn"));
        playCardBut.setEnabled(false);
        return;
    }
    int cardTypeToPlay = -1;
    switch(itemObj.itype) {
        case SOCDevCardConstants.KNIGHT:
            if (game.canPlayKnight(playerNumber)) {
                cardTypeToPlay = SOCDevCardConstants.KNIGHT;
            } else if (game.isGameOptionSet(SOCGameOption.K_SC_PIRI)) {
                playerInterface.printKeyed("hpan.devcards.warship.cannotnow");
            // "You cannot convert a ship to a warship right now."
            }
            break;
        case SOCDevCardConstants.ROADS:
            if (game.canPlayRoadBuilding(playerNumber)) {
                cardTypeToPlay = SOCDevCardConstants.ROADS;
            } else if (player.getNumPieces(SOCPlayingPiece.ROAD) == 0) {
                if (game.hasSeaBoard && (player.getNumPieces(SOCPlayingPiece.SHIP) == 0))
                    playerInterface.printKeyed("hpan.devcards.roads_ships.none");
                else
                    // "You have no roads or ships left to place."
                    playerInterface.printKeyed("hpan.devcards.roads.none");
            // "You have no roads left to place."
            }
            break;
        case SOCDevCardConstants.DISC:
            if (game.canPlayDiscovery(playerNumber)) {
                cardTypeToPlay = SOCDevCardConstants.DISC;
            }
            break;
        case SOCDevCardConstants.MONO:
            if (game.canPlayMonopoly(playerNumber)) {
                cardTypeToPlay = SOCDevCardConstants.MONO;
            }
            break;
        default:
            playerInterface.printKeyed("hpan.devcards.interror.ctype", itemObj.itype, itemText);
    }
    if (cardTypeToPlay != -1) {
        client.getGameManager().playDevCard(game, cardTypeToPlay);
        disableBankUndoButton();
    }
}
Also used : SOCInventoryItem(soc.game.SOCInventoryItem) SOCDevCard(soc.game.SOCDevCard)

Aggregations

SOCDevCard (soc.game.SOCDevCard)1 SOCInventoryItem (soc.game.SOCInventoryItem)1