Checkstyle

This commit is contained in:
jendave
2012-01-30 18:04:25 +00:00
parent 5904ed5130
commit e926c78598
6 changed files with 259 additions and 81 deletions

View File

@@ -1,3 +1,20 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package forge.deck;
import java.io.BufferedWriter;
@@ -37,9 +54,9 @@ import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
/**
* TODO: Write javadoc for this type.
*
*
*/
public class DeckIO {
@@ -49,7 +66,7 @@ public class DeckIO {
private static final String PLAYER = "Player";
private static final String CSTM_POOL = "Custom Pool";
/** Constant <code>BDKFileFilter</code>. */
static FilenameFilter bdkFileFilter = new FilenameFilter() {
private static FilenameFilter bdkFileFilter = new FilenameFilter() {
@Override
public boolean accept(final File dir, final String name) {
return name.endsWith(".bdk");
@@ -86,6 +103,7 @@ public class DeckIO {
return "Simple Deck File .html";
}
};
/**
* <p>
* readDeck.
@@ -97,9 +115,15 @@ public class DeckIO {
*/
public static Deck readDeck(final File deckFile) {
return readDeck(FileUtil.readFile(deckFile));
return DeckIO.readDeck(FileUtil.readFile(deckFile));
}
/**
* Read deck.
*
* @param deckFileLines the deck file lines
* @return the deck
*/
public static Deck readDeck(final List<String> deckFileLines) {
final Map<String, List<String>> sections = SectionUtil.parseSections(deckFileLines);
if (sections.isEmpty()) {
@@ -110,12 +134,12 @@ public class DeckIO {
final String firstLine = deckFileLines.get(0);
if (!firstLine.startsWith("[") || firstLine.equalsIgnoreCase("[general]")) {
readDeckOldMetadata(deckFileLines.iterator(), d);
DeckIO.readDeckOldMetadata(deckFileLines.iterator(), d);
} else {
readDeckMetadata(sections.get("metadata"), d);
DeckIO.readDeckMetadata(sections.get("metadata"), d);
}
d.setMain(readCardList(sections.get("main")));
d.setSideboard(readCardList(sections.get("sideboard")));
d.setMain(DeckIO.readCardList(sections.get("main")));
d.setSideboard(DeckIO.readCardList(sections.get("sideboard")));
return d;
}
@@ -136,19 +160,19 @@ public class DeckIO {
value = linedata[1];
}
if (NAME.equalsIgnoreCase(field)) {
if (DeckIO.NAME.equalsIgnoreCase(field)) {
d.setName(value);
} else if (COMMENT.equalsIgnoreCase(field)) {
} else if (DeckIO.COMMENT.equalsIgnoreCase(field)) {
d.setComment(value);
} else if (DECK_TYPE.equalsIgnoreCase(field)) {
} else if (DeckIO.DECK_TYPE.equalsIgnoreCase(field)) {
d.setDeckType(GameType.smartValueOf(value));
} else if (CSTM_POOL.equalsIgnoreCase(field)) {
} else if (DeckIO.CSTM_POOL.equalsIgnoreCase(field)) {
d.setCustomPool(value.equalsIgnoreCase("true"));
} else if (PLAYER.equalsIgnoreCase(field)) {
} else if (DeckIO.PLAYER.equalsIgnoreCase(field)) {
if ("human".equalsIgnoreCase(value)) {
d.setPlayerType(PlayerType.HUMAN);
@@ -266,9 +290,9 @@ public class DeckIO {
public static File makeFileName(final String deckName, final GameType deckType) {
final File path = ForgeProps.getFile(NewConstants.NEW_DECKS);
if (deckType == GameType.Draft) {
return new File(path, deriveFileName(deckName) + ".bdk");
return new File(path, DeckIO.deriveFileName(deckName) + ".bdk");
} else {
return new File(path, deriveFileName(deckName) + ".dck");
return new File(path, DeckIO.deriveFileName(deckName) + ".dck");
}
}
@@ -281,7 +305,7 @@ public class DeckIO {
* @return a File
*/
public static File makeFileName(final Deck deck) {
return makeFileName(deck.getName(), deck.getDeckType());
return DeckIO.makeFileName(deck.getName(), deck.getDeckType());
}
/**
@@ -292,10 +316,10 @@ public class DeckIO {
* a Deck[]
*/
public static void writeDraftDecks(final Deck[] drafts) {
final File f = makeFileName(drafts[0]);
final File f = DeckIO.makeFileName(drafts[0]);
f.mkdir();
for (int i = 0; i < drafts.length; i++) {
FileUtil.writeFile(new File(f, i + ".dck"), serializeDeck(drafts[i]));
FileUtil.writeFile(new File(f, i + ".dck"), DeckIO.serializeDeck(drafts[i]));
}
}
@@ -315,25 +339,25 @@ public class DeckIO {
final List<String> out = new ArrayList<String>();
out.add(String.format("[metadata]"));
out.add(String.format("%s=%s", NAME, d.getName().replaceAll("\n", "")));
out.add(String.format("%s=%s", DECK_TYPE, d.getDeckType()));
out.add(String.format("%s=%s", DeckIO.NAME, d.getName().replaceAll("\n", "")));
out.add(String.format("%s=%s", DeckIO.DECK_TYPE, d.getDeckType()));
// these are optional
if (d.getComment() != null) {
out.add(String.format("%s=%s", COMMENT, d.getComment().replaceAll("\n", "")));
out.add(String.format("%s=%s", DeckIO.COMMENT, d.getComment().replaceAll("\n", "")));
}
if (d.getPlayerType() != null) {
out.add(String.format("%s=%s", PLAYER, d.getPlayerType()));
out.add(String.format("%s=%s", DeckIO.PLAYER, d.getPlayerType()));
}
if (d.isCustomPool()) {
out.add(String.format("%s=%s", CSTM_POOL, "true"));
out.add(String.format("%s=%s", DeckIO.CSTM_POOL, "true"));
}
out.add(String.format("%s", "[main]"));
out.addAll(writeCardPool(d.getMain()));
out.addAll(DeckIO.writeCardPool(d.getMain()));
out.add(String.format("%s", "[sideboard]"));
out.addAll(writeCardPool(d.getSideboard()));
out.addAll(DeckIO.writeCardPool(d.getSideboard()));
return out;
}
@@ -442,7 +466,7 @@ public class DeckIO {
* a {@link java.io.File} object.
*/
public static void writeDeck(final Deck d, final File f) {
FileUtil.writeFile(f, serializeDeck(d));
FileUtil.writeFile(f, DeckIO.serializeDeck(d));
}
/**
@@ -458,7 +482,7 @@ public class DeckIO {
public static void writeDeckHtml(final Deck d, final File f) {
try {
final BufferedWriter writer = new BufferedWriter(new FileWriter(f));
writeDeckHtml(d, writer);
DeckIO.writeDeckHtml(d, writer);
writer.close();
} catch (final IOException e) {
throw new RuntimeException(e);
@@ -469,12 +493,14 @@ public class DeckIO {
* <p>
* readAllDecks.
* </p>
* @return
*
* @param deckDir the deck dir
* @return the map
*/
public static final Map<String, Deck> readAllDecks(File deckDir) {
Map<String, Deck> result = new HashMap<String, Deck>();
public static final Map<String, Deck> readAllDecks(final File deckDir) {
final Map<String, Deck> result = new HashMap<String, Deck>();
final List<String> decksThatFailedToLoad = new ArrayList<String>();
File[] files = deckDir.listFiles(DeckIO.DCK_FILE_FILTER);
final File[] files = deckDir.listFiles(DeckIO.DCK_FILE_FILTER);
for (final File file : files) {
try {
final Deck newDeck = DeckIO.readDeck(file);
@@ -495,9 +521,15 @@ public class DeckIO {
return result;
}
public static final Map<String, Deck[]> readAllDraftDecks(File deckDir) {
Map<String, Deck[]> result = new HashMap<String, Deck[]>();
File[] files = deckDir.listFiles(DeckIO.bdkFileFilter);
/**
* Read all draft decks.
*
* @param deckDir the deck dir
* @return the map
*/
public static final Map<String, Deck[]> readAllDraftDecks(final File deckDir) {
final Map<String, Deck[]> result = new HashMap<String, Deck[]>();
final File[] files = deckDir.listFiles(DeckIO.bdkFileFilter);
for (final File file : files) {
final Deck[] d = new Deck[8];

View File

@@ -48,7 +48,7 @@ import forge.card.MtgDataParser;
public final class CardDb {
private static volatile CardDb onlyInstance = null; // 'volatile' keyword
// makes this working
private final String FOIL_SUFFIX = " foil";
private final String foilSuffix = " foil";
/**
* Instance.
@@ -190,11 +190,17 @@ public final class CardDb {
return new ImmutablePair<String, String>(cardName, null);
}
private boolean isFoil(String cardName) {
return cardName.toLowerCase().endsWith(FOIL_SUFFIX) && cardName.length() > 5;
private boolean isFoil(final String cardName) {
return cardName.toLowerCase().endsWith(this.foilSuffix) && (cardName.length() > 5);
}
public String removeFoilSuffix(String cardName) {
/**
* Removes the foil suffix.
*
* @param cardName the card name
* @return the string
*/
public String removeFoilSuffix(final String cardName) {
return cardName.substring(0, cardName.length() - 5);
}
@@ -206,8 +212,8 @@ public final class CardDb {
* @return true, if is card supported
*/
public boolean isCardSupported(final String cardName0) {
boolean isFoil = isFoil(cardName0);
String cardName = isFoil ? removeFoilSuffix(cardName0) : cardName0;
final boolean isFoil = this.isFoil(cardName0);
final String cardName = isFoil ? this.removeFoilSuffix(cardName0) : cardName0;
final ImmutablePair<String, String> nameWithSet = CardDb.splitCardName(cardName);
if (nameWithSet.right == null) {
return this.uniqueCards.containsKey(nameWithSet.left.toLowerCase());
@@ -231,7 +237,7 @@ public final class CardDb {
* @return the card
*/
public CardPrinted getCard(final String name) {
return getCard(name, false);
return this.getCard(name, false);
}
// Advanced fetch by name+set
@@ -317,6 +323,12 @@ public final class CardDb {
return result;
}
/**
* Gets the cards from latest sets.
*
* @param names the names
* @return the cards from latest sets
*/
public List<CardPrinted> getCardsFromLatestSets(final Iterable<String> names) {
final List<CardPrinted> result = new ArrayList<CardPrinted>();
for (final String name : names) {
@@ -346,13 +358,19 @@ public final class CardDb {
return this.allCardsFlat;
}
public CardPrinted getCard(String name0, boolean fromLatestSet) {
/**
* Gets the card.
*
* @param name0 the name0
* @param fromLatestSet the from latest set
* @return the card
*/
public CardPrinted getCard(final String name0, final boolean fromLatestSet) {
// Sometimes they read from decks things like "CardName|Set" - but we
// can handle it
boolean isFoil = isFoil(name0);
String name = isFoil ? removeFoilSuffix(name0) : name0;
final boolean isFoil = this.isFoil(name0);
final String name = isFoil ? this.removeFoilSuffix(name0) : name0;
CardPrinted result = null;
final ImmutablePair<String, String> nameWithSet = CardDb.splitCardName(name);
@@ -366,8 +384,8 @@ public final class CardDb {
}
} else {
// OK, plain name here
Predicate<CardPrinted> predicate = CardPrinted.Predicates.name(nameWithSet.left);
List<CardPrinted> namedCards = predicate.select(this.allCardsFlat);
final Predicate<CardPrinted> predicate = CardPrinted.Predicates.name(nameWithSet.left);
final List<CardPrinted> namedCards = predicate.select(this.allCardsFlat);
if (namedCards.isEmpty()) {
throw new NoSuchElementException(String.format("Card '%s' not found in our database.", name));
}
@@ -375,9 +393,9 @@ public final class CardDb {
// Find card with maximal set index
result = namedCards.get(0);
int resIndex = SetUtils.getSetByCode((result).getSet()).getIndex();
for (CardPrinted card : namedCards) {
for (final CardPrinted card : namedCards) {
int thisIndex = SetUtils.getSetByCode((card).getSet()).getIndex();
final int thisIndex = SetUtils.getSetByCode((card).getSet()).getIndex();
if (thisIndex > resIndex) {
result = card;
resIndex = thisIndex;

View File

@@ -1,3 +1,20 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package forge.item;
import java.io.File;
@@ -22,23 +39,37 @@ public class PreconDeck implements InventoryItemFromSet {
private final String set;
private final SellRules recommendedDeals;
/* (non-Javadoc)
* @see forge.item.InventoryItemFromSet#getName()
*/
@Override
public String getName() {
return deck.getName();
}
/* (non-Javadoc)
* @see forge.item.InventoryItemFromSet#getImageFilename()
*/
@Override
public String getImageFilename() {
return "precons/" + this.imageFilename;
}
/* (non-Javadoc)
* @see forge.item.InventoryItem#getType()
*/
@Override
public String getType() {
return "Prebuilt Deck";
}
/**
* Instantiates a new precon deck.
*
* @param f the f
*/
public PreconDeck(final File f) {
List<String> deckLines = FileUtil.readFile(f);
Map<String, List<String>> sections = SectionUtil.parseSections(deckLines);
@@ -64,15 +95,28 @@ public class PreconDeck implements InventoryItemFromSet {
}
/**
* Gets the deck.
*
* @return the deck
*/
public final Deck getDeck() {
return deck;
}
/**
* Gets the recommended deals.
*
* @return the recommended deals
*/
public final SellRules getRecommendedDeals() {
return recommendedDeals;
}
/* (non-Javadoc)
* @see forge.item.InventoryItemFromSet#getSet()
*/
@Override
public String getSet() {
return set;

View File

@@ -1,3 +1,20 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package forge.quest;
import java.util.List;
@@ -15,6 +32,11 @@ public class SellRules {
private int minDifficulty = 0;
private int maxDifficulty = 5;
/**
* Instantiates a new sell rules.
*
* @param questShop the quest shop
*/
public SellRules(List<String> questShop) {
if (null == questShop || questShop.isEmpty()) {
return;
@@ -37,6 +59,12 @@ public class SellRules {
}
}
/**
* Meets requiremnts.
*
* @param quest the quest
* @return true, if successful
*/
public boolean meetsRequiremnts(QuestData quest) {
if (quest.getWin() < minWins) {
return false;
@@ -48,6 +76,11 @@ public class SellRules {
return true;
}
/**
* Gets the cost.
*
* @return the cost
*/
public final int getCost() {
return cost;
}

View File

@@ -1,3 +1,20 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package forge.quest.data;
import java.io.File;
@@ -12,23 +29,29 @@ import org.apache.commons.lang3.StringUtils;
import forge.deck.DeckIO;
import forge.item.PreconDeck;
/**
/**
* Very simple function - store all precons.
*
*
*/
public class QuestPreconManager {
/** The decks. */
final List<PreconDeck> decks = new ArrayList<PreconDeck>();
public QuestPreconManager(File deckDir) {
/**
* Instantiates a new quest precon manager.
*
* @param deckDir the deck dir
*/
public QuestPreconManager(final File deckDir) {
final List<String> decksThatFailedToLoad = new ArrayList<String>();
File[] files = deckDir.listFiles(DeckIO.DCK_FILE_FILTER);
final File[] files = deckDir.listFiles(DeckIO.DCK_FILE_FILTER);
for (final File file : files) {
try {
decks.add(new PreconDeck(file));
this.decks.add(new PreconDeck(file));
} catch (final NoSuchElementException ex) {
final String message = String.format("%s failed to load because ---- %s", file.getName(), ex.getMessage());
final String message = String.format("%s failed to load because ---- %s", file.getName(),
ex.getMessage());
decksThatFailedToLoad.add(message);
}
}
@@ -40,15 +63,15 @@ public class QuestPreconManager {
}
}
/**
* TODO: Write javadoc for this method.
* @param q
* @return
*
* @param q the q
* @return the decks for current
*/
public List<PreconDeck> getDecksForCurrent(QuestData q) {
List<PreconDeck> meetRequirements = new ArrayList<PreconDeck>();
for (PreconDeck deck : decks) {
public List<PreconDeck> getDecksForCurrent(final QuestData q) {
final List<PreconDeck> meetRequirements = new ArrayList<PreconDeck>();
for (final PreconDeck deck : this.decks) {
if (deck.getRecommendedDeals().meetsRequiremnts(q)) {
meetRequirements.add(deck);
}
@@ -56,9 +79,13 @@ public class QuestPreconManager {
return meetRequirements;
}
/**
* Gets the decks.
*
* @return the decks
*/
public final List<PreconDeck> getDecks() {
return decks;
return this.decks;
}
}

View File

@@ -97,9 +97,9 @@ public final class QuestUtilCards {
* @return the array list
*/
public ArrayList<CardPrinted> addCards(final Predicate<CardPrinted> fSets) {
final int nCommon = qpref.getPreferenceInt(QPref.BOOSTER_COMMONS);
final int nUncommon = qpref.getPreferenceInt(QPref.BOOSTER_UNCOMMONS);
final int nRare = qpref.getPreferenceInt(QPref.BOOSTER_RARES);
final int nCommon = this.qpref.getPreferenceInt(QPref.BOOSTER_COMMONS);
final int nUncommon = this.qpref.getPreferenceInt(QPref.BOOSTER_UNCOMMONS);
final int nRare = this.qpref.getPreferenceInt(QPref.BOOSTER_RARES);
final ArrayList<CardPrinted> newCards = new ArrayList<CardPrinted>();
newCards.addAll(BoosterUtils.generateCards(fSets, nCommon, CardRarity.Common, null));
@@ -170,9 +170,9 @@ public final class QuestUtilCards {
* the idx difficulty
*/
public void setupNewGameCardPool(final Predicate<CardPrinted> filter, final int idxDifficulty) {
final int nC = qpref.getPreferenceInt(QPref.STARTING_COMMONS, idxDifficulty);
final int nU = qpref.getPreferenceInt(QPref.STARTING_UNCOMMONS, idxDifficulty);
final int nR = qpref.getPreferenceInt(QPref.STARTING_RARES, idxDifficulty);
final int nC = this.qpref.getPreferenceInt(QPref.STARTING_COMMONS, idxDifficulty);
final int nU = this.qpref.getPreferenceInt(QPref.STARTING_UNCOMMONS, idxDifficulty);
final int nR = this.qpref.getPreferenceInt(QPref.STARTING_RARES, idxDifficulty);
this.addAllCards(BoosterUtils.getQuestStarterDeck(filter, nC, nU, nR));
}
@@ -209,6 +209,12 @@ public final class QuestUtilCards {
}
}
/**
* Buy precon deck.
*
* @param precon the precon
* @param value the value
*/
public void buyPreconDeck(final PreconDeck precon, final int value) {
if (this.q.getCredits() >= value) {
this.q.setCredits(this.q.getCredits() - value);
@@ -227,7 +233,7 @@ public final class QuestUtilCards {
* the price
*/
public void sellCard(final CardPrinted card, final int price) {
sellCard(card, price, true);
this.sellCard(card, price, true);
}
/**
@@ -308,27 +314,46 @@ public final class QuestUtilCards {
* Generate cards in shop.
*/
final Predicate<CardSet> filterExt = CardSet.Predicates.Presets.SETS_IN_EXT;
/** The filter t2booster. */
final Predicate<CardSet> filterT2booster = Predicate.and(CardSet.Predicates.CAN_MAKE_BOOSTER,
CardSet.Predicates.Presets.SETS_IN_STANDARD);
/** The filter ext but t2. */
final Predicate<CardSet> filterExtButT2 = Predicate.and(CardSet.Predicates.CAN_MAKE_BOOSTER,
Predicate.and(filterExt, Predicate.not(CardSet.Predicates.Presets.SETS_IN_STANDARD)));
Predicate.and(this.filterExt, Predicate.not(CardSet.Predicates.Presets.SETS_IN_STANDARD)));
/** The filter not ext. */
final Predicate<CardSet> filterNotExt = Predicate.and(CardSet.Predicates.CAN_MAKE_BOOSTER,
Predicate.not(filterExt));
Predicate.not(this.filterExt));
public void generateBoostersInShop(int count) {
/**
* Generate boosters in shop.
*
* @param count the count
*/
public void generateBoostersInShop(final int count) {
for (int i = 0; i < count; i++) {
final int rollD100 = MyRandom.getRandom().nextInt(100);
final Predicate<CardSet> filter = rollD100 < 40 ? filterT2booster : (rollD100 < 75 ? filterExtButT2
: filterNotExt);
final Predicate<CardSet> filter = rollD100 < 40 ? this.filterT2booster
: (rollD100 < 75 ? this.filterExtButT2 : this.filterNotExt);
this.q.getShopList().addAllCards(filter.random(SetUtils.getAllSets(), 1, BoosterPack.FN_FROM_SET));
}
}
public void generatePreconsInShop(int count) {
List<PreconDeck> validDecks = QuestData.getPreconManager().getDecksForCurrent(q);
/**
* Generate precons in shop.
*
* @param count the count
*/
public void generatePreconsInShop(final int count) {
final List<PreconDeck> validDecks = QuestData.getPreconManager().getDecksForCurrent(this.q);
this.q.getShopList().addAllCards(Predicate.getTrue(PreconDeck.class).random(validDecks, count));
}
/**
* Generate cards in shop.
*/
public void generateCardsInShop() {
final BoosterGenerator pack = new BoosterGenerator(CardDb.instance().getAllCards());
@@ -336,14 +361,13 @@ public final class QuestUtilCards {
final int winPacks = this.q.getWin() / 10;
final int totalPacks = Math.min(levelPacks + winPacks, 6);
this.q.getShopList().clear();
for (int i = 0; i < totalPacks; i++) {
this.q.getShopList().addAllCards(pack.getBoosterPack(7, 3, 1, 0, 0, 0, 0, 0, 0));
}
generateBoostersInShop(totalPacks);
generatePreconsInShop(totalPacks);
this.generateBoostersInShop(totalPacks);
this.generatePreconsInShop(totalPacks);
this.addBasicLands(this.q.getShopList(), 10, 5);
}