From bfeb3a7ca964acf9fa466cd2598969676d292bd8 Mon Sep 17 00:00:00 2001 From: RickyRister <42636155+RickyRister@users.noreply.github.com> Date: Fri, 23 Jan 2026 01:05:25 -0800 Subject: [PATCH] [DeckListModel] Refactor to use forEachCard in legality check (#6550) --- .../models/deck_list/deck_list_model.cpp | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.cpp b/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.cpp index 8459f306c..9ccb0b1ad 100644 --- a/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.cpp +++ b/libcockatrice_models/libcockatrice/models/deck_list/deck_list_model.cpp @@ -738,29 +738,26 @@ static bool isCardQuantityLegalForFormat(const QString &format, const CardInfo & return quantity <= maxAllowed; } +static bool isCardNodeLegalForFormat(const QString &format, const InnerDecklistNode *zone, const DecklistCardNode *card) +{ + Q_UNUSED(zone) + + // unknown cards are not legal + ExactCard exactCard = CardDatabaseManager::query()->getCard(card->toCardRef()); + if (!exactCard) { + return false; + } + + // actual check + return isCardQuantityLegalForFormat(format, exactCard.getInfo(), card->getNumber()); +} + void DeckListModel::refreshCardFormatLegalities() { - InnerDecklistNode *listRoot = deckList->getTree()->getRoot(); + QString format = deckList->getGameFormat(); - for (int i = 0; i < listRoot->size(); i++) { - auto *currentZone = static_cast(listRoot->at(i)); - for (int j = 0; j < currentZone->size(); j++) { - auto *currentCard = static_cast(currentZone->at(j)); - - // TODO: better sanity checking - if (currentCard == nullptr) { - continue; - } - - ExactCard exactCard = CardDatabaseManager::query()->getCard(currentCard->toCardRef()); - if (!exactCard) { - continue; - } - - QString format = deckList->getGameFormat(); - bool legal = isCardQuantityLegalForFormat(format, exactCard.getInfo(), currentCard->getNumber()); - - currentCard->setFormatLegality(legal); - } - } + deckList->forEachCard([&format](const InnerDecklistNode *zone, DecklistCardNode *card) { + bool legal = isCardNodeLegalForFormat(format, zone, card); + card->setFormatLegality(legal); + }); }