Some housekeeping

This commit is contained in:
Krazy
2017-06-07 19:38:18 +00:00
parent 3e72db117f
commit 7633eab1cc
3 changed files with 89 additions and 90 deletions

View File

@@ -6,12 +6,12 @@
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
@@ -39,14 +39,14 @@ import java.util.Map.Entry;
public final class CardDb implements ICardDatabase, IDeckGenPool { public final class CardDb implements ICardDatabase, IDeckGenPool {
public final static String foilSuffix = "+"; public final static String foilSuffix = "+";
public final static char NameSetSeparator = '|'; public final static char NameSetSeparator = '|';
// need this to obtain cardReference by name+set+artindex // need this to obtain cardReference by name+set+artindex
private final ListMultimap<String, PaperCard> allCardsByName = Multimaps.newListMultimap(new TreeMap<String,Collection<PaperCard>>(String.CASE_INSENSITIVE_ORDER), CollectionSuppliers.<PaperCard>arrayLists()); private final ListMultimap<String, PaperCard> allCardsByName = Multimaps.newListMultimap(new TreeMap<String,Collection<PaperCard>>(String.CASE_INSENSITIVE_ORDER), CollectionSuppliers.<PaperCard>arrayLists());
private final Map<String, PaperCard> uniqueCardsByName = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER); private final Map<String, PaperCard> uniqueCardsByName = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER);
private final Map<String, CardRules> rulesByName; private final Map<String, CardRules> rulesByName;
private final Map<String, ICardFace> facesByName = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER); private final Map<String, ICardFace> facesByName = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER);
private static Map<String, String> artPrefs = new HashMap<String, String>(); private static Map<String, String> artPrefs = new HashMap<String, String>();
private final Map<String, String> alternateName = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER); private final Map<String, String> alternateName = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER);
private final Map<String, Integer> artIds = new HashMap<String, Integer>(); private final Map<String, Integer> artIds = new HashMap<String, Integer>();
@@ -60,32 +60,32 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
Earliest(false), Earliest(false),
EarliestCoreExp(true), EarliestCoreExp(true),
Random(false); Random(false);
final boolean filterSets; final boolean filterSets;
private SetPreference(boolean filterIrregularSets) { private SetPreference(boolean filterIrregularSets) {
filterSets = filterIrregularSets; filterSets = filterIrregularSets;
} }
public boolean accept(CardEdition ed) { public boolean accept(CardEdition ed) {
if (ed == null) return false; if (ed == null) return false;
return !filterSets || ed.getType() == Type.CORE || ed.getType() == Type.EXPANSION || ed.getType() == Type.REPRINT; return !filterSets || ed.getType() == Type.CORE || ed.getType() == Type.EXPANSION || ed.getType() == Type.REPRINT;
} }
} }
// NO GETTERS/SETTERS HERE! // NO GETTERS/SETTERS HERE!
public static class CardRequest { public static class CardRequest {
public String cardName; public String cardName;
public String edition; public String edition;
public int artIndex; public int artIndex;
public boolean isFoil; public boolean isFoil;
private CardRequest(String name, String edition, int artIndex, boolean isFoil) { private CardRequest(String name, String edition, int artIndex, boolean isFoil) {
cardName = name; cardName = name;
this.edition = edition; this.edition = edition;
this.artIndex = artIndex; this.artIndex = artIndex;
this.isFoil = isFoil; this.isFoil = isFoil;
} }
public static CardRequest fromString(String name) { public static CardRequest fromString(String name) {
boolean isFoil = name.endsWith(foilSuffix); boolean isFoil = name.endsWith(foilSuffix);
if (isFoil) { if (isFoil) {
@@ -107,7 +107,7 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
cardName = cardName.substring(0, cardName.length() - foilSuffix.length()); cardName = cardName.substring(0, cardName.length() - foilSuffix.length());
isFoil = true; isFoil = true;
} }
int artIndex = artPos > 0 ? Integer.parseInt(nameParts[artPos]) : 0; int artIndex = artPos > 0 ? Integer.parseInt(nameParts[artPos]) : 0;
String setName = setPos > 0 ? nameParts[setPos] : null; String setName = setPos > 0 ? nameParts[setPos] : null;
if ("???".equals(setName)) { if ("???".equals(setName)) {
@@ -124,7 +124,7 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
// create faces list from rules // create faces list from rules
for (final CardRules rule : rules.values() ) { for (final CardRules rule : rules.values() ) {
final ICardFace main = rule.getMainPart(); final ICardFace main = rule.getMainPart();
facesByName.put(main.getName(), main); facesByName.put(main.getName(), main);
if (main.getAltName() != null) { if (main.getAltName() != null) {
alternateName.put(main.getAltName(), main.getName()); alternateName.put(main.getAltName(), main.getName());
@@ -138,7 +138,7 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
} }
} }
} }
private void addSetCard(CardEdition e, CardInSet cis, CardRules cr) { private void addSetCard(CardEdition e, CardInSet cis, CardRules cr) {
int artIdx = 1; int artIdx = 1;
String key = e.getCode() + "/" + cis.name; String key = e.getCode() + "/" + cis.name;
@@ -171,7 +171,7 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
Date today = new Date(); Date today = new Date();
for (CardEdition e : editions.getOrderedEditions()) { for (CardEdition e : editions.getOrderedEditions()) {
boolean isCoreExpSet = e.getType() == CardEdition.Type.CORE || e.getType() == CardEdition.Type.EXPANSION || e.getType() == CardEdition.Type.REPRINT; boolean isCoreExpSet = e.getType() == CardEdition.Type.CORE || e.getType() == CardEdition.Type.EXPANSION || e.getType() == CardEdition.Type.REPRINT;
if (logMissingPerEdition && isCoreExpSet) { if (logMissingPerEdition && isCoreExpSet) {
System.out.print(e.getName() + " (" + e.getCards().length + " cards)"); System.out.print(e.getName() + " (" + e.getCards().length + " cards)");
} }
@@ -203,7 +203,7 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
missingCards.clear(); missingCards.clear();
artIds.clear(); artIds.clear();
} }
if (logMissingSummary) { if (logMissingSummary) {
System.out.printf("Totally %d cards not implemented: %s\n", allMissingCards.size(), StringUtils.join(allMissingCards, " | ")); System.out.printf("Totally %d cards not implemented: %s\n", allMissingCards.size(), StringUtils.join(allMissingCards, " | "));
} }
@@ -309,7 +309,7 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
} }
return tryGetCard(request); return tryGetCard(request);
} }
public int getCardCollectorNumber(String cardName, String reqEdition) { public int getCardCollectorNumber(String cardName, String reqEdition) {
cardName = getName(cardName); cardName = getName(cardName);
CardEdition edition = editions.get(reqEdition); CardEdition edition = editions.get(reqEdition);
@@ -499,7 +499,7 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
Collection<PaperCard> cards = getAllCards(cardName); Collection<PaperCard> cards = getAllCards(cardName);
if (null == cards) { if (null == cards) {
return 0; return 0;
} }
for (PaperCard pc : cards) { for (PaperCard pc : cards) {
if (pc.getEdition().equalsIgnoreCase(setName)) { if (pc.getEdition().equalsIgnoreCase(setName)) {
@@ -539,7 +539,7 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
} }
return cardName; return cardName;
} }
@Override @Override
public List<PaperCard> getAllCards(String cardName) { public List<PaperCard> getAllCards(String cardName) {
return allCardsByName.get(getName(cardName)); return allCardsByName.get(getName(cardName));
@@ -582,7 +582,7 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
return false; return false;
} }
} }
public StringBuilder appendCardToStringBuilder(PaperCard card, StringBuilder sb) { public StringBuilder appendCardToStringBuilder(PaperCard card, StringBuilder sb) {
final boolean hasBadSetInfo = "???".equals(card.getEdition()) || StringUtils.isBlank(card.getEdition()); final boolean hasBadSetInfo = "???".equals(card.getEdition()) || StringUtils.isBlank(card.getEdition());
sb.append(card.getName()); sb.append(card.getName());
@@ -601,51 +601,49 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
return sb; return sb;
} }
public PaperCard createUnsuportedCard(String cardName) { public PaperCard createUnsupportedCard(String cardName) {
CardRequest request = CardRequest.fromString(cardName); CardRequest request = CardRequest.fromString(cardName);
CardEdition cE = CardEdition.UNKNOWN; CardEdition cardEdition = CardEdition.UNKNOWN;
CardRarity cR = CardRarity.Unknown; CardRarity cardRarity = CardRarity.Unknown;
// May iterate over editions and find out if there is any card named 'cardName' but not implemented with Forge script. // May iterate over editions and find out if there is any card named 'cardName' but not implemented with Forge script.
if (StringUtils.isBlank(request.edition)) { if (StringUtils.isBlank(request.edition)) {
for (CardEdition e : editions) { for (CardEdition edition : editions) {
for (CardInSet cs : e.getCards()) { for (CardInSet cardInSet : edition.getCards()) {
if (cs.name.equals(request.cardName)) { if (cardInSet.name.equals(request.cardName)) {
cE = e; cardEdition = edition;
cR = cs.rarity; cardRarity = cardInSet.rarity;
break; break;
} }
} }
if (cE != CardEdition.UNKNOWN) { if (cardEdition != CardEdition.UNKNOWN) {
break; break;
} }
} }
} } else {
else { cardEdition = editions.get(request.edition);
cE = editions.get(request.edition); if (cardEdition != null) {
if (cE != null) { for (CardInSet cardInSet : cardEdition.getCards()) {
for (CardInSet cs : cE.getCards()) { if (cardInSet.name.equals(request.cardName)) {
if (cs.name.equals(request.cardName)) { cardRarity = cardInSet.rarity;
cR = cs.rarity;
break; break;
} }
} }
} }
else { else {
cE = CardEdition.UNKNOWN; cardEdition = CardEdition.UNKNOWN;
} }
} }
// Write to log that attempt, if (cardRarity == CardRarity.Unknown) {
if (cR == CardRarity.Unknown) {
System.err.println(String.format("An unknown card found when loading Forge decks: \"%s\" Forge does not know of such a card's existence. Have you mistyped the card name?", cardName)); System.err.println(String.format("An unknown card found when loading Forge decks: \"%s\" Forge does not know of such a card's existence. Have you mistyped the card name?", cardName));
} else {
System.err.println(String.format("An unsupported card was requested: \"%s\" from \"%s\" set. We're sorry, but you cannot use this card yet.", request.cardName, cardEdition.getName()));
} }
else {
System.err.println(String.format("An unsupported card was requested: \"%s\" from \"%s\" set. We're sorry, but you cannot use this card yet.", request.cardName, cE.getName())); return new PaperCard(CardRules.getUnsupportedCardNamed(request.cardName), cardEdition.getCode(), cardRarity, 1);
}
return new PaperCard(CardRules.getUnsupportedCardNamed(request.cardName), cE.getCode(), cR, 1);
} }
private final Editor editor = new Editor(); private final Editor editor = new Editor();
@@ -663,7 +661,7 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
} }
result = rulesByName.put(cardName, rules); result = rulesByName.put(cardName, rules);
// 1. generate all paper cards from edition data we have (either explicit, or found in res/editions, or add to unknown edition) // 1. generate all paper cards from edition data we have (either explicit, or found in res/editions, or add to unknown edition)
List<PaperCard> paperCards = new ArrayList<PaperCard>(); List<PaperCard> paperCards = new ArrayList<PaperCard>();
if (null == whenItWasPrinted || whenItWasPrinted.isEmpty()) { if (null == whenItWasPrinted || whenItWasPrinted.isEmpty()) {

View File

@@ -6,17 +6,26 @@
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package forge.deck; package forge.deck;
import com.google.common.collect.Lists;
import forge.StaticData;
import forge.card.CardDb;
import forge.item.PaperCard;
import forge.util.ItemPool;
import forge.util.ItemPoolSorter;
import forge.util.MyRandom;
import org.apache.commons.lang3.StringUtils;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@@ -24,17 +33,6 @@ import java.util.Map.Entry;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import com.google.common.collect.Lists;
import forge.StaticData;
import forge.card.CardDb;
import forge.item.PaperCard;
import forge.util.ItemPool;
import forge.util.ItemPoolSorter;
import forge.util.MyRandom;
public class CardPool extends ItemPool<PaperCard> { public class CardPool extends ItemPool<PaperCard> {
private static final long serialVersionUID = -5379091255613968393L; private static final long serialVersionUID = -5379091255613968393L;
@@ -47,7 +45,7 @@ public class CardPool extends ItemPool<PaperCard> {
this(); this();
this.addAll(cards); this.addAll(cards);
} }
public void add(final String cardName, final int amount) { public void add(final String cardName, final int amount) {
if (cardName.contains("|")) { if (cardName.contains("|")) {
// an encoded cardName with set and possibly art index was passed, split it and pass in full // an encoded cardName with set and possibly art index was passed, split it and pass in full
@@ -62,7 +60,7 @@ public class CardPool extends ItemPool<PaperCard> {
} else { } else {
this.add(cardName, null, -1, amount); this.add(cardName, null, -1, amount);
} }
} }
public void add(final String cardName, final String setCode) { public void add(final String cardName, final String setCode) {
this.add(cardName, setCode, -1, 1); this.add(cardName, setCode, -1, 1);
@@ -74,50 +72,51 @@ public class CardPool extends ItemPool<PaperCard> {
// NOTE: ART indices are "1" -based // NOTE: ART indices are "1" -based
public void add(String cardName, String setCode, final int artIndex, final int amount) { public void add(String cardName, String setCode, final int artIndex, final int amount) {
PaperCard cp = StaticData.instance().getCommonCards().getCard(cardName, setCode, artIndex);
final boolean isCommonCard = cp != null; PaperCard paperCard = StaticData.instance().getCommonCards().getCard(cardName, setCode, artIndex);
final boolean isCommonCard = paperCard != null;
if (!isCommonCard) { if (!isCommonCard) {
cp = StaticData.instance().getVariantCards().getCard(cardName, setCode); paperCard = StaticData.instance().getVariantCards().getCard(cardName, setCode);
if (cp == null) { if (paperCard == null) {
StaticData.instance().attemptToLoadCard(cardName, setCode); StaticData.instance().attemptToLoadCard(cardName, setCode);
cp = StaticData.instance().getVariantCards().getCard(cardName, setCode); paperCard = StaticData.instance().getVariantCards().getCard(cardName, setCode);
} }
} }
int artCount = 1; int artCount = 1;
if (cp != null ) { if (paperCard != null) {
setCode = cp.getEdition(); setCode = paperCard.getEdition();
cardName = cp.getName(); cardName = paperCard.getName();
artCount = isCommonCard ? StaticData.instance().getCommonCards().getArtCount(cardName, setCode) : 1; artCount = isCommonCard ? StaticData.instance().getCommonCards().getArtCount(cardName, setCode) : 1;
} } else {
else { paperCard = StaticData.instance().getCommonCards().createUnsupportedCard(cardName);
cp = StaticData.instance().getCommonCards().createUnsuportedCard(cardName);
} }
boolean artIndexExplicitlySet = artIndex > 0 || Character.isDigit(cardName.charAt(cardName.length()-1)) && cardName.charAt(cardName.length()-2) == CardDb.NameSetSeparator; boolean artIndexExplicitlySet = artIndex > 0 || Character.isDigit(cardName.charAt(cardName.length() - 1)) && cardName.charAt(cardName.length() - 2) == CardDb.NameSetSeparator;
if (artIndexExplicitlySet || artCount <= 1) { if (artIndexExplicitlySet || artCount <= 1) {
// either a specific art index is specified, or there is only one art, so just add the card // either a specific art index is specified, or there is only one art, so just add the card
this.add(cp, amount); this.add(paperCard, amount);
} else { } else {
// random art index specified, make sure we get different groups of cards with different art // random art index specified, make sure we get different groups of cards with different art
int[] artGroups = MyRandom.splitIntoRandomGroups(amount, artCount); int[] artGroups = MyRandom.splitIntoRandomGroups(amount, artCount);
for (int i = 1; i <= artGroups.length; i++) { for (int i = 1; i <= artGroups.length; i++) {
int cnt = artGroups[i-1]; int cnt = artGroups[i - 1];
if (cnt <= 0) if (cnt <= 0) {
continue; continue;
PaperCard cp_random = isCommonCard }
? StaticData.instance().getCommonCards().getCard(cardName, setCode, i) PaperCard randomCard = StaticData.instance().getCommonCards().getCard(cardName, setCode, i);
: StaticData.instance().getVariantCards().getCard(cardName, setCode, i); this.add(randomCard, cnt);
this.add(cp_random, cnt);
} }
} }
} }
/** /**
* Add all from a List of CardPrinted. * Add all from a List of CardPrinted.
* *
* @param list * @param list
* CardPrinteds to add * CardPrinteds to add
*/ */
@@ -194,25 +193,25 @@ public class CardPool extends ItemPool<PaperCard> {
} }
return pool; return pool;
} }
public String toCardList(String separator) { public String toCardList(String separator) {
List<Entry<PaperCard, Integer>> main2sort = Lists.newArrayList(this); List<Entry<PaperCard, Integer>> main2sort = Lists.newArrayList(this);
Collections.sort(main2sort, ItemPoolSorter.BY_NAME_THEN_SET); Collections.sort(main2sort, ItemPoolSorter.BY_NAME_THEN_SET);
final CardDb commonDb = StaticData.instance().getCommonCards(); final CardDb commonDb = StaticData.instance().getCommonCards();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
boolean isFirst = true; boolean isFirst = true;
for (final Entry<PaperCard, Integer> e : main2sort) { for (final Entry<PaperCard, Integer> e : main2sort) {
if(!isFirst) if(!isFirst)
sb.append(separator); sb.append(separator);
else else
isFirst = false; isFirst = false;
CardDb db = !e.getKey().getRules().isVariant() ? commonDb : StaticData.instance().getVariantCards(); CardDb db = !e.getKey().getRules().isVariant() ? commonDb : StaticData.instance().getVariantCards();
sb.append(e.getValue()).append(" "); sb.append(e.getValue()).append(" ");
db.appendCardToStringBuilder(e.getKey(), sb); db.appendCardToStringBuilder(e.getKey(), sb);
} }
return sb.toString(); return sb.toString();
} }

View File

@@ -143,6 +143,8 @@ public class DialogChooseColors {
left.setOpaque(false); left.setOpaque(false);
left.add(new FLabel.Builder().text("Distribution").fontSize(18).build(), "gaptop 10"); left.add(new FLabel.Builder().text("Distribution").fontSize(18).build(), "gaptop 10");
left.add(new FTextPane("Hover over each item for a more detailed description."), "gaptop 20");
final JXButtonPanel poolTypePanel = new JXButtonPanel(); final JXButtonPanel poolTypePanel = new JXButtonPanel();
final String radioConstraints = "h 25px!, gaptop 5"; final String radioConstraints = "h 25px!, gaptop 5";
poolTypePanel.add(radBalanced, radioConstraints); poolTypePanel.add(radBalanced, radioConstraints);
@@ -201,9 +203,9 @@ public class DialogChooseColors {
cbxArtifacts.setVisible(!radSurpriseMe.isSelected() && !radBoosters.isSelected()); cbxArtifacts.setVisible(!radSurpriseMe.isSelected() && !radBoosters.isSelected());
numberOfBoostersField.setVisible(radBoosters.isSelected()); numberOfBoostersField.setVisible(radBoosters.isSelected());
radBalanced.setToolTipText("A balanced distribution will provide a roughly equal number of cards in each selected color."); radBalanced.setToolTipText("A \"Balanced\" distribution will provide a roughly equal number of cards in each selected color.");
radRandom.setToolTipText("A random distribution will be almost entirely randomly selected. This ignores any color selections."); radRandom.setToolTipText("A \"True Random\" distribution will be almost entirely randomly selected. This ignores any color selections.");
radSurpriseMe.setToolTipText("This is the same as a balanced distribution, except the colors picked will be random and you will not be told what they are."); radSurpriseMe.setToolTipText("This is the same as a \"Balanced\" distribution, except the colors picked will be random and you will not be told what they are.");
radBoosters.setToolTipText("This ignores all color settings and instead generates a card pool out of a specified number of booster packs."); radBoosters.setToolTipText("This ignores all color settings and instead generates a card pool out of a specified number of booster packs.");
cbxArtifacts.setToolTipText("When selected, artifacts will be included in your pool regardless of color selections. This mimics the old card pool behavior."); cbxArtifacts.setToolTipText("When selected, artifacts will be included in your pool regardless of color selections. This mimics the old card pool behavior.");