move format check code

This commit is contained in:
RickyRister 2025-12-30 22:12:55 -08:00
parent ae137832b0
commit 1113eb4abe
4 changed files with 35 additions and 25 deletions

View file

@ -75,6 +75,16 @@ QString CardInfo::getCorrectedName() const
return result.remove(rmrx).replace(spacerx, space); return result.remove(rmrx).replace(spacerx, space);
} }
bool CardInfo::isLegalInFormat(const QString &format) const
{
if (format.isEmpty()) {
return true;
}
QString formatLegality = getProperty("format-" + format);
return formatLegality == "legal" || formatLegality == "restricted";
}
void CardInfo::addToSet(const CardSetPtr &_set, const PrintingInfo _info) void CardInfo::addToSet(const CardSetPtr &_set, const PrintingInfo _info)
{ {
if (!_set->contains(smartThis)) { if (!_set->contains(smartThis)) {

View file

@ -291,6 +291,15 @@ public:
*/ */
[[nodiscard]] QString getCorrectedName() const; [[nodiscard]] QString getCorrectedName() const;
/**
* @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
* "legal" or "restricted".
* @param format The format's name. If empty, will always return true.
* @return Whether the card is legal in the given format.
*/
[[nodiscard]] bool isLegalInFormat(const QString &format) const;
/** /**
* @brief Adds a printing to a specific set. * @brief Adds a printing to a specific set.
* *

View file

@ -420,7 +420,7 @@ QModelIndex DeckListModel::addCard(const ExactCard &card, const QString &zoneNam
auto *decklistCard = auto *decklistCard =
deckList->addCard(cardInfo->getName(), zoneName, insertRow, cardSetName, printingInfo.getProperty("num"), deckList->addCard(cardInfo->getName(), zoneName, insertRow, cardSetName, printingInfo.getProperty("num"),
printingInfo.getProperty("uuid"), isCardLegalForCurrentFormat(cardInfo)); printingInfo.getProperty("uuid"), cardInfo->isLegalInFormat(deckList->getGameFormat()));
beginInsertRows(parentIndex, insertRow, insertRow); beginInsertRows(parentIndex, insertRow, insertRow);
cardNode = new DecklistModelCardNode(decklistCard, groupNode, insertRow); cardNode = new DecklistModelCardNode(decklistCard, groupNode, insertRow);
@ -661,18 +661,6 @@ QList<QString> DeckListModel::getZones() const
return zones; return zones;
} }
bool DeckListModel::isCardLegalForCurrentFormat(const CardInfoPtr cardInfo)
{
if (!deckList->getGameFormat().isEmpty()) {
if (cardInfo->getProperties().contains("format-" + deckList->getGameFormat())) {
QString formatLegality = cardInfo->getProperty("format-" + deckList->getGameFormat());
return formatLegality == "legal" || formatLegality == "restricted";
}
return false;
}
return true;
}
static int maxAllowedForLegality(const FormatRules &format, const QString &legality) static int maxAllowedForLegality(const FormatRules &format, const QString &legality)
{ {
for (const AllowedCount &c : format.allowedCounts) { for (const AllowedCount &c : format.allowedCounts) {
@ -683,25 +671,29 @@ static int maxAllowedForLegality(const FormatRules &format, const QString &legal
return -1; // unknown legality → treat as illegal return -1; // unknown legality → treat as illegal
} }
bool DeckListModel::isCardQuantityLegalForCurrentFormat(const CardInfoPtr cardInfo, int quantity) static bool isCardQuantityLegalForFormat(const QString &format, const CardInfo &cardInfo, int quantity)
{ {
auto formatRules = CardDatabaseManager::query()->getFormat(deckList->getGameFormat()); if (format.isEmpty()) {
return true;
}
auto formatRules = CardDatabaseManager::query()->getFormat(format);
if (!formatRules) { if (!formatRules) {
return true; return true;
} }
// Exceptions always win // Exceptions always win
if (cardHasAnyException(*cardInfo, *formatRules)) { if (cardHasAnyException(cardInfo, *formatRules)) {
return true; return true;
} }
const QString legalityProp = "format-" + deckList->getGameFormat(); const QString legalityProp = "format-" + format;
if (!cardInfo->getProperties().contains(legalityProp)) { if (!cardInfo.getProperties().contains(legalityProp)) {
return false; return false;
} }
const QString legality = cardInfo->getProperty(legalityProp); const QString legality = cardInfo.getProperty(legalityProp);
int maxAllowed = maxAllowedForLegality(*formatRules, legality); int maxAllowed = maxAllowedForLegality(*formatRules, legality);
@ -735,10 +727,11 @@ void DeckListModel::refreshCardFormatLegalities()
continue; continue;
} }
bool legal = isCardLegalForCurrentFormat(exactCard.getCardPtr()); QString format = deckList->getGameFormat();
bool legal = exactCard.getInfo().isLegalInFormat(format);
if (legal) { if (legal) {
legal = isCardQuantityLegalForCurrentFormat(exactCard.getCardPtr(), currentCard->getNumber()); legal = isCardQuantityLegalForFormat(format, exactCard.getInfo(), currentCard->getNumber());
} }
currentCard->setFormatLegality(legal); currentCard->setFormatLegality(legal);

View file

@ -358,10 +358,6 @@ public:
*/ */
[[nodiscard]] QList<QString> getZones() const; [[nodiscard]] QList<QString> getZones() const;
bool isCardLegalForCurrentFormat(CardInfoPtr cardInfo);
bool isCardQuantityLegalForCurrentFormat(CardInfoPtr cardInfo, int quantity);
void refreshCardFormatLegalities();
/** /**
* @brief Sets the criteria used to group cards in the model. * @brief Sets the criteria used to group cards in the model.
* @param newCriteria The new grouping criteria. * @param newCriteria The new grouping criteria.
@ -403,6 +399,8 @@ private:
return dynamic_cast<T>(root); return dynamic_cast<T>(root);
return dynamic_cast<T>(static_cast<AbstractDecklistNode *>(index.internalPointer())); return dynamic_cast<T>(static_cast<AbstractDecklistNode *>(index.internalPointer()));
} }
void refreshCardFormatLegalities();
}; };
#endif #endif