mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-14 17:58:01 +00:00
Merge branch 'artpreference-carddb-fix' into 'master'
Re-add to carddb the integration with CardPreferences on Mobile version See merge request core-developers/forge!5316
This commit is contained in:
@@ -148,6 +148,19 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
|
||||
return !StringUtils.isNumeric(s);
|
||||
}
|
||||
|
||||
private static CardRequest fromPreferredArtEntry(String preferredArt, boolean isFoil){
|
||||
// Preferred Art Entry are supposed to be cardName|setCode|artIndex only
|
||||
String[] info = TextUtil.split(preferredArt, NameSetSeparator);
|
||||
if (info.length != 3)
|
||||
return null;
|
||||
try {
|
||||
String cardName = info[0];
|
||||
String setCode = info[1];
|
||||
int artIndex = Integer.parseInt(info[2]);
|
||||
return new CardRequest(cardName, setCode, artIndex, isFoil, IPaperCard.NO_COLLECTOR_NUMBER);
|
||||
} catch (NumberFormatException ex){ return null; }
|
||||
}
|
||||
|
||||
public static CardRequest fromString(String reqInfo) {
|
||||
if (reqInfo == null)
|
||||
return null;
|
||||
@@ -179,21 +192,25 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
|
||||
cardName = cardName.substring(0, cardName.length() - foilSuffix.length());
|
||||
isFoil = true;
|
||||
}
|
||||
|
||||
String preferredArt = artPrefs.get(cardName);
|
||||
int artIndex = artPos > 0 ? Integer.parseInt(info[artPos]) : IPaperCard.NO_ART_INDEX; // default: no art index
|
||||
if (preferredArt != null) { //account for preferred art if needed
|
||||
System.err.println("I AM HERE - DECIDE WHAT TO DO");
|
||||
}
|
||||
String collectorNumber = cNrPos > 0 ? info[cNrPos].substring(1, info[cNrPos].length() - 1) : IPaperCard.NO_COLLECTOR_NUMBER;
|
||||
String setName = setPos > 0 ? info[setPos] : null;
|
||||
if (setName != null && setName.equals(CardEdition.UNKNOWN.getCode())) { // ???
|
||||
setName = null;
|
||||
String setCode = setPos > 0 ? info[setPos] : null;
|
||||
if (setCode != null && setCode.equals(CardEdition.UNKNOWN.getCode())) { // ???
|
||||
setCode = null;
|
||||
}
|
||||
if (setCode == null) {
|
||||
String preferredArt = artPrefs.get(cardName);
|
||||
if (preferredArt != null) { //account for preferred art if needed
|
||||
CardRequest request = fromPreferredArtEntry(preferredArt, isFoil);
|
||||
if (request != null) // otherwise, simply discard it and go on.
|
||||
return request;
|
||||
System.err.println(String.format("[LOG]: Faulty Entry in Preferred Art for Card %s - Please check!", cardName));
|
||||
}
|
||||
}
|
||||
// finally, check whether any between artIndex and CollectorNumber has been set
|
||||
if (collectorNumber.equals(IPaperCard.NO_COLLECTOR_NUMBER) && artIndex == IPaperCard.NO_ART_INDEX)
|
||||
artIndex = IPaperCard.DEFAULT_ART_INDEX;
|
||||
return new CardRequest(cardName, setName, artIndex, isFoil, collectorNumber);
|
||||
return new CardRequest(cardName, setCode, artIndex, isFoil, collectorNumber);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -373,11 +390,11 @@ public final class CardDb implements ICardDatabase, IDeckGenPool {
|
||||
return pc;
|
||||
}
|
||||
|
||||
public boolean setPreferredArt(String cardName, String preferredArt) {
|
||||
CardRequest request = CardRequest.fromString(cardName + NameSetSeparator + preferredArt);
|
||||
PaperCard pc = tryGetCard(request);
|
||||
public boolean setPreferredArt(String cardName, String setCode, int artIndex) {
|
||||
String cardRequestForPreferredArt = CardRequest.compose(cardName, setCode, artIndex);
|
||||
PaperCard pc = this.getCard(cardRequestForPreferredArt);
|
||||
if (pc != null) {
|
||||
artPrefs.put(cardName, preferredArt);
|
||||
artPrefs.put(cardName, cardRequestForPreferredArt);
|
||||
uniqueCardsByName.put(cardName, pc);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2085,6 +2085,58 @@ public class CardDbTestCase extends ForgeCardMockTestCase {
|
||||
assertEquals(maxArtIndex, 13);
|
||||
}
|
||||
|
||||
@Test void prepareTestCaseForSetPreferredArtTest(){
|
||||
String setCode = this.editionsCounterspell[0];
|
||||
int artIndex = 4; // non-existing
|
||||
String cardRequest = CardDb.CardRequest.compose(this.cardNameCounterspell, setCode, artIndex);
|
||||
PaperCard nonExistingCounterSpell = this.cardDb.getCard(cardRequest);
|
||||
assertNull(nonExistingCounterSpell);
|
||||
}
|
||||
|
||||
@Test void setPreferredArtForCard(){
|
||||
String cardName = "Mountain";
|
||||
String setCode = "3ED";
|
||||
int artIndex = 5;
|
||||
assertFalse(this.cardDb.setPreferredArt(cardName, setCode, artIndex));
|
||||
assertTrue(this.cardDb.setPreferredArt(cardName, setCode, 1));
|
||||
}
|
||||
|
||||
|
||||
@Test void testThatWithCardPreferenceSetAndNoRequestForSpecificEditionAlwaysReturnsPreferredArt(){
|
||||
String cardRequest = CardDb.CardRequest.compose("Island", "MIR", 3);
|
||||
PaperCard islandCard = this.cardDb.getCard(cardRequest);
|
||||
assertNotNull(islandCard);
|
||||
assertEquals(islandCard.getName(), "Island");
|
||||
assertEquals(islandCard.getEdition(), "MIR");
|
||||
assertEquals(islandCard.getArtIndex(), 3);
|
||||
|
||||
// now set preferred art
|
||||
assertTrue(this.cardDb.setPreferredArt("Island", "MIR", 3));
|
||||
// Now requesting for a different Island
|
||||
cardRequest = CardDb.CardRequest.compose("Island", "TMP", 1);
|
||||
islandCard = this.cardDb.getCard(cardRequest);
|
||||
assertNotNull(islandCard);
|
||||
// Now card should be from the preferred art no matter the request
|
||||
assertEquals(islandCard.getName(), "Island");
|
||||
assertEquals(islandCard.getEdition(), "TMP");
|
||||
assertEquals(islandCard.getArtIndex(), 1);
|
||||
|
||||
// Now just asking for an Island
|
||||
islandCard = this.cardDb.getCard("Island");
|
||||
assertNotNull(islandCard);
|
||||
assertEquals(islandCard.getName(), "Island");
|
||||
assertEquals(islandCard.getEdition(), "MIR");
|
||||
assertEquals(islandCard.getArtIndex(), 3);
|
||||
|
||||
// Now asking for a foiled island - I will get the one from preferred art - but foiled
|
||||
cardRequest = CardDb.CardRequest.compose("Island", true);
|
||||
islandCard = this.cardDb.getCard(cardRequest);
|
||||
assertNotNull(islandCard);
|
||||
assertEquals(islandCard.getName(), "Island");
|
||||
assertEquals(islandCard.getEdition(), "MIR");
|
||||
assertEquals(islandCard.getArtIndex(), 3);
|
||||
assertTrue(islandCard.isFoil());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1281,7 +1281,7 @@ public class FDeckEditor extends TabPageScreen<FDeckEditor> {
|
||||
if (result != card) {
|
||||
cardManager.replaceAll(card, result);
|
||||
}
|
||||
prefs.setPreferredArt(result.getEdition() + CardDb.NameSetSeparator + result.getArtIndex());
|
||||
prefs.setPreferredArt(result.getEdition(), result.getArtIndex());
|
||||
CardPreferences.save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import java.util.Map;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import forge.card.CardDb;
|
||||
import forge.util.TextUtil;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
@@ -113,8 +115,31 @@ public class CardPreferences {
|
||||
}
|
||||
if (preferredArt0.equals(preferredArt)) { return; }
|
||||
|
||||
if (FModel.getMagicDb().getCommonCards().setPreferredArt(cardName, preferredArt0)) {
|
||||
preferredArt = preferredArt0;
|
||||
try {
|
||||
String infoCardName;
|
||||
String infoSetCode;
|
||||
int infoArtIndex;
|
||||
String[] prefArtInfos =TextUtil.split(preferredArt0, CardDb.NameSetSeparator);
|
||||
if (prefArtInfos.length == 2){
|
||||
// legacy format
|
||||
infoCardName = this.cardName;
|
||||
infoSetCode = prefArtInfos[0];
|
||||
infoArtIndex = Integer.parseInt(prefArtInfos[1]);
|
||||
} else {
|
||||
infoCardName = prefArtInfos[0];
|
||||
infoSetCode = prefArtInfos[1];
|
||||
infoArtIndex = Integer.parseInt(prefArtInfos[2]);
|
||||
}
|
||||
if (!this.cardName.equals(infoCardName)) // extra sanity check
|
||||
return;
|
||||
if (FModel.getMagicDb().getCommonCards().setPreferredArt(cardName, infoSetCode, infoArtIndex))
|
||||
preferredArt = preferredArt0;
|
||||
} catch (NumberFormatException ex){
|
||||
System.err.println("ERROR with Existing Preferred Card Entry: " + preferredArt0);
|
||||
}
|
||||
}
|
||||
|
||||
public void setPreferredArt(String setCode, int artIndex) {
|
||||
this.setPreferredArt(setCode + CardDb.NameSetSeparator + artIndex);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user