hide some readers, collections and simple enums as nested classes,

This commit is contained in:
Maxmtg
2013-11-21 22:53:20 +00:00
parent 86b422230e
commit 17b97aca28
45 changed files with 712 additions and 578 deletions

View File

@@ -8,12 +8,9 @@ import java.util.TreeMap;
import forge.card.CardDb;
import forge.card.CardEdition;
import forge.card.CardRules;
import forge.card.EditionCollection;
import forge.card.FatPackTemplate;
import forge.card.FormatCollection;
import forge.card.PrintSheet;
import forge.card.SealedProductTemplate;
import forge.game.GameFormat;
import forge.item.FatPack;
import forge.item.SealedProduct;
import forge.util.storage.IStorage;
import forge.util.storage.StorageBase;
@@ -27,12 +24,11 @@ public class StaticData {
private final CardDb commonCards;
private final CardDb variantCards;
private final EditionCollection editions;
private final FormatCollection formats;
private final IStorage<SealedProductTemplate> boosters;
private final IStorage<SealedProductTemplate> specialBoosters;
private final IStorage<SealedProductTemplate> tournaments;
private final IStorage<FatPackTemplate> fatPacks;
private final CardEdition.Collection editions;
private final IStorage<SealedProduct.Template> boosters;
private final IStorage<SealedProduct.Template> specialBoosters;
private final IStorage<SealedProduct.Template> tournaments;
private final IStorage<FatPack.Template> fatPacks;
private final IStorage<PrintSheet> printSheets;
private static StaticData lastInstance = null;
@@ -40,7 +36,7 @@ public class StaticData {
public StaticData(ICardStorageReader reader, String editionFolder, String blockDataFolder) {
this.editions = new EditionCollection(new CardEdition.Reader(new File(editionFolder)));
this.editions = new CardEdition.Collection(new CardEdition.Reader(new File(editionFolder)));
lastInstance = this;
final Map<String, CardRules> regularCards = new TreeMap<String, CardRules>(String.CASE_INSENSITIVE_ORDER);
@@ -62,12 +58,10 @@ public class StaticData {
variantCards = new CardDb(variantsCards, editions, false);
this.formats = new FormatCollection(new GameFormat.Reader(new File(blockDataFolder, "formats.txt")));
this.boosters = new StorageBase<SealedProductTemplate>("Boosters", editions.getBoosterGenerator());
this.specialBoosters = new StorageBase<SealedProductTemplate>("Special boosters", new SealedProductTemplate.Reader(new File(blockDataFolder, "boosters-special.txt")));
this.tournaments = new StorageBase<SealedProductTemplate>("Starter sets", new SealedProductTemplate.Reader(new File(blockDataFolder, "starters.txt")));
this.fatPacks = new StorageBase<FatPackTemplate>("Fat packs", new FatPackTemplate.Reader("res/blockdata/fatpacks.txt"));
this.boosters = new StorageBase<SealedProduct.Template>("Boosters", editions.getBoosterGenerator());
this.specialBoosters = new StorageBase<SealedProduct.Template>("Special boosters", new SealedProduct.Template.Reader(new File(blockDataFolder, "boosters-special.txt")));
this.tournaments = new StorageBase<SealedProduct.Template>("Starter sets", new SealedProduct.Template.Reader(new File(blockDataFolder, "starters.txt")));
this.fatPacks = new StorageBase<FatPack.Template>("Fat packs", new FatPack.Template.Reader("res/blockdata/fatpacks.txt"));
this.printSheets = new StorageBase<PrintSheet>("Special print runs", new PrintSheet.Reader(new File(blockDataFolder, "printsheets.txt")));
}
@@ -76,30 +70,26 @@ public class StaticData {
}
public final EditionCollection getEditions() {
public final CardEdition.Collection getEditions() {
return this.editions;
}
public final FormatCollection getFormats() {
return this.formats;
}
/** @return {@link forge.util.storage.IStorageView}<{@link forge.card.FatPackTemplate}> */
public IStorage<FatPackTemplate> getFatPacks() {
/** @return {@link forge.util.storage.IStorageView}<{@link forge.item.FatPackTemplate}> */
public IStorage<FatPack.Template> getFatPacks() {
return fatPacks;
}
/** @return {@link forge.util.storage.IStorageView}<{@link forge.card.BoosterTemplate}> */
public final IStorage<SealedProductTemplate> getTournamentPacks() {
public final IStorage<SealedProduct.Template> getTournamentPacks() {
return tournaments;
}
/** @return {@link forge.util.storage.IStorageView}<{@link forge.card.BoosterTemplate}> */
public final IStorage<SealedProductTemplate> getBoosters() {
public final IStorage<SealedProduct.Template> getBoosters() {
return boosters;
}
public final IStorage<SealedProductTemplate> getSpecialBoosters() {
public final IStorage<SealedProduct.Template> getSpecialBoosters() {
return specialBoosters;
}

View File

@@ -1,7 +0,0 @@
package forge.card;
public enum AggregationMethod {
USE_ACTIVE_FACE,
USE_PRIMARY_FACE,
AGGREGATE;
}

View File

@@ -37,6 +37,7 @@ import com.google.common.collect.Lists;
import forge.StaticData;
import forge.item.PaperCard;
import forge.item.IPaperCard;
import forge.item.SealedProduct;
import forge.util.TextUtil;
/**
@@ -57,7 +58,7 @@ public class BoosterGenerator {
return cachedSheets.get(key);
}
public static final List<PaperCard> getBoosterPack(SealedProductTemplate booster) {
public static final List<PaperCard> getBoosterPack(SealedProduct.Template booster) {
List<PaperCard> result = new ArrayList<PaperCard>();
for(Pair<String, Integer> slot : booster.getSlots()) {
String slotType = slot.getLeft(); // add expansion symbol here?

View File

@@ -1,49 +0,0 @@
/*
* Forge: Play Magic: the Gathering.
* Copyright (C) 2011 Forge Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge.card;
/**
* The Enum CardCoreType.
*/
public enum CardCoreType {
/** The Artifact. */
Artifact,
/** The Creature. */
Creature,
/** The Enchantment. */
Enchantment,
/** The Instant. */
Instant,
/** The Land. */
Land,
/** The Plane. */
Plane,
/** The Planeswalker. */
Planeswalker,
/** The Scheme. */
Scheme,
/** The Sorcery. */
Sorcery,
/** The Tribal. */
Tribal,
/** The Vanguard. */
Vanguard,
/** The Phenomenon. */
Phenomenon
}

View File

@@ -54,10 +54,10 @@ public final class CardDb implements ICardDatabase {
private final List<PaperCard> allCards = new ArrayList<PaperCard>();
private final List<PaperCard> roAllCards = Collections.unmodifiableList(allCards);
private final Collection<PaperCard> roUniqueCards = Collections.unmodifiableCollection(uniqueCardsByName.values());
private final EditionCollection editions;
private final CardEdition.Collection editions;
public CardDb(Map<String, CardRules> rules, EditionCollection editions0, boolean logMissingCards) {
public CardDb(Map<String, CardRules> rules, CardEdition.Collection editions0, boolean logMissingCards) {
this.rulesByName = rules;
this.editions = editions0;
List<String> missingCards = new ArrayList<String>();

View File

@@ -22,17 +22,25 @@ import java.io.FilenameFilter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import forge.item.SealedProduct;
import forge.util.FileSection;
import forge.util.FileUtil;
import forge.util.IItemReader;
import forge.util.storage.StorageBase;
import forge.util.storage.StorageReaderBase;
import forge.util.storage.StorageReaderFolder;
@@ -98,7 +106,7 @@ public final class CardEdition implements Comparable<CardEdition> { // immutable
private int boosterArts = 1;
private SealedProductTemplate boosterTpl = null;
private SealedProduct.Template boosterTpl = null;
private CardEdition(CardInSet[] cards) {
this.cards = cards;
@@ -202,7 +210,7 @@ public final class CardEdition implements Comparable<CardEdition> { // immutable
return boosterArts;
}
public SealedProductTemplate getBoosterTemplate() {
public SealedProduct.Template getBoosterTemplate() {
return boosterTpl;
}
@@ -247,7 +255,7 @@ public final class CardEdition implements Comparable<CardEdition> { // immutable
res.boosterArts = section.getInt("BoosterCovers", 1);
String boosterDesc = section.get("Booster");
res.boosterTpl = boosterDesc == null ? null : new SealedProductTemplate(res.code, SealedProductTemplate.Reader.parseSlots(boosterDesc));
res.boosterTpl = boosterDesc == null ? null : new SealedProduct.Template(res.code, SealedProduct.Template.Reader.parseSlots(boosterDesc));
res.alias = section.get("alias");
res.whiteBorder = "white".equalsIgnoreCase(section.get("border"));
@@ -299,4 +307,100 @@ public final class CardEdition implements Comparable<CardEdition> { // immutable
};
}
public static class Collection extends StorageBase<CardEdition> {
private final Map<String, CardEdition> aliasToEdition = new TreeMap<String, CardEdition>(String.CASE_INSENSITIVE_ORDER);
public Collection(IItemReader<CardEdition> reader) {
super("Card editions", reader);
for (CardEdition ee : this) {
String alias = ee.getAlias();
if (null != alias) {
aliasToEdition.put(alias, ee);
}
aliasToEdition.put(ee.getCode2(), ee);
}
}
/**
* Gets a sets by code. It will search first by three letter codes, then by aliases and two-letter codes.
*
* @param code
* the code
* @return the sets the by code
*/
@Override
public CardEdition get(final String code) {
CardEdition baseResult = super.get(code);
return baseResult == null ? aliasToEdition.get(code) : baseResult;
}
public Iterable<CardEdition> getOrderedEditions() {
List<CardEdition> res = Lists.newArrayList(this);
Collections.sort(res);
Collections.reverse(res);
return res;
}
/**
* Gets the sets by code or throw.
*
* @param code
* the code
* @return the sets the by code or throw
*/
public CardEdition getEditionByCodeOrThrow(final String code) {
final CardEdition set = this.get(code);
if (null == set) {
throw new RuntimeException(String.format("Edition with code '%s' not found", code));
}
return set;
}
// used by image generating code
/**
* Gets the code2 by code.
*
* @param code
* the code
* @return the code2 by code
*/
public String getCode2ByCode(final String code) {
final CardEdition set = this.get(code);
return set == null ? "" : set.getCode2();
}
public final Function<String, CardEdition> FN_EDITION_BY_CODE = new Function<String, CardEdition>() {
@Override
public CardEdition apply(String code) {
return Collection.this.get(code);
};
};
/**
* TODO: Write javadoc for this method.
* @return
*/
public IItemReader<SealedProduct.Template> getBoosterGenerator() {
// TODO Auto-generated method stub
return new StorageReaderBase<SealedProduct.Template>(null) {
@Override
public Map<String, SealedProduct.Template> readAll() {
Map<String, SealedProduct.Template> map = new TreeMap<String, SealedProduct.Template>(String.CASE_INSENSITIVE_ORDER);
for(CardEdition ce : Collection.this) {
map.put(ce.getCode(), ce.getBoosterTemplate());
}
return map;
}
@Override
public String getItemKey(SealedProduct.Template item) {
return item.getEdition();
}
};
}
}
}

View File

@@ -22,6 +22,13 @@ import forge.card.mana.ManaCost;
*/
final class CardFace implements ICardFace {
public enum FaceSelectionMethod { //
USE_ACTIVE_FACE,
USE_PRIMARY_FACE,
COMBINE;
}
private final static List<String> emptyList = Collections.unmodifiableList(new ArrayList<String>());
private final static Map<String, String> emptyMap = Collections.unmodifiableMap(new TreeMap<String, String>());

View File

@@ -120,7 +120,7 @@ public final class CardRules implements ICardCharacteristics {
public String getName() {
switch(splitType.getAggregationMethod()) {
case AGGREGATE:
case COMBINE:
return mainPart.getName() + " // " + otherPart.getName();
default:
return mainPart.getName();
@@ -134,7 +134,7 @@ public final class CardRules implements ICardCharacteristics {
@Override
public CardType getType() {
switch(splitType.getAggregationMethod()) {
case AGGREGATE: // no cards currently have different types
case COMBINE: // no cards currently have different types
return CardType.combine(mainPart.getType(), otherPart.getType());
default:
return mainPart.getType();
@@ -145,7 +145,7 @@ public final class CardRules implements ICardCharacteristics {
@Override
public ManaCost getManaCost() {
switch(splitType.getAggregationMethod()) {
case AGGREGATE:
case COMBINE:
return ManaCost.combine(mainPart.getManaCost(), otherPart.getManaCost());
default:
return mainPart.getManaCost();
@@ -155,7 +155,7 @@ public final class CardRules implements ICardCharacteristics {
@Override
public ColorSet getColor() {
switch(splitType.getAggregationMethod()) {
case AGGREGATE:
case COMBINE:
return ColorSet.fromMask(mainPart.getColor().getColor() | otherPart.getColor().getColor());
default:
return mainPart.getColor();
@@ -171,7 +171,7 @@ public final class CardRules implements ICardCharacteristics {
@Override
public String getOracleText() {
switch(splitType.getAggregationMethod()) {
case AGGREGATE:
case COMBINE:
return mainPart.getOracleText() + "\r\n\r\n" + otherPart.getOracleText();
default:
return mainPart.getOracleText();

View File

@@ -170,7 +170,7 @@ public final class CardRulesPredicates {
*/
public static Predicate<CardRules> coreType(final boolean isEqual, final String what) {
try {
return CardRulesPredicates.coreType(isEqual, Enum.valueOf(CardCoreType.class, what));
return CardRulesPredicates.coreType(isEqual, Enum.valueOf(CardType.CoreType.class, what));
} catch (final Exception e) {
return com.google.common.base.Predicates.alwaysFalse();
}
@@ -185,7 +185,7 @@ public final class CardRulesPredicates {
* the type
* @return the predicate
*/
public static Predicate<CardRules> coreType(final boolean isEqual, final CardCoreType type) {
public static Predicate<CardRules> coreType(final boolean isEqual, final CardType.CoreType type) {
return new PredicateCoreType(type, isEqual);
}
@@ -200,7 +200,7 @@ public final class CardRulesPredicates {
*/
public static Predicate<CardRules> superType(final boolean isEqual, final String what) {
try {
return CardRulesPredicates.superType(isEqual, Enum.valueOf(CardSuperType.class, what));
return CardRulesPredicates.superType(isEqual, Enum.valueOf(CardType.SuperType.class, what));
} catch (final Exception e) {
return com.google.common.base.Predicates.alwaysFalse();
}
@@ -215,7 +215,7 @@ public final class CardRulesPredicates {
* the type
* @return the predicate
*/
public static Predicate<CardRules> superType(final boolean isEqual, final CardSuperType type) {
public static Predicate<CardRules> superType(final boolean isEqual, final CardType.SuperType type) {
return new PredicateSuperType(type, isEqual);
}
@@ -396,7 +396,7 @@ public final class CardRulesPredicates {
}
private static class PredicateCoreType implements Predicate<CardRules> {
private final CardCoreType operand;
private final CardType.CoreType operand;
private final boolean shouldBeEqual;
@Override
@@ -407,14 +407,14 @@ public final class CardRulesPredicates {
return this.shouldBeEqual == card.getType().typeContains(this.operand);
}
public PredicateCoreType(final CardCoreType type, final boolean wantEqual) {
public PredicateCoreType(final CardType.CoreType type, final boolean wantEqual) {
this.operand = type;
this.shouldBeEqual = wantEqual;
}
}
private static class PredicateSuperType implements Predicate<CardRules> {
private final CardSuperType operand;
private final CardType.SuperType operand;
private final boolean shouldBeEqual;
@Override
@@ -422,7 +422,7 @@ public final class CardRulesPredicates {
return this.shouldBeEqual == card.getType().superTypeContains(this.operand);
}
public PredicateSuperType(final CardSuperType type, final boolean wantEqual) {
public PredicateSuperType(final CardType.SuperType type, final boolean wantEqual) {
this.operand = type;
this.shouldBeEqual = wantEqual;
}
@@ -448,21 +448,21 @@ public final class CardRulesPredicates {
/** The Constant isCreature. */
public static final Predicate<CardRules> IS_CREATURE = CardRulesPredicates
.coreType(true, CardCoreType.Creature);
.coreType(true, CardType.CoreType.Creature);
public static final Predicate<CardRules> IS_LEGENDARY = CardRulesPredicates
.superType(true, CardSuperType.Legendary);
.superType(true, CardType.SuperType.Legendary);
/** The Constant isArtifact. */
public static final Predicate<CardRules> IS_ARTIFACT = CardRulesPredicates
.coreType(true, CardCoreType.Artifact);
.coreType(true, CardType.CoreType.Artifact);
/** The Constant isEquipment. */
public static final Predicate<CardRules> IS_EQUIPMENT = CardRulesPredicates
.subType("Equipment");
/** The Constant isLand. */
public static final Predicate<CardRules> IS_LAND = CardRulesPredicates.coreType(true, CardCoreType.Land);
public static final Predicate<CardRules> IS_LAND = CardRulesPredicates.coreType(true, CardType.CoreType.Land);
/** The Constant isBasicLand. */
public static final Predicate<CardRules> IS_BASIC_LAND = new Predicate<CardRules>() {
@@ -480,31 +480,17 @@ public final class CardRulesPredicates {
}
};
/** The Constant isPlaneswalker. */
public static final Predicate<CardRules> IS_PLANESWALKER = CardRulesPredicates.coreType(true,
CardCoreType.Planeswalker);
/** The Constant isInstant. */
public static final Predicate<CardRules> IS_INSTANT = CardRulesPredicates.coreType(true, CardCoreType.Instant);
/** The Constant isSorcery. */
public static final Predicate<CardRules> IS_SORCERY = CardRulesPredicates.coreType(true, CardCoreType.Sorcery);
/** The Constant isEnchantment. */
public static final Predicate<CardRules> IS_ENCHANTMENT = CardRulesPredicates.coreType(true, CardCoreType.Enchantment);
public static final Predicate<CardRules> IS_PLANE = CardRulesPredicates.coreType(true, CardCoreType.Plane);
public static final Predicate<CardRules> IS_PHENOMENON = CardRulesPredicates.coreType(true, CardCoreType.Phenomenon);
public static final Predicate<CardRules> IS_PLANESWALKER = CardRulesPredicates.coreType(true, CardType.CoreType.Planeswalker);
public static final Predicate<CardRules> IS_INSTANT = CardRulesPredicates.coreType(true, CardType.CoreType.Instant);
public static final Predicate<CardRules> IS_SORCERY = CardRulesPredicates.coreType(true, CardType.CoreType.Sorcery);
public static final Predicate<CardRules> IS_ENCHANTMENT = CardRulesPredicates.coreType(true, CardType.CoreType.Enchantment);
public static final Predicate<CardRules> IS_PLANE = CardRulesPredicates.coreType(true, CardType.CoreType.Plane);
public static final Predicate<CardRules> IS_PHENOMENON = CardRulesPredicates.coreType(true, CardType.CoreType.Phenomenon);
public static final Predicate<CardRules> IS_PLANE_OR_PHENOMENON = Predicates.or(IS_PLANE, IS_PHENOMENON);
public static final Predicate<CardRules> IS_SCHEME = CardRulesPredicates.coreType(true, CardCoreType.Scheme);
public static final Predicate<CardRules> IS_VANGUARD = CardRulesPredicates.coreType(true, CardCoreType.Vanguard);
/** The Constant isNonLand. */
public static final Predicate<CardRules> IS_NON_LAND = CardRulesPredicates.coreType(false, CardCoreType.Land);
/** The Constant isNonCreatureSpell. */
public static final Predicate<CardRules> IS_NON_CREATURE_SPELL = Predicates.not(Predicates.or(
Presets.IS_CREATURE, Presets.IS_LAND));
public static final Predicate<CardRules> IS_SCHEME = CardRulesPredicates.coreType(true, CardType.CoreType.Scheme);
public static final Predicate<CardRules> IS_VANGUARD = CardRulesPredicates.coreType(true, CardType.CoreType.Vanguard);
public static final Predicate<CardRules> IS_NON_LAND = CardRulesPredicates.coreType(false, CardType.CoreType.Land);
public static final Predicate<CardRules> IS_NON_CREATURE_SPELL = Predicates.not(Predicates.or(Presets.IS_CREATURE, Presets.IS_LAND));
/** The Constant IS_NONCREATURE_SPELL_FOR_GENERATOR. **/
@SuppressWarnings("unchecked")

View File

@@ -1,24 +1,26 @@
package forge.card;
import forge.card.CardFace.FaceSelectionMethod;
public enum CardSplitType
{
None(AggregationMethod.USE_PRIMARY_FACE, null),
Transform(AggregationMethod.USE_ACTIVE_FACE, CardCharacteristicName.Transformed),
Split(AggregationMethod.AGGREGATE, CardCharacteristicName.RightSplit),
Flip(AggregationMethod.USE_PRIMARY_FACE, CardCharacteristicName.Flipped),
None(FaceSelectionMethod.USE_PRIMARY_FACE, null),
Transform(FaceSelectionMethod.USE_ACTIVE_FACE, CardCharacteristicName.Transformed),
Split(FaceSelectionMethod.COMBINE, CardCharacteristicName.RightSplit),
Flip(FaceSelectionMethod.USE_PRIMARY_FACE, CardCharacteristicName.Flipped),
// used by 12 licid creatures to switch type into enchantment aura
Licid(AggregationMethod.USE_PRIMARY_FACE, CardCharacteristicName.Licid);
Licid(FaceSelectionMethod.USE_PRIMARY_FACE, CardCharacteristicName.Licid);
private CardSplitType(AggregationMethod calcMode, CardCharacteristicName stateName) {
private CardSplitType(FaceSelectionMethod calcMode, CardCharacteristicName stateName) {
method = calcMode;
this.changedStateName = stateName;
}
public AggregationMethod getAggregationMethod() {
public FaceSelectionMethod getAggregationMethod() {
return method;
}
private final AggregationMethod method;
private final FaceSelectionMethod method;
private final CardCharacteristicName changedStateName;
public static CardSplitType smartValueOf(String text) {

View File

@@ -1,35 +0,0 @@
/*
* Forge: Play Magic: the Gathering.
* Copyright (C) 2011 Forge Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge.card;
/**
* The Enum CardSuperType.
*/
public enum CardSuperType {
/** The Basic. */
Basic,
/** The Legendary. */
Legendary,
/** The Snow. */
Snow,
/** The Ongoing. */
Ongoing,
/** The World. */
World
}

View File

@@ -34,20 +34,64 @@ import org.apache.commons.lang3.StringUtils;
*/
public final class CardType implements Comparable<CardType> {
public enum CoreType {
/** The Artifact. */
Artifact,
/** The Creature. */
Creature,
/** The Enchantment. */
Enchantment,
/** The Instant. */
Instant,
/** The Land. */
Land,
/** The Plane. */
Plane,
/** The Planeswalker. */
Planeswalker,
/** The Scheme. */
Scheme,
/** The Sorcery. */
Sorcery,
/** The Tribal. */
Tribal,
/** The Vanguard. */
Vanguard,
/** The Phenomenon. */
Phenomenon
}
public enum SuperType {
/** The Basic. */
Basic,
/** The Legendary. */
Legendary,
/** The Snow. */
Snow,
/** The Ongoing. */
Ongoing,
/** The World. */
World
}
private final List<String> subType = new ArrayList<String>();
private final EnumSet<CardCoreType> coreType = EnumSet.noneOf(CardCoreType.class);
private final EnumSet<CardSuperType> superType = EnumSet.noneOf(CardSuperType.class);
private final EnumSet<CardType.CoreType> coreType = EnumSet.noneOf(CardType.CoreType.class);
private final EnumSet<CardType.SuperType> superType = EnumSet.noneOf(CardType.SuperType.class);
private String calculatedType = null; // since obj is immutable, this is
// calc'd once
// This will be useful for faster parses
private static HashMap<String, CardCoreType> stringToCoreType = new HashMap<String, CardCoreType>();
private static HashMap<String, CardSuperType> stringToSuperType = new HashMap<String, CardSuperType>();
private static HashMap<String, CardType.CoreType> stringToCoreType = new HashMap<String, CardType.CoreType>();
private static HashMap<String, CardType.SuperType> stringToSuperType = new HashMap<String, CardType.SuperType>();
static {
for (final CardSuperType st : CardSuperType.values()) {
for (final CardType.SuperType st : CardType.SuperType.values()) {
CardType.stringToSuperType.put(st.name(), st);
}
for (final CardCoreType ct : CardCoreType.values()) {
for (final CardType.CoreType ct : CardType.CoreType.values()) {
CardType.stringToCoreType.put(ct.name(), ct);
}
}
@@ -104,13 +148,13 @@ public final class CardType implements Comparable<CardType> {
return;
}
final CardCoreType ct = CardType.stringToCoreType.get(type);
final CardType.CoreType ct = CardType.stringToCoreType.get(type);
if (ct != null) {
this.coreType.add(ct);
return;
}
final CardSuperType st = CardType.stringToSuperType.get(type);
final CardType.SuperType st = CardType.stringToSuperType.get(type);
if (st != null) {
this.superType.add(st);
return;
@@ -124,56 +168,56 @@ public final class CardType implements Comparable<CardType> {
return this.subType.contains(operand);
}
public boolean typeContains(final CardCoreType operand) {
public boolean typeContains(final CardType.CoreType operand) {
return this.coreType.contains(operand);
}
public boolean superTypeContains(final CardSuperType operand) {
public boolean superTypeContains(final CardType.SuperType operand) {
return this.superType.contains(operand);
}
public boolean isCreature() {
return this.coreType.contains(CardCoreType.Creature);
return this.coreType.contains(CardType.CoreType.Creature);
}
public boolean isPlaneswalker() {
return this.coreType.contains(CardCoreType.Planeswalker);
return this.coreType.contains(CardType.CoreType.Planeswalker);
}
public boolean isLand() {
return this.coreType.contains(CardCoreType.Land);
return this.coreType.contains(CardType.CoreType.Land);
}
public boolean isArtifact() {
return this.coreType.contains(CardCoreType.Artifact);
return this.coreType.contains(CardType.CoreType.Artifact);
}
public boolean isInstant() {
return this.coreType.contains(CardCoreType.Instant);
return this.coreType.contains(CardType.CoreType.Instant);
}
public boolean isSorcery() {
return this.coreType.contains(CardCoreType.Sorcery);
return this.coreType.contains(CardType.CoreType.Sorcery);
}
public boolean isVanguard() {
return this.coreType.contains(CardCoreType.Vanguard);
return this.coreType.contains(CardType.CoreType.Vanguard);
}
public boolean isScheme() {
return this.coreType.contains(CardCoreType.Scheme);
return this.coreType.contains(CardType.CoreType.Scheme);
}
public boolean isEnchantment() {
return this.coreType.contains(CardCoreType.Enchantment);
return this.coreType.contains(CardType.CoreType.Enchantment);
}
public boolean isBasic() {
return this.superType.contains(CardSuperType.Basic);
return this.superType.contains(CardType.SuperType.Basic);
}
public boolean isLegendary() {
return this.superType.contains(CardSuperType.Legendary);
return this.superType.contains(CardType.SuperType.Legendary);
}
public boolean isBasicLand() {
@@ -198,10 +242,10 @@ public final class CardType implements Comparable<CardType> {
public List<String> getTypesBeforeDash() {
final ArrayList<String> types = new ArrayList<String>();
for (final CardSuperType st : this.superType) {
for (final CardType.SuperType st : this.superType) {
types.add(st.name());
}
for (final CardCoreType ct : this.coreType) {
for (final CardType.CoreType ct : this.coreType) {
types.add(ct.name());
}
return types;
@@ -227,11 +271,11 @@ public final class CardType implements Comparable<CardType> {
}
public boolean isPlane() {
return this.coreType.contains(CardCoreType.Plane);
return this.coreType.contains(CardType.CoreType.Plane);
}
public boolean isPhenomenon() {
return this.coreType.contains(CardCoreType.Phenomenon);
return this.coreType.contains(CardType.CoreType.Phenomenon);
}

View File

@@ -1,128 +0,0 @@
/*
* Forge: Play Magic: the Gathering.
* Copyright (C) 2011 Forge Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge.card;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import forge.util.IItemReader;
import forge.util.storage.StorageBase;
import forge.util.storage.StorageReaderBase;
public final class EditionCollection extends StorageBase<CardEdition> {
private final Map<String, CardEdition> aliasToEdition = new TreeMap<String, CardEdition>(String.CASE_INSENSITIVE_ORDER);
public EditionCollection(IItemReader<CardEdition> reader) {
super("Card editions", reader);
for (CardEdition ee : this) {
String alias = ee.getAlias();
if (null != alias) {
aliasToEdition.put(alias, ee);
}
aliasToEdition.put(ee.getCode2(), ee);
}
}
/**
* Gets a sets by code. It will search first by three letter codes, then by aliases and two-letter codes.
*
* @param code
* the code
* @return the sets the by code
*/
@Override
public CardEdition get(final String code) {
CardEdition baseResult = super.get(code);
return baseResult == null ? aliasToEdition.get(code) : baseResult;
}
public Iterable<CardEdition> getOrderedEditions() {
List<CardEdition> res = Lists.newArrayList(this);
Collections.sort(res);
Collections.reverse(res);
return res;
}
/**
* Gets the sets by code or throw.
*
* @param code
* the code
* @return the sets the by code or throw
*/
public CardEdition getEditionByCodeOrThrow(final String code) {
final CardEdition set = this.get(code);
if (null == set) {
throw new RuntimeException(String.format("Edition with code '%s' not found", code));
}
return set;
}
// used by image generating code
/**
* Gets the code2 by code.
*
* @param code
* the code
* @return the code2 by code
*/
public String getCode2ByCode(final String code) {
final CardEdition set = this.get(code);
return set == null ? "" : set.getCode2();
}
public final Function<String, CardEdition> FN_EDITION_BY_CODE = new Function<String, CardEdition>() {
@Override
public CardEdition apply(String code) {
return EditionCollection.this.get(code);
};
};
/**
* TODO: Write javadoc for this method.
* @return
*/
public IItemReader<SealedProductTemplate> getBoosterGenerator() {
// TODO Auto-generated method stub
return new StorageReaderBase<SealedProductTemplate>(null) {
@Override
public Map<String, SealedProductTemplate> readAll() {
Map<String, SealedProductTemplate> map = new TreeMap<String, SealedProductTemplate>(String.CASE_INSENSITIVE_ORDER);
for(CardEdition ce : EditionCollection.this) {
map.put(ce.getCode(), ce.getBoosterTemplate());
}
return map;
}
@Override
public String getItemKey(SealedProductTemplate item) {
return item.getEdition();
}
};
}
}

View File

@@ -1,75 +0,0 @@
package forge.card;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import forge.util.TextUtil;
import forge.util.storage.StorageReaderFile;
/**
* TODO: Write javadoc for this type.
*
*/
public class FatPackTemplate extends SealedProductTemplate {
private final int cntBoosters;
public int getCntBoosters() { return cntBoosters; }
private FatPackTemplate(String edition, int boosters, Iterable<Pair<String, Integer>> itrSlots)
{
super(edition, itrSlots);
cntBoosters = boosters;
}
public static final class Reader extends StorageReaderFile<FatPackTemplate> {
public Reader(String pathname) {
super(pathname, FatPackTemplate.FN_GET_NAME);
}
@Override
protected FatPackTemplate read(String line, int i) {
String[] headAndData = TextUtil.split(line, ':', 2);
final String edition = headAndData[0];
final String[] data = TextUtil.splitWithParenthesis(headAndData[1], ',');
int nBoosters = 6;
List<Pair<String, Integer>> slots = new ArrayList<Pair<String,Integer>>();
for(String slotDesc : data) {
String[] kv = TextUtil.split(slotDesc, ' ', 2);
if (kv[1].startsWith("Booster"))
nBoosters = Integer.parseInt(kv[0]);
else
slots.add(ImmutablePair.of(kv[1], Integer.parseInt(kv[0])));
}
return new FatPackTemplate(edition, nBoosters, slots);
}
}
@Override
public String toString() {
if (0 >= cntBoosters) {
return "no cards";
}
StringBuilder s = new StringBuilder();
for(Pair<String, Integer> p : slots) {
s.append(p.getRight()).append(" ").append(p.getLeft()).append(", ");
}
// trim the last comma and space
if( s.length() > 0 )
s.replace(s.length() - 2, s.length(), "");
if (0 < cntBoosters) {
if( s.length() > 0 )
s.append(" and ");
s.append(cntBoosters).append(" booster packs ");
}
return s.toString();
}
}

View File

@@ -12,12 +12,13 @@ import com.google.common.collect.Iterables;
import forge.StaticData;
import forge.item.PaperCard;
import forge.item.SealedProduct;
import forge.util.ItemPoolView;
public class UnOpenedProduct implements IUnOpenedProduct {
private final SealedProductTemplate tpl;
private final SealedProduct.Template tpl;
private final Map<String, PrintSheet> sheets;
private boolean poolLimited = false; // if true after successful generation cards are removed from printsheets.
@@ -31,23 +32,23 @@ public class UnOpenedProduct implements IUnOpenedProduct {
// Means to select from all unique cards (from base game, ie. no schemes or avatars)
public UnOpenedProduct(SealedProductTemplate template) {
public UnOpenedProduct(SealedProduct.Template template) {
tpl = template;
sheets = null;
}
// Invoke this constructor only if you are sure that the pool is not equal to deafult carddb
public UnOpenedProduct(SealedProductTemplate template, ItemPoolView<PaperCard> pool) {
public UnOpenedProduct(SealedProduct.Template template, ItemPoolView<PaperCard> pool) {
this(template, pool.toFlatList());
}
public UnOpenedProduct(SealedProductTemplate template, Iterable<PaperCard> cards) {
public UnOpenedProduct(SealedProduct.Template template, Iterable<PaperCard> cards) {
tpl = template;
sheets = new TreeMap<String, PrintSheet>();
prebuildSheets(cards);
}
public UnOpenedProduct(SealedProductTemplate sealedProductTemplate, Predicate<PaperCard> filterPrinted) {
public UnOpenedProduct(SealedProduct.Template sealedProductTemplate, Predicate<PaperCard> filterPrinted) {
this(sealedProductTemplate, Iterables.filter(StaticData.instance().getCommonCards().getAllCards(), filterPrinted));
}

View File

@@ -26,7 +26,7 @@ import java.util.Map.Entry;
import org.apache.commons.lang3.Range;
import forge.StaticData;
import forge.card.CardCoreType;
import forge.card.CardType;
import forge.card.ColorSet;
import forge.item.PaperCard;
import forge.item.IPaperCard;
@@ -178,7 +178,7 @@ public enum DeckFormat {
int phenoms = 0;
for (Entry<PaperCard, Integer> cp : planes) {
if (cp.getKey().getRules().getType().typeContains(CardCoreType.Phenomenon)) {
if (cp.getKey().getRules().getType().typeContains(CardType.CoreType.Phenomenon)) {
phenoms++;
}
if (cp.getValue() > 1) {

View File

@@ -1,215 +0,0 @@
/*
* Forge: Play Magic: the Gathering.
* Copyright (C) 2011 Forge Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge.game;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists;
import forge.StaticData;
import forge.item.PaperCard;
import forge.item.IPaperCard;
import forge.util.FileSection;
import forge.util.storage.StorageReaderFileSections;
/**
* TODO: Write javadoc for this type.
*
*/
public class GameFormat implements Comparable<GameFormat> {
private final String name;
// contains allowed sets, when empty allows all sets
protected final List<String> allowedSetCodes;
protected final List<String> bannedCardNames;
protected final transient List<String> allowedSetCodes_ro;
protected final transient List<String> bannedCardNames_ro;
protected final transient Predicate<PaperCard> filterRules;
protected final transient Predicate<PaperCard> filterPrinted;
private final int index;
/**
* Instantiates a new game format.
*
* @param fName
* the f name
* @param sets
* the sets
* @param bannedCards
* the banned cards
*/
public GameFormat(final String fName, final Iterable<String> sets, final List<String> bannedCards) {
this(fName, sets, bannedCards, 0);
}
public GameFormat(final String fName, final Iterable<String> sets, final List<String> bannedCards, int compareIdx) {
this.index = compareIdx;
this.name = fName;
this.allowedSetCodes = sets == null ? new ArrayList<String>() : Lists.newArrayList(sets);
this.bannedCardNames = bannedCards == null ? new ArrayList<String>() : Lists.newArrayList(bannedCards);
this.allowedSetCodes_ro = Collections.unmodifiableList(allowedSetCodes);
this.bannedCardNames_ro = Collections.unmodifiableList(bannedCardNames);
this.filterRules = this.buildFilterRules();
this.filterPrinted = this.buildFilterPrinted();
}
private Predicate<PaperCard> buildFilterPrinted() {
final Predicate<PaperCard> banNames = Predicates.not(IPaperCard.Predicates.names(this.bannedCardNames));
if (this.allowedSetCodes == null || this.allowedSetCodes.isEmpty()) {
return banNames;
}
return Predicates.and(banNames, IPaperCard.Predicates.printedInSets(this.allowedSetCodes, true));
}
private Predicate<PaperCard> buildFilterRules() {
final Predicate<PaperCard> banNames = Predicates.not(IPaperCard.Predicates.names(this.bannedCardNames));
if (this.allowedSetCodes == null || this.allowedSetCodes.isEmpty()) {
return banNames;
}
return Predicates.and(banNames, StaticData.instance().getCommonCards().wasPrintedInSets(this.allowedSetCodes));
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return this.name;
}
/**
* Gets the set list (for GameFormatQuest).
*
* @return list of allowed set codes
*/
public List<String> getAllowedSetCodes() {
return this.allowedSetCodes_ro;
}
/**
* Gets the banned cards (for GameFormatQuest).
*
* @return list of banned card names
*/
public List<String> getBannedCardNames() {
return this.bannedCardNames_ro;
}
/**
* Gets the filter rules.
*
* @return the filter rules
*/
public Predicate<PaperCard> getFilterRules() {
return this.filterRules;
}
/**
* Gets the filter printed.
*
* @return the filter printed
*/
public Predicate<PaperCard> getFilterPrinted() {
return this.filterPrinted;
}
/**
* 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);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.name + " (format)";
}
public static final Function<GameFormat, String> FN_GET_NAME = new Function<GameFormat, String>() {
@Override
public String apply(GameFormat arg1) {
return arg1.getName();
}
};
/* (non-Javadoc)
* just used for ordering -- comparing the name is sufficient
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(GameFormat other) {
if (null == other) {
return 1;
}
return index - other.index;
}
public int getIndex() {
return index;
}
/**
* Instantiates a new format utils.
*/
public static class Reader extends StorageReaderFileSections<GameFormat> {
public Reader(File file0) {
super(file0, GameFormat.FN_GET_NAME);
}
@Override
protected GameFormat read(String title, Iterable<String> body, int idx) {
List<String> sets = null; // default: all sets allowed
List<String> bannedCards = null; // default: nothing banned
FileSection section = FileSection.parse(body, ":");
String strSets = section.get("sets");
if ( null != strSets ) {
sets = Arrays.asList(strSets.split(", "));
}
String strCars = section.get("banned");
if ( strCars != null ) {
bannedCards = Arrays.asList(strCars.split("; "));
}
return new GameFormat(title, sets, bannedCards, 1 + idx);
}
}
}

View File

@@ -1,8 +0,0 @@
/**
*
*/
/**
* @author Max
*
*/
package forge.game;

View File

@@ -22,22 +22,21 @@ import com.google.common.base.Function;
import forge.StaticData;
import forge.card.CardEdition;
import forge.card.SealedProductTemplate;
import forge.util.MyRandom;
public class BoosterPack extends OpenablePack {
public class BoosterPack extends SealedProduct {
private final int artIndex;
private final int hash;
public static final Function<CardEdition, BoosterPack> FN_FROM_SET = new Function<CardEdition, BoosterPack>() {
@Override
public BoosterPack apply(final CardEdition arg1) {
SealedProductTemplate d = StaticData.instance().getBoosters().get(arg1.getCode());
Template d = StaticData.instance().getBoosters().get(arg1.getCode());
return new BoosterPack(arg1.getName(), d);
}
};
public BoosterPack(final String name0, final SealedProductTemplate boosterData) {
public BoosterPack(final String name0, final Template boosterData) {
super(name0, boosterData);
int maxIdx = StaticData.instance().getEditions().get(boosterData.getEdition()).getCntBoosterPictures();
artIndex = MyRandom.getRandom().nextInt(maxIdx) + 1;
@@ -58,7 +57,7 @@ public class BoosterPack extends OpenablePack {
return new BoosterPack(name, contents);
}
public SealedProductTemplate getBoosterData() {
public Template getBoosterData() {
return contents;
}

View File

@@ -21,25 +21,29 @@ package forge.item;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.base.Function;
import forge.StaticData;
import forge.card.BoosterGenerator;
import forge.card.CardEdition;
import forge.card.FatPackTemplate;
import forge.util.TextUtil;
import forge.util.storage.StorageReaderFile;
public class FatPack extends OpenablePack {
public class FatPack extends SealedProduct {
public static final Function<CardEdition, FatPack> FN_FROM_SET = new Function<CardEdition, FatPack>() {
@Override
public FatPack apply(final CardEdition arg1) {
FatPackTemplate d = StaticData.instance().getFatPacks().get(arg1.getCode());
FatPack.Template d = StaticData.instance().getFatPacks().get(arg1.getCode());
return new FatPack(arg1.getName(), d);
}
};
private final FatPackTemplate fpData;
private final FatPack.Template fpData;
public FatPack(final String name0, final FatPackTemplate fpData0) {
public FatPack(final String name0, final FatPack.Template fpData0) {
super(name0, StaticData.instance().getBoosters().get(fpData0.getEdition()));
fpData = fpData0;
}
@@ -73,4 +77,65 @@ public class FatPack extends OpenablePack {
public int getTotalCards() {
return super.getTotalCards() * fpData.getCntBoosters() + fpData.getNumberOfCardsExpected();
}
public static class Template extends SealedProduct.Template {
private final int cntBoosters;
public int getCntBoosters() { return cntBoosters; }
private Template(String edition, int boosters, Iterable<Pair<String, Integer>> itrSlots)
{
super(edition, itrSlots);
cntBoosters = boosters;
}
public static final class Reader extends StorageReaderFile<Template> {
public Reader(String pathname) {
super(pathname, Template.FN_GET_NAME);
}
@Override
protected Template read(String line, int i) {
String[] headAndData = TextUtil.split(line, ':', 2);
final String edition = headAndData[0];
final String[] data = TextUtil.splitWithParenthesis(headAndData[1], ',');
int nBoosters = 6;
List<Pair<String, Integer>> slots = new ArrayList<Pair<String,Integer>>();
for(String slotDesc : data) {
String[] kv = TextUtil.split(slotDesc, ' ', 2);
if (kv[1].startsWith("Booster"))
nBoosters = Integer.parseInt(kv[0]);
else
slots.add(ImmutablePair.of(kv[1], Integer.parseInt(kv[0])));
}
return new FatPack.Template(edition, nBoosters, slots);
}
}
@Override
public String toString() {
if (0 >= cntBoosters) {
return "no cards";
}
StringBuilder s = new StringBuilder();
for(Pair<String, Integer> p : slots) {
s.append(p.getRight()).append(" ").append(p.getLeft()).append(", ");
}
// trim the last comma and space
if( s.length() > 0 )
s.replace(s.length() - 2, s.length(), "");
if (0 < cntBoosters) {
if( s.length() > 0 )
s.append(" and ");
s.append(cntBoosters).append(" booster packs ");
}
return s.toString();
}
}
}

View File

@@ -1,107 +0,0 @@
/*
* Forge: Play Magic: the Gathering.
* Copyright (C) 2011 Forge Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge.item;
import java.util.List;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import forge.StaticData;
import forge.card.BoosterGenerator;
import forge.card.CardRulesPredicates;
import forge.card.SealedProductTemplate;
import forge.util.Aggregates;
public abstract class OpenablePack implements InventoryItemFromSet {
protected final SealedProductTemplate contents;
protected final String name;
private final int hash;
private List<PaperCard> cards = null;
public OpenablePack(String name0, SealedProductTemplate boosterData) {
if (null == name0) { throw new IllegalArgumentException("name0 must not be null"); }
if (null == boosterData) { throw new IllegalArgumentException("boosterData must not be null"); }
contents = boosterData;
name = name0;
hash = name.hashCode() ^ getClass().hashCode() ^ contents.hashCode();
}
@Override
public final String getName() {
return name + " " + getItemType();
}
public String getDescription() {
return contents.toString();
}
@Override
public final String getEdition() {
return contents.getEdition();
}
public final List<PaperCard> getCards() {
if (null == cards) {
cards = generate();
}
return cards;
}
public int getTotalCards() {
return contents.getNumberOfCardsExpected();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
OpenablePack other = (OpenablePack)obj;
return name.equals(other.name) && contents.equals(other.contents);
}
@Override
public int hashCode() {
return hash;
}
protected List<PaperCard> generate() {
return BoosterGenerator.getBoosterPack(contents);
}
protected PaperCard getRandomBasicLand(final String setCode) {
return this.getRandomBasicLands(setCode, 1).get(0);
}
protected List<PaperCard> getRandomBasicLands(final String setCode, final int count) {
Predicate<PaperCard> cardsRule = Predicates.and(
IPaperCard.Predicates.printedInSet(setCode),
Predicates.compose(CardRulesPredicates.Presets.IS_BASIC_LAND, PaperCard.FN_GET_RULES));
return Aggregates.random(Iterables.filter(StaticData.instance().getCommonCards().getAllCards(), cardsRule), count);
}
}

View File

@@ -0,0 +1,213 @@
/*
* Forge: Play Magic: the Gathering.
* Copyright (C) 2011 Forge Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge.item;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import forge.StaticData;
import forge.card.BoosterGenerator;
import forge.card.BoosterSlots;
import forge.card.CardRulesPredicates;
import forge.util.Aggregates;
import forge.util.TextUtil;
import forge.util.storage.StorageReaderFile;
public abstract class SealedProduct implements InventoryItemFromSet {
protected final Template contents;
protected final String name;
private final int hash;
private List<PaperCard> cards = null;
public SealedProduct(String name0, Template boosterData) {
if (null == name0) { throw new IllegalArgumentException("name0 must not be null"); }
if (null == boosterData) { throw new IllegalArgumentException("boosterData must not be null"); }
contents = boosterData;
name = name0;
hash = name.hashCode() ^ getClass().hashCode() ^ contents.hashCode();
}
@Override
public final String getName() {
return name + " " + getItemType();
}
public String getDescription() {
return contents.toString();
}
@Override
public final String getEdition() {
return contents.getEdition();
}
public final List<PaperCard> getCards() {
if (null == cards) {
cards = generate();
}
return cards;
}
public int getTotalCards() {
return contents.getNumberOfCardsExpected();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
SealedProduct other = (SealedProduct)obj;
return name.equals(other.name) && contents.equals(other.contents);
}
@Override
public int hashCode() {
return hash;
}
protected List<PaperCard> generate() {
return BoosterGenerator.getBoosterPack(contents);
}
protected PaperCard getRandomBasicLand(final String setCode) {
return this.getRandomBasicLands(setCode, 1).get(0);
}
protected List<PaperCard> getRandomBasicLands(final String setCode, final int count) {
Predicate<PaperCard> cardsRule = Predicates.and(
IPaperCard.Predicates.printedInSet(setCode),
Predicates.compose(CardRulesPredicates.Presets.IS_BASIC_LAND, PaperCard.FN_GET_RULES));
return Aggregates.random(Iterables.filter(StaticData.instance().getCommonCards().getAllCards(), cardsRule), count);
}
public static class Template {
@SuppressWarnings("unchecked")
public final static Template genericBooster = new Template(null, Lists.newArrayList(
Pair.of(BoosterSlots.COMMON, 10), Pair.of(BoosterSlots.UNCOMMON, 3),
Pair.of(BoosterSlots.RARE_MYTHIC, 1), Pair.of(BoosterSlots.BASIC_LAND, 1)
));
protected final List<Pair<String, Integer>> slots;
protected final String name;
public final List<Pair<String, Integer>> getSlots() {
return slots;
}
public final String getEdition() {
return name;
}
public Template(Iterable<Pair<String, Integer>> itrSlots)
{
this(null, itrSlots);
}
public Template(String name0, Iterable<Pair<String, Integer>> itrSlots)
{
slots = Lists.newArrayList(itrSlots);
name = name0;
}
public Template(String code, String boosterDesc) {
this(code, Reader.parseSlots(boosterDesc));
}
public int getNumberOfCardsExpected() {
int sum = 0;
for(Pair<String, Integer> p : slots) {
sum += p.getRight().intValue();
}
return sum;
}
public static final Function<? super Template, String> FN_GET_NAME = new Function<Template, String>() {
@Override
public String apply(Template arg1) {
return arg1.name;
}
};
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append("consisting of ");
for(Pair<String, Integer> p : slots) {
s.append(p.getRight()).append(" ").append(p.getLeft()).append(", ");
}
// trim the last comma and space
s.replace(s.length() - 2, s.length(), "");
// put an 'and' before the previous comma
int lastCommaIdx = s.lastIndexOf(",");
if (0 < lastCommaIdx) {
s.replace(lastCommaIdx+1, lastCommaIdx+1, " and");
}
return s.toString();
}
public final static class Reader extends StorageReaderFile<Template> {
public Reader(File file) {
super(file, Template.FN_GET_NAME);
}
public static List<Pair<String, Integer>> parseSlots(String data) {
final String[] dataz = TextUtil.splitWithParenthesis(data, ',');
List<Pair<String, Integer>> slots = new ArrayList<Pair<String,Integer>>();
for(String slotDesc : dataz) {
String[] kv = TextUtil.splitWithParenthesis(slotDesc, ' ', 2);
slots.add(ImmutablePair.of(kv[1], Integer.parseInt(kv[0])));
}
return slots;
}
@Override
protected Template read(String line, int i) {
String[] headAndData = TextUtil.split(line, ':', 2);
return new Template(headAndData[0], parseSlots(headAndData[1]));
}
}
}
}

View File

@@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package forge.card;
package forge.item;
import java.io.File;
import java.util.ArrayList;
@@ -28,6 +28,7 @@ import org.apache.commons.lang3.tuple.Pair;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import forge.card.BoosterSlots;
import forge.util.TextUtil;
import forge.util.storage.StorageReaderFile;

View File

@@ -24,20 +24,19 @@ import com.google.common.base.Function;
import forge.StaticData;
import forge.card.BoosterGenerator;
import forge.card.CardEdition;
import forge.card.SealedProductTemplate;
public class TournamentPack extends OpenablePack {
public class TournamentPack extends SealedProduct {
/** The Constant fnFromSet. */
public static final Function<CardEdition, TournamentPack> FN_FROM_SET = new Function<CardEdition, TournamentPack>() {
@Override
public TournamentPack apply(final CardEdition arg1) {
SealedProductTemplate d = StaticData.instance().getTournamentPacks().get(arg1.getCode());
Template d = StaticData.instance().getTournamentPacks().get(arg1.getCode());
return new TournamentPack(arg1.getName(), d);
}
};
public TournamentPack(final String name0, final SealedProductTemplate boosterData) {
public TournamentPack(final String name0, final Template boosterData) {
super(name0, boosterData);
}