Search in sources :

Example 1 with CompanionAbility

use of mage.abilities.keyword.CompanionAbility in project mage by magefree.

the class Commander method validate.

@Override
public boolean validate(Deck deck) {
    boolean valid = true;
    errorsList.clear();
    FilterMana colorIdentity = new FilterMana();
    Set<Card> commanders = new HashSet<>();
    Card companion;
    int sbsize = deck.getSideboard().size();
    Card card1;
    Card card2;
    Card card3;
    Iterator<Card> iter;
    switch(deck.getSideboard().size()) {
        case 1:
            companion = null;
            commanders.add(deck.getSideboard().iterator().next());
            break;
        case 2:
            iter = deck.getSideboard().iterator();
            card1 = iter.next();
            card2 = iter.next();
            if (card1.getAbilities().stream().anyMatch(CompanionAbility.class::isInstance)) {
                companion = card1;
                commanders.add(card2);
            } else if (card2.getAbilities().stream().anyMatch(CompanionAbility.class::isInstance)) {
                companion = card2;
                commanders.add(card1);
            } else {
                companion = null;
                commanders.add(card1);
                commanders.add(card2);
            }
            break;
        case 3:
            iter = deck.getSideboard().iterator();
            card1 = iter.next();
            card2 = iter.next();
            card3 = iter.next();
            if (card1.getAbilities().stream().anyMatch(CompanionAbility.class::isInstance)) {
                companion = card1;
                commanders.add(card2);
                commanders.add(card3);
            } else if (card2.getAbilities().stream().anyMatch(CompanionAbility.class::isInstance)) {
                companion = card2;
                commanders.add(card1);
                commanders.add(card3);
            } else if (card3.getAbilities().stream().anyMatch(CompanionAbility.class::isInstance)) {
                companion = card3;
                commanders.add(card1);
                commanders.add(card2);
            } else {
                companion = null;
                addError(DeckValidatorErrorType.PRIMARY, "Commander", "Sideboard must contain only the commander(s) and up to 1 companion");
                valid = false;
            }
            break;
        default:
            companion = null;
            addError(DeckValidatorErrorType.PRIMARY, "Commander", "Sideboard must contain only the commander(s) and up to 1 companion");
            valid = false;
    }
    if (companion != null && deck.getCards().size() + deck.getSideboard().size() != 101) {
        addError(DeckValidatorErrorType.DECK_SIZE, "Deck", "Must contain " + 101 + " cards (companion doesn't count for deck size): has " + (deck.getCards().size() + deck.getSideboard().size()) + " cards");
        valid = false;
    } else if (companion == null && deck.getCards().size() + deck.getSideboard().size() != 100) {
        addError(DeckValidatorErrorType.DECK_SIZE, "Deck", "Must contain " + 100 + " cards: has " + (deck.getCards().size() + deck.getSideboard().size()) + " cards");
        valid = false;
    }
    Map<String, Integer> counts = new HashMap<>();
    countCards(counts, deck.getCards());
    countCards(counts, deck.getSideboard());
    valid = checkCounts(1, counts) && valid;
    for (String bannedCard : banned) {
        if (counts.containsKey(bannedCard)) {
            addError(DeckValidatorErrorType.BANNED, bannedCard, "Banned", true);
            valid = false;
        }
    }
    Set<String> commanderNames = new HashSet<>();
    for (Card commander : commanders) {
        commanderNames.add(commander.getName());
    }
    if (commanders.size() == 2 && commanders.stream().map(MageObject::getAbilities).filter(abilities -> abilities.contains(PartnerAbility.getInstance())).count() != 2 && commanders.stream().map(MageObject::getAbilities).filter(abilities -> abilities.contains(FriendsForeverAbility.getInstance())).count() != 2 && !CardUtil.castStream(commanders.stream().map(MageObject::getAbilities), PartnerWithAbility.class).map(PartnerWithAbility::getPartnerName).allMatch(commanderNames::contains)) {
        for (Card commander : commanders) {
            addError(DeckValidatorErrorType.PRIMARY, commander.getName(), "Commander with invalid Partner (" + commander.getName() + ')', true);
            valid = false;
        }
    }
    for (Card commander : commanders) {
        if (bannedCommander.contains(commander.getName())) {
            addError(DeckValidatorErrorType.PRIMARY, commander.getName(), "Commander banned (" + commander.getName() + ')', true);
            valid = false;
        }
        if ((!commander.hasCardTypeForDeckbuilding(CardType.CREATURE) || !commander.isLegendary()) && !commander.getAbilities().contains(CanBeYourCommanderAbility.getInstance())) {
            addError(DeckValidatorErrorType.PRIMARY, commander.getName(), "Commander invalid (" + commander.getName() + ')', true);
            valid = false;
        }
        if (commanders.size() == 2 && bannedPartner.contains(commander.getName())) {
            addError(DeckValidatorErrorType.PRIMARY, commander.getName(), "Commander Partner banned (" + commander.getName() + ')', true);
            valid = false;
        }
        ManaUtil.collectColorIdentity(colorIdentity, commander.getColorIdentity());
    }
    // no needs in cards check on wrong commanders
    if (!valid) {
        return false;
    }
    for (Card card : deck.getCards()) {
        if (!ManaUtil.isColorIdentityCompatible(colorIdentity, card.getColorIdentity())) {
            addError(DeckValidatorErrorType.OTHER, card.getName(), "Invalid color (" + colorIdentity.toString() + ')', true);
            valid = false;
        }
    }
    for (Card card : deck.getSideboard()) {
        if (!ManaUtil.isColorIdentityCompatible(colorIdentity, card.getColorIdentity())) {
            addError(DeckValidatorErrorType.OTHER, card.getName(), "Invalid color (" + colorIdentity.toString() + ')', true);
            valid = false;
        }
    }
    for (Card card : deck.getCards()) {
        if (!isSetAllowed(card.getExpansionSetCode())) {
            if (!legalSets(card)) {
                addError(DeckValidatorErrorType.WRONG_SET, card.getName(), "Not allowed Set: " + card.getExpansionSetCode(), true);
                valid = false;
            }
        }
    }
    for (Card card : deck.getSideboard()) {
        if (!isSetAllowed(card.getExpansionSetCode())) {
            if (!legalSets(card)) {
                addError(DeckValidatorErrorType.WRONG_SET, card.getName(), "Not allowed Set: " + card.getExpansionSetCode(), true);
                valid = false;
            }
        }
    }
    // Check for companion legality
    if (companion != null) {
        Set<Card> cards = new HashSet<>(deck.getCards());
        cards.addAll(commanders);
        for (Ability ability : companion.getAbilities()) {
            if (ability instanceof CompanionAbility) {
                CompanionAbility companionAbility = (CompanionAbility) ability;
                if (!companionAbility.isLegal(cards, getDeckMinSize())) {
                    addError(DeckValidatorErrorType.PRIMARY, companion.getName(), "Commander Companion (deck invalid for companion)", true);
                    valid = false;
                }
                break;
            }
        }
    }
    return valid;
}
Also used : PartnerAbility(mage.abilities.keyword.PartnerAbility) java.util(java.util) DeckValidatorErrorType(mage.cards.decks.DeckValidatorErrorType) FriendsForeverAbility(mage.abilities.keyword.FriendsForeverAbility) PartnerWithAbility(mage.abilities.keyword.PartnerWithAbility) Deck(mage.cards.decks.Deck) CanBeYourCommanderAbility(mage.abilities.common.CanBeYourCommanderAbility) Sets(mage.cards.Sets) CardUtil(mage.util.CardUtil) Constructed(mage.cards.decks.Constructed) ManaUtil(mage.util.ManaUtil) ExpansionSet(mage.cards.ExpansionSet) CardType(mage.constants.CardType) MageObject(mage.MageObject) CompanionAbility(mage.abilities.keyword.CompanionAbility) ObjectColor(mage.ObjectColor) Card(mage.cards.Card) Ability(mage.abilities.Ability) FilterMana(mage.filter.FilterMana) PartnerAbility(mage.abilities.keyword.PartnerAbility) FriendsForeverAbility(mage.abilities.keyword.FriendsForeverAbility) PartnerWithAbility(mage.abilities.keyword.PartnerWithAbility) CanBeYourCommanderAbility(mage.abilities.common.CanBeYourCommanderAbility) CompanionAbility(mage.abilities.keyword.CompanionAbility) Ability(mage.abilities.Ability) FilterMana(mage.filter.FilterMana) CompanionAbility(mage.abilities.keyword.CompanionAbility) MageObject(mage.MageObject) Card(mage.cards.Card)

Example 2 with CompanionAbility

use of mage.abilities.keyword.CompanionAbility in project mage by magefree.

the class Brawl method validate.

@Override
public boolean validate(Deck deck) {
    boolean valid = true;
    errorsList.clear();
    Card brawler = null;
    Card companion = null;
    FilterMana colorIdentity = new FilterMana();
    if (deck.getSideboard().size() == 1) {
        for (Card card : deck.getSideboard()) {
            brawler = card;
        }
    } else if (deck.getSideboard().size() == 2) {
        Iterator<Card> iter = deck.getSideboard().iterator();
        Card card1 = iter.next();
        Card card2 = iter.next();
        if (card1.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card1;
            brawler = card2;
        } else if (card2.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card2;
            brawler = card1;
        } else {
            addError(DeckValidatorErrorType.PRIMARY, "Brawl", "Sideboard must contain only the brawler and up to 1 companion");
            valid = false;
        }
    } else {
        addError(DeckValidatorErrorType.PRIMARY, "Brawl", "Sideboard must contain only the brawler and up to 1 companion");
        valid = false;
    }
    if (brawler != null) {
        ManaUtil.collectColorIdentity(colorIdentity, brawler.getColorIdentity());
        if (bannedCommander.contains(brawler.getName())) {
            addError(DeckValidatorErrorType.PRIMARY, brawler.getName(), "Brawler banned (" + brawler.getName() + ')', true);
            valid = false;
        }
        if (!((brawler.hasCardTypeForDeckbuilding(CardType.CREATURE) && brawler.isLegendary()) || brawler.hasCardTypeForDeckbuilding(CardType.PLANESWALKER) || brawler.getAbilities().contains(CanBeYourCommanderAbility.getInstance()))) {
            addError(DeckValidatorErrorType.PRIMARY, brawler.getName(), "Brawler Invalid (" + brawler.getName() + ')', true);
            valid = false;
        }
    }
    if (companion != null && deck.getCards().size() + deck.getSideboard().size() != getDeckMinSize() + 1) {
        addError(DeckValidatorErrorType.DECK_SIZE, "Deck", "Must contain " + (getDeckMinSize() + 1) + " cards (companion doesn't count in deck size requirement): has " + (deck.getCards().size() + deck.getSideboard().size()) + " cards");
        valid = false;
    } else if (companion == null && deck.getCards().size() + deck.getSideboard().size() != getDeckMinSize()) {
        addError(DeckValidatorErrorType.DECK_SIZE, "Deck", "Must contain " + getDeckMinSize() + " cards: has " + (deck.getCards().size() + deck.getSideboard().size()) + " cards");
        valid = false;
    }
    Map<String, Integer> counts = new HashMap<>();
    countCards(counts, deck.getCards());
    countCards(counts, deck.getSideboard());
    valid = checkCounts(1, counts) && valid;
    for (String bannedCard : banned) {
        if (counts.containsKey(bannedCard)) {
            addError(DeckValidatorErrorType.BANNED, bannedCard, "Banned", true);
            valid = false;
        }
    }
    Set<String> basicsInDeck = new HashSet<>();
    if (colorIdentity.isColorless()) {
        for (Card card : deck.getCards()) {
            if (basicLandNames.contains(card.getName())) {
                basicsInDeck.add(card.getName());
            }
        }
    }
    for (Card card : deck.getCards()) {
        if (!ManaUtil.isColorIdentityCompatible(colorIdentity, card.getColorIdentity()) && !(colorIdentity.isColorless() && basicsInDeck.size() == 1 && basicsInDeck.contains(card.getName()))) {
            addError(DeckValidatorErrorType.OTHER, card.getName(), "Invalid color (" + colorIdentity.toString() + ')', true);
            valid = false;
        }
    }
    for (Card card : deck.getSideboard()) {
        if (!ManaUtil.isColorIdentityCompatible(colorIdentity, card.getColorIdentity()) && !(colorIdentity.isColorless() && basicsInDeck.size() == 1 && basicsInDeck.contains(card.getName()))) {
            addError(DeckValidatorErrorType.OTHER, card.getName(), "Invalid color (" + colorIdentity.toString() + ')', true);
            valid = false;
        }
    }
    for (Card card : deck.getCards()) {
        if (!isSetAllowed(card.getExpansionSetCode())) {
            if (!legalSets(card)) {
                addError(DeckValidatorErrorType.WRONG_SET, card.getName(), "Not allowed Set: " + card.getExpansionSetCode(), true);
                valid = false;
            }
        }
    }
    for (Card card : deck.getSideboard()) {
        if (!isSetAllowed(card.getExpansionSetCode())) {
            if (!legalSets(card)) {
                addError(DeckValidatorErrorType.WRONG_SET, card.getName(), "Not allowed Set: " + card.getExpansionSetCode(), true);
                valid = false;
            }
        }
    }
    // Check for companion legality
    if (companion != null) {
        Set<Card> cards = new HashSet<>(deck.getCards());
        cards.add(brawler);
        for (Ability ability : companion.getAbilities()) {
            if (ability instanceof CompanionAbility) {
                CompanionAbility companionAbility = (CompanionAbility) ability;
                if (!companionAbility.isLegal(cards, getDeckMinSize())) {
                    addError(DeckValidatorErrorType.PRIMARY, companion.getName(), "Brawl Companion Invalid", true);
                    valid = false;
                }
                break;
            }
        }
    }
    return valid;
}
Also used : java.util(java.util) DeckValidatorErrorType(mage.cards.decks.DeckValidatorErrorType) CardType(mage.constants.CardType) CompanionAbility(mage.abilities.keyword.CompanionAbility) Deck(mage.cards.decks.Deck) CanBeYourCommanderAbility(mage.abilities.common.CanBeYourCommanderAbility) Card(mage.cards.Card) Constructed(mage.cards.decks.Constructed) ManaUtil(mage.util.ManaUtil) Ability(mage.abilities.Ability) FilterMana(mage.filter.FilterMana) CompanionAbility(mage.abilities.keyword.CompanionAbility) CanBeYourCommanderAbility(mage.abilities.common.CanBeYourCommanderAbility) Ability(mage.abilities.Ability) FilterMana(mage.filter.FilterMana) CompanionAbility(mage.abilities.keyword.CompanionAbility) Card(mage.cards.Card)

Example 3 with CompanionAbility

use of mage.abilities.keyword.CompanionAbility in project mage by magefree.

the class PennyDreadfulCommander method validate.

@Override
public boolean validate(Deck deck) {
    boolean valid = true;
    errorsList.clear();
    FilterMana colorIdentity = new FilterMana();
    Set<Card> commanders = new HashSet<>();
    Card companion = null;
    if (deck.getSideboard().size() == 1) {
        commanders.add(deck.getSideboard().iterator().next());
    } else if (deck.getSideboard().size() == 2) {
        Iterator<Card> iter = deck.getSideboard().iterator();
        Card card1 = iter.next();
        Card card2 = iter.next();
        if (card1.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card1;
            commanders.add(card2);
        } else if (card2.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card2;
            commanders.add(card1);
        } else {
            commanders.add(card1);
            commanders.add(card2);
        }
    } else if (deck.getSideboard().size() == 3) {
        Iterator<Card> iter = deck.getSideboard().iterator();
        Card card1 = iter.next();
        Card card2 = iter.next();
        Card card3 = iter.next();
        if (card1.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card1;
            commanders.add(card2);
            commanders.add(card3);
        } else if (card2.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card2;
            commanders.add(card1);
            commanders.add(card3);
        } else if (card3.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card3;
            commanders.add(card1);
            commanders.add(card2);
        } else {
            addError(DeckValidatorErrorType.PRIMARY, "Commander", "Sideboard must contain only the commander(s) and up to 1 companion");
            valid = false;
        }
    } else {
        addError(DeckValidatorErrorType.PRIMARY, "Commander", "Sideboard must contain only the commander(s) and up to 1 companion");
        valid = false;
    }
    if (companion != null && deck.getCards().size() + deck.getSideboard().size() != 101) {
        addError(DeckValidatorErrorType.DECK_SIZE, "Deck", "Must contain " + 101 + " cards (companion doesn't count for deck size): has " + (deck.getCards().size() + deck.getSideboard().size()) + " cards");
        valid = false;
    } else if (companion == null && deck.getCards().size() + deck.getSideboard().size() != 100) {
        addError(DeckValidatorErrorType.DECK_SIZE, "Deck", "Must contain " + 100 + " cards: has " + (deck.getCards().size() + deck.getSideboard().size()) + " cards");
        valid = false;
    }
    Map<String, Integer> counts = new HashMap<>();
    countCards(counts, deck.getCards());
    countCards(counts, deck.getSideboard());
    valid = checkCounts(1, counts) && valid;
    if (pdAllowed.isEmpty()) {
        pdAllowed.putAll(PennyDreadfulLegalityUtil.getLegalCardList());
    }
    for (String wantedCard : counts.keySet()) {
        if (!(pdAllowed.containsKey(wantedCard))) {
            addError(DeckValidatorErrorType.BANNED, wantedCard, "Banned", true);
            valid = false;
        }
    }
    Set<String> commanderNames = new HashSet<>();
    for (Card commander : commanders) {
        commanderNames.add(commander.getName());
    }
    for (Card commander : commanders) {
        if (bannedCommander.contains(commander.getName())) {
            addError(DeckValidatorErrorType.PRIMARY, commander.getName(), "Commander banned (" + commander.getName() + ')', true);
            valid = false;
        }
        if ((!commander.hasCardTypeForDeckbuilding(CardType.CREATURE) || !commander.isLegendary()) && !commander.getAbilities().contains(CanBeYourCommanderAbility.getInstance())) {
            addError(DeckValidatorErrorType.PRIMARY, commander.getName(), "Commander invalid (" + commander.getName() + ')', true);
            valid = false;
        }
        if (commanders.size() == 2) {
            if (!commander.getAbilities().contains(PartnerAbility.getInstance())) {
                boolean partnersWith = commander.getAbilities().stream().filter(PartnerWithAbility.class::isInstance).map(PartnerWithAbility.class::cast).map(PartnerWithAbility::getPartnerName).anyMatch(commanderNames::contains);
                if (!partnersWith) {
                    addError(DeckValidatorErrorType.PRIMARY, commander.getName(), "Commander without Partner (" + commander.getName() + ')', true);
                    valid = false;
                }
            }
        }
        ManaUtil.collectColorIdentity(colorIdentity, commander.getColorIdentity());
    }
    // no needs in cards check on wrong commanders
    if (!valid) {
        return false;
    }
    for (Card card : deck.getCards()) {
        if (!ManaUtil.isColorIdentityCompatible(colorIdentity, card.getColorIdentity())) {
            addError(DeckValidatorErrorType.OTHER, card.getName(), "Invalid color (" + colorIdentity.toString() + ')', true);
            valid = false;
        }
    }
    for (Card card : deck.getSideboard()) {
        if (!ManaUtil.isColorIdentityCompatible(colorIdentity, card.getColorIdentity())) {
            addError(DeckValidatorErrorType.OTHER, card.getName(), "Invalid color (" + colorIdentity.toString() + ')', true);
            valid = false;
        }
    }
    for (Card card : deck.getCards()) {
        if (!isSetAllowed(card.getExpansionSetCode())) {
            if (!legalSets(card)) {
                addError(DeckValidatorErrorType.WRONG_SET, card.getName(), "Not allowed Set: " + card.getExpansionSetCode(), true);
                valid = false;
            }
        }
    }
    for (Card card : deck.getSideboard()) {
        if (!isSetAllowed(card.getExpansionSetCode())) {
            if (!legalSets(card)) {
                addError(DeckValidatorErrorType.WRONG_SET, card.getName(), "Not allowed Set: " + card.getExpansionSetCode(), true);
                valid = false;
            }
        }
    }
    // Check for companion legality
    if (companion != null) {
        Set<Card> cards = new HashSet<>(deck.getCards());
        cards.addAll(commanders);
        for (Ability ability : companion.getAbilities()) {
            if (ability instanceof CompanionAbility) {
                CompanionAbility companionAbility = (CompanionAbility) ability;
                if (!companionAbility.isLegal(cards, getDeckMinSize())) {
                    addError(DeckValidatorErrorType.PRIMARY, companion.getName(), "Commander Companion (deck invalid for companion)", true);
                    valid = false;
                }
                break;
            }
        }
    }
    return valid;
}
Also used : PartnerAbility(mage.abilities.keyword.PartnerAbility) PennyDreadfulLegalityUtil(mage.cards.decks.PennyDreadfulLegalityUtil) java.util(java.util) DeckValidatorErrorType(mage.cards.decks.DeckValidatorErrorType) PartnerWithAbility(mage.abilities.keyword.PartnerWithAbility) Deck(mage.cards.decks.Deck) CanBeYourCommanderAbility(mage.abilities.common.CanBeYourCommanderAbility) Sets(mage.cards.Sets) Constructed(mage.cards.decks.Constructed) ManaUtil(mage.util.ManaUtil) ExpansionSet(mage.cards.ExpansionSet) CardType(mage.constants.CardType) CompanionAbility(mage.abilities.keyword.CompanionAbility) Card(mage.cards.Card) Ability(mage.abilities.Ability) FilterMana(mage.filter.FilterMana) PartnerAbility(mage.abilities.keyword.PartnerAbility) PartnerWithAbility(mage.abilities.keyword.PartnerWithAbility) CanBeYourCommanderAbility(mage.abilities.common.CanBeYourCommanderAbility) CompanionAbility(mage.abilities.keyword.CompanionAbility) Ability(mage.abilities.Ability) FilterMana(mage.filter.FilterMana) CompanionAbility(mage.abilities.keyword.CompanionAbility) Card(mage.cards.Card) PartnerWithAbility(mage.abilities.keyword.PartnerWithAbility)

Example 4 with CompanionAbility

use of mage.abilities.keyword.CompanionAbility in project mage by magefree.

the class FreeformCommander method validate.

@Override
public boolean validate(Deck deck) {
    boolean valid = true;
    errorsList.clear();
    FilterMana colorIdentity = new FilterMana();
    Set<Card> commanders = new HashSet<>();
    Card companion = null;
    if (deck.getSideboard().size() == 1) {
        commanders.add(deck.getSideboard().iterator().next());
    } else if (deck.getSideboard().size() == 2) {
        Iterator<Card> iter = deck.getSideboard().iterator();
        Card card1 = iter.next();
        Card card2 = iter.next();
        if (card1.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card1;
            commanders.add(card2);
        } else if (card2.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card2;
            commanders.add(card1);
        } else {
            commanders.add(card1);
            commanders.add(card2);
        }
    } else if (deck.getSideboard().size() == 3) {
        Iterator<Card> iter = deck.getSideboard().iterator();
        Card card1 = iter.next();
        Card card2 = iter.next();
        Card card3 = iter.next();
        if (card1.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card1;
            commanders.add(card2);
            commanders.add(card3);
        } else if (card2.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card2;
            commanders.add(card1);
            commanders.add(card3);
        } else if (card3.getAbilities().stream().anyMatch(ability -> ability instanceof CompanionAbility)) {
            companion = card3;
            commanders.add(card1);
            commanders.add(card2);
        } else {
            addError(DeckValidatorErrorType.PRIMARY, "Commander", "Sideboard must contain only the commander(s) and up to 1 companion");
            valid = false;
        }
    } else {
        addError(DeckValidatorErrorType.PRIMARY, "Commander", "Sideboard must contain only the commander(s) and up to 1 companion");
        valid = false;
    }
    if (companion != null && deck.getCards().size() + deck.getSideboard().size() != 101) {
        addError(DeckValidatorErrorType.DECK_SIZE, "Deck", "Must contain " + 101 + " cards (companion doesn't count for deck size): has " + (deck.getCards().size() + deck.getSideboard().size()) + " cards");
        valid = false;
    } else if (companion == null && deck.getCards().size() + deck.getSideboard().size() != 100) {
        addError(DeckValidatorErrorType.DECK_SIZE, "Deck", "Must contain " + 100 + " cards: has " + (deck.getCards().size() + deck.getSideboard().size()) + " cards");
        valid = false;
    }
    Map<String, Integer> counts = new HashMap<>();
    countCards(counts, deck.getCards());
    countCards(counts, deck.getSideboard());
    valid = checkCounts(1, counts) && valid;
    Set<String> commanderNames = new HashSet<>();
    for (Card commander : commanders) {
        commanderNames.add(commander.getName());
    }
    for (Card commander : commanders) {
        if (!commander.hasCardTypeForDeckbuilding(CardType.CREATURE) && !commander.isLegendary()) {
            addError(DeckValidatorErrorType.PRIMARY, commander.getName(), "For Freeform Commander, the commander must be a creature or be legendary. Yours was: " + commander.getName(), true);
            valid = false;
        }
        if (commanders.size() == 2) {
            if (!commander.getAbilities().contains(PartnerAbility.getInstance())) {
                boolean partnersWith = commander.getAbilities().stream().filter(PartnerWithAbility.class::isInstance).map(PartnerWithAbility.class::cast).map(PartnerWithAbility::getPartnerName).anyMatch(commanderNames::contains);
                if (!partnersWith) {
                    addError(DeckValidatorErrorType.PRIMARY, commander.getName(), "Commander without Partner (" + commander.getName() + ')', true);
                    valid = false;
                }
            }
        }
        ManaUtil.collectColorIdentity(colorIdentity, commander.getColorIdentity());
    }
    // no needs in cards check on wrong commanders
    if (!valid) {
        return false;
    }
    for (Card card : deck.getCards()) {
        if (!ManaUtil.isColorIdentityCompatible(colorIdentity, card.getColorIdentity())) {
            addError(DeckValidatorErrorType.OTHER, card.getName(), "Invalid color (" + colorIdentity.toString() + ')', true);
            valid = false;
        }
    }
    for (Card card : deck.getSideboard()) {
        if (!ManaUtil.isColorIdentityCompatible(colorIdentity, card.getColorIdentity())) {
            addError(DeckValidatorErrorType.OTHER, card.getName(), "Invalid color (" + colorIdentity.toString() + ')', true);
            valid = false;
        }
    }
    // Check for companion legality
    if (companion != null) {
        Set<Card> cards = new HashSet<>(deck.getCards());
        cards.addAll(commanders);
        for (Ability ability : companion.getAbilities()) {
            if (ability instanceof CompanionAbility) {
                CompanionAbility companionAbility = (CompanionAbility) ability;
                if (!companionAbility.isLegal(cards, getDeckMinSize())) {
                    addError(DeckValidatorErrorType.PRIMARY, companion.getName(), "Commander Companion (deck invalid for companion)", true);
                    valid = false;
                }
                break;
            }
        }
    }
    return valid;
}
Also used : PartnerAbility(mage.abilities.keyword.PartnerAbility) java.util(java.util) DeckValidatorErrorType(mage.cards.decks.DeckValidatorErrorType) PartnerWithAbility(mage.abilities.keyword.PartnerWithAbility) Deck(mage.cards.decks.Deck) Sets(mage.cards.Sets) Constructed(mage.cards.decks.Constructed) ManaUtil(mage.util.ManaUtil) ExpansionSet(mage.cards.ExpansionSet) CardType(mage.constants.CardType) CompanionAbility(mage.abilities.keyword.CompanionAbility) Card(mage.cards.Card) Ability(mage.abilities.Ability) FilterMana(mage.filter.FilterMana) PartnerAbility(mage.abilities.keyword.PartnerAbility) PartnerWithAbility(mage.abilities.keyword.PartnerWithAbility) CompanionAbility(mage.abilities.keyword.CompanionAbility) Ability(mage.abilities.Ability) FilterMana(mage.filter.FilterMana) CompanionAbility(mage.abilities.keyword.CompanionAbility) Card(mage.cards.Card) PartnerWithAbility(mage.abilities.keyword.PartnerWithAbility)

Aggregations

java.util (java.util)4 Ability (mage.abilities.Ability)4 CompanionAbility (mage.abilities.keyword.CompanionAbility)4 Card (mage.cards.Card)4 Constructed (mage.cards.decks.Constructed)4 Deck (mage.cards.decks.Deck)4 DeckValidatorErrorType (mage.cards.decks.DeckValidatorErrorType)4 CardType (mage.constants.CardType)4 FilterMana (mage.filter.FilterMana)4 ManaUtil (mage.util.ManaUtil)4 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 MageObject (mage.MageObject)1 ObjectColor (mage.ObjectColor)1 FriendsForeverAbility (mage.abilities.keyword.FriendsForeverAbility)1 PennyDreadfulLegalityUtil (mage.cards.decks.PennyDreadfulLegalityUtil)1 CardUtil (mage.util.CardUtil)1