From cc60f7eea77a6ada4b461cb0ff669b5eece87400 Mon Sep 17 00:00:00 2001 From: Maxmtg Date: Sun, 19 Feb 2012 16:57:35 +0000 Subject: [PATCH] res/deck/cube dir is for decks that are intended to be cubes. One can read values from KeyValue lines via FileSection class (it parses by itself and provides a default value for bad parses) CustomLimited is more related to limited games and its moved to game.limited package --- .gitattributes | 4 +- .../java/forge/card/BoosterGenerator.java | 9 + src/main/java/forge/deck/CardCollections.java | 7 +- src/main/java/forge/deck/CustomLimited.java | 400 ------------------ src/main/java/forge/deck/Deck.java | 2 + .../java/forge/deck/io/CubeSerializer.java | 88 ---- .../java/forge/deck/io/DeckFileHeader.java | 7 +- .../java/forge/deck/io/DeckSerializer.java | 4 +- .../java/forge/deck/io/OldDeckParser.java | 22 +- .../java/forge/game/limited/BoosterDraft.java | 11 +- .../forge/game/limited/CustomLimited.java | 240 +++++++++++ .../java/forge/game/limited/SealedDeck.java | 7 +- src/main/java/forge/item/PreconDeck.java | 3 +- src/main/java/forge/util/FileSection.java | 48 +++ src/main/java/forge/util/SectionUtil.java | 20 - 15 files changed, 335 insertions(+), 537 deletions(-) delete mode 100644 src/main/java/forge/deck/CustomLimited.java delete mode 100644 src/main/java/forge/deck/io/CubeSerializer.java create mode 100644 src/main/java/forge/game/limited/CustomLimited.java create mode 100644 src/main/java/forge/util/FileSection.java diff --git a/.gitattributes b/.gitattributes index 7359de2f9dd..35293393650 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11214,7 +11214,6 @@ src/main/java/forge/control/match/ControlWinLose.java -text src/main/java/forge/control/match/package-info.java -text src/main/java/forge/control/package-info.java -text src/main/java/forge/deck/CardCollections.java -text -src/main/java/forge/deck/CustomLimited.java svneol=native#text/plain src/main/java/forge/deck/Deck.java svneol=native#text/plain src/main/java/forge/deck/DeckBase.java -text src/main/java/forge/deck/DeckRecognizer.java -text @@ -11229,7 +11228,6 @@ src/main/java/forge/deck/generate/GenerateConstructedMultiColorDeck.java svneol= src/main/java/forge/deck/generate/GenerateDeckUtil.java -text src/main/java/forge/deck/generate/GenerateThemeDeck.java svneol=native#text/plain src/main/java/forge/deck/generate/package-info.java svneol=native#text/plain -src/main/java/forge/deck/io/CubeSerializer.java -text src/main/java/forge/deck/io/DeckFileHeader.java -text src/main/java/forge/deck/io/DeckSerializer.java -text src/main/java/forge/deck/io/DeckSerializerBase.java -text @@ -11251,6 +11249,7 @@ src/main/java/forge/game/limited/BoosterDraftAI.java svneol=native#text/plain src/main/java/forge/game/limited/CCnt.java svneol=native#text/plain src/main/java/forge/game/limited/CardPoolLimitation.java -text src/main/java/forge/game/limited/CardRatings.java -text +src/main/java/forge/game/limited/CustomLimited.java svneol=native#text/plain src/main/java/forge/game/limited/DeckColors.java svneol=native#text/plain src/main/java/forge/game/limited/IBoosterDraft.java svneol=native#text/plain src/main/java/forge/game/limited/SealedDeck.java svneol=native#text/plain @@ -11360,6 +11359,7 @@ src/main/java/forge/quest/package-info.java svneol=native#text/plain src/main/java/forge/util/Base64Coder.java svneol=native#text/plain src/main/java/forge/util/CopyFiles.java svneol=native#text/plain src/main/java/forge/util/FileFinder.java svneol=native#text/plain +src/main/java/forge/util/FileSection.java -text src/main/java/forge/util/FileUtil.java svneol=native#text/plain src/main/java/forge/util/FolderMap.java svneol=native#text/plain src/main/java/forge/util/FolderMapView.java -text diff --git a/src/main/java/forge/card/BoosterGenerator.java b/src/main/java/forge/card/BoosterGenerator.java index 7a05425f960..4545e2382e8 100644 --- a/src/main/java/forge/card/BoosterGenerator.java +++ b/src/main/java/forge/card/BoosterGenerator.java @@ -21,6 +21,7 @@ import java.security.InvalidParameterException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import net.slightlymagic.braids.util.lambda.Lambda1; @@ -233,6 +234,14 @@ public class BoosterGenerator { return temp; } + public final List getBoosterPack(final Map numbers, + final int nRareSlots, final int nDoubls, final int nAnyCard) { + return getBoosterPack( numbers.get(CardRarity.Common), numbers.get(CardRarity.Uncommon), nRareSlots, + numbers.get(CardRarity.Rare), numbers.get(CardRarity.MythicRare), numbers.get(CardRarity.Special), + nDoubls, nAnyCard, numbers.get(CardRarity.BasicLand) ); + } + + /** * So many parameters are needed for custom limited cardpools,. * diff --git a/src/main/java/forge/deck/CardCollections.java b/src/main/java/forge/deck/CardCollections.java index acab4ed1df1..72a8539ad7e 100644 --- a/src/main/java/forge/deck/CardCollections.java +++ b/src/main/java/forge/deck/CardCollections.java @@ -2,7 +2,6 @@ package forge.deck; import java.io.File; -import forge.deck.io.CubeSerializer; import forge.deck.io.DeckSerializer; import forge.deck.io.DeckSetSerializer; import forge.deck.io.OldDeckParser; @@ -18,7 +17,7 @@ public class CardCollections { private final IFolderMap constructed; private final IFolderMap draft; private final IFolderMap sealed; - private final IFolderMap cube; + private final IFolderMap cube; /** * TODO: Write javadoc for Constructor. @@ -28,7 +27,7 @@ public class CardCollections { constructed = new FolderMap(new DeckSerializer(new File(file, "constructed"))); draft = new FolderMap(new DeckSetSerializer(new File(file, "draft"))); sealed = new FolderMap(new DeckSetSerializer(new File(file, "sealed"))); - cube = new FolderMap(new CubeSerializer(new File(file, "cube"))); + cube = new FolderMap(new DeckSerializer(new File(file, "cube"))); // remove this after most people have been switched to new layout OldDeckParser oldParser = new OldDeckParser(file, constructed, draft, sealed, cube); @@ -43,7 +42,7 @@ public class CardCollections { return draft; } - public final IFolderMap getCubes() { + public final IFolderMap getCubes() { return cube; } diff --git a/src/main/java/forge/deck/CustomLimited.java b/src/main/java/forge/deck/CustomLimited.java deleted file mode 100644 index 03877e02657..00000000000 --- a/src/main/java/forge/deck/CustomLimited.java +++ /dev/null @@ -1,400 +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.deck; - -import java.util.List; -import java.util.Map; - -import forge.AllZone; -import forge.item.CardPrinted; -import forge.item.ItemPoolView; -import forge.util.SectionUtil; - -/** - *

- * CustomDraft class. - *

- * - * @author Forge - * @version $Id$ - */ -public class CustomLimited extends DeckBase { - - /** - * TODO: Write javadoc for Constructor. - * @param name0 - */ - public CustomLimited(String name0) { - super(name0); - } - - private static final long serialVersionUID = 7435640939026612173L; - - /** The Type. */ - // private String type; - - /** The Deck file. */ - private String deckFile; - - /** The Ignore rarity. */ - private Boolean ignoreRarity; - - /** The Singleton. */ - private Boolean singleton = false; - - /** The Num cards. */ - private int numCards = 15; - - /** The Num specials. */ - private int numSpecials = 0; - - /** The Num mythics. */ - private int numMythics = 1; - - /** The Num rares. */ - private int numRares = 1; - - /** The Num uncommons. */ - private int numUncommons = 3; - - /** The Num commons. */ - private int numCommons = 11; - - /** The Num double faced. */ - private int numDoubleFaced = 0; - - /** The Num packs. */ - private int numPacks = 3; - - private DeckSection cardPool; - - /** The Land set code. */ - private String landSetCode = AllZone.getCardFactory().getCard("Plains", AllZone.getHumanPlayer()) - .getMostRecentSet(); - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return this.getName(); - } - - /** - * Parses the. - * - * @param dfData - * the df data - * @return the custom limited - */ - public static CustomLimited parse(final List dfData) { - - Map data = SectionUtil.parseKvPairs(dfData, ":"); - - final CustomLimited cd = new CustomLimited(data.get("Name")); - cd.setIgnoreRarity("True".equalsIgnoreCase(data.get("IgnoreRarity"))); - cd.setSingleton("True".equalsIgnoreCase(data.get("Singleton"))); - - - for (final String dd : dfData) { - final String[] v = dd.split(":", 2); - final String key = v[0]; - final String value = v.length > 1 ? v[1].trim() : ""; - - - if (key.equalsIgnoreCase("LandSetCode")) { - cd.setLandSetCode(value); - } - - if (key.equalsIgnoreCase("NumCards")) { - cd.setNumCards(Integer.parseInt(value)); - } - if (key.equalsIgnoreCase("NumDoubleFaced")) { - cd.setNumDoubleFaced(Integer.parseInt(value)); - } - if (key.equalsIgnoreCase("NumSpecials")) { - cd.setNumSpecials(Integer.parseInt(value)); - } - if (key.equalsIgnoreCase("NumMythics")) { - cd.setNumMythics(Integer.parseInt(value)); - } - if (key.equalsIgnoreCase("NumRares")) { - cd.setNumRares(Integer.parseInt(value)); - } - if (key.equalsIgnoreCase("NumUncommons")) { - cd.setNumUncommons(Integer.parseInt(value)); - } - if (key.equalsIgnoreCase("NumCommons")) { - cd.setNumCommons(Integer.parseInt(value)); - } - if (key.equalsIgnoreCase("NumPacks")) { - cd.setNumPacks(Integer.parseInt(value)); - } - } - return cd; - } - - /** - * Gets the num cards. - * - * @return the numCards - */ - public int getNumCards() { - return this.numCards; - } - - /** - * Sets the num cards. - * - * @param numCardsIn - * the numCards to set - */ - public void setNumCards(final int numCardsIn) { - this.numCards = numCardsIn; - } - - /** - * Gets the num packs. - * - * @return the numPacks - */ - public int getNumPacks() { - return this.numPacks; - } - - /** - * Sets the num packs. - * - * @param numPacksIn - * the numPacks to set - */ - public void setNumPacks(final int numPacksIn) { - this.numPacks = numPacksIn; - } - - /** - * Gets the num specials. - * - * @return the numSpecials - */ - public int getNumSpecials() { - return this.numSpecials; - } - - /** - * Sets the num specials. - * - * @param numSpecialsIn - * the numSpecials to set - */ - public void setNumSpecials(final int numSpecialsIn) { - this.numSpecials = numSpecialsIn; - } - - /** - * Gets the singleton. - * - * @return the singleton - */ - public Boolean getSingleton() { - return this.singleton; - } - - /** - * Sets the singleton. - * - * @param singletonIn - * the singleton to set - */ - public void setSingleton(final Boolean singletonIn) { - this.singleton = singletonIn; - } - - /** - * Gets the ignore rarity. - * - * @return the ignoreRarity - */ - public Boolean getIgnoreRarity() { - return this.ignoreRarity; - } - - /** - * Sets the ignore rarity. - * - * @param ignoreRarityIn - * the ignoreRarity to set - */ - public void setIgnoreRarity(final Boolean ignoreRarityIn) { - this.ignoreRarity = ignoreRarityIn; - } - - /** - * Gets the num uncommons. - * - * @return the numUncommons - */ - public int getNumUncommons() { - return this.numUncommons; - } - - /** - * Sets the num uncommons. - * - * @param numUncommonsIn - * the numUncommons to set - */ - public void setNumUncommons(final int numUncommonsIn) { - this.numUncommons = numUncommonsIn; - } - - /** - * Gets the num commons. - * - * @return the numCommons - */ - public int getNumCommons() { - return this.numCommons; - } - - /** - * Sets the num commons. - * - * @param numCommonsIn - * the numCommons to set - */ - public void setNumCommons(final int numCommonsIn) { - this.numCommons = numCommonsIn; - } - - /** - * Gets the num rares. - * - * @return the numRares - */ - public int getNumRares() { - return this.numRares; - } - - /** - * Sets the num rares. - * - * @param numRaresIn - * the numRares to set - */ - public void setNumRares(final int numRaresIn) { - this.numRares = numRaresIn; - } - - /** - * Gets the num mythics. - * - * @return the numMythics - */ - public int getNumMythics() { - return this.numMythics; - } - - /** - * Sets the num mythics. - * - * @param numMythicsIn - * the numMythics to set - */ - public void setNumMythics(final int numMythicsIn) { - this.numMythics = numMythicsIn; - } - - /** - * Gets the deck file. - * - * @return the deckFile - */ - public String getDeckFile() { - return this.deckFile; - } - - /** - * Sets the deck file. - * - * @param deckFileIn - * the deckFile to set - */ - public void setDeckFile(final String deckFileIn) { - this.deckFile = deckFileIn; - } - - /** - * Gets the land set code. - * - * @return the landSetCode - */ - public String getLandSetCode() { - return this.landSetCode; - } - - /** - * Sets the land set code. - * - * @param landSetCodeIn - * the landSetCode to set - */ - public void setLandSetCode(final String landSetCodeIn) { - this.landSetCode = landSetCodeIn; - } - - /** - * Gets the num double faced. - * - * @return the numDoubleFaced - */ - public int getNumDoubleFaced() { - return this.numDoubleFaced; - } - - /** - * Sets the num double faced. - * - * @param numDoubleFacedIn - * the numDoubleFaced to set - */ - public void setNumDoubleFaced(final int numDoubleFacedIn) { - this.numDoubleFaced = numDoubleFacedIn; - } - - /* (non-Javadoc) - * @see forge.item.CardCollectionBase#getCardPool() - */ - @Override - public ItemPoolView getCardPool() { - return cardPool; - } - - /* (non-Javadoc) - * @see forge.deck.DeckBase#getInstance(java.lang.String) - */ - @Override - protected DeckBase newInstance(String name0) { - return new CustomLimited(name0); - } - - - -} diff --git a/src/main/java/forge/deck/Deck.java b/src/main/java/forge/deck/Deck.java index b2c5ce43600..79946343d46 100644 --- a/src/main/java/forge/deck/Deck.java +++ b/src/main/java/forge/deck/Deck.java @@ -153,6 +153,8 @@ public class Deck extends DeckBase implements Serializable, IHasName { } DeckFileHeader dh = DeckSerializer.readDeckMetadata(sections); + if ( dh == null ) + return null; final Deck d = new Deck(dh.getName()); d.setComment(dh.getComment()); diff --git a/src/main/java/forge/deck/io/CubeSerializer.java b/src/main/java/forge/deck/io/CubeSerializer.java deleted file mode 100644 index 10ad3ade9bf..00000000000 --- a/src/main/java/forge/deck/io/CubeSerializer.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Forge: Play Magic: the Gathering. - * Copyright (C) 2011 Nate - * - * 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.deck.io; - -import java.io.File; -import java.io.FilenameFilter; - -import forge.deck.CustomLimited; - -/** - * TODO: Write javadoc for this type. - * - */ -public class CubeSerializer extends DeckSerializerBase { - - public CubeSerializer(File deckDir0) { - super(deckDir0); - } - - - /* (non-Javadoc) - * @see forge.deck.IDeckSerializer#save(forge.item.CardCollectionBase, java.io.File) - */ - @Override - public void save(CustomLimited unit) { - - } - - /* (non-Javadoc) - * @see forge.deck.IDeckSerializer#erase(forge.item.CardCollectionBase, java.io.File) - */ - @Override - public void erase(CustomLimited unit) { - } - - - - /** - * - * Make file name. - * - * @param deckName - * @param deckType - * a GameType - * @return a File - */ - public File makeFileFor(final CustomLimited deck) { - return new File(getDirectory(), deriveFileName(cleanDeckName(deck.getName())) + ".cub"); - } - - /* (non-Javadoc) - * @see forge.deck.io.DeckSerializerBase#read(java.io.File) - */ - @Override - protected CustomLimited read(File file) { - return null; - } - - /* (non-Javadoc) - * @see forge.deck.io.DeckSerializerBase#getFileFilter() - */ - @Override - protected FilenameFilter getFileFilter() { - return new FilenameFilter() { - - @Override - public boolean accept(File dir, String name) { - return dir.getPath().endsWith(".cub"); - } - }; - } - -} diff --git a/src/main/java/forge/deck/io/DeckFileHeader.java b/src/main/java/forge/deck/io/DeckFileHeader.java index 21d57137f96..8ba89d4919e 100644 --- a/src/main/java/forge/deck/io/DeckFileHeader.java +++ b/src/main/java/forge/deck/io/DeckFileHeader.java @@ -1,9 +1,8 @@ package forge.deck.io; -import java.util.Map; - import forge.PlayerType; import forge.game.GameType; +import forge.util.FileSection; /** * TODO: Write javadoc for this type. @@ -30,11 +29,11 @@ public class DeckFileHeader { * TODO: Write javadoc for Constructor. * @param parseKvPairs */ - public DeckFileHeader(Map kvPairs) { + public DeckFileHeader(FileSection kvPairs) { name = kvPairs.get(NAME); comment = kvPairs.get(COMMENT); deckType = GameType.smartValueOf(kvPairs.get(DECK_TYPE), GameType.Constructed); - customPool = "true".equalsIgnoreCase(kvPairs.get(CSTM_POOL)); + customPool = kvPairs.getBoolean(CSTM_POOL); playerType = "computer".equalsIgnoreCase(kvPairs.get(PLAYER)) || "ai".equalsIgnoreCase(kvPairs.get(PLAYER_TYPE)) ? PlayerType.COMPUTER : PlayerType.HUMAN; } diff --git a/src/main/java/forge/deck/io/DeckSerializer.java b/src/main/java/forge/deck/io/DeckSerializer.java index 94dfb768d29..cad7b40602d 100644 --- a/src/main/java/forge/deck/io/DeckSerializer.java +++ b/src/main/java/forge/deck/io/DeckSerializer.java @@ -34,8 +34,8 @@ import javax.swing.filechooser.FileFilter; import forge.Card; import forge.deck.Deck; import forge.item.CardPrinted; +import forge.util.FileSection; import forge.util.FileUtil; -import forge.util.SectionUtil; import freemarker.template.Configuration; import freemarker.template.DefaultObjectWrapper; import freemarker.template.Template; @@ -248,7 +248,7 @@ public class DeckSerializer extends DeckSerializerBase { if ( map == null ) { return null; } List lines = map.get("metadata"); if (lines == null) { return null; } - DeckFileHeader d = new DeckFileHeader(SectionUtil.parseKvPairs(lines, "=")); + DeckFileHeader d = new DeckFileHeader(FileSection.parse(lines, "=")); return d; diff --git a/src/main/java/forge/deck/io/OldDeckParser.java b/src/main/java/forge/deck/io/OldDeckParser.java index 26db842ab80..71b0b3e71f6 100644 --- a/src/main/java/forge/deck/io/OldDeckParser.java +++ b/src/main/java/forge/deck/io/OldDeckParser.java @@ -14,7 +14,6 @@ import org.apache.commons.lang3.tuple.MutablePair; import org.apache.commons.lang3.tuple.Pair; import forge.PlayerType; -import forge.deck.CustomLimited; import forge.deck.Deck; import forge.deck.DeckSet; import forge.util.FileUtil; @@ -44,7 +43,7 @@ public class OldDeckParser { * @param cube2 */ public OldDeckParser(File file, IFolderMap constructed2, IFolderMap draft2, - IFolderMap sealed2, IFolderMap cube2) { + IFolderMap sealed2, IFolderMap cube2) { deckDir = file; sealed = sealed2; constructed = constructed2; @@ -61,7 +60,7 @@ public class OldDeckParser { protected final IFolderMap getDraft() { return draft; } - protected final IFolderMap getCube() { + protected final IFolderMap getCube() { return cube; } protected final File getDeckDir() { @@ -71,7 +70,7 @@ public class OldDeckParser { private final IFolderMap sealed; private final IFolderMap constructed; private final IFolderMap draft; - private final IFolderMap cube; + private final IFolderMap cube; private final File deckDir; /** * TODO: Write javadoc for this method. @@ -127,9 +126,22 @@ public class OldDeckParser { Map> sections = SectionUtil.parseSections(fileLines); DeckFileHeader dh = DeckSerializer.readDeckMetadata(sections); String name = dh.getName(); + if (dh.isCustomPool()) { - continue; + try { + cube.add(Deck.fromLines(fileLines)); + importedOk = true; + } catch (NoSuchElementException ex) { + if (!allowDeleteUnsupportedConstructed) { + String msg = String.format("Can not convert deck '%s' for some unsupported cards it contains. %n%s%n%nMay Forge delete all such decks?", name, ex.getMessage()); + allowDeleteUnsupportedConstructed = JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, msg, "Problem converting decks", JOptionPane.YES_NO_OPTION); + } } + if (importedOk || allowDeleteUnsupportedConstructed) { + f.delete(); + } + continue; + } switch(dh.getDeckType()) { case Constructed: diff --git a/src/main/java/forge/game/limited/BoosterDraft.java b/src/main/java/forge/game/limited/BoosterDraft.java index 4d6cc208781..7f27e099367 100644 --- a/src/main/java/forge/game/limited/BoosterDraft.java +++ b/src/main/java/forge/game/limited/BoosterDraft.java @@ -31,6 +31,7 @@ import javax.swing.JOptionPane; import net.slightlymagic.braids.util.lambda.Lambda1; import net.slightlymagic.maxmtg.Closure1; +import forge.AllZone; import forge.Card; import forge.CardList; import forge.Constant; @@ -38,7 +39,6 @@ import forge.SetUtils; import forge.card.BoosterGenerator; import forge.card.CardBlock; import forge.card.CardSet; -import forge.deck.CustomLimited; import forge.deck.Deck; import forge.gui.GuiUtils; import forge.item.CardDb; @@ -143,7 +143,7 @@ public final class BoosterDraft implements IBoosterDraft { case Custom: final List myDrafts = this.loadCustomDrafts("res/draft/", ".draft"); - if (myDrafts.size() < 1) { + if (myDrafts.isEmpty()) { JOptionPane .showMessageDialog(null, "No custom draft files found.", "", JOptionPane.INFORMATION_MESSAGE); } else { @@ -162,7 +162,7 @@ public final class BoosterDraft implements IBoosterDraft { private void setupCustomDraft(final CustomLimited draft) { final ItemPoolView dPool = draft.getCardPool(); if (dPool == null) { - throw new RuntimeException("BoosterGenerator : deck not found - " + draft.getDeckFile()); + throw new RuntimeException("BoosterGenerator : deck not found"); } final BoosterGenerator bpCustom = new BoosterGenerator(dPool); @@ -176,8 +176,7 @@ public final class BoosterDraft implements IBoosterDraft { return pack.getSingletonBoosterPack(draft.getNumCards()); } } - return pack.getBoosterPack(draft.getNumCommons(), draft.getNumUncommons(), 0, draft.getNumRares(), - draft.getNumMythics(), draft.getNumSpecials(), 0, 0, 0); + return pack.getBoosterPack(draft.getNumRarity(), 0, 0, 0); } }; @@ -210,7 +209,7 @@ public final class BoosterDraft implements IBoosterDraft { for (final String element : dList) { if (element.endsWith(fileExtension)) { final List dfData = FileUtil.readFile(lookupFolder + element); - customs.add(CustomLimited.parse(dfData)); + customs.add(CustomLimited.parse(dfData, AllZone.getDecks().getCubes())); } } return customs; diff --git a/src/main/java/forge/game/limited/CustomLimited.java b/src/main/java/forge/game/limited/CustomLimited.java new file mode 100644 index 00000000000..318ac8faae9 --- /dev/null +++ b/src/main/java/forge/game/limited/CustomLimited.java @@ -0,0 +1,240 @@ +/* + * 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.game.limited; + +import java.util.EnumMap; +import java.util.List; +import java.util.Map; + +import forge.AllZone; +import forge.card.CardRarity; +import forge.deck.Deck; +import forge.deck.DeckBase; +import forge.item.CardDb; +import forge.item.CardPrinted; +import forge.item.ItemPool; +import forge.item.ItemPoolView; +import forge.util.FileSection; +import forge.util.IFolderMapView; + +/** + *

+ * CustomDraft class. + *

+ * + * @author Forge + * @version $Id$ + */ +public class CustomLimited extends DeckBase { + + /** + * TODO: Write javadoc for Constructor. + * @param name0 + */ + public CustomLimited(String name0) { + super(name0); + } + + private static final long serialVersionUID = 7435640939026612173L; + + /** The Ignore rarity. */ + private Boolean ignoreRarity; + + /** The Singleton. */ + private Boolean singleton = false; + + /** The Num cards. */ + private int numCards = 15; + + + private final Map numRarity = new EnumMap(CardRarity.class); + + /** The Num packs. */ + private int numPacks = 3; + + private transient ItemPoolView cardPool; + + /** The Land set code. */ + private String landSetCode = AllZone.getCardFactory().getCard("Plains", AllZone.getHumanPlayer()) + .getMostRecentSet(); + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return this.getName(); + } + + /** + * Parses the. + * + * @param dfData + * the df data + * @return the custom limited + */ + public static CustomLimited parse(final List dfData, IFolderMapView cubes) { + + FileSection data = FileSection.parse(dfData, ":"); + + final CustomLimited cd = new CustomLimited(data.get("Name")); + cd.setIgnoreRarity(data.getBoolean("IgnoreRarity")); + cd.setSingleton(data.getBoolean("Singleton")); + cd.setLandSetCode(data.get("LandSetCode")); + cd.numCards = data.getInt("NumCards", 15); + + cd.numRarity.put(CardRarity.BasicLand, data.getInt("NumBasicLands", 1)); + cd.numRarity.put(CardRarity.Special, data.getInt("NumSpecials")); + cd.numRarity.put(CardRarity.Rare, data.getInt("NumRares", 1)); + cd.numRarity.put(CardRarity.MythicRare, data.getInt("NumMythics")); + cd.numRarity.put(CardRarity.Uncommon, data.getInt("NumUncommons", 3)); + cd.numRarity.put(CardRarity.Common, data.getInt("NumCommons", 10)); + + cd.numPacks = data.getInt("NumPacks"); + + String deckName = data.get("DeckFile"); + Deck deckCube = cubes.get(deckName); + cd.cardPool = deckCube == null ? ItemPool.createFrom(CardDb.instance().getAllUniqueCards(), CardPrinted.class) : deckCube.getMain(); + + return cd; + } + + /** + * Gets the num cards. + * + * @return the numCards + */ + public int getNumCards() { + return this.numCards; + } + + /** + * Sets the num cards. + * + * @param numCardsIn + * the numCards to set + */ + public void setNumCards(final int numCardsIn) { + this.numCards = numCardsIn; + } + + /** + * Gets the num packs. + * + * @return the numPacks + */ + public int getNumPacks() { + return this.numPacks; + } + + /** + * Sets the num packs. + * + * @param numPacksIn + * the numPacks to set + */ + public void setNumPacks(final int numPacksIn) { + this.numPacks = numPacksIn; + } + + /** + * Gets the singleton. + * + * @return the singleton + */ + public Boolean getSingleton() { + return this.singleton; + } + + /** + * Sets the singleton. + * + * @param singletonIn + * the singleton to set + */ + public void setSingleton(final Boolean singletonIn) { + this.singleton = singletonIn; + } + + /** + * Gets the ignore rarity. + * + * @return the ignoreRarity + */ + public Boolean getIgnoreRarity() { + return this.ignoreRarity; + } + + /** + * Sets the ignore rarity. + * + * @param ignoreRarityIn + * the ignoreRarity to set + */ + public void setIgnoreRarity(final Boolean ignoreRarityIn) { + this.ignoreRarity = ignoreRarityIn; + } + + /** + * Gets the land set code. + * + * @return the landSetCode + */ + public String getLandSetCode() { + return this.landSetCode; + } + + /** + * Sets the land set code. + * + * @param landSetCodeIn + * the landSetCode to set + */ + public void setLandSetCode(final String landSetCodeIn) { + this.landSetCode = landSetCodeIn; + } + + /* (non-Javadoc) + * @see forge.item.CardCollectionBase#getCardPool() + */ + @Override + public ItemPoolView getCardPool() { + return cardPool; + } + + /* (non-Javadoc) + * @see forge.deck.DeckBase#getInstance(java.lang.String) + */ + @Override + protected DeckBase newInstance(String name0) { + return new CustomLimited(name0); + } + + /** + * TODO: Write javadoc for this method. + * @return + */ + public Map getNumRarity() { + return numRarity; + } + + + +} diff --git a/src/main/java/forge/game/limited/SealedDeck.java b/src/main/java/forge/game/limited/SealedDeck.java index 4f920e09aa3..19e0b2c712f 100644 --- a/src/main/java/forge/game/limited/SealedDeck.java +++ b/src/main/java/forge/game/limited/SealedDeck.java @@ -36,7 +36,6 @@ import forge.card.BoosterGenerator; import forge.card.CardBlock; import forge.card.CardSet; import forge.card.spellability.AbilityMana; -import forge.deck.CustomLimited; import forge.deck.Deck; import forge.gui.GuiUtils; import forge.item.CardDb; @@ -140,7 +139,7 @@ public class SealedDeck { for (final String element : dList) { if (element.endsWith(".sealed")) { final ArrayList dfData = FileUtil.readFile("res/sealed/" + element); - final CustomLimited cs = CustomLimited.parse(dfData); + final CustomLimited cs = CustomLimited.parse(dfData, AllZone.getDecks().getCubes()); customs.add(cs); } } @@ -164,9 +163,7 @@ public class SealedDeck { return pack.getSingletonBoosterPack(draft.getNumCards()); } } - return pack.getBoosterPack(draft.getNumCommons(), draft.getNumUncommons(), 0, - draft.getNumRares(), draft.getNumMythics(), draft.getNumSpecials(), - draft.getNumDoubleFaced(), 0, 0); + return pack.getBoosterPack(draft.getNumRarity(), 0, 0, 0); } }; diff --git a/src/main/java/forge/item/PreconDeck.java b/src/main/java/forge/item/PreconDeck.java index a1625d6a1c9..e596be96902 100644 --- a/src/main/java/forge/item/PreconDeck.java +++ b/src/main/java/forge/item/PreconDeck.java @@ -24,6 +24,7 @@ import java.util.Map; import forge.SetUtils; import forge.deck.Deck; import forge.quest.SellRules; +import forge.util.FileSection; import forge.util.FileUtil; import forge.util.SectionUtil; @@ -83,7 +84,7 @@ public class PreconDeck implements InventoryItemFromSet { String setProxy = "n/a"; - Map kv = SectionUtil.parseKvPairs(sections.get("metadata"), "="); + FileSection kv = FileSection.parse(sections.get("metadata"), "="); imageFilename = kv.get("Image"); description = kv.get("Description"); diff --git a/src/main/java/forge/util/FileSection.java b/src/main/java/forge/util/FileSection.java new file mode 100644 index 00000000000..1645b2fca48 --- /dev/null +++ b/src/main/java/forge/util/FileSection.java @@ -0,0 +1,48 @@ +package forge.util; + +import java.util.Map; +import java.util.TreeMap; + +/** + * TODO: Write javadoc for this type. + * + */ +public class FileSection { + + private final Map lines = new TreeMap(String.CASE_INSENSITIVE_ORDER); + + private FileSection() {} + + public static FileSection parse(Iterable lines, String kvSeparator) { + FileSection result = new FileSection(); + + for (final String dd : lines) { + final String[] v = dd.split(kvSeparator, 2); + result.lines.put(v[0], v.length > 1 ? v[1].trim() : ""); + } + + return result; + } + + public String get(String fieldName) { + return lines.get(fieldName); + } + + public int getInt(String fieldName) { return getInt(fieldName, 0); } + public int getInt(String fieldName, int defaultValue) { + try{ + return Integer.parseInt(get(fieldName)); + } catch( NumberFormatException ex ) { + return defaultValue; + } + } + + public boolean getBoolean(String fieldName) { return getBoolean(fieldName, false); } + public boolean getBoolean(String fieldName, boolean defaultValue) { + String s = get(fieldName); + if ( s == null ) return defaultValue; + return "true".equalsIgnoreCase(s); + } + + +} diff --git a/src/main/java/forge/util/SectionUtil.java b/src/main/java/forge/util/SectionUtil.java index a4599641f6c..c67e5285593 100644 --- a/src/main/java/forge/util/SectionUtil.java +++ b/src/main/java/forge/util/SectionUtil.java @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.TreeMap; /** * Divides file into sections and joins them back to stringlist to save. @@ -79,23 +78,4 @@ public class SectionUtil { return result; } - /** - * Parses the kv pairs. - * - * @param lines the lines - * @return the map - */ - public static Map parseKvPairs(final List lines, String separator ) { - if ( null == lines ) - return null; - - final Map result = new TreeMap(String.CASE_INSENSITIVE_ORDER); - - for (final String dd : lines) { - final String[] v = dd.split(separator, 2); - result.put(v[0], v.length > 1 ? v[1].trim() : ""); - } - - return result; - } }