From 01ca0205e0258435d65fb12a68424a8e62c1e610 Mon Sep 17 00:00:00 2001 From: Maxmtg Date: Sun, 25 Nov 2012 20:14:39 +0000 Subject: [PATCH] commander stored as a single card in deck/Deck.java removed inpropper use of getType of InventoryItem objects --- src/main/java/forge/deck/Deck.java | 41 +++++---- src/main/java/forge/item/CardDb.java | 2 + src/main/java/forge/item/ItemPredicate.java | 94 ++++---------------- src/main/java/forge/item/TournamentPack.java | 6 +- 4 files changed, 51 insertions(+), 92 deletions(-) diff --git a/src/main/java/forge/deck/Deck.java b/src/main/java/forge/deck/Deck.java index 003265ce843..92422852d80 100644 --- a/src/main/java/forge/deck/Deck.java +++ b/src/main/java/forge/deck/Deck.java @@ -35,6 +35,7 @@ import com.google.common.base.Function; import forge.deck.io.DeckFileHeader; import forge.deck.io.DeckSerializer; import forge.gui.deckeditor.tables.TableSorter; +import forge.item.CardDb; import forge.item.CardPrinted; import forge.item.ItemPoolView; import forge.game.GameType; @@ -62,7 +63,7 @@ public class Deck extends DeckBase { private final DeckSection main; private final DeckSection sideboard; - private final DeckSection commander; + private CardPrinted commander; private final DeckSection planes; private final DeckSection schemes; @@ -85,7 +86,7 @@ public class Deck extends DeckBase { super(name0); this.main = new DeckSection(); this.sideboard = new DeckSection(); - this.commander = new DeckSection(); + this.commander = null; this.planes = new DeckSection(); this.schemes = new DeckSection(); } @@ -203,7 +204,9 @@ public class Deck extends DeckBase { d.getMain().set(Deck.readCardList(sections.get("main"))); d.getSideboard().set(Deck.readCardList(sections.get("sideboard"))); - d.getCommander().set(Deck.readCardList(sections.get("commander"))); + List cmd = Deck.readCardList(sections.get("commander")); + String cmdName = cmd.isEmpty() ? null : cmd.get(0); + d.commander = CardDb.instance().isCardSupported(cmdName) ? CardDb.instance().getCard(cmdName) : null; d.getPlanes().set(Deck.readCardList(sections.get("planes"))); d.getSchemes().set(Deck.readCardList(sections.get("schemes"))); return d; @@ -244,16 +247,20 @@ public class Deck extends DeckBase { Collections.sort(main2sort, TableSorter.BY_NAME_THEN_SET); final List out = new ArrayList(); for (final Entry e : main2sort) { - final CardPrinted card = e.getKey(); - final boolean hasBadSetInfo = "???".equals(card.getEdition()) || StringUtils.isBlank(card.getEdition()); - if (hasBadSetInfo) { - out.add(String.format("%d %s", e.getValue(), card.getName())); - } else { - out.add(String.format("%d %s|%s", e.getValue(), card.getName(), card.getEdition())); - } + out.add(serializeSingleCard(e.getKey(), e.getValue())); } return out; } + + private static String serializeSingleCard(CardPrinted card, Integer n) { + + final boolean hasBadSetInfo = "???".equals(card.getEdition()) || StringUtils.isBlank(card.getEdition()); + if (hasBadSetInfo) { + return String.format("%d %s", n, card.getName()); + } else { + return String.format("%d %s|%s", n, card.getName(), card.getEdition()); + } + } /** *

@@ -279,8 +286,10 @@ public class Deck extends DeckBase { out.add(String.format("%s", "[sideboard]")); out.addAll(Deck.writeCardPool(this.getSideboard())); - out.add(String.format("%s", "[commander]")); - out.addAll(Deck.writeCardPool(this.getCommander())); + if ( getCommander() != null ) { + out.add(String.format("%s", "[commander]")); + out.add(Deck.serializeSingleCard(getCommander(), 1)); + } out.add(String.format("%s", "[planes]")); out.addAll(Deck.writeCardPool(this.getPlanes())); @@ -346,14 +355,14 @@ public class Deck extends DeckBase { if(type == GameType.Commander) {//Must contain exactly 1 legendary Commander and no sideboard. //TODO:Enforce color identity - if(getCommander().countAll() != 1) + if ( null == getCommander()) return false; - if(!getCommander().toFlatList().get(0).getType().contains("Legendary")) + if(!getCommander().getCard().getType().isLegendary()) return false; //No sideboarding in Commander - if(getSideboard().countAll() > 0) + if(!getSideboard().isEmpty()) return false; } else if(type == GameType.Planechase) @@ -389,7 +398,7 @@ public class Deck extends DeckBase { /** * @return the commander */ - public DeckSection getCommander() { + public CardPrinted getCommander() { return commander; } diff --git a/src/main/java/forge/item/CardDb.java b/src/main/java/forge/item/CardDb.java index dd4a96ed0f4..6d1605dba6f 100644 --- a/src/main/java/forge/item/CardDb.java +++ b/src/main/java/forge/item/CardDb.java @@ -220,6 +220,8 @@ public final class CardDb { * @return true, if is card supported */ public boolean isCardSupported(final String cardName0) { + if ( null == cardName0 ) return false; // obviously + final boolean isFoil = this.isFoil(cardName0); final String cardName = isFoil ? this.removeFoilSuffix(cardName0) : cardName0; final ImmutablePair nameWithSet = CardDb.splitCardName(cardName); diff --git a/src/main/java/forge/item/ItemPredicate.java b/src/main/java/forge/item/ItemPredicate.java index e53c78712d2..9caeb713594 100644 --- a/src/main/java/forge/item/ItemPredicate.java +++ b/src/main/java/forge/item/ItemPredicate.java @@ -13,141 +13,85 @@ public abstract class ItemPredicate { // Static builder methods - they choose concrete implementation by // themselves - /** - * Booster Pack. - * - * @return the predicate - */ - public static Predicate boosterPack() { - return new PredicateBoosterPack(); - } - - /** - * Fat Pack. - * - * @return the predicate - */ - public static Predicate fatPack() { - return new PredicateFatPack(); - } - - /** - * Tournament Pack. - * - * @return the predicate - */ - public static Predicate tournamentPack() { - return new PredicateTournamentPack(); - } - - /** - * Starter Deck. - * - * @return the predicate - */ - public static Predicate starterDeck() { - return new PredicateStarterDeck(); - } - - /** - * Prebuilt Deck. - * - * @return the predicate - */ - public static Predicate prebuiltDeck() { - return new PredicatePrebuiltDeck(); - } /** * Checks that the inventory item is a Booster Pack. * * @return the predicate */ - public static class PredicateBoosterPack implements Predicate { + public static Predicate IsBoosterPack = new Predicate() { @Override public boolean apply(final InventoryItem card) { - return card.getType() == "Booster Pack"; + return card instanceof BoosterPack; } - } + }; /** * Checks that the inventory item is a Fat Pack. * * @return the predicate */ - public static class PredicateFatPack implements Predicate { + public static Predicate IsFatPack = new Predicate() { @Override public boolean apply(final InventoryItem card) { - return card.getType() == "Fat Pack"; + return card instanceof FatPack; } - } + }; /** * Checks that the inventory item is a Tournament Pack. * * @return the predicate */ - public static class PredicateTournamentPack implements Predicate { + public static Predicate IsTournamentPack = new Predicate() { @Override public boolean apply(final InventoryItem card) { - return card.getType() == "Tournament Pack"; + return card instanceof TournamentPack && !((TournamentPack)card).isStarterDeck(); } - } + }; /** * Checks that the inventory item is a Starter Deck. * * @return the predicate */ - public static class PredicateStarterDeck implements Predicate { + public static Predicate IsStarterDeck = new Predicate() { @Override public boolean apply(final InventoryItem card) { - return card.getType() == "Starter Deck"; + return card instanceof TournamentPack && ((TournamentPack)card).isStarterDeck(); } - } + }; /** * Checks that the inventory item is a Prebuilt Deck. * * @return the predicate */ - public static class PredicatePrebuiltDeck implements Predicate { + public static Predicate IsPrebuiltDeck = new Predicate() { @Override public boolean apply(final InventoryItem card) { - return card.getType() == "Prebuilt Deck"; + return card instanceof PreconDeck; } - } - + }; + + /** * The Class Presets. */ public static class Presets { - /** The Item IsBoosterPack. */ - public static final Predicate IS_BOOSTER_PACK = boosterPack(); - /** The Item IsFatPack. */ - public static final Predicate IS_FAT_PACK = fatPack(); - - /** The Item IsTournamentPack. */ - public static final Predicate IS_TOURNAMENT_PACK = tournamentPack(); /** The Item IsPack. */ @SuppressWarnings("unchecked") - public static final Predicate IS_PACK = Predicates.or(IS_BOOSTER_PACK, IS_FAT_PACK, IS_TOURNAMENT_PACK); - - /** The Item IsStarterDeck. */ - public static final Predicate IS_STARTER_DECK = starterDeck(); - - /** The Item IsPrebuiltDeck. */ - public static final Predicate IS_PREBUILT_DECK = prebuiltDeck(); + public static final Predicate IS_PACK = Predicates.or(IsBoosterPack, IsFatPack, IsTournamentPack); /** The Item IsDeck. */ - public static final Predicate IS_DECK = Predicates.or(IS_STARTER_DECK, IS_PREBUILT_DECK); + public static final Predicate IS_DECK = Predicates.or(IsStarterDeck, IsPrebuiltDeck); } } diff --git a/src/main/java/forge/item/TournamentPack.java b/src/main/java/forge/item/TournamentPack.java index 837005dc9ac..070bbb13e3d 100644 --- a/src/main/java/forge/item/TournamentPack.java +++ b/src/main/java/forge/item/TournamentPack.java @@ -58,10 +58,14 @@ public class TournamentPack extends OpenablePack { return ImageCache.SEALED_PRODUCT + "tournamentpacks/" + this.contents.getEdition(); } + public final boolean isStarterDeck() { + return contents.getCommon() < 30; + } + @Override public final String getType() { - return contents.getCommon() >= 30 ? "Tournament Pack" : "Starter Deck"; + return !isStarterDeck() ? "Tournament Pack" : "Starter Deck"; } @Override