From 74a017586a9da16a6359f6e763aa074ce77813b5 Mon Sep 17 00:00:00 2001 From: Lymia Aluysia Date: Sat, 24 Sep 2016 14:15:47 -0500 Subject: [PATCH] Exclude custom sets from constructed formats. --- .../main/java/mage/cards/ExpansionSet.java | 4 ++ Mage/src/main/java/mage/cards/Sets.java | 16 ++++--- .../java/mage/cards/decks/Constructed.java | 47 +++++++++++-------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/Mage/src/main/java/mage/cards/ExpansionSet.java b/Mage/src/main/java/mage/cards/ExpansionSet.java index e7a789efd4..4af631d6b0 100644 --- a/Mage/src/main/java/mage/cards/ExpansionSet.java +++ b/Mage/src/main/java/mage/cards/ExpansionSet.java @@ -68,6 +68,8 @@ public abstract class ExpansionSet implements Serializable { protected String packageName; protected int maxCardNumberInBooster; // used to ommit cards with collector numbers beyond the regular cards in a set for boosters + protected boolean isCustomSet = false; + protected final EnumMap> savedCards; public ExpansionSet(String name, String code, String packageName, Date releaseDate, SetType setType) { @@ -374,6 +376,8 @@ public abstract class ExpansionSet implements Serializable { return null; } + public boolean isCustomSet() { return isCustomSet; } + public void removeSavedCards() { savedCards.clear(); } diff --git a/Mage/src/main/java/mage/cards/Sets.java b/Mage/src/main/java/mage/cards/Sets.java index f2aaea4437..794b8e0536 100644 --- a/Mage/src/main/java/mage/cards/Sets.java +++ b/Mage/src/main/java/mage/cards/Sets.java @@ -30,11 +30,7 @@ package mage.cards; import java.io.FileNotFoundException; import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; +import java.util.*; import mage.cards.decks.DeckCardInfo; import mage.cards.decks.DeckCardLists; @@ -61,6 +57,8 @@ public class Sets extends HashMap { return fINSTANCE; } + private Set customSets = new HashSet<>(); + private Sets() { ArrayList packages = new ArrayList<>(); packages.add("mage.sets"); @@ -72,8 +70,14 @@ public class Sets extends HashMap { } } - private void addSet(ExpansionSet set) { + public void addSet(ExpansionSet set) { + if(containsKey(set.getCode())) throw new IllegalArgumentException("Set code "+set.getCode()+" already exists."); this.put(set.getCode(), set); + if(set.isCustomSet) customSets.add(set.getCode()); + } + + public static boolean isCustomSet(String setCode) { + return getInstance().customSets.contains(setCode); } /** diff --git a/Mage/src/main/java/mage/cards/decks/Constructed.java b/Mage/src/main/java/mage/cards/decks/Constructed.java index 7824e178fe..ad4a541365 100644 --- a/Mage/src/main/java/mage/cards/decks/Constructed.java +++ b/Mage/src/main/java/mage/cards/decks/Constructed.java @@ -27,13 +27,10 @@ */ package mage.cards.decks; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; import mage.cards.Card; +import mage.cards.Sets; import mage.cards.repository.CardInfo; import mage.cards.repository.CardRepository; import mage.constants.Rarity; @@ -52,6 +49,9 @@ public class Constructed extends DeckValidator { protected List setCodes = new ArrayList<>(); protected List rarities = new ArrayList<>(); + protected boolean allowAllCustomSets = false; + protected Set allowedCustomSetCodes = new HashSet<>(); + public Constructed() { super("Constructed"); } @@ -126,22 +126,21 @@ public class Constructed extends DeckValidator { } } - if (!setCodes.isEmpty()) { - for (Card card : deck.getCards()) { - if (!setCodes.contains(card.getExpansionSetCode())) { - if( !legalSets(card) ){ - valid = false; - } - } - } - for (Card card : deck.getSideboard()) { - if (!setCodes.contains(card.getExpansionSetCode())) { - if( !legalSets(card) ){ - valid = false; - } + for (Card card : deck.getCards()) { + if (!isSetAllowed(card.getExpansionSetCode())) { + if( !legalSets(card) ){ + valid = false; } } } + for (Card card : deck.getSideboard()) { + if (!isSetAllowed(card.getExpansionSetCode())) { + if( !legalSets(card) ){ + valid = false; + } + } + } + logger.debug("DECK validate end: " + name + " deckname: " + deck.getName() + " invalids:" + invalid.size()); return valid; } @@ -167,6 +166,16 @@ public class Constructed extends DeckValidator { return legal; } + /** + * Checks if a given set is legal in this format. + * @param code - the set code to check + * @return Whether the set is legal in this format. + */ + protected boolean isSetAllowed(String code) { + if(Sets.isCustomSet(code)) return allowAllCustomSets || allowedCustomSetCodes.contains(code); + else return setCodes.isEmpty() || setCodes.contains(code); + } + /** * Checks if the given card is legal in any of the given sets * @param card - the card to check @@ -177,7 +186,7 @@ public class Constructed extends DeckValidator { boolean legal = false; List cardInfos = CardRepository.instance.findCards(card.getName()); for (CardInfo cardInfo : cardInfos) { - if (setCodes.contains(cardInfo.getSetCode())) { + if (isSetAllowed(cardInfo.getSetCode())) { legal = true; break; }