mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 04:38:00 +00:00
Support respecting saved card preferred art on future sessions
This commit is contained in:
@@ -22,10 +22,12 @@ import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.ListMultimap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Multimaps;
|
||||
|
||||
import forge.card.CardEdition.CardInSet;
|
||||
import forge.card.CardEdition.Type;
|
||||
import forge.item.PaperCard;
|
||||
import forge.util.*;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
@@ -40,10 +42,10 @@ public final class CardDb implements ICardDatabase {
|
||||
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 = new TreeMap<String, PaperCard>(String.CASE_INSENSITIVE_ORDER);
|
||||
private final Map<String, CardRules> rulesByName;
|
||||
private static Map<String, String> artPrefs = new HashMap<String, String>();
|
||||
|
||||
private final List<PaperCard> allCards = new ArrayList<PaperCard>();
|
||||
private final List<PaperCard> roAllCards = Collections.unmodifiableList(allCards);
|
||||
private final Collection<PaperCard> roUniqueCards = Collections.unmodifiableCollection(uniqueCardsByName.values());
|
||||
private final CardEdition.Collection editions;
|
||||
|
||||
public enum SetPreference {
|
||||
@@ -174,8 +176,23 @@ public final class CardDb implements ICardDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean setPreferredArt(String cardName, String preferredArt) {
|
||||
CardRequest request = CardRequest.fromString(cardName + NameSetSeparator + preferredArt);
|
||||
PaperCard pc = tryGetCard(request);
|
||||
if (pc != null) {
|
||||
artPrefs.put(cardName, preferredArt);
|
||||
uniqueCardsByName.put(cardName, pc);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaperCard getCard(final String cardName) {
|
||||
public PaperCard getCard(String cardName) {
|
||||
String preferredArt = artPrefs.get(cardName);
|
||||
if (preferredArt != null) { //account for preferred art if needed
|
||||
cardName += NameSetSeparator + preferredArt;
|
||||
}
|
||||
CardRequest request = CardRequest.fromString(cardName);
|
||||
return tryGetCard(request);
|
||||
}
|
||||
@@ -345,10 +362,10 @@ public final class CardDb implements ICardDatabase {
|
||||
return cnt;
|
||||
}
|
||||
|
||||
// returns a list of all cards from their respective latest editions
|
||||
// returns a list of all cards from their respective latest (or preferred) editions
|
||||
@Override
|
||||
public Collection<PaperCard> getUniqueCards() {
|
||||
return roUniqueCards;
|
||||
return uniqueCardsByName.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,6 +21,7 @@ import forge.assets.FSkin;
|
||||
import forge.assets.FSkinFont;
|
||||
import forge.assets.FSkinImage;
|
||||
import forge.assets.FTextureRegionImage;
|
||||
import forge.card.CardDb;
|
||||
import forge.card.CardEdition;
|
||||
import forge.card.CardPreferences;
|
||||
import forge.card.CardRulesPredicates;
|
||||
@@ -746,7 +747,13 @@ public class FDeckEditor extends TabPageScreen<FDeckEditor> {
|
||||
cardManager.setPool(cardpool);
|
||||
break;
|
||||
default:
|
||||
if (cardManager.getConfig().getUniqueCardsOnly()) {
|
||||
cardManager.setWantUnique(false); //prevent needing to perform logic to calculate uniqueness since we're passing a unique collection
|
||||
cardManager.setPool(ItemPool.createFrom(FModel.getMagicDb().getCommonCards().getUniqueCards(), PaperCard.class), true);
|
||||
}
|
||||
else {
|
||||
cardManager.setPool(ItemPool.createFrom(FModel.getMagicDb().getCommonCards().getAllCards(), PaperCard.class), true);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -846,7 +853,7 @@ public class FDeckEditor extends TabPageScreen<FDeckEditor> {
|
||||
if (result != card) {
|
||||
cardManager.replaceAll(card, result);
|
||||
}
|
||||
prefs.setPreferredArt(result.getEdition() + "|" + result.getArtIndex());
|
||||
prefs.setPreferredArt(result.getEdition() + CardDb.NameSetSeparator + result.getArtIndex());
|
||||
CardPreferences.save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package forge.card;
|
||||
|
||||
import forge.item.IPaperCard;
|
||||
import forge.model.FModel;
|
||||
import forge.properties.ForgeConstants;
|
||||
import forge.util.XmlUtil;
|
||||
|
||||
@@ -24,11 +25,11 @@ public class CardPreferences {
|
||||
private static Map<String, CardPreferences> allPrefs = new HashMap<String, CardPreferences>();
|
||||
|
||||
public static CardPreferences getPrefs(IPaperCard card) {
|
||||
String key = card.getName(); //TODO: Consider include art/set in key
|
||||
CardPreferences prefs = allPrefs.get(key);
|
||||
String cardName = card.getName();
|
||||
CardPreferences prefs = allPrefs.get(cardName);
|
||||
if (prefs == null) {
|
||||
prefs = new CardPreferences();
|
||||
allPrefs.put(key, prefs);
|
||||
prefs = new CardPreferences(cardName);
|
||||
allPrefs.put(cardName, prefs);
|
||||
}
|
||||
return prefs;
|
||||
}
|
||||
@@ -41,14 +42,11 @@ public class CardPreferences {
|
||||
final Document document = builder.parse(ForgeConstants.CARD_PREFS_FILE);
|
||||
final NodeList cards = document.getElementsByTagName("card");
|
||||
for (int i = 0; i < cards.getLength(); i++) {
|
||||
final CardPreferences prefs = new CardPreferences();
|
||||
final Element el = (Element)cards.item(i);
|
||||
allPrefs.put(el.getAttribute("name"), prefs);
|
||||
prefs.starCount = Integer.parseInt(el.getAttribute("stars"));
|
||||
prefs.preferredArt = el.getAttribute("art");
|
||||
if (prefs.preferredArt.length() == 0) {
|
||||
prefs.preferredArt = null; //don't store empty string
|
||||
}
|
||||
final CardPreferences prefs = new CardPreferences(el.getAttribute("name"));
|
||||
allPrefs.put(prefs.cardName, prefs);
|
||||
prefs.setStarCount(getIntAttribute(el, "stars"));
|
||||
prefs.setPreferredArt(getStringAttribute(el, "art"));
|
||||
}
|
||||
}
|
||||
catch (FileNotFoundException e) {
|
||||
@@ -62,6 +60,25 @@ public class CardPreferences {
|
||||
}
|
||||
}
|
||||
|
||||
private static int getIntAttribute(Element el, String name) {
|
||||
String value = el.getAttribute(name);
|
||||
if (value.length() > 0) {
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
}
|
||||
catch (Exception ex) {}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static String getStringAttribute(Element el, String name) {
|
||||
String value = el.getAttribute(name);
|
||||
if (value.length() > 0) {
|
||||
return value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void save() {
|
||||
try {
|
||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
@@ -91,10 +108,12 @@ public class CardPreferences {
|
||||
}
|
||||
}
|
||||
|
||||
private final String cardName;
|
||||
private int starCount;
|
||||
private String preferredArt;
|
||||
|
||||
private CardPreferences() {
|
||||
private CardPreferences(String cardName0) {
|
||||
cardName = cardName0;
|
||||
}
|
||||
|
||||
public int getStarCount() {
|
||||
@@ -110,6 +129,14 @@ public class CardPreferences {
|
||||
}
|
||||
|
||||
public void setPreferredArt(String preferredArt0) {
|
||||
if (preferredArt0 == null) {
|
||||
preferredArt = null;
|
||||
return;
|
||||
}
|
||||
if (preferredArt0.equals(preferredArt)) { return; }
|
||||
|
||||
if (FModel.getMagicDb().getCommonCards().setPreferredArt(cardName, preferredArt0)) {
|
||||
preferredArt = preferredArt0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user