Game: fixed that search by card name was able to return a wrong card (example: cheat command)

This commit is contained in:
Oleg Agafonov 2023-05-08 22:58:30 +04:00
parent 6144dc64b7
commit 1dea98cf1f
13 changed files with 39 additions and 52 deletions

View file

@ -291,7 +291,7 @@ public final class DeckGenerator {
for (ColoredManaSymbol c : ColoredManaSymbol.values()) {
String landName = DeckGeneratorPool.getBasicLandName(c.toString());
criteria.rarities(Rarity.LAND).nameExact(landName);
criteria.rarities(Rarity.LAND).name(landName);
List<CardInfo> cards = CardRepository.instance.findCards(criteria);
if (cards.isEmpty()) { // Workaround to get basic lands if lands are not available for the given sets
criteria.setCodes("M15");

View file

@ -154,9 +154,11 @@ public class AddLandDialog extends MageDialog {
criteria.ignoreSetsWithSnowLands();
}
if (mode == DeckEditorMode.FREE_BUILDING && expansionInfo != null && CardRepository.haveSnowLands(expansionInfo.getCode())) {
criteria.name(landName); // snow basics added only if in free mode and the chosen set has exclusively snow basics
// snow basics added only if in free mode and the chosen set has exclusively snow basics
// use contains to find snow lands too
criteria.nameContains(landName);
} else {
criteria.nameExact(landName);
criteria.name(landName);
}
criteria.rarities(Rarity.LAND);
List<CardInfo> cards = CardRepository.instance.findCards(criteria);

View file

@ -577,7 +577,7 @@ public enum WizardCardsImageSource implements CardImageSource {
private void getLandVariations(LinkedHashMap<String, String> setLinks, String cardSet, int multiverseId, String cardName) throws IOException, NumberFormatException {
CardCriteria criteria = new CardCriteria();
criteria.nameExact(cardName);
criteria.name(cardName);
criteria.setCodes(cardSet);
List<CardInfo> cards = CardRepository.instance.findCards(criteria);

View file

@ -2222,7 +2222,7 @@ public class ComputerPlayer extends PlayerImpl implements Player {
if (!landSets.isEmpty()) {
criteria.setCodes(landSets.toArray(new String[landSets.size()]));
}
criteria.rarities(Rarity.LAND).nameExact(landName);
criteria.rarities(Rarity.LAND).name(landName);
List<CardInfo> cards = CardRepository.instance.findCards(criteria);
if (cards.isEmpty()) {

View file

@ -8,6 +8,7 @@ import mage.abilities.effects.ContinuousEffect;
import mage.abilities.effects.Effect;
import mage.abilities.effects.common.InfoEffect;
import mage.cards.Card;
import mage.cards.decks.importer.CardLookup;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
@ -528,7 +529,7 @@ public final class SystemUtil {
// simple cast (without targets or modes)
// find card info
CardInfo cardInfo = CardRepository.instance.findCard(command.cardName);
CardInfo cardInfo = CardLookup.instance.lookupCardInfo(command.cardName, command.cardSet).orElse(null);
if (cardInfo == null) {
logger.warn("Unknown card for stack command [" + command.cardName + "]: " + line);
continue;
@ -579,7 +580,7 @@ public final class SystemUtil {
continue;
}
List<CardInfo> cards = null;
List<CardInfo> cards;
if (command.cardSet.isEmpty()) {
// by name
cards = CardRepository.instance.findCards(command.cardName);
@ -589,7 +590,11 @@ public final class SystemUtil {
}
if (cards.isEmpty()) {
logger.warn("Unknown card [" + command.cardName + "]: " + line);
logger.warn(String.format("Unknown card [%s%s]: %s",
command.cardSet.isEmpty() ? "" : command.cardSet + "-",
command.cardName,
line
));
continue;
}

View file

@ -314,7 +314,7 @@ public final class EldritchMoon extends ExpansionSet {
for (String name : meldCardNames) {
cardInfos.addAll(CardRepository.instance.findCards(new CardCriteria()
.setCodes(this.code)
.nameExact(name)));
.name(name)));
}
return cardInfos;
}

View file

@ -56,7 +56,7 @@ public class ConjureCardEffect extends OneShotEffect {
}
CardInfo cardInfo = CardRepository
.instance
.findCards(new CardCriteria().nameExact(cardName))
.findCards(new CardCriteria().name(cardName))
.stream()
.findFirst()
.orElse(null);

View file

@ -62,7 +62,7 @@ public class DraftFromSpellbookEffect extends OneShotEffect {
}
CardInfo cardInfo = CardRepository
.instance
.findCards(new CardCriteria().nameExact(cardName))
.findCards(new CardCriteria().name(cardName))
.stream()
.findFirst()
.orElse(null);

View file

@ -1,12 +1,12 @@
package mage.cards.decks.importer;
import java.util.List;
import java.util.Optional;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
import java.util.List;
import java.util.Optional;
public class CardLookup {
public static final CardLookup instance = new CardLookup();

View file

@ -2,10 +2,8 @@ package mage.cards.decks.importer;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -28,14 +26,7 @@ public class DraftLogImporter extends PlainTextDeckImporter {
Matcher pickMatcher = PICK_PATTERN.matcher(line);
if (pickMatcher.matches()) {
String name = pickMatcher.group(1);
List<CardInfo> cards = getCardLookup().lookupCardInfo(new CardCriteria().setCodes(currentSet).name(name));
CardInfo card = null;
if (!cards.isEmpty()) {
card = cards.get(0);
} else {
card = getCardLookup().lookupCardInfo(name).orElse(null);
}
CardInfo card = getCardLookup().lookupCardInfo(name, currentSet).orElse(null);
if (card != null) {
deckList.getCards().add(new DeckCardInfo(card.getName(), card.getCardNumber(), card.getSetCode()));
} else {

View file

@ -2,11 +2,7 @@ package mage.cards.decks.importer;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.util.RandomUtil;
import java.util.List;
/**
* @author BetaSteward_at_googlemail.com
@ -37,16 +33,9 @@ public class MWSDeckImporter extends PlainTextDeckImporter {
try {
int num = Integer.parseInt(lineNum);
CardInfo cardInfo = null;
if (!setCode.isEmpty()) {
CardCriteria criteria = new CardCriteria();
criteria.name(lineName);
criteria.setCodes(setCode);
List<CardInfo> cards = getCardLookup().lookupCardInfo(criteria);
if (!cards.isEmpty()) {
cardInfo = cards.get(RandomUtil.nextInt(cards.size()));
}
}
if (cardInfo == null) {
if (setCode.isEmpty()) {
cardInfo = getCardLookup().lookupCardInfo(lineName, setCode).orElse(null);
} else {
cardInfo = getCardLookup().lookupCardInfo(lineName).orElse(null);
}

View file

@ -18,8 +18,8 @@ import java.util.List;
*/
public class CardCriteria {
private String nameContains;
private String name;
private String nameExact;
private String rules;
private final List<String> setCodes;
private final List<String> ignoreSetCodes; // sets to ignore, use with little amount of sets (example: ignore sets with snow lands)
@ -119,13 +119,13 @@ public class CardCriteria {
return this;
}
public CardCriteria name(String name) {
this.name = name;
public CardCriteria nameContains(String str) {
this.nameContains = str;
return this;
}
public CardCriteria nameExact(String nameExact) {
this.nameExact = nameExact;
public CardCriteria name(String name) {
this.name = name;
return this;
}
@ -220,12 +220,12 @@ public class CardCriteria {
where.eq("nightCard", nightCard);
where.eq("splitCardHalf", false);
int clausesCount = 2;
if (name != null) {
where.like("name", new SelectArg('%' + name + '%'));
if (nameContains != null) {
where.like("name", new SelectArg('%' + nameContains + '%'));
clausesCount++;
}
if (nameExact != null) {
where.like("name", new SelectArg(nameExact));
if (name != null) {
where.eq("name", new SelectArg(name));
clausesCount++;
}
if (rules != null) {
@ -397,12 +397,12 @@ public class CardCriteria {
return this;
}
public String getName() {
return name;
public String getNameContains() {
return nameContains;
}
public String getNameExact() {
return nameExact;
public String getName() {
return name;
}
public String getRules() {

View file

@ -68,7 +68,7 @@ public final class TournamentUtil {
} else {
criteria.ignoreSetsWithSnowLands();
}
criteria.rarities(Rarity.LAND).nameExact(landName);
criteria.rarities(Rarity.LAND).name(landName);
List<CardInfo> lands = CardRepository.instance.findCards(criteria);
List<Card> cards = new ArrayList<>();
if (!lands.isEmpty()) {