refactor: removed unused data from images download, improved code

This commit is contained in:
Oleg Agafonov 2023-09-17 14:50:33 +04:00
parent 4e77ccb381
commit 0bb837cbe5
7 changed files with 29 additions and 57 deletions

View file

@ -68,7 +68,7 @@ public enum ScryfallImageSource implements CardImageSource {
// tokens support only direct links // tokens support only direct links
if (isToken) { if (isToken) {
baseUrl = ScryfallImageSupportTokens.findTokenLink(card.getSet(), card.getName(), card.getType()); baseUrl = ScryfallImageSupportTokens.findTokenLink(card.getSet(), card.getName(), card.getImageNumber());
alternativeUrl = null; alternativeUrl = null;
} }

View file

@ -2177,8 +2177,8 @@ public class ScryfallImageSupportTokens {
return supportedSets; return supportedSets;
} }
public static String findTokenLink(String setCode, String tokenName, Integer tokenNumber) { public static String findTokenLink(String setCode, String tokenName, Integer imageNumber) {
String search = setCode + "/" + tokenName + (!tokenNumber.equals(0) ? "/" + tokenNumber : ""); String search = setCode + "/" + tokenName + (!imageNumber.equals(0) ? "/" + imageNumber : "");
return supportedCards.getOrDefault(search, null); return supportedCards.getOrDefault(search, null);
} }
} }

View file

@ -81,7 +81,7 @@ public enum TokensMtgImageSource implements CardImageSource {
public CardImageUrls generateTokenUrl(CardDownloadData card) throws IOException { public CardImageUrls generateTokenUrl(CardDownloadData card) throws IOException {
String name = card.getName(); String name = card.getName();
String set = card.getSet(); String set = card.getSet();
int type = card.getType(); int imageNumber = card.getImageNumber();
// handle emblems // handle emblems
if (name.toLowerCase(Locale.ENGLISH).contains("emblem")) { if (name.toLowerCase(Locale.ENGLISH).contains("emblem")) {
@ -110,14 +110,14 @@ public enum TokensMtgImageSource implements CardImageSource {
} }
TokenData tokenData; TokenData tokenData;
if (type == 0) { if (imageNumber == 0) {
tokenData = list.get(0); tokenData = list.get(0);
} else { } else {
if (type > list.size()) { if (imageNumber > list.size()) {
LOGGER.warn("Not enough images variants for token with type number " + type + ", name " + name + ", set " + set + '.'); LOGGER.warn("Not enough images variants for token with type number " + imageNumber + ", name " + name + ", set " + set + '.');
return null; return null;
} }
tokenData = list.get(card.getType() - 1); tokenData = list.get(card.getImageNumber() - 1);
} }
String url = "https://tokens.mtg.onl/tokens/" + tokenData.getExpansionSetCode().trim() + '_' String url = "https://tokens.mtg.onl/tokens/" + tokenData.getExpansionSetCode().trim() + '_'

View file

@ -14,7 +14,7 @@ public class CardDownloadData {
private String fileName = ""; private String fileName = "";
private String set; private String set;
private final String collectorId; private final String collectorId;
private final Integer type; private final Integer imageNumber;
private boolean token; private boolean token;
private final boolean twoFacedCard; private final boolean twoFacedCard;
private final boolean secondSide; private final boolean secondSide;
@ -23,31 +23,30 @@ public class CardDownloadData {
private boolean splitCard; private boolean splitCard;
private final boolean usesVariousArt; private final boolean usesVariousArt;
private String tokenClassName; private String tokenClassName;
private boolean isType2;
public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer type) { public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer imageNumber) {
this(name, setCode, collectorId, usesVariousArt, type, false, ""); this(name, setCode, collectorId, usesVariousArt, imageNumber, false, "");
} }
public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer type, boolean token) { public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer imageNumber, boolean token) {
this(name, setCode, collectorId, usesVariousArt, type, token, false, false, ""); this(name, setCode, collectorId, usesVariousArt, imageNumber, token, false, false, "");
} }
public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer type, boolean token, String fileName) { public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer imageNumber, boolean token, String fileName) {
this(name, setCode, collectorId, usesVariousArt, type, token, false, false, ""); this(name, setCode, collectorId, usesVariousArt, imageNumber, token, false, false, "");
this.fileName = fileName; this.fileName = fileName;
} }
public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer type, boolean token, boolean twoFacedCard, boolean secondSide) { public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer imageNumber, boolean token, boolean twoFacedCard, boolean secondSide) {
this(name, setCode, collectorId, usesVariousArt, type, token, twoFacedCard, secondSide, ""); this(name, setCode, collectorId, usesVariousArt, imageNumber, token, twoFacedCard, secondSide, "");
} }
public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer type, boolean token, boolean twoFacedCard, boolean secondSide, String tokenClassName) { public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer imageNumber, boolean token, boolean twoFacedCard, boolean secondSide, String tokenClassName) {
this.name = name; this.name = name;
this.set = setCode; this.set = setCode;
this.collectorId = collectorId; this.collectorId = collectorId;
this.usesVariousArt = usesVariousArt; this.usesVariousArt = usesVariousArt;
this.type = type; this.imageNumber = imageNumber;
this.token = token; this.token = token;
this.twoFacedCard = twoFacedCard; this.twoFacedCard = twoFacedCard;
this.secondSide = secondSide; this.secondSide = secondSide;
@ -60,7 +59,7 @@ public class CardDownloadData {
this.fileName = card.fileName; this.fileName = card.fileName;
this.set = card.set; this.set = card.set;
this.collectorId = card.collectorId; this.collectorId = card.collectorId;
this.type = card.type; this.imageNumber = card.imageNumber;
this.token = card.token; this.token = card.token;
this.twoFacedCard = card.twoFacedCard; this.twoFacedCard = card.twoFacedCard;
this.secondSide = card.secondSide; this.secondSide = card.secondSide;
@ -69,7 +68,6 @@ public class CardDownloadData {
this.splitCard = card.splitCard; this.splitCard = card.splitCard;
this.usesVariousArt = card.usesVariousArt; this.usesVariousArt = card.usesVariousArt;
this.tokenClassName = card.tokenClassName; this.tokenClassName = card.tokenClassName;
this.isType2 = card.isType2;
} }
@Override @Override
@ -96,10 +94,8 @@ public class CardDownloadData {
if (this.twoFacedCard != other.twoFacedCard) { if (this.twoFacedCard != other.twoFacedCard) {
return false; return false;
} }
if (this.secondSide != other.secondSide) {
return false; return this.secondSide == other.secondSide;
}
return this.isType2 == other.isType2;
} }
@Override @Override
@ -108,11 +104,10 @@ public class CardDownloadData {
hash = 47 * hash + (this.name != null ? this.name.hashCode() : 0); hash = 47 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 47 * hash + (this.set != null ? this.set.hashCode() : 0); hash = 47 * hash + (this.set != null ? this.set.hashCode() : 0);
hash = 47 * hash + (this.collectorId != null ? this.collectorId.hashCode() : 0); hash = 47 * hash + (this.collectorId != null ? this.collectorId.hashCode() : 0);
hash = 47 * hash + (this.type != null ? this.type.hashCode() : 0); hash = 47 * hash + (this.imageNumber != null ? this.imageNumber.hashCode() : 0);
hash = 47 * hash + (this.token ? 1 : 0); hash = 47 * hash + (this.token ? 1 : 0);
hash = 47 * hash + (this.twoFacedCard ? 1 : 0); hash = 47 * hash + (this.twoFacedCard ? 1 : 0);
hash = 47 * hash + (this.secondSide ? 1 : 0); hash = 47 * hash + (this.secondSide ? 1 : 0);
hash = 47 * hash + (this.isType2 ? 1 : 0);
return hash; return hash;
} }
@ -217,8 +212,8 @@ public class CardDownloadData {
this.splitCard = splitCard; this.splitCard = splitCard;
} }
public Integer getType() { public Integer getImageNumber() {
return type; return imageNumber;
} }
public boolean getUsesVariousArt() { public boolean getUsesVariousArt() {
@ -232,12 +227,4 @@ public class CardDownloadData {
public void setFlippedSide(boolean flippedSide) { public void setFlippedSide(boolean flippedSide) {
this.flippedSide = flippedSide; this.flippedSide = flippedSide;
} }
public boolean isType2() {
return isType2;
}
public void setType2(boolean type2) {
isType2 = type2;
}
} }

View file

@ -368,7 +368,7 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
for (CardDownloadData data : cardsMissing) { for (CardDownloadData data : cardsMissing) {
if (data.isToken()) { if (data.isToken()) {
if (selectedSource.isTokenSource() if (selectedSource.isTokenSource()
&& selectedSource.isTokenImageProvided(data.getSet(), data.getName(), data.getType()) && selectedSource.isTokenImageProvided(data.getSet(), data.getName(), data.getImageNumber())
&& selectedSets.contains(data.getSet())) { && selectedSets.contains(data.getSet())) {
numberTokenImagesAvailable++; numberTokenImagesAvailable++;
cardsDownloadQueue.add(data); cardsDownloadQueue.add(data);
@ -428,23 +428,12 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
} }
private static List<CardDownloadData> prepareMissingCards(List<CardInfo> allCards, boolean redownloadMode) { private static List<CardDownloadData> prepareMissingCards(List<CardInfo> allCards, boolean redownloadMode) {
// get filter for Standard Type 2 cards
Set<String> type2SetsFilter = new HashSet<>();
List<String> constructedFormats = ConstructedFormats.getSetsByFormat(ConstructedFormats.STANDARD);
if (constructedFormats != null && !constructedFormats.isEmpty()) {
type2SetsFilter.addAll(constructedFormats);
} else {
logger.warn("No formats defined. Try connecting to a server first!");
}
// prepare checking list // prepare checking list
List<CardDownloadData> allCardsUrls = Collections.synchronizedList(new ArrayList<>()); List<CardDownloadData> allCardsUrls = Collections.synchronizedList(new ArrayList<>());
try { try {
allCards.parallelStream().forEach(card -> { allCards.parallelStream().forEach(card -> {
if (!card.getCardNumber().isEmpty() && !"0".equals(card.getCardNumber()) && !card.getSetCode().isEmpty()) { if (!card.getCardNumber().isEmpty() && !"0".equals(card.getCardNumber()) && !card.getSetCode().isEmpty()) {
String cardName = card.getName(); String cardName = card.getName();
boolean isType2 = type2SetsFilter.contains(card.getSetCode());
CardDownloadData url = new CardDownloadData(cardName, card.getSetCode(), card.getCardNumber(), card.usesVariousArt(), 0, false, card.isDoubleFaced(), card.isNightCard()); CardDownloadData url = new CardDownloadData(cardName, card.getSetCode(), card.getCardNumber(), card.usesVariousArt(), 0, false, card.isDoubleFaced(), card.isNightCard());
// variations must have diff file names with additional postfix // variations must have diff file names with additional postfix
@ -454,7 +443,6 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
url.setFlipCard(card.isFlipCard()); url.setFlipCard(card.isFlipCard());
url.setSplitCard(card.isSplitCard()); url.setSplitCard(card.isSplitCard());
url.setType2(isType2);
// main side // main side
allCardsUrls.add(url); allCardsUrls.add(url);
@ -477,7 +465,6 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
secondSideCard.getCardNumber(), secondSideCard.getCardNumber(),
card.usesVariousArt(), card.usesVariousArt(),
0, false, card.isDoubleFaced(), true); 0, false, card.isDoubleFaced(), true);
url.setType2(isType2);
allCardsUrls.add(url); allCardsUrls.add(url);
} }
if (card.isFlipCard()) { if (card.isFlipCard()) {
@ -492,7 +479,6 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
0, false, card.isDoubleFaced(), card.isNightCard()); 0, false, card.isDoubleFaced(), card.isNightCard());
cardDownloadData.setFlipCard(true); cardDownloadData.setFlipCard(true);
cardDownloadData.setFlippedSide(true); cardDownloadData.setFlippedSide(true);
cardDownloadData.setType2(isType2);
allCardsUrls.add(cardDownloadData); allCardsUrls.add(cardDownloadData);
} }
if (card.getMeldsToCardName() != null) { if (card.getMeldsToCardName() != null) {
@ -512,7 +498,6 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
meldsToCard.getCardNumber(), meldsToCard.getCardNumber(),
card.usesVariousArt(), card.usesVariousArt(),
0, false, false, false); 0, false, false, false);
url.setType2(isType2);
allCardsUrls.add(url); allCardsUrls.add(url);
} }
if (card.isModalDoubleFacedCard()) { if (card.isModalDoubleFacedCard()) {
@ -525,7 +510,6 @@ public class DownloadPicturesService extends DefaultBoundedRangeModel implements
card.getCardNumber(), card.getCardNumber(),
card.usesVariousArt(), card.usesVariousArt(),
0, false, true, true); 0, false, true, true);
cardDownloadData.setType2(isType2);
allCardsUrls.add(cardDownloadData); allCardsUrls.add(cardDownloadData);
} }
} else if (card.getCardNumber().isEmpty() || "0".equals(card.getCardNumber())) { } else if (card.getCardNumber().isEmpty() || "0".equals(card.getCardNumber())) {

View file

@ -86,6 +86,7 @@ public final class ImageCache {
tokenFile = getTFile(path); tokenFile = getTFile(path);
// try token from card // try token from card
// TODO: return image from another set on empty image?
if (tokenFile == null || !tokenFile.exists()) { if (tokenFile == null || !tokenFile.exists()) {
CardDownloadData tempInfo = new CardDownloadData(info); CardDownloadData tempInfo = new CardDownloadData(info);
tempInfo.setToken(false); tempInfo.setToken(false);

View file

@ -117,8 +117,8 @@ public final class CardImageUtils {
String setPath = buildImagePathToSet(card); String setPath = buildImagePathToSet(card);
String prefixType = ""; String prefixType = "";
if (card.getType() != 0) { if (card.getImageNumber() != 0) {
prefixType = " " + card.getType(); prefixType = " " + card.getImageNumber();
} }
String cardName = card.getFileName(); String cardName = card.getFileName();