diff --git a/src/main/java/forge/quest/data/GameFormatQuest.java b/src/main/java/forge/quest/data/GameFormatQuest.java index 00ab56be056..7ff17f065b6 100644 --- a/src/main/java/forge/quest/data/GameFormatQuest.java +++ b/src/main/java/forge/quest/data/GameFormatQuest.java @@ -21,11 +21,15 @@ import java.util.Collections; import java.util.List; import java.util.ArrayList; + +import com.google.common.base.Function; import com.google.common.base.Predicate; import forge.Singletons; import forge.card.CardEdition; +import forge.card.CardRulesPredicates; import forge.game.GameFormat; +import forge.item.CardPrinted; /** @@ -33,8 +37,54 @@ import forge.game.GameFormat; * is not immutable. This class is necessary because we may wish to update * its contents in certain circumstances, and it was safer to create a new * class than to make the preset game formats modifiable. + * + * N.B! This class MUST NOT INHERIT GameFormat.java as long as that class + * has its data in immutable lists, otherwise this class WILL NOT WORK! + * The whole purpose of this class is to allow modification of the lists, + * otherwise the plain GameFormat.java would have sufficed! */ -public final class GameFormatQuest extends GameFormat { +public final class GameFormatQuest { + + private final String name; + // contains allowed sets, when empty allows all sets + private final List allowedSetCodes; + private final List bannedCardNames; + + private Predicate filterRules; + private Predicate filterPrinted; + + /** + * Instantiates a new game format based on an existing format. + * + * @param toCopy + * an existing format + */ + public GameFormatQuest(final GameFormat toCopy) { + this.name = new String(toCopy.getName()); + + this.allowedSetCodes = new ArrayList(); + allowedSetCodes.addAll(toCopy.getAllowedSetCodes()); + + this.bannedCardNames = new ArrayList(); + bannedCardNames.addAll(toCopy.getBannedCardNames()); + + this.filterRules = this.buildFilterRules(); + this.filterPrinted = this.buildFilterPrinted(); + } + + /** + * Instantiates an empty new game format. + * + * @param newName + * String, the name + */ + public GameFormatQuest(final String newName) { + this.name = new String(newName); + + this.allowedSetCodes = new ArrayList(); + this.bannedCardNames = new ArrayList(); + } + /** * Instantiates a new game format based on two lists. @@ -47,29 +97,41 @@ public final class GameFormatQuest extends GameFormat { * List, these will be the banned cards */ public GameFormatQuest(final String newName, final List setsToAllow, final List cardsToBan) { - super(newName, setsToAllow, cardsToBan); + this.name = new String(newName); + + this.allowedSetCodes = new ArrayList(); + allowedSetCodes.addAll(setsToAllow); + + this.bannedCardNames = new ArrayList(); + bannedCardNames.addAll(cardsToBan); + + this.filterRules = this.buildFilterRules(); + this.filterPrinted = this.buildFilterPrinted(); } - /** - * Instantiates a new game format based on an existing format. - * - * @param toCopy - * an existing format - */ - public GameFormatQuest(final GameFormat toCopy) { - this(toCopy.getName(), toCopy.getAllowedSetCodes(), toCopy.getBannedCardNames()); + private Predicate buildFilterPrinted() { + final Predicate banNames = CardPrinted.Predicates.namesExcept(this.bannedCardNames); + if (this.allowedSetCodes.isEmpty()) { + return banNames; + } + return com.google.common.base.Predicates.and(banNames, CardPrinted.Predicates.printedInSets(this.allowedSetCodes, true)); } + private Predicate buildFilterRules() { + final Predicate banNames = CardPrinted.Predicates.namesExcept(this.bannedCardNames); + if (this.allowedSetCodes.isEmpty()) { + return banNames; + } + return com.google.common.base.Predicates.and(banNames, com.google.common.base.Predicates.compose(CardRulesPredicates.wasPrintedInSets(this.allowedSetCodes), CardPrinted.FN_GET_RULES)); + } /** * * Updates the filters based on the current list data. */ public void updateFilters() { - // nothing to do here. - // predicates hold references to lists and thus get auto updated. - - // remove this method after reading. + this.filterRules = this.buildFilterRules(); + this.filterPrinted = this.buildFilterPrinted(); } /** @@ -92,6 +154,51 @@ public final class GameFormatQuest extends GameFormat { } } + /** + * @param banCard + * the card to ban + */ + public void addBannedCard(final String banCard) { + if (!bannedCardNames.contains(banCard)) { + bannedCardNames.add(banCard); + } + } + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + return this.name; + } + + /** + * Gets the filter rules. + * + * @return the filter rules + */ + public Predicate getFilterRules() { + return this.filterRules; + } + + /** + * Gets the filter printed. + * + * @return the filter printed + */ + public Predicate getFilterPrinted() { + return this.filterPrinted; + } + + /** + * Gets the set list. + * + * @return unmodifiable list of allowed set codes + */ + public List getAllowedSetCodes() { + return Collections.unmodifiableList(this.allowedSetCodes); + } /** * Get the list of excluded sets. @@ -128,6 +235,26 @@ public final class GameFormatQuest extends GameFormat { updateFilters(); } + /** + * Gets the banned cards. + * + * @return unmodifiable list of banned card names + */ + public List getBannedCardNames() { + return Collections.unmodifiableList(this.bannedCardNames); + } + + /** + * Checks if is sets the legal. + * + * @param setCode + * the set code + * @return true, if is sets the legal + */ + public boolean isSetLegal(final String setCode) { + return this.allowedSetCodes.isEmpty() || this.allowedSetCodes.contains(setCode); + } + /** * Checks if the current format contains sets with snow-land (horrible hack...). * @return boolean, contains snow-land sets. @@ -137,6 +264,17 @@ public final class GameFormatQuest extends GameFormat { return (this.isSetLegal("ICE") || this.isSetLegal("CSP")); } + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return this.name + " (format)"; + } + + /** * The Class Predicates. */ @@ -165,5 +303,11 @@ public final class GameFormatQuest extends GameFormat { } } + public static final Function FN_GET_NAME = new Function() { + @Override + public String apply(GameFormat arg1) { + return arg1.getName(); + } + }; }