Added old decklist files compatibility loading (free to change card numbers, names and codes, see #4332)

This commit is contained in:
Oleg Agafonov 2018-01-03 07:43:15 +04:00
parent b0b5fe2036
commit 204a602b36
2 changed files with 49 additions and 14 deletions

View file

@ -67,22 +67,47 @@ public class DckDeckImporter extends DeckImporter {
int count = Integer.parseInt(m.group(2));
String setCode = m.group(3);
String cardNum = m.group(4);
String cardName = m.group(5);
cardNum = cardNum == null ? "" : cardNum.trim();
setCode = setCode == null ? "" : setCode.trim();
cardName = cardName == null ? "" : cardName.trim();
// search priority: set/code -> name
// with bulletproof on card number or name changes
DeckCardInfo deckCardInfo = null;
CardInfo cardInfo = CardRepository.instance.findCard(setCode, cardNum);
if (cardInfo == null) {
// Try alternate based on name
String cardName = m.group(5);
if (cardName != null && !cardName.isEmpty()) {
cardInfo = CardRepository.instance.findPreferedCoreExpansionCard(cardName, false);
sbMessage.append("Could not find card '").append(cardName).append("' in set ").append(setCode).append(" of number ").append(cardNum).append(".\n");
if (cardInfo != null) {
sbMessage.append("Made substitution of ").append(cardInfo.getCardNumber()).append(", ").append(cardInfo.getCard().getExpansionSetCode()).append(" instead.\n");
}
// search by number
CardInfo foundedCard = CardRepository.instance.findCard(setCode, cardNum);
boolean wasOutdated = false;
if ((foundedCard != null) && !foundedCard.getName().equals(cardName)){
sbMessage.append("Line ").append(lineCount).append(": ").append("founded outdated card number or name, will try to replace: ").append(line).append('\n');
wasOutdated = true;
foundedCard = null;
}
// search by name
if (foundedCard == null) {
if(!wasOutdated){
sbMessage.append("Line ").append(lineCount).append(": ").append("can't find card by number, will try ro replace: ").append(line).append('\n');
}
if (!cardName.equals("")) {
foundedCard = CardRepository.instance.findPreferedCoreExpansionCard(cardName, false, setCode);
}
if (foundedCard != null) {
sbMessage.append("Line ").append(lineCount).append(": ")
.append("replaced to [").append(foundedCard.getSetCode()).append(":").append(foundedCard.getCardNumberAsInt()).append("] ")
.append(foundedCard.getName()).append('\n');
}else{
sbMessage.append("Line ").append(lineCount).append(": ").append("ERROR, can't find card [").append(cardName).append("]").append('\n');
}
}
if (cardInfo != null) {
deckCardInfo = new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode());
if (foundedCard != null) {
deckCardInfo = new DeckCardInfo(foundedCard.getName(), foundedCard.getCardNumber(), foundedCard.getSetCode());
}
if (deckCardInfo != null) {
for (int i = 0; i < count; i++) {
@ -92,8 +117,6 @@ public class DckDeckImporter extends DeckImporter {
deckList.getSideboard().add(deckCardInfo);
}
}
} else {
sbMessage.append("Could not find card '").append("' at line ").append(lineCount).append(": ").append(line).append('\n');
}
} else if (line.startsWith("NAME:")) {
deckList.setName(line.substring(5, line.length()));

View file

@ -327,12 +327,19 @@ public enum CardRepository {
}
public CardInfo findPreferedCoreExpansionCard(String name, boolean caseInsensitive) {
return findPreferedCoreExpansionCard(name, caseInsensitive, null);
}
public CardInfo findPreferedCoreExpansionCard(String name, boolean caseInsensitive, String preferedSetCode) {
List<CardInfo> cards;
if (caseInsensitive) {
cards = findCardsCaseInsensitive(name);
} else {
cards = findCards(name);
}
if (!cards.isEmpty()) {
Date lastReleaseDate = null;
Date lastExpansionDate = null;
@ -340,6 +347,11 @@ public enum CardRepository {
for (CardInfo cardinfo : cards) {
ExpansionInfo set = ExpansionRepository.instance.getSetByCode(cardinfo.getSetCode());
if (set != null) {
if ((preferedSetCode != null) && (preferedSetCode.equals(set.getCode()))){
return cardinfo;
}
if ((set.getType() == SetType.EXPANSION || set.getType() == SetType.CORE)
&& (lastExpansionDate == null || set.getReleaseDate().after(lastExpansionDate))) {
cardToUse = cardinfo;