diff --git a/.gitattributes b/.gitattributes index af3d5d7f197..1aa59953712 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11908,7 +11908,7 @@ src/main/java/forge/card/abilityfactory/AbilityFactoryToken.java svneol=native#t src/main/java/forge/card/abilityfactory/AbilityFactoryTurns.java svneol=native#text/plain src/main/java/forge/card/abilityfactory/AbilityFactoryZoneAffecting.java svneol=native#text/plain src/main/java/forge/card/abilityfactory/package-info.java svneol=native#text/plain -src/main/java/forge/card/cardfactory/AbstractCardFactory.java svneol=native#text/plain +src/main/java/forge/card/cardfactory/CardFactory.java svneol=native#text/plain src/main/java/forge/card/cardfactory/CardFactoryArtifacts.java -text src/main/java/forge/card/cardfactory/CardFactoryAuras.java svneol=native#text/plain src/main/java/forge/card/cardfactory/CardFactoryCreatures.java svneol=native#text/plain @@ -11920,7 +11920,6 @@ src/main/java/forge/card/cardfactory/CardFactoryLands.java svneol=native#text/pl src/main/java/forge/card/cardfactory/CardFactoryPlaneswalkers.java svneol=native#text/plain src/main/java/forge/card/cardfactory/CardFactorySorceries.java svneol=native#text/plain src/main/java/forge/card/cardfactory/CardFactoryUtil.java svneol=native#text/plain -src/main/java/forge/card/cardfactory/PreloadingCardFactory.java svneol=native#text/plain src/main/java/forge/card/cardfactory/package-info.java svneol=native#text/plain src/main/java/forge/card/cost/Cost.java svneol=native#text/plain src/main/java/forge/card/cost/CostDiscard.java -text @@ -12427,12 +12426,10 @@ src/site/apt/index.apt -text src/test/java/forge/BoosterDraft1Test.java svneol=native#text/plain src/test/java/forge/BoosterDraftTest.java svneol=native#text/plain src/test/java/forge/CardColorTest.java svneol=native#text/plain -src/test/java/forge/CardReaderTest.java svneol=native#text/plain src/test/java/forge/DeckWantsTest.java -text src/test/java/forge/GuiDownloadPicturesLQTest.java svneol=native#text/plain src/test/java/forge/GuiDownloadSetPicturesLQTest.java svneol=native#text/plain src/test/java/forge/GuiMigrateLocalMWSSetPicturesHQTest.java svneol=native#text/plain -src/test/java/forge/GuiMultipleBlockers4Test.java svneol=native#text/plain src/test/java/forge/GuiProgressBarWindowTest.java svneol=native#text/plain src/test/java/forge/PanelTest.java svneol=native#text/plain src/test/java/forge/ReadDraftRankingsTest.java -text diff --git a/src/main/java/forge/AllZone.java b/src/main/java/forge/AllZone.java index b4255b59a98..80bc047bc68 100644 --- a/src/main/java/forge/AllZone.java +++ b/src/main/java/forge/AllZone.java @@ -20,8 +20,8 @@ package forge; import java.util.Arrays; import java.util.List; +import forge.card.cardfactory.CardFactory; import forge.card.cardfactory.CardFactoryInterface; -import forge.card.cardfactory.PreloadingCardFactory; import forge.card.replacement.ReplacementHandler; import forge.card.trigger.TriggerHandler; import forge.control.input.InputControl; @@ -205,7 +205,7 @@ public final class AllZone { if (AllZone.cardFactory == null) { // setCardFactory(new // LazyCardFactory(ForgeProps.getFile(CARDSFOLDER))); - AllZone.setCardFactory(new PreloadingCardFactory(ForgeProps.getFile(NewConstants.CARDSFOLDER))); + AllZone.setCardFactory(new CardFactory(ForgeProps.getFile(NewConstants.CARDSFOLDER))); } return AllZone.cardFactory; } diff --git a/src/main/java/forge/CardReader.java b/src/main/java/forge/CardReader.java index 48dcdaccd5f..f5892a6479c 100644 --- a/src/main/java/forge/CardReader.java +++ b/src/main/java/forge/CardReader.java @@ -28,7 +28,6 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Locale; -import java.util.Map; import java.util.StringTokenizer; import java.util.regex.Pattern; import java.util.zip.ZipEntry; @@ -57,7 +56,7 @@ import forge.view.SplashFrame; * @author Forge * @version $Id$ */ -public class CardReader implements Runnable { +public class CardReader { // PM private static final String CARD_FILE_DOT_EXTENSION = ".txt"; @@ -101,8 +100,6 @@ public class CardReader implements Runnable { // 10:54 // PM - private transient Map mapToFill; - private transient List listRulesToFill; private transient File cardsfolder; private transient ZipFile zip; @@ -118,33 +115,6 @@ public class CardReader implements Runnable { // 8/18/11 10:56 PM - /** - * Instantiates a new card reader. - * - * @param theCardsFolder - * the the cards folder - * @param theMapToFill - * the the map to fill - */ - public CardReader(final File theCardsFolder, final Map theMapToFill) { - this(theCardsFolder, theMapToFill, null, true); - } - - /** - * This is a convenience for CardReader(cardsfolder, mapToFill, true); . - * - * @param theCardsFolder - * indicates location of the cardsFolder - * @param theMapToFill - * maps card names to Card instances; this is where we place the - * cards once read - * @param listRules2Fill - * List - */ - public CardReader(final File theCardsFolder, final Map theMapToFill, - final List listRules2Fill) { - this(theCardsFolder, theMapToFill, listRules2Fill, true); - } /** *

@@ -163,20 +133,9 @@ public class CardReader implements Runnable { * if true, attempts to load cards from a zip file, if one * exists. */ - public CardReader(final File theCardsFolder, final Map theMapToFill, - final List listRules2Fill, final boolean useZip) { - if (theMapToFill == null) { - throw new NullPointerException("theMapToFill must not be null."); - // by - // Braids - // on - // 8/18/11 - // 10:53 - // PM - } - this.mapToFill = theMapToFill; + public CardReader(final File theCardsFolder, final boolean useZip) { + // These read data for lightweight classes. - this.listRulesToFill = listRules2Fill == null ? new ArrayList() : listRules2Fill; this.rulesReader = new CardRulesReader(); if (!theCardsFolder.exists()) { @@ -206,24 +165,16 @@ public class CardReader implements Runnable { + theCardsFolder.getAbsolutePath() + "\"."); } - } - - if (useZip && (this.zip != null)) { - this.zipEnum = this.zip.entries(); - this.estimatedFilesRemaining = this.zip.size(); + if (this.zip != null) { + this.zipEnum = this.zip.entries(); + this.estimatedFilesRemaining = this.zip.size(); + } } this.setEncoding(CardReader.DEFAULT_CHARSET_NAME); } // CardReader() - /** - * Reads the rest of ALL the cards into memory. This is not lazy. - */ - @Override - public final void run() { - this.loadCards(); - } /** * Starts reading cards into memory until the given card is found. @@ -236,8 +187,9 @@ public class CardReader implements Runnable { * * @return the Card or null if it was not found. */ - protected final Card loadCards() { - Card result = null; + public final List loadCards() { + + List result = new ArrayList(); final FProgressBar barProgress = SplashFrame.PROGRESS_BAR; // Iterate through txt files or zip archive. @@ -266,7 +218,7 @@ public class CardReader implements Runnable { continue; } - result = this.loadCard(cardTxtFile); + result.add(this.loadCard(cardTxtFile)); barProgress.increment(); } // endfor @@ -283,7 +235,7 @@ public class CardReader implements Runnable { continue; } - result = this.loadCard(entry); + result.add(this.loadCard(entry)); barProgress.increment(); } } // endif @@ -338,30 +290,15 @@ public class CardReader implements Runnable { * * @return the card loaded from the stream */ - protected final Card loadCard(final InputStream inputStream) { + protected final CardRules loadCard(final InputStream inputStream) { this.rulesReader.reset(); InputStreamReader isr = new InputStreamReader(inputStream, this.charset); List allLines = FileUtil.readAllLines(isr, true); - final Card card = CardReader.readCard(allLines, this.rulesReader, this.mapToFill); - - if (card.isInAlternateState()) { - card.setState(CardCharactersticName.Original); - } - this.listRulesToFill.add(this.rulesReader.getCard()); - this.mapToFill.put(card.getName(), card); - return card; - } - - /** - * Read card. - * - * @param lines the lines - * @return the card - */ - public static Card readCard(final Iterable lines) { - return CardReader.readCard(lines, null, null); + CardReader.loadCard(allLines, this.rulesReader); + return this.rulesReader.getCard(); + } /** @@ -372,175 +309,178 @@ public class CardReader implements Runnable { * @param mapToFill is used to eliminate duplicates * @return the card */ - public static Card readCard(final Iterable lines, final CardRulesReader rulesReader, final Map mapToFill) { - final Card card = new Card(); - boolean ignoreTheRest = false; + public static void loadCard(final Iterable lines, final CardRulesReader rulesReader) { for (String line : lines) { - if (ignoreTheRest || line.isEmpty()) { + if (line.isEmpty() || line.charAt(0) == '#') { continue; } - - switch(line.charAt(0)) { // this is a simple state machine to gain some performance - case '#': - continue; - - case 'A': - if (line.equals("ALTERNATE")) { - CardCharactersticName mode; - if (card.isFlipCard()) { - mode = CardCharactersticName.Flipped; - } else if (card.isDoubleFaced()) { - mode = CardCharactersticName.Transformed; - } else { - mode = card.isTransformable(); - } - card.addAlternateState(mode); - card.setState(mode); - } else if (line.startsWith("A:")) { - card.addIntrinsicAbility(line.substring(2)); - } else if (line.startsWith("AlternateMode")) { - //System.out.println(card.getName()); - final CardCharactersticName value = CardCharactersticName.smartValueOf(line.substring("AlternateMode:".length())); - if (value == CardCharactersticName.Flipped) { - card.setFlipCard(true); - } else if (value == CardCharactersticName.Transformed) { - card.setDoubleFaced(true); - } else { - card.setTransformable(value); - } - } - break; - - case 'C': - if (line.startsWith("Colors")) { - final String value = line.substring(7); - final ArrayList newCols = new ArrayList(); - final CardColor newCol = new CardColor(card); - for (final String col : value.split(",")) { - newCol.addToCardColor(col); - } - newCol.fixColorless(); - newCols.add(newCol); - - card.setColor(newCols); - card.setCardColorsOverridden(true); - } - break; - - case 'E': - if ("End".equals(line)) { - ignoreTheRest = true; // have to deplete the iterator - continue; - // otherwise the underlying class would close its stream on finalize only - } - break; - - case 'K': - if (line.startsWith("K:")) { - final String value = line.substring(2); - card.addIntrinsicKeyword(value); - } - break; - - case 'L': - if (line.startsWith("Loyalty")) { - final String[] splitStr = line.split(":"); - final int loyal = Integer.parseInt(splitStr[1]); - card.setBaseLoyalty(loyal); - } - break; - - case 'M': - if (line.startsWith("ManaCost")) { - final String value = line.substring(9); - // System.out.println(s); - if (!"no cost".equals(value)) { - card.setManaCost(new CardManaCost(new ManaCostParser(value))); - } - } - break; - - case 'N': - if (line.startsWith("Name")) { - final String value = line.substring(5); - // System.out.println(s); - if ((mapToFill != null) && mapToFill.containsKey(value)) { - break; // this card has already been loaded. - } else { - card.setName(value); - } - } - break; - - case 'P': - if (line.startsWith("PT")) { - final String value = line.substring(3); - final String[] powTough = value.split("/"); - int att; - if (powTough[0].contains("*")) { - att = 0; - } else { - att = Integer.parseInt(powTough[0]); - } - - int def; - if (powTough[1].contains("*")) { - def = 0; - } else { - def = Integer.parseInt(powTough[1]); - } - - card.setBaseAttackString(powTough[0]); - card.setBaseDefenseString(powTough[1]); - card.setBaseAttack(att); - card.setBaseDefense(def); - } - break; - - case 'R': - if (line.startsWith("R:")) { - card.addReplacementEffect(ReplacementHandler.parseReplacement(line.substring(2), card)); - } - break; - - case 'S': - if (line.startsWith("S:")) { - card.addStaticAbilityString(line.substring(2)); - } else if (line.startsWith("SVar")) { - final String[] value = line.split(":", 3); - card.setSVar(value[1], value[2]); - } else if (line.startsWith("SetInfo")) { - final String value = line.substring(8); - card.addSet(new EditionInfo(value)); - // 8/18/11 11:08 PM - } - break; - - case 'T': - if (line.startsWith("Types")) { - CardReader.addTypes(card, line.substring(6)); - } else if (line.startsWith("Text")) { - String value = line.substring(5); - // if (!t.equals("no text")); - if ("no text".equals(value)) { - value = ""; - } - card.setText(value); - } else if (line.startsWith("T:")) { - card.addTrigger(TriggerHandler.parseTrigger(line.substring(2), card, true)); - } - break; - - } - - if (null != rulesReader) { - rulesReader.parseLine(line); - } - } // while !End - return card; + + rulesReader.parseLine(line); + } } + public static Card readCard(final Iterable lines) + { + final Card card = new Card(); + + for (String line : lines) { + + if (line.isEmpty() || line.charAt(0) == '#') { + continue; + } + + parseCardLine(card, line); + } // while !End + + if (card.isInAlternateState()) { + card.setState(CardCharactersticName.Original); + } + + return card; + } + + + private static void parseCardLine(Card card, String line) { + char firstCh = line.charAt(0); + + switch(firstCh) { // this is a simple state machine to gain some performance + case 'A': + if (line.equals("ALTERNATE")) { + CardCharactersticName mode; + if (card.isFlipCard()) { + mode = CardCharactersticName.Flipped; + } else if (card.isDoubleFaced()) { + mode = CardCharactersticName.Transformed; + } else { + mode = card.isTransformable(); + } + card.addAlternateState(mode); + card.setState(mode); + } else if (line.startsWith("A:")) { + card.addIntrinsicAbility(line.substring(2)); + } else if (line.startsWith("AlternateMode")) { + //System.out.println(card.getName()); + final CardCharactersticName value = CardCharactersticName.smartValueOf(line.substring("AlternateMode:".length())); + if (value == CardCharactersticName.Flipped) { + card.setFlipCard(true); + } else if (value == CardCharactersticName.Transformed) { + card.setDoubleFaced(true); + } else { + card.setTransformable(value); + } + } + break; + + case 'C': + if (line.startsWith("Colors")) { + final String value = line.substring(7); + final ArrayList newCols = new ArrayList(); + final CardColor newCol = new CardColor(card); + for (final String col : value.split(",")) { + newCol.addToCardColor(col); + } + newCol.fixColorless(); + newCols.add(newCol); + + card.setColor(newCols); + card.setCardColorsOverridden(true); + } + break; + + case 'K': + if (line.startsWith("K:")) { + final String value = line.substring(2); + card.addIntrinsicKeyword(value); + } + break; + + case 'L': + if (line.startsWith("Loyalty")) { + final String[] splitStr = line.split(":"); + final int loyal = Integer.parseInt(splitStr[1]); + card.setBaseLoyalty(loyal); + } + break; + + case 'M': + if (line.startsWith("ManaCost")) { + final String value = line.substring(9); + // System.out.println(s); + if (!"no cost".equals(value)) { + card.setManaCost(new CardManaCost(new ManaCostParser(value))); + } + } + break; + + case 'N': + if (line.startsWith("Name")) { + final String value = line.substring(5); + card.setName(value); + } + break; + + case 'P': + if (line.startsWith("PT")) { + final String value = line.substring(3); + final String[] powTough = value.split("/"); + int att; + if (powTough[0].contains("*")) { + att = 0; + } else { + att = Integer.parseInt(powTough[0]); + } + + int def; + if (powTough[1].contains("*")) { + def = 0; + } else { + def = Integer.parseInt(powTough[1]); + } + + card.setBaseAttackString(powTough[0]); + card.setBaseDefenseString(powTough[1]); + card.setBaseAttack(att); + card.setBaseDefense(def); + } + break; + + case 'R': + if (line.startsWith("R:")) { + card.addReplacementEffect(ReplacementHandler.parseReplacement(line.substring(2), card)); + } + break; + + case 'S': + if (line.startsWith("S:")) { + card.addStaticAbilityString(line.substring(2)); + } else if (line.startsWith("SVar")) { + final String[] value = line.split(":", 3); + card.setSVar(value[1], value[2]); + } else if (line.startsWith("SetInfo")) { + final String value = line.substring(8); + card.addSet(new EditionInfo(value)); + // 8/18/11 11:08 PM + } + break; + + case 'T': + if (line.startsWith("Types")) { + CardReader.addTypes(card, line.substring(6)); + } else if (line.startsWith("Text")) { + String value = line.substring(5); + // if (!t.equals("no text")); + if ("no text".equals(value)) { + value = ""; + } + card.setText(value); + } else if (line.startsWith("T:")) { + card.addTrigger(TriggerHandler.parseTrigger(line.substring(2), card, true)); + } + break; + + } + } /** * Set the character encoding to use when loading cards. * @@ -559,7 +499,7 @@ public class CardReader implements Runnable { * * @return a new Card instance */ - protected final Card loadCard(final File pathToTxtFile) { + protected final CardRules loadCard(final File pathToTxtFile) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(pathToTxtFile); @@ -586,12 +526,11 @@ public class CardReader implements Runnable { * * @return a new Card instance */ - protected final Card loadCard(final ZipEntry entry) { + protected final CardRules loadCard(final ZipEntry entry) { InputStream zipInputStream = null; try { zipInputStream = this.zip.getInputStream(entry); return this.loadCard(zipInputStream); - } catch (final IOException exn) { throw new RuntimeException(exn); // PM diff --git a/src/main/java/forge/card/CardRules.java b/src/main/java/forge/card/CardRules.java index fbeea4fa9e2..df65dce1cd0 100644 --- a/src/main/java/forge/card/CardRules.java +++ b/src/main/java/forge/card/CardRules.java @@ -59,6 +59,8 @@ public final class CardRules { private final boolean hasOtherFace; + private List originalScript; + // Ctor and builders are needed here /** * Gets the name. @@ -234,14 +236,14 @@ public final class CardRules { * @param removedFromAIDecks * the removed from ai decks */ - public CardRules(final CardRuleCharacteristics chars, final boolean isDoubleFacedCard, final CardRules otherPart, + public CardRules(final CardRuleCharacteristics chars, List forgeScript, final boolean isDoubleFacedCard, final CardRules otherPart, final boolean removedFromRandomDecks, final boolean removedFromAIDecks) { this.characteristics = chars; this.slavePart = otherPart; this.hasOtherFace = isDoubleFacedCard; this.isRemovedFromAIDecks = removedFromAIDecks; this.isRemovedFromRandomDecks = removedFromRandomDecks; - + this.originalScript = forgeScript == null ? null : new ArrayList(forgeScript); // System.out.println(cardName); if (this.getType().isCreature()) { @@ -897,4 +899,12 @@ public final class CardRules { CardRarity.Special); } } + + /** + * TODO: Write javadoc for this method. + * @return + */ + public Iterable getCardScript() { + return originalScript; + } } diff --git a/src/main/java/forge/card/CardRulesReader.java b/src/main/java/forge/card/CardRulesReader.java index f8bbac42489..dd8175097f2 100644 --- a/src/main/java/forge/card/CardRulesReader.java +++ b/src/main/java/forge/card/CardRulesReader.java @@ -17,6 +17,8 @@ */ package forge.card; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import forge.card.mana.ManaCostShard; @@ -43,6 +45,7 @@ public class CardRulesReader { private boolean removedFromAIDecks = false; private boolean removedFromRandomDecks = false; + private List originalScript = new ArrayList(); // Reset all fields to parse next card (to avoid allocating new // CardRulesReader N times) @@ -54,6 +57,7 @@ public class CardRulesReader { this.curCharacteristics = 0; this.removedFromAIDecks = false; this.removedFromRandomDecks = false; + originalScript.clear(); // this.isDoubleFacedCard = false; // this.isFlipCard = false; } @@ -65,10 +69,10 @@ public class CardRulesReader { */ public final CardRules getCard() { final boolean hasOtherPart = this.characteristics[1] != null; - final CardRules otherPart = hasOtherPart ? new CardRules(this.characteristics[1], true, null, + final CardRules otherPart = hasOtherPart ? new CardRules(this.characteristics[1], null, true, null, this.removedFromRandomDecks, this.removedFromAIDecks) : null; - return new CardRules(this.characteristics[0], hasOtherPart, otherPart, this.removedFromRandomDecks, + return new CardRules(this.characteristics[0], originalScript, hasOtherPart, otherPart, this.removedFromRandomDecks, this.removedFromAIDecks); } @@ -79,6 +83,9 @@ public class CardRulesReader { * the line */ public final void parseLine(final String line) { + + originalScript.add(line); + switch( line.charAt(0)){ case 'A': if (line.startsWith("AlternateMode:")) { diff --git a/src/main/java/forge/card/MtgDataParser.java b/src/main/java/forge/card/MtgDataParser.java index 34486d4e76f..cf6c8656147 100644 --- a/src/main/java/forge/card/MtgDataParser.java +++ b/src/main/java/forge/card/MtgDataParser.java @@ -149,9 +149,9 @@ public final class MtgDataParser implements Iterator { return null; } - final CardRules otherPart = hasOtherPart ? new CardRules(this.chars[1], hasOtherPart, null, false, false) + final CardRules otherPart = hasOtherPart ? new CardRules(this.chars[1], null, hasOtherPart, null, false, false) : null; - return new CardRules(this.chars[0], hasOtherPart, otherPart, false, false); + return new CardRules(this.chars[0], null, hasOtherPart, otherPart, false, false); } private String readSingleCard(final CardRuleCharacteristics ret) { diff --git a/src/main/java/forge/card/abilityfactory/AbilityFactoryChoose.java b/src/main/java/forge/card/abilityfactory/AbilityFactoryChoose.java index 624bb2ea7cb..65a3c6bf6aa 100644 --- a/src/main/java/forge/card/abilityfactory/AbilityFactoryChoose.java +++ b/src/main/java/forge/card/abilityfactory/AbilityFactoryChoose.java @@ -1623,7 +1623,7 @@ public final class AbilityFactoryChoose { // still missing a listener to display the card preview // in the right name = choice.getSelectedValue(); - if (AllZone.getCardFactory().getCard(name, p).isValid(valid, host.getController(), host)) { + if (AllZone.getCardFactory().getCard(CardDb.instance().getCard(name), p).isValid(valid, host.getController(), host)) { host.setNamedCard(choice.getSelectedValue()); ok = true; } diff --git a/src/main/java/forge/card/abilityfactory/AbilityFactoryCopy.java b/src/main/java/forge/card/abilityfactory/AbilityFactoryCopy.java index 6b54d567c7d..a5e0485bf68 100644 --- a/src/main/java/forge/card/abilityfactory/AbilityFactoryCopy.java +++ b/src/main/java/forge/card/abilityfactory/AbilityFactoryCopy.java @@ -44,6 +44,7 @@ import forge.game.player.ComputerUtil; import forge.game.player.Player; import forge.game.zone.ZoneType; import forge.gui.GuiUtils; +import forge.item.CardDb; import forge.util.MyRandom; /** @@ -408,7 +409,8 @@ public final class AbilityFactoryCopy { Card copy; if (!c.isToken()) { // copy creature and put it onto the battlefield - copy = AllZone.getCardFactory().getCard(c.getName(), sa.getActivatingPlayer()); + + copy = AllZone.getCardFactory().getCard(CardDb.instance().getCard(c), sa.getActivatingPlayer()); // when copying something stolen: copy.addController(sa.getActivatingPlayer()); diff --git a/src/main/java/forge/card/cardfactory/AbstractCardFactory.java b/src/main/java/forge/card/cardfactory/CardFactory.java similarity index 77% rename from src/main/java/forge/card/cardfactory/AbstractCardFactory.java rename to src/main/java/forge/card/cardfactory/CardFactory.java index 3043ab35a76..9c061a3a417 100644 --- a/src/main/java/forge/card/cardfactory/AbstractCardFactory.java +++ b/src/main/java/forge/card/cardfactory/CardFactory.java @@ -20,24 +20,24 @@ package forge.card.cardfactory; import java.io.File; import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; - import forge.AllZone; import forge.Card; import forge.CardCharactersticName; -import forge.CardList; +import forge.CardReader; +import forge.CardUtil; import forge.Singletons; +import forge.card.CardRules; import forge.card.spellability.SpellAbility; import forge.card.spellability.SpellPermanent; import forge.card.spellability.Target; +import forge.error.ErrorViewer; import forge.game.player.ComputerUtil; import forge.game.player.Player; +import forge.gui.GuiUtils; +import forge.item.CardDb; +import forge.item.CardPrinted; import forge.properties.ForgeProps; import forge.properties.NewConstants; -import forge.util.FileUtil; /** *

@@ -57,22 +57,7 @@ import forge.util.FileUtil; * @author Forge * @version $Id$ */ -public abstract class AbstractCardFactory implements CardFactoryInterface { - /** - * This maps card name Strings to Card instances. The Card instances have no - * owner, and lack abilities. - * - * To get a full-fledged card, see allCards field or the iterator method. - */ - private final Map map = new TreeMap(); - - /** This is a special list of cards, with all abilities attached. */ - protected List allCardsReadOnly; - - private Set removedCardList; - private final Card blankCard = new Card(); // new code - - private final CardList copiedList = new CardList(); +public class CardFactory implements CardFactoryInterface { /** *

@@ -82,45 +67,23 @@ public abstract class AbstractCardFactory implements CardFactoryInterface { * @param file * a {@link java.io.File} object. */ - protected AbstractCardFactory(final File file) { - final SpellAbility spell = new SpellAbility(SpellAbility.getSpell(), this.blankCard) { - // neither computer nor human play can play this card - @Override - public boolean canPlay() { - return false; - } + private final CardReader reader; + + public CardFactory(final File file) { - @Override - public void resolve() { - } - }; - this.blankCard.addSpellAbility(spell); - spell.setManaCost("1"); - this.blankCard.setName("Removed Card"); - - // owner and controller will be wrong sometimes - // but I don't think it will matter - // theoretically blankCard will go to the wrong graveyard - this.blankCard.setOwner(AllZone.getHumanPlayer()); - - this.removedCardList = new TreeSet(FileUtil.readFile(ForgeProps.getFile(NewConstants.REMOVED))); + GuiUtils.checkEDT("CardFactory$constructor", false); + reader = new CardReader(ForgeProps.getFile(NewConstants.CARDSFOLDER), true); + try { + // this fills in our map of card names to Card instances. + final List listCardRules = reader.loadCards(); + CardDb.setup(listCardRules.iterator()); + } catch (final Exception ex) { + ErrorViewer.showError(ex); + } + } // constructor - /** - * Getter for allCards. - * - * @return allCards - */ - - /** - * Getter for map. - * - * @return map - */ - protected final Map getMap() { - return this.map; - } /** *

@@ -137,9 +100,8 @@ public abstract class AbstractCardFactory implements CardFactoryInterface { if (in.isInAlternateState()) { in.setState(CardCharactersticName.Original); } - final Card out = this.getCard(in.getName(), in.getOwner()); + final Card out = this.getCard(CardDb.instance().getCard(in), in.getOwner()); out.setUniqueNumber(in.getUniqueNumber()); - out.setCurSetCode(in.getCurSetCode()); CardFactoryUtil.copyCharacteristics(in, out); if (in.hasAlternateState()) { @@ -244,25 +206,37 @@ public abstract class AbstractCardFactory implements CardFactoryInterface { * blankCard */ @Override - public final Card getCard(final String cardName, final Player owner) { - if (this.removedCardList.contains(cardName) || cardName.equals(this.blankCard.getName())) { - return this.blankCard; - } + public final Card getCard(final CardPrinted cp, final Player owner) { //System.out.println(cardName); - return this.getCard2(cardName, owner); + Card c = this.getCard2(cp.getCard().getCardScript(), owner); + + if (c != null) { + c.setCurSetCode(cp.getEdition()); + c.setRandomPicture(cp.getArtIndex() + 1); + c.setImageFilename(cp.getImageFilename()); + + if (c.hasAlternateState()) { + if (c.isFlipCard()) { + c.setState(CardCharactersticName.Flipped); + } + if (c.isDoubleFaced()) { + c.setState(CardCharactersticName.Transformed); + } + c.setImageFilename(CardUtil.buildFilename(c)); + c.setState(CardCharactersticName.Original); + } + } + // else throw "Unsupported card"; + return c; + } - protected Card getCard2(final String cardName, final Player owner) { + protected Card getCard2(final Iterable script, final Player owner) { // o should be Card object - final Card o = this.map.get(cardName); - if (o == null) { - final StringBuilder sb = new StringBuilder(); - sb.append("CardFactory : getCard() invalid card name - ").append(cardName); - throw new RuntimeException(sb.toString()); - } - - return getCard2(o, owner); + final Card o = CardReader.readCard(script); + o.setOwner(owner); + return buildAbilities(o); } public static Card getCard2(final Card o, final Player owner) { diff --git a/src/main/java/forge/card/cardfactory/CardFactoryInterface.java b/src/main/java/forge/card/cardfactory/CardFactoryInterface.java index 13ad76710de..e3dd11df09c 100644 --- a/src/main/java/forge/card/cardfactory/CardFactoryInterface.java +++ b/src/main/java/forge/card/cardfactory/CardFactoryInterface.java @@ -21,6 +21,7 @@ package forge.card.cardfactory; import forge.Card; import forge.card.spellability.SpellAbility; import forge.game.player.Player; +import forge.item.CardPrinted; /** * The Interface CardFactoryInterface. @@ -65,5 +66,5 @@ public interface CardFactoryInterface { * @return a {@link forge.Card} instance, owned by owner; or the special * blankCard */ - Card getCard(String cardName, Player owner); + Card getCard(CardPrinted card, Player owner); } diff --git a/src/main/java/forge/card/cardfactory/PreloadingCardFactory.java b/src/main/java/forge/card/cardfactory/PreloadingCardFactory.java deleted file mode 100644 index 2b67500a424..00000000000 --- a/src/main/java/forge/card/cardfactory/PreloadingCardFactory.java +++ /dev/null @@ -1,113 +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 . - */ -package forge.card.cardfactory; - -import java.io.File; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -import javax.swing.SwingUtilities; - -import forge.AllZone; -import forge.Card; -import forge.CardReader; -import forge.card.CardRules; -import forge.error.ErrorViewer; -import forge.gui.GuiUtils; -import forge.gui.toolbox.FProgressBar; -import forge.item.CardDb; -import forge.properties.ForgeProps; -import forge.properties.NewConstants; -import forge.view.SplashFrame; - -/** - *

- * CardFactory class. - *

- * - * TODO The map field contains Card instances that have not gone through - * getCard2, and thus lack abilities. However, when a new Card is requested via - * getCard, it is this map's values that serve as the templates for the values - * it returns. This class has another field, allCards, which is another copy of - * the card database. These cards have abilities attached to them, and are owned - * by the human player by default. It would be better memory-wise if we had - * only one or the other. We may experiment in the future with using - * allCard-type values for the map instead of the less complete ones that exist - * there today. - * - * @author Forge - * @version $Id$ - */ -public class PreloadingCardFactory extends AbstractCardFactory { - - /** - *

- * Constructor for CardFactory. - *

- * - * @param filename - * a {@link java.lang.String} object. - */ - public PreloadingCardFactory(final String filename) { - this(new File(filename)); - } - - /** - *

- * Constructor for CardFactory. - *

- * - * @param file - * a {@link java.io.File} object. - */ - public PreloadingCardFactory(final File file) { - super(file); - GuiUtils.checkEDT("PreloadingCardFactory$constructor", false); - - try { - this.readCards(file); - - } catch (final Exception ex) { - ErrorViewer.showError(ex); - } - } // constructor - - /** - *

- * readCards. - *

- * - * @param file - * a {@link java.io.File} object. - */ - protected final void readCards(final File file) { - this.getMap().clear(); - - final List listCardRules = new ArrayList(); - final CardReader read = new CardReader(ForgeProps.getFile(NewConstants.CARDSFOLDER), this.getMap(), - listCardRules); - - // this fills in our map of card names to Card instances. - read.run(); - CardDb.setup(listCardRules.iterator()); - - } // readCard() - - -} // end class PreloadingCardFactory diff --git a/src/main/java/forge/game/limited/LimitedDeck.java b/src/main/java/forge/game/limited/LimitedDeck.java index 366b4b12506..151021ea6d5 100644 --- a/src/main/java/forge/game/limited/LimitedDeck.java +++ b/src/main/java/forge/game/limited/LimitedDeck.java @@ -3,7 +3,6 @@ package forge.game.limited; import java.util.HashMap; import java.util.Map; -import forge.AllZone; import forge.Card; import forge.CardList; import forge.CardListFilter; @@ -14,6 +13,8 @@ import forge.card.CardManaCost; import forge.card.DeckWants; import forge.card.mana.ManaCostShard; import forge.deck.Deck; +import forge.item.CardDb; +import forge.item.CardPrinted; import forge.util.MyRandom; /** @@ -130,9 +131,8 @@ public class LimitedDeck extends Deck { // if no playable cards remain fill up with basic lands for (int i = 0; i < 5; i++) { if (clrCnts[i].getCount() > 0) { - final Card c = AllZone.getCardFactory().getCard(clrCnts[i].getColor(), AllZone.getComputerPlayer()); - c.setCurSetCode(IBoosterDraft.LAND_SET_CODE[0]); - deckList.add(c); + final CardPrinted cp = CardDb.instance().getCard(clrCnts[i].getColor(), IBoosterDraft.LAND_SET_CODE[0]); + deckList.add(cp.toForgeCard()); break; } } @@ -207,9 +207,8 @@ public class LimitedDeck extends Deck { } for (int j = 0; j <= nLand; j++) { - final Card c = AllZone.getCardFactory().getCard(clrCnts[i].getColor(), AllZone.getComputerPlayer()); - c.setCurSetCode(IBoosterDraft.LAND_SET_CODE[0]); - deckList.add(c); + final CardPrinted cp = CardDb.instance().getCard(clrCnts[i].getColor(), IBoosterDraft.LAND_SET_CODE[0]); + deckList.add(cp.toForgeCard()); landsAdded++; } } @@ -219,13 +218,12 @@ public class LimitedDeck extends Deck { int n = 0; while (landsNeeded > 0) { if (clrCnts[n].getCount() > 0) { - final Card c = AllZone.getCardFactory().getCard(clrCnts[n].getColor(), AllZone.getComputerPlayer()); - c.setCurSetCode(IBoosterDraft.LAND_SET_CODE[0]); - deckList.add(c); + CardPrinted cp = CardDb.instance().getCard(clrCnts[n].getColor(), IBoosterDraft.LAND_SET_CODE[0]); + deckList.add(cp.toForgeCard()); landsNeeded--; if (Constant.Runtime.DEV_MODE[0]) { - System.out.println("AddBasics: " + c.getName()); + System.out.println("AddBasics: " + cp.getName()); } } if (++n > 4) { diff --git a/src/main/java/forge/gui/GuiDisplayUtil.java b/src/main/java/forge/gui/GuiDisplayUtil.java index e09d5189ae1..68e4c9757d9 100644 --- a/src/main/java/forge/gui/GuiDisplayUtil.java +++ b/src/main/java/forge/gui/GuiDisplayUtil.java @@ -580,7 +580,7 @@ public final class GuiDisplayUtil { for (final String element : data) { final String[] cardinfo = element.trim().split("\\|"); - final Card c = AllZone.getCardFactory().getCard(cardinfo[0], player); + final Card c = AllZone.getCardFactory().getCard(CardDb.instance().getCard(cardinfo[0]), player); boolean hasSetCurSet = false; for (final String info : cardinfo) { diff --git a/src/main/java/forge/item/CardPrinted.java b/src/main/java/forge/item/CardPrinted.java index b73aa31a8d0..7d7ae63ba6d 100644 --- a/src/main/java/forge/item/CardPrinted.java +++ b/src/main/java/forge/item/CardPrinted.java @@ -25,12 +25,10 @@ import org.apache.commons.lang3.ArrayUtils; import forge.AllZone; import forge.Card; -import forge.CardCharactersticName; import forge.CardUtil; import forge.card.CardRarity; import forge.card.CardRules; import forge.game.player.Player; -import forge.util.MyRandom; import forge.util.closures.Lambda1; import forge.util.closures.Predicate; import forge.util.closures.PredicateString; @@ -291,30 +289,7 @@ public final class CardPrinted implements Comparable, InventoryItem * @return the card */ public Card toForgeCard(Player owner) { - final Card c = AllZone.getCardFactory().getCard(this.name, owner); - if (c != null) { - c.setCurSetCode(this.getEdition()); - c.setRandomPicture(this.artIndex + 1); - c.setImageFilename(this.getImageFilename()); - - if (c.hasAlternateState()) { - if (c.isFlipCard()) { - c.setState(CardCharactersticName.Flipped); - } - if (c.isDoubleFaced()) { - c.setState(CardCharactersticName.Transformed); - } - c.setImageFilename(CardUtil.buildFilename(c)); - c.setState(CardCharactersticName.Original); - } - } - - final int cntVariants = getCard().getEditionInfo(getEdition()).getCopiesCount(); - if (cntVariants > 1) { - c.setRandomPicture(MyRandom.getRandom().nextInt(cntVariants - 1) + 1); - c.setImageFilename(CardUtil.buildFilename(c)); - } - // else throw "Unsupported card"; + final Card c = AllZone.getCardFactory().getCard(this, owner); return c; } diff --git a/src/main/java/forge/quest/QuestUtil.java b/src/main/java/forge/quest/QuestUtil.java index c7277a5fbb0..22517f8637e 100644 --- a/src/main/java/forge/quest/QuestUtil.java +++ b/src/main/java/forge/quest/QuestUtil.java @@ -21,8 +21,9 @@ import forge.AllZone; import forge.Card; import forge.CardList; import forge.CardUtil; -import forge.card.cardfactory.AbstractCardFactory; +import forge.card.cardfactory.CardFactory; import forge.game.player.Player; +import forge.item.CardDb; import forge.quest.bazaar.QuestPetController; import java.util.List; @@ -96,7 +97,7 @@ public class QuestUtil { if (pet != null) { Card c = pet.getPetCard(qc.getAssets()); if (c != null) { - Card copy = AbstractCardFactory.getCard2(c, AllZone.getHumanPlayer()); + Card copy = CardFactory.getCard2(c, AllZone.getHumanPlayer()); copy.setSickness(true); copy.addController(AllZone.getHumanPlayer()); copy.setImageName(c.getImageName()); @@ -190,9 +191,7 @@ public class QuestUtil { } // Standard card creation else { - tempcard = AllZone.getCardFactory().getCard(name, owner); - tempcard.setCurSetCode(tempcard.getMostRecentSet()); - tempcard.setImageFilename(CardUtil.buildFilename(tempcard)); + tempcard = CardDb.instance().getCard(name, true).toForgeCard(owner); } return tempcard; } diff --git a/src/test/java/forge/CardReaderTest.java b/src/test/java/forge/CardReaderTest.java deleted file mode 100644 index 3a69ebc7615..00000000000 --- a/src/test/java/forge/CardReaderTest.java +++ /dev/null @@ -1,128 +0,0 @@ -package forge; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import net.slightlymagic.braids.util.testng.BraidsAssertFunctions; -import net.slightlymagic.braids.util.testng.ClumsyRunnable; - -import org.testng.Assert; -import org.testng.annotations.Test; - -import forge.properties.ForgeProps; -import forge.properties.NewConstants; - -/** - * Created by hand to test the CardReader class. - */ -@Test(groups = { "UnitTest" }, enabled = false) -public class CardReaderTest { - - /** The default test-timeout. */ - public static final int TEST_TIMEOUT = 1000; - - /** The estimated number of cards in the cardsfolder. */ - public static final int ESTIMATED_CARDS_IN_FOLDER = 9001; - - /** - * Test_ read card_null map. - */ - @Test(groups = { "UnitTest", "fast" }, timeOut = CardReaderTest.TEST_TIMEOUT) - public final void test_ReadCard_nullMap() { - final ClumsyRunnable withScissors = new ClumsyRunnable() { - @Override - public void run() throws Exception { - new CardReader(ForgeProps.getFile(NewConstants.CARDSFOLDER), null); - } - }; - - BraidsAssertFunctions.assertThrowsException(NullPointerException.class, withScissors); - } - - /** - * Test_ read card_null cards folder. - */ - @Test(groups = { "UnitTest", "fast" }, timeOut = CardReaderTest.TEST_TIMEOUT) - public final void test_ReadCard_nullCardsFolder() { - final ClumsyRunnable withScissors = new ClumsyRunnable() { - @Override - public void run() throws Exception { - final Map map = new HashMap(); - new CardReader(null, map); - } - }; - - BraidsAssertFunctions.assertThrowsException(NullPointerException.class, withScissors); - } - - /** - * Test_ read card_nonexistent cards folder. - */ - @Test(groups = { "UnitTest", "fast" }, timeOut = CardReaderTest.TEST_TIMEOUT) - public final void test_ReadCard_nonexistentCardsFolder() { - final ClumsyRunnable withScissors = new ClumsyRunnable() { - @Override - public void run() throws Exception { - final Map map = new HashMap(); - new CardReader( - new File( - "this_does_not_exist_fjksdjfsdjfkdjslkfksdlajfikajfklsdhfksdalfhjklsdahfeakslfdsfdsfdsfdsfdssfc"), - map); - } - }; - - BraidsAssertFunctions.assertThrowsException(RuntimeException.class, withScissors); - } - - /** - * Test_ read card_file not folder. - * - * @throws IOException - * Signals that an I/O exception has occurred. - */ - @Test(groups = { "UnitTest", "fast" }, timeOut = CardReaderTest.TEST_TIMEOUT) - public final void test_ReadCard_fileNotFolder() throws IOException { - - final File tmpFile = File.createTempFile("just-a-file", ".testng.tmp"); - tmpFile.deleteOnExit(); // request VM to delete later - - final ClumsyRunnable withScissors = new ClumsyRunnable() { - @Override - public void run() throws Exception { - final Map map = new HashMap(); - new CardReader(tmpFile, map); - } - }; - - BraidsAssertFunctions.assertThrowsException(RuntimeException.class, withScissors); - } - - /** - * Test_ read card_run_nonzip. - */ - @Test(groups = { "slow" }, enabled = false) - public final void test_ReadCard_run_nonzip() { - final Map map = new HashMap(2 * CardReaderTest.ESTIMATED_CARDS_IN_FOLDER); - final File cardsfolder = ForgeProps.getFile(NewConstants.CARDSFOLDER); - final CardReader cardReader = new CardReader(cardsfolder, map, null, false); - cardReader.run(); - Assert.assertNotNull(map.get("Elvish Warrior"), "Elvish Warrior was loaded"); - Assert.assertNotNull(map.get("Savannah Lions"), "Savannah Lions were loaded"); - } - - /** - * Test_ read card_run_zip. - */ - @Test(groups = { "slow" }, enabled = false) - public final void test_ReadCard_run_zip() { - final Map map = new HashMap(2 * CardReaderTest.ESTIMATED_CARDS_IN_FOLDER); - final File cardsfolder = ForgeProps.getFile(NewConstants.CARDSFOLDER); - final CardReader cardReader = new CardReader(cardsfolder, map); - cardReader.run(); - Assert.assertNotNull(map.get("Elvish Warrior"), "Elvish Warrior was loaded"); - Assert.assertNotNull(map.get("Savannah Lions"), "Savannah Lions were loaded"); - } - -} diff --git a/src/test/java/forge/GuiMultipleBlockers4Test.java b/src/test/java/forge/GuiMultipleBlockers4Test.java deleted file mode 100644 index a4824e9d8ce..00000000000 --- a/src/test/java/forge/GuiMultipleBlockers4Test.java +++ /dev/null @@ -1,28 +0,0 @@ -package forge; - -import org.testng.annotations.Test; - -import forge.gui.GuiMultipleBlockers; - -/** - * Created by IntelliJ IDEA. User: dhudson - */ -@Test(groups = { "UnitTest" }, timeOut = 1000, enabled = false) -public class GuiMultipleBlockers4Test { - - /** - * Gui multiple blockers4 test1. - */ - @Test(timeOut = 1000, enabled = false) - public void guiMultipleBlockers4Test1() { - final CardList list = new CardList(); - list.add(AllZone.getCardFactory().getCard("Elvish Piper", null)); - list.add(AllZone.getCardFactory().getCard("Lantern Kami", null)); - list.add(AllZone.getCardFactory().getCard("Frostling", null)); - list.add(AllZone.getCardFactory().getCard("Frostling", null)); - - for (int i = 0; i < 2; i++) { - new GuiMultipleBlockers(null, list, i + 1); - } - } -} diff --git a/src/test/java/forge/RunTest.java b/src/test/java/forge/RunTest.java index cf48e34f1fa..ef92d338081 100644 --- a/src/test/java/forge/RunTest.java +++ b/src/test/java/forge/RunTest.java @@ -1,17 +1,8 @@ package forge; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - import org.testng.annotations.Test; -import forge.card.CardManaCost; import forge.card.cardfactory.CardFactoryInterface; -import forge.card.mana.ManaCost; -import forge.card.mana.ManaCostParser; -import forge.control.input.InputPayManaCostUtil; import forge.game.phase.CombatUtil; /** @@ -36,6 +27,7 @@ public class RunTest { Card c; final CardFactoryInterface cf = AllZone.getCardFactory(); // ********* test Card + /* c = cf.getCard("Elvish Warrior", AllZone.getComputerPlayer()); this.check("1", c.getOwner().isComputer()); this.check("1.1", c.getName().equals("Elvish Warrior")); @@ -221,7 +213,7 @@ public class RunTest { // test Input_PayManaCostUtil this.check("98", InputPayManaCostUtil.getLongColorString("G").equals(Constant.Color.GREEN)); this.check("99", InputPayManaCostUtil.getLongColorString("1").equals(Constant.Color.COLORLESS)); - +*/ /* * check("101", Input_PayManaCostUtil.isManaNeeded(Constant.Color.Green, * new ManaCost("5")) == true); check("102", @@ -255,7 +247,7 @@ public class RunTest { c2.addIntrinsicKeyword("Flying"); this.check("109", CombatUtil.canBlock(c, c2)); this.check("110", !CombatUtil.canBlock(c2, c)); - +/* c = cf.getCard("Fyndhorn Elves", null); c2 = cf.getCard("Talas Warrior", null); this.check("110a", !CombatUtil.canBlock(c2, c)); @@ -326,6 +318,7 @@ public class RunTest { this.check("125", CardUtil.getConvertedManaCost("R R R") == 3); this.check("126", CardUtil.getConvertedManaCost("1") == 1); this.check("127", CardUtil.getConvertedManaCost("2/R 2/G 2/W 2/B 2/U") == 10); + */ } // test() /**