Search in sources :

Example 1 with Deck

use of mage.cards.decks.Deck in project mage by magefree.

the class ComputerPlayer method buildDeckWithNormalCards.

public static Deck buildDeckWithNormalCards(int deckMinSize, List<Card> cardPool, final List<ColoredManaSymbol> colors) {
    // top 23 cards plus basic lands until 40 deck size
    Deck deck = new Deck();
    final int DECK_SIZE = deckMinSize != 0 ? deckMinSize : 40;
    // 23 from 40
    final int DECK_CARDS_COUNT = Math.floorDiv(deckMinSize * 23, 40);
    final int DECK_LANDS_COUNT = DECK_SIZE - DECK_CARDS_COUNT;
    // sort card pool by top score
    List<Card> sortedCards = new ArrayList<>(cardPool);
    Collections.sort(sortedCards, new Comparator<Card>() {

        @Override
        public int compare(Card o1, Card o2) {
            Integer score1 = RateCard.rateCard(o1, colors);
            Integer score2 = RateCard.rateCard(o2, colors);
            return score2.compareTo(score1);
        }
    });
    // get top cards
    int cardNum = 0;
    while (deck.getCards().size() < DECK_CARDS_COUNT && sortedCards.size() > cardNum) {
        Card card = sortedCards.get(cardNum);
        if (!card.isBasic()) {
            deck.getCards().add(card);
            deck.getSideboard().remove(card);
        }
        cardNum++;
    }
    // add basic lands by color percent
    // TODO:  compensate for non basic lands
    Mana mana = new Mana();
    for (Card card : deck.getCards()) {
        mana.add(card.getManaCost().getMana());
    }
    double total = mana.getBlack() + mana.getBlue() + mana.getGreen() + mana.getRed() + mana.getWhite();
    // most frequent land is forest by default
    int mostLand = 0;
    String mostLandName = "Forest";
    if (mana.getGreen() > 0) {
        int number = (int) Math.round(mana.getGreen() / total * DECK_LANDS_COUNT);
        addBasicLands(deck, "Forest", number);
        mostLand = number;
    }
    if (mana.getBlack() > 0) {
        int number = (int) Math.round(mana.getBlack() / total * DECK_LANDS_COUNT);
        addBasicLands(deck, "Swamp", number);
        if (number > mostLand) {
            mostLand = number;
            mostLandName = "Swamp";
        }
    }
    if (mana.getBlue() > 0) {
        int number = (int) Math.round(mana.getBlue() / total * DECK_LANDS_COUNT);
        addBasicLands(deck, "Island", number);
        if (number > mostLand) {
            mostLand = number;
            mostLandName = "Island";
        }
    }
    if (mana.getWhite() > 0) {
        int number = (int) Math.round(mana.getWhite() / total * DECK_LANDS_COUNT);
        addBasicLands(deck, "Plains", number);
        if (number > mostLand) {
            mostLand = number;
            mostLandName = "Plains";
        }
    }
    if (mana.getRed() > 0) {
        int number = (int) Math.round(mana.getRed() / total * DECK_LANDS_COUNT);
        addBasicLands(deck, "Mountain", number);
        if (number > mostLand) {
            mostLandName = "Plains";
        }
    }
    // adds remaining lands (most popular name)
    addBasicLands(deck, mostLandName, DECK_SIZE - deck.getCards().size());
    return deck;
}
Also used : ConditionalMana(mage.ConditionalMana) Mana(mage.Mana) Deck(mage.cards.decks.Deck) RateCard(mage.game.draft.RateCard) Card(mage.cards.Card) FilterCard(mage.filter.FilterCard)

Example 2 with Deck

use of mage.cards.decks.Deck in project mage by magefree.

the class ComputerPlayer method buildDeckWithOnlyBasicLands.

public static Deck buildDeckWithOnlyBasicLands(int deckMinSize, List<Card> cardPool) {
    // random cards from card pool
    Deck deck = new Deck();
    final int DECK_SIZE = deckMinSize != 0 ? deckMinSize : 40;
    List<Card> sortedCards = new ArrayList<>(cardPool);
    if (!sortedCards.isEmpty()) {
        while (deck.getCards().size() < DECK_SIZE) {
            deck.getCards().add(sortedCards.get(RandomUtil.nextInt(sortedCards.size())));
        }
        return deck;
    } else {
        addBasicLands(deck, "Forest", DECK_SIZE);
        return deck;
    }
}
Also used : Deck(mage.cards.decks.Deck) RateCard(mage.game.draft.RateCard) Card(mage.cards.Card) FilterCard(mage.filter.FilterCard)

Example 3 with Deck

use of mage.cards.decks.Deck in project mage by magefree.

the class FreeformUnlimitedCommanderTest method test_validate_returnsTrue.

@Test
public void test_validate_returnsTrue() {
    // Arrange
    FreeformUnlimitedCommander deck = new FreeformUnlimitedCommander();
    Deck example = new Deck();
    // Act
    boolean actual = deck.validate(example);
    // Assert
    Assert.assertTrue(actual);
}
Also used : FreeformUnlimitedCommander(mage.deck.FreeformUnlimitedCommander) Deck(mage.cards.decks.Deck) Test(org.junit.Test)

Example 4 with Deck

use of mage.cards.decks.Deck in project mage by magefree.

the class LegalityLabel method validateDeck.

public void validateDeck(File deckFile) {
    deckFile = deckFile.getAbsoluteFile();
    if (!deckFile.exists()) {
        errorMessage = String.format("Deck file '%s' does not exist.", deckFile.getAbsolutePath());
        showStateUnknown("<html><body><b>No Deck loaded!</b></body></html>");
        return;
    }
    try {
        StringBuilder errorMessages = new StringBuilder();
        Deck deck = Deck.load(DeckImporter.importDeckFromFile(deckFile.getAbsolutePath(), errorMessages, false), true, true);
        errorMessage = errorMessages.toString();
        validateDeck(deck);
    } catch (Exception ex) {
        errorMessage = String.format("Error importing deck from file '%s'!", deckFile.getAbsolutePath());
    }
}
Also used : Deck(mage.cards.decks.Deck)

Example 5 with Deck

use of mage.cards.decks.Deck in project mage by magefree.

the class CallbackClientImpl method processCallback.

@Override
public synchronized void processCallback(final ClientCallback callback) {
    callback.decompressData();
    SaveObjectUtil.saveObject(callback.getData(), callback.getMethod().toString());
    SwingUtilities.invokeLater(() -> {
        try {
            logger.debug(callback.getMessageId() + " -- " + callback.getMethod());
            switch(callback.getMethod()) {
                case START_GAME:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        GameManager.instance.setCurrentPlayerUUID(message.getPlayerId());
                        gameStarted(message.getGameId(), message.getPlayerId());
                        break;
                    }
                case START_TOURNAMENT:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        tournamentStarted(message.getGameId(), message.getPlayerId());
                        break;
                    }
                case START_DRAFT:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        draftStarted(message.getGameId(), message.getPlayerId());
                        break;
                    }
                case REPLAY_GAME:
                    {
                        replayGame(callback.getObjectId());
                        break;
                    }
                case SHOW_TOURNAMENT:
                    {
                        showTournament(callback.getObjectId());
                        break;
                    }
                case WATCHGAME:
                    {
                        watchGame(callback.getObjectId());
                        break;
                    }
                case CHATMESSAGE:
                    {
                        ChatMessage message = (ChatMessage) callback.getData();
                        // Drop messages from ignored users
                        if (message.getUsername() != null && IgnoreList.IGNORED_MESSAGE_TYPES.contains(message.getMessageType())) {
                            final String serverAddress = SessionHandler.getSession().getServerHostname().orElse("");
                            if (IgnoreList.userIsIgnored(serverAddress, message.getUsername())) {
                                break;
                            }
                        }
                        ChatPanelBasic panel = MageFrame.getChat(callback.getObjectId());
                        if (panel != null) {
                            // play the sound related to the message
                            if (message.getSoundToPlay() != null) {
                                switch(message.getSoundToPlay()) {
                                    case PlayerLeft:
                                        AudioManager.playPlayerLeft();
                                        break;
                                    case PlayerQuitTournament:
                                        AudioManager.playPlayerQuitTournament();
                                        break;
                                    case PlayerSubmittedDeck:
                                        AudioManager.playPlayerSubmittedDeck();
                                        break;
                                    case PlayerWhispered:
                                        AudioManager.playPlayerWhispered();
                                        break;
                                }
                            }
                            // send start message to chat if not done yet
                            if (!panel.isStartMessageDone()) {
                                createChatStartMessage(panel);
                            }
                            // send the message to subchat if exists and it's not a game message
                            if (message.getMessageType() != MessageType.GAME && panel.getConnectedChat() != null) {
                                panel.getConnectedChat().receiveMessage(message.getUsername(), message.getMessage(), message.getTime(), message.getTurnInfo(), message.getMessageType(), ChatMessage.MessageColor.BLACK);
                            } else {
                                panel.receiveMessage(message.getUsername(), message.getMessage(), message.getTime(), message.getTurnInfo(), message.getMessageType(), message.getColor());
                            }
                        }
                        break;
                    }
                case SERVER_MESSAGE:
                    {
                        if (callback.getData() != null) {
                            ChatMessage message = (ChatMessage) callback.getData();
                            if (message.getColor() == ChatMessage.MessageColor.RED) {
                                JOptionPane.showMessageDialog(null, message.getMessage(), "Server message", JOptionPane.WARNING_MESSAGE);
                            } else {
                                JOptionPane.showMessageDialog(null, message.getMessage(), "Server message", JOptionPane.INFORMATION_MESSAGE);
                            }
                        }
                        break;
                    }
                case JOINED_TABLE:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        joinedTable(message.getRoomId(), message.getTableId(), message.getFlag());
                        break;
                    }
                case REPLAY_INIT:
                    {
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            panel.init((GameView) callback.getData());
                        }
                        break;
                    }
                case REPLAY_DONE:
                    {
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            panel.endMessage(null, null, (String) callback.getData(), callback.getMessageId());
                        }
                        break;
                    }
                case REPLAY_UPDATE:
                    {
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            panel.updateGame((GameView) callback.getData());
                        }
                        break;
                    }
                case GAME_INIT:
                    {
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_INIT", callback.getObjectId(), callback.getData());
                            panel.init((GameView) callback.getData());
                        }
                        break;
                    }
                case GAME_OVER:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            Session session = SessionHandler.getSession();
                            if (session.isJsonLogActive()) {
                                UUID gameId = callback.getObjectId();
                                appendJsonEvent("GAME_OVER", callback.getObjectId(), message);
                                String logFileName = "game-" + gameId + ".json";
                                S3Uploader.upload(logFileName, gameId.toString());
                            }
                            panel.endMessage(message.getGameView(), message.getOptions(), message.getMessage(), callback.getMessageId());
                        }
                        break;
                    }
                case GAME_ERROR:
                    {
                        frame.showErrorDialog("Game Error", (String) callback.getData());
                        break;
                    }
                case GAME_ASK:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_ASK", callback.getObjectId(), message);
                            panel.ask(message.getMessage(), message.getGameView(), callback.getMessageId(), message.getOptions());
                        }
                        break;
                    }
                case GAME_TARGET:
                    {
                        // e.g. Pick triggered ability
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_TARGET", callback.getObjectId(), message);
                            panel.pickTarget(message.getGameView(), message.getOptions(), message.getMessage(), message.getCardsView1(), message.getTargets(), message.isFlag(), callback.getMessageId());
                        }
                        break;
                    }
                case GAME_SELECT:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_SELECT", callback.getObjectId(), message);
                            panel.select(message.getGameView(), message.getOptions(), message.getMessage(), callback.getMessageId());
                        }
                        break;
                    }
                case GAME_CHOOSE_ABILITY:
                    {
                        AbilityPickerView abilityPickerView = (AbilityPickerView) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_CHOOSE_ABILITY", callback.getObjectId(), callback.getData());
                            panel.pickAbility(abilityPickerView.getGameView(), null, abilityPickerView);
                        }
                        break;
                    }
                case GAME_CHOOSE_PILE:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_CHOOSE_PILE", callback.getObjectId(), message);
                            panel.pickPile(message.getGameView(), message.getOptions(), message.getMessage(), message.getCardsView1(), message.getCardsView2());
                        }
                        break;
                    }
                case GAME_CHOOSE_CHOICE:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_CHOOSE_CHOICE", callback.getObjectId(), message);
                            panel.getChoice(message.getGameView(), message.getOptions(), message.getChoice(), callback.getObjectId());
                        }
                        break;
                    }
                case GAME_PLAY_MANA:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_PLAY_MANA", callback.getObjectId(), message);
                            panel.playMana(message.getGameView(), message.getOptions(), message.getMessage(), callback.getMessageId());
                        }
                        break;
                    }
                case GAME_PLAY_XMANA:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_PLAY_XMANA", callback.getObjectId(), message);
                            panel.playXMana(message.getGameView(), message.getOptions(), message.getMessage(), callback.getMessageId());
                        }
                        break;
                    }
                case GAME_GET_AMOUNT:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_GET_AMOUNT", callback.getObjectId(), message);
                            panel.getAmount(message.getGameView(), message.getOptions(), message.getMin(), message.getMax(), message.getMessage());
                        }
                        break;
                    }
                case GAME_GET_MULTI_AMOUNT:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_GET_MULTI_AMOUNT", callback.getObjectId(), message);
                            panel.getMultiAmount(message.getMessages(), message.getGameView(), message.getOptions(), message.getMin(), message.getMax());
                        }
                        break;
                    }
                case GAME_UPDATE:
                    {
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            appendJsonEvent("GAME_UPDATE", callback.getObjectId(), callback.getData());
                            // update after undo wtf?!
                            panel.updateGame((GameView) callback.getData(), true, null, null);
                        }
                        break;
                    }
                case GAME_REDRAW_GUI:
                    {
                        // re-draw game's gui elements like attack arrows
                        // uses for client side only (example: update after scrollbars support)
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            panel.updateGame();
                        }
                        break;
                    }
                case END_GAME_INFO:
                    {
                        MageFrame.getInstance().showGameEndDialog((GameEndView) callback.getData());
                        break;
                    }
                case SHOW_USERMESSAGE:
                    {
                        List<String> messageData = (List<String>) callback.getData();
                        if (messageData.size() == 2) {
                            JOptionPane.showMessageDialog(null, messageData.get(1), messageData.get(0), JOptionPane.WARNING_MESSAGE);
                        }
                        break;
                    }
                case GAME_INFORM:
                    {
                        if (callback.getMessageId() > gameInformMessageId) {
                            {
                                GameClientMessage message = (GameClientMessage) callback.getData();
                                GamePanel panel = MageFrame.getGame(callback.getObjectId());
                                if (panel != null) {
                                    appendJsonEvent("GAME_INFORM", callback.getObjectId(), message);
                                    panel.inform(message.getMessage(), message.getGameView(), callback.getMessageId());
                                }
                            }
                        // no longer needed because phase skip handling on server side now
                        } else {
                            logger.warn(new StringBuilder("message out of sequence - ignoring").append("MessageId = ").append(callback.getMessageId()).append(" method = ").append(callback.getMethod()));
                        }
                        gameInformMessageId = messageId;
                        break;
                    }
                case GAME_INFORM_PERSONAL:
                    {
                        GameClientMessage message = (GameClientMessage) callback.getData();
                        GamePanel panel = MageFrame.getGame(callback.getObjectId());
                        if (panel != null) {
                            JOptionPane.showMessageDialog(panel, message.getMessage(), "Game message", JOptionPane.INFORMATION_MESSAGE);
                        }
                        break;
                    }
                case SIDEBOARD:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        DeckView deckView = message.getDeck();
                        Deck deck = DeckUtil.construct(deckView);
                        if (message.getFlag()) {
                            construct(deck, message.getTableId(), message.getTime());
                        } else {
                            sideboard(deck, message.getTableId(), message.getTime());
                        }
                        break;
                    }
                case VIEW_LIMITED_DECK:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        DeckView deckView = message.getDeck();
                        Deck deck = DeckUtil.construct(deckView);
                        viewLimitedDeck(deck, message.getTableId(), message.getTime());
                        break;
                    }
                case VIEW_SIDEBOARD:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        viewSideboard(message.getGameId(), message.getPlayerId());
                        break;
                    }
                case CONSTRUCT:
                    {
                        TableClientMessage message = (TableClientMessage) callback.getData();
                        DeckView deckView = message.getDeck();
                        Deck deck = DeckUtil.construct(deckView);
                        construct(deck, message.getTableId(), message.getTime());
                        break;
                    }
                case DRAFT_OVER:
                    {
                        MageFrame.removeDraft(callback.getObjectId());
                        break;
                    }
                case DRAFT_INIT:
                case DRAFT_PICK:
                    {
                        DraftClientMessage message = (DraftClientMessage) callback.getData();
                        DraftPanel panel = MageFrame.getDraft(callback.getObjectId());
                        if (panel != null) {
                            panel.loadBooster(message.getDraftPickView());
                        }
                        break;
                    }
                case DRAFT_UPDATE:
                    {
                        DraftPanel panel = MageFrame.getDraft(callback.getObjectId());
                        DraftClientMessage message = (DraftClientMessage) callback.getData();
                        if (panel != null) {
                            panel.updateDraft(message.getDraftView());
                        }
                        break;
                    }
                case TOURNAMENT_INIT:
                    {
                        break;
                    }
                case USER_REQUEST_DIALOG:
                    {
                        frame.showUserRequestDialog((UserRequestMessage) callback.getData());
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
            // sync message for server side events only
            if (!callback.getMethod().isClientSideMessage()) {
                messageId = callback.getMessageId();
            }
        } catch (Exception ex) {
            handleException(ex);
        }
    });
}
Also used : ChatPanelBasic(mage.client.chat.ChatPanelBasic) Deck(mage.cards.decks.Deck) GamePanel(mage.client.game.GamePanel) DraftPanel(mage.client.draft.DraftPanel) IgnoreList(mage.client.util.IgnoreList) List(java.util.List) UUID(java.util.UUID) Session(mage.remote.Session)

Aggregations

Deck (mage.cards.decks.Deck)27 Card (mage.cards.Card)11 Player (mage.players.Player)7 Ability (mage.abilities.Ability)5 Test (org.junit.Test)5 java.util (java.util)4 CompanionAbility (mage.abilities.keyword.CompanionAbility)4 Constructed (mage.cards.decks.Constructed)4 DeckValidatorErrorType (mage.cards.decks.DeckValidatorErrorType)4 CardType (mage.constants.CardType)4 FilterMana (mage.filter.FilterMana)4 Game (mage.game.Game)4 TwoPlayerDuel (mage.game.TwoPlayerDuel)4 UUID (java.util.UUID)3 CanBeYourCommanderAbility (mage.abilities.common.CanBeYourCommanderAbility)3 PartnerAbility (mage.abilities.keyword.PartnerAbility)3 PartnerWithAbility (mage.abilities.keyword.PartnerWithAbility)3 ExpansionSet (mage.cards.ExpansionSet)3 Sets (mage.cards.Sets)3 DeckCardLists (mage.cards.decks.DeckCardLists)3