Skip to content

Commit

Permalink
refactor: removed unused data from images download, improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
JayDi85 committed Sep 17, 2023
1 parent 4e77ccb commit 0bb837c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private CardImageUrls innerGenerateURL(CardDownloadData card, boolean isToken) {

// tokens support only direct links
if (isToken) {
baseUrl = ScryfallImageSupportTokens.findTokenLink(card.getSet(), card.getName(), card.getType());
baseUrl = ScryfallImageSupportTokens.findTokenLink(card.getSet(), card.getName(), card.getImageNumber());
alternativeUrl = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2177,8 +2177,8 @@ public static Map<String, String> getSupportedSets() {
return supportedSets;
}

public static String findTokenLink(String setCode, String tokenName, Integer tokenNumber) {
String search = setCode + "/" + tokenName + (!tokenNumber.equals(0) ? "/" + tokenNumber : "");
public static String findTokenLink(String setCode, String tokenName, Integer imageNumber) {
String search = setCode + "/" + tokenName + (!imageNumber.equals(0) ? "/" + imageNumber : "");
return supportedCards.getOrDefault(search, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private String getEmblemName(String originalName) {
public CardImageUrls generateTokenUrl(CardDownloadData card) throws IOException {
String name = card.getName();
String set = card.getSet();
int type = card.getType();
int imageNumber = card.getImageNumber();

// handle emblems
if (name.toLowerCase(Locale.ENGLISH).contains("emblem")) {
Expand Down Expand Up @@ -110,14 +110,14 @@ public CardImageUrls generateTokenUrl(CardDownloadData card) throws IOException
}

TokenData tokenData;
if (type == 0) {
if (imageNumber == 0) {
tokenData = list.get(0);
} else {
if (type > list.size()) {
LOGGER.warn("Not enough images variants for token with type number " + type + ", name " + name + ", set " + set + '.');
if (imageNumber > list.size()) {
LOGGER.warn("Not enough images variants for token with type number " + imageNumber + ", name " + name + ", set " + set + '.');
return null;
}
tokenData = list.get(card.getType() - 1);
tokenData = list.get(card.getImageNumber() - 1);
}

String url = "https://tokens.mtg.onl/tokens/" + tokenData.getExpansionSetCode().trim() + '_'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CardDownloadData {
private String fileName = "";
private String set;
private final String collectorId;
private final Integer type;
private final Integer imageNumber;
private boolean token;
private final boolean twoFacedCard;
private final boolean secondSide;
Expand All @@ -23,31 +23,30 @@ public class CardDownloadData {
private boolean splitCard;
private final boolean usesVariousArt;
private String tokenClassName;
private boolean isType2;

public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer type) {
this(name, setCode, collectorId, usesVariousArt, type, false, "");
public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer imageNumber) {
this(name, setCode, collectorId, usesVariousArt, imageNumber, false, "");
}

public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer type, boolean token) {
this(name, setCode, collectorId, usesVariousArt, type, token, false, false, "");
public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer imageNumber, boolean token) {
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) {
this(name, setCode, collectorId, usesVariousArt, type, token, false, false, "");
public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer imageNumber, boolean token, String fileName) {
this(name, setCode, collectorId, usesVariousArt, imageNumber, token, false, false, "");
this.fileName = fileName;
}

public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer type, boolean token, boolean twoFacedCard, boolean secondSide) {
this(name, setCode, collectorId, usesVariousArt, type, token, twoFacedCard, secondSide, "");
public CardDownloadData(String name, String setCode, String collectorId, boolean usesVariousArt, Integer imageNumber, boolean token, boolean twoFacedCard, boolean 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.set = setCode;
this.collectorId = collectorId;
this.usesVariousArt = usesVariousArt;
this.type = type;
this.imageNumber = imageNumber;
this.token = token;
this.twoFacedCard = twoFacedCard;
this.secondSide = secondSide;
Expand All @@ -60,7 +59,7 @@ public CardDownloadData(final CardDownloadData card) {
this.fileName = card.fileName;
this.set = card.set;
this.collectorId = card.collectorId;
this.type = card.type;
this.imageNumber = card.imageNumber;
this.token = card.token;
this.twoFacedCard = card.twoFacedCard;
this.secondSide = card.secondSide;
Expand All @@ -69,7 +68,6 @@ public CardDownloadData(final CardDownloadData card) {
this.splitCard = card.splitCard;
this.usesVariousArt = card.usesVariousArt;
this.tokenClassName = card.tokenClassName;
this.isType2 = card.isType2;
}

@Override
Expand All @@ -96,10 +94,8 @@ public boolean equals(Object obj) {
if (this.twoFacedCard != other.twoFacedCard) {
return false;
}
if (this.secondSide != other.secondSide) {
return false;
}
return this.isType2 == other.isType2;

return this.secondSide == other.secondSide;
}

@Override
Expand All @@ -108,11 +104,10 @@ public int hashCode() {
hash = 47 * hash + (this.name != null ? this.name.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.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.twoFacedCard ? 1 : 0);
hash = 47 * hash + (this.secondSide ? 1 : 0);
hash = 47 * hash + (this.isType2 ? 1 : 0);
return hash;
}

Expand Down Expand Up @@ -217,8 +212,8 @@ public void setSplitCard(boolean splitCard) {
this.splitCard = splitCard;
}

public Integer getType() {
return type;
public Integer getImageNumber() {
return imageNumber;
}

public boolean getUsesVariousArt() {
Expand All @@ -232,12 +227,4 @@ public boolean isFlippedSide() {
public void setFlippedSide(boolean flippedSide) {
this.flippedSide = flippedSide;
}

public boolean isType2() {
return isType2;
}

public void setType2(boolean type2) {
isType2 = type2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ private void reloadCardsToDownload(String selectedItem) {
for (CardDownloadData data : cardsMissing) {
if (data.isToken()) {
if (selectedSource.isTokenSource()
&& selectedSource.isTokenImageProvided(data.getSet(), data.getName(), data.getType())
&& selectedSource.isTokenImageProvided(data.getSet(), data.getName(), data.getImageNumber())
&& selectedSets.contains(data.getSet())) {
numberTokenImagesAvailable++;
cardsDownloadQueue.add(data);
Expand Down Expand Up @@ -428,23 +428,12 @@ private static String createDownloadName(CardInfo card) {
}

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
List<CardDownloadData> allCardsUrls = Collections.synchronizedList(new ArrayList<>());
try {
allCards.parallelStream().forEach(card -> {
if (!card.getCardNumber().isEmpty() && !"0".equals(card.getCardNumber()) && !card.getSetCode().isEmpty()) {
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());

// variations must have diff file names with additional postfix
Expand All @@ -454,7 +443,6 @@ private static List<CardDownloadData> prepareMissingCards(List<CardInfo> allCard

url.setFlipCard(card.isFlipCard());
url.setSplitCard(card.isSplitCard());
url.setType2(isType2);

// main side
allCardsUrls.add(url);
Expand All @@ -477,7 +465,6 @@ private static List<CardDownloadData> prepareMissingCards(List<CardInfo> allCard
secondSideCard.getCardNumber(),
card.usesVariousArt(),
0, false, card.isDoubleFaced(), true);
url.setType2(isType2);
allCardsUrls.add(url);
}
if (card.isFlipCard()) {
Expand All @@ -492,7 +479,6 @@ private static List<CardDownloadData> prepareMissingCards(List<CardInfo> allCard
0, false, card.isDoubleFaced(), card.isNightCard());
cardDownloadData.setFlipCard(true);
cardDownloadData.setFlippedSide(true);
cardDownloadData.setType2(isType2);
allCardsUrls.add(cardDownloadData);
}
if (card.getMeldsToCardName() != null) {
Expand All @@ -512,7 +498,6 @@ private static List<CardDownloadData> prepareMissingCards(List<CardInfo> allCard
meldsToCard.getCardNumber(),
card.usesVariousArt(),
0, false, false, false);
url.setType2(isType2);
allCardsUrls.add(url);
}
if (card.isModalDoubleFacedCard()) {
Expand All @@ -525,7 +510,6 @@ private static List<CardDownloadData> prepareMissingCards(List<CardInfo> allCard
card.getCardNumber(),
card.usesVariousArt(),
0, false, true, true);
cardDownloadData.setType2(isType2);
allCardsUrls.add(cardDownloadData);
}
} else if (card.getCardNumber().isEmpty() || "0".equals(card.getCardNumber())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public final class ImageCache {
tokenFile = getTFile(path);

// try token from card
// TODO: return image from another set on empty image?
if (tokenFile == null || !tokenFile.exists()) {
CardDownloadData tempInfo = new CardDownloadData(info);
tempInfo.setToken(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public static String buildImagePathToCardOrToken(CardDownloadData card) {
String setPath = buildImagePathToSet(card);

String prefixType = "";
if (card.getType() != 0) {
prefixType = " " + card.getType();
if (card.getImageNumber() != 0) {
prefixType = " " + card.getImageNumber();
}

String cardName = card.getFileName();
Expand Down

0 comments on commit 0bb837c

Please sign in to comment.