[CardInfo] Refactor: add getLegalityProp method (#6536)

This commit is contained in:
RickyRister 2026-01-22 20:33:21 -08:00 committed by GitHub
parent 39ddaa0c35
commit 2e1a0bec93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 20 additions and 9 deletions

View file

@ -75,13 +75,18 @@ QString CardInfo::getCorrectedName() const
return result.remove(rmrx).replace(spacerx, space); return result.remove(rmrx).replace(spacerx, space);
} }
QString CardInfo::getLegalityProp(const QString &format) const
{
return getProperty("format-" + format);
}
bool CardInfo::isLegalInFormat(const QString &format) const bool CardInfo::isLegalInFormat(const QString &format) const
{ {
if (format.isEmpty()) { if (format.isEmpty()) {
return true; return true;
} }
QString formatLegality = getProperty("format-" + format); QString formatLegality = getLegalityProp(format);
return formatLegality == "legal" || formatLegality == "restricted"; return formatLegality == "legal" || formatLegality == "restricted";
} }

View file

@ -291,6 +291,14 @@ public:
*/ */
[[nodiscard]] QString getCorrectedName() const; [[nodiscard]] QString getCorrectedName() const;
/**
* @brief Gets the card's legality value for the given format.
* The legality prop for a format is stored in the property map under the key "format-<name>"
* @param format The format's name.
* @return The card's legality value for the format. Empty if not found.
*/
[[nodiscard]] QString getLegalityProp(const QString &format) const;
/** /**
* @brief Checks if the card is legal in the given format. * @brief Checks if the card is legal in the given format.
* A card is considered legal in a format if its properties map contains an entry for "format-<name>", with value * A card is considered legal in a format if its properties map contains an entry for "format-<name>", with value

View file

@ -143,13 +143,12 @@ static void setupParserRules()
search["FormatQuery"] = [](const peg::SemanticValues &sv) -> Filter { search["FormatQuery"] = [](const peg::SemanticValues &sv) -> Filter {
if (sv.choice() == 0) { if (sv.choice() == 0) {
const auto format = std::any_cast<QString>(sv[0]); const auto format = std::any_cast<QString>(sv[0]);
return return [=](const CardData &x) -> bool { return x->getLegalityProp(format) == "legal"; };
[=](const CardData &x) -> bool { return x->getProperty(QString("format-%1").arg(format)) == "legal"; };
} }
const auto format = std::any_cast<QString>(sv[1]); const auto format = std::any_cast<QString>(sv[1]);
const auto legality = std::any_cast<QString>(sv[0]); const auto legality = std::any_cast<QString>(sv[0]);
return [=](const CardData &x) -> bool { return x->getProperty(QString("format-%1").arg(format)) == legality; }; return [=](const CardData &x) -> bool { return x->getLegalityProp(format) == legality; };
}; };
search["Legality"] = [](const peg::SemanticValues &sv) -> QString { search["Legality"] = [](const peg::SemanticValues &sv) -> QString {
switch (tolower(std::string(sv.sv())[0])) { switch (tolower(std::string(sv.sv())[0])) {

View file

@ -275,7 +275,7 @@ bool FilterItem::acceptCmc(const CardInfoPtr info) const
bool FilterItem::acceptFormat(const CardInfoPtr info) const bool FilterItem::acceptFormat(const CardInfoPtr info) const
{ {
return info->getProperty(QString("format-%1").arg(term.toLower())) == "legal"; return info->getLegalityProp(term.toLower()) == "legal";
} }
bool FilterItem::acceptLoyalty(const CardInfoPtr info) const bool FilterItem::acceptLoyalty(const CardInfoPtr info) const

View file

@ -719,13 +719,12 @@ static bool isCardQuantityLegalForFormat(const QString &format, const CardInfo &
return true; return true;
} }
const QString legalityProp = "format-" + format; // check legality prop
if (!cardInfo.getProperties().contains(legalityProp)) { const QString legality = cardInfo.getLegalityProp(format);
if (legality.isEmpty()) {
return false; return false;
} }
const QString legality = cardInfo.getProperty(legalityProp);
int maxAllowed = maxAllowedForLegality(*formatRules, legality); int maxAllowed = maxAllowedForLegality(*formatRules, legality);
if (maxAllowed == -1) { if (maxAllowed == -1) {