[DeckListModel] Refactor: Don't access underlying decklist for iteration

This commit is contained in:
RickyRister 2025-12-16 01:57:40 -08:00
parent 64bb5355ff
commit 49a4bb1a3c
7 changed files with 52 additions and 85 deletions

View file

@ -21,32 +21,26 @@ void DeckListStatisticsAnalyzer::update()
manaCurveMap.clear(); manaCurveMap.clear();
manaDevotionMap.clear(); manaDevotionMap.clear();
auto nodes = model->getDeckList()->getCardNodes(); QList<ExactCard> cards = model->getCards();
for (auto *node : nodes) { for (const ExactCard &card : cards) {
CardInfoPtr info = CardDatabaseManager::query()->getCardInfo(node->getName()); // ---- Mana curve ----
if (!info) if (config.computeManaCurve) {
continue; manaCurveMap[card.getInfo().getCmc().toInt()]++;
}
for (int i = 0; i < node->getNumber(); ++i) { // ---- Mana base ----
// ---- Mana curve ---- if (config.computeManaBase) {
if (config.computeManaCurve) { auto mana = determineManaProduction(card.getInfo().getText());
manaCurveMap[info->getCmc().toInt()]++; for (auto it = mana.begin(); it != mana.end(); ++it)
} manaBaseMap[it.key()] += it.value();
}
// ---- Mana base ---- // ---- Devotion ----
if (config.computeManaBase) { if (config.computeDevotion) {
auto mana = determineManaProduction(info->getText()); auto devo = countManaSymbols(card.getInfo().getManaCost());
for (auto it = mana.begin(); it != mana.end(); ++it) for (auto &d : devo)
manaBaseMap[it.key()] += it.value(); manaDevotionMap[d.first] += d.second;
}
// ---- Devotion ----
if (config.computeDevotion) {
auto devo = countManaSymbols(info->getManaCost());
for (auto &d : devo)
manaDevotionMap[d.first] += d.second;
}
} }
} }

View file

@ -224,14 +224,10 @@ QMap<QString, int> DlgSelectSetForCards::getSetsForCards()
if (!model) if (!model)
return setCounts; return setCounts;
DeckList *decklist = model->getDeckList(); QList<QString> cardNames = model->getCardNames();
if (!decklist)
return setCounts;
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes(); for (auto cardName : cardNames) {
CardInfoPtr infoPtr = CardDatabaseManager::query()->getCardInfo(cardName);
for (auto currentCard : cardsInDeck) {
CardInfoPtr infoPtr = CardDatabaseManager::query()->getCardInfo(currentCard->getName());
if (!infoPtr) if (!infoPtr)
continue; continue;
@ -267,19 +263,15 @@ void DlgSelectSetForCards::updateCardLists()
} }
} }
DeckList *decklist = model->getDeckList(); QList<QString> cardNames = model->getCardNames();
if (!decklist)
return;
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes(); for (auto cardName : cardNames) {
for (auto currentCard : cardsInDeck) {
bool found = false; bool found = false;
QString foundSetName; QString foundSetName;
// Check across all sets if the card is present // Check across all sets if the card is present
for (auto it = selectedCardsBySet.begin(); it != selectedCardsBySet.end(); ++it) { for (auto it = selectedCardsBySet.begin(); it != selectedCardsBySet.end(); ++it) {
if (it.value().contains(currentCard->getName())) { if (it.value().contains(cardName)) {
found = true; found = true;
foundSetName = it.key(); // Store the set name where it was found foundSetName = it.key(); // Store the set name where it was found
break; // Stop at the first match break; // Stop at the first match
@ -288,16 +280,16 @@ void DlgSelectSetForCards::updateCardLists()
if (!found) { if (!found) {
// The card was not in any selected set // The card was not in any selected set
ExactCard card = CardDatabaseManager::query()->getCard({currentCard->getName()}); ExactCard card = CardDatabaseManager::query()->getCard({cardName});
CardInfoPictureWidget *picture_widget = new CardInfoPictureWidget(uneditedCardsFlowWidget); CardInfoPictureWidget *picture_widget = new CardInfoPictureWidget(uneditedCardsFlowWidget);
picture_widget->setCard(card); picture_widget->setCard(card);
uneditedCardsFlowWidget->addWidget(picture_widget); uneditedCardsFlowWidget->addWidget(picture_widget);
} else { } else {
ExactCard card = CardDatabaseManager::query()->getCard( ExactCard card =
{currentCard->getName(), CardDatabaseManager::getInstance() CardDatabaseManager::query()->getCard({cardName, CardDatabaseManager::getInstance()
->query() ->query()
->getSpecificPrinting(currentCard->getName(), foundSetName, "") ->getSpecificPrinting(cardName, foundSetName, "")
.getUuid()}); .getUuid()});
CardInfoPictureWidget *picture_widget = new CardInfoPictureWidget(modifiedCardsFlowWidget); CardInfoPictureWidget *picture_widget = new CardInfoPictureWidget(modifiedCardsFlowWidget);
picture_widget->setCard(card); picture_widget->setCard(card);
modifiedCardsFlowWidget->addWidget(picture_widget); modifiedCardsFlowWidget->addWidget(picture_widget);
@ -356,20 +348,16 @@ QMap<QString, QStringList> DlgSelectSetForCards::getCardsForSets()
if (!model) if (!model)
return setCards; return setCards;
DeckList *decklist = model->getDeckList(); QList<QString> cardNames = model->getCardNames();
if (!decklist)
return setCards;
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes(); for (auto cardName : cardNames) {
CardInfoPtr infoPtr = CardDatabaseManager::query()->getCardInfo(cardName);
for (auto currentCard : cardsInDeck) {
CardInfoPtr infoPtr = CardDatabaseManager::query()->getCardInfo(currentCard->getName());
if (!infoPtr) if (!infoPtr)
continue; continue;
SetToPrintingsMap setMap = infoPtr->getSets(); SetToPrintingsMap setMap = infoPtr->getSets();
for (auto it = setMap.begin(); it != setMap.end(); ++it) { for (auto it = setMap.begin(); it != setMap.end(); ++it) {
setCards[it.key()].append(currentCard->getName()); setCards[it.key()].append(cardName);
} }
} }

View file

@ -306,19 +306,12 @@ int CardAmountWidget::countCardsInZone(const QString &deckZone)
return -1; return -1;
} }
DeckList *decklist = deckModel->getDeckList(); QList<ExactCard> cards = deckModel->getCardsForZone(deckZone);
if (!decklist) {
return -1;
}
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes({deckZone});
int count = 0; int count = 0;
for (auto currentCard : cardsInDeck) { for (auto currentCard : cards) {
for (int k = 0; k < currentCard->getNumber(); ++k) { if (currentCard.getPrinting().getUuid() == rootCard.getPrinting().getProperty("uuid")) {
if (currentCard->getCardProviderId() == rootCard.getPrinting().getProperty("uuid")) { count++;
count++;
}
} }
} }

View file

@ -64,14 +64,10 @@ void VisualDatabaseDisplayNameFilterWidget::actLoadFromDeck()
if (!deckListModel) if (!deckListModel)
return; return;
DeckList *decklist = deckListModel->getDeckList();
if (!decklist)
return;
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes(); QList<QString> cardNames = deckListModel->getCardNames();
for (auto cardName : cardNames) {
for (auto currentCard : cardsInDeck) { createNameFilter(cardName);
createNameFilter(currentCard->getName());
} }
updateFilterModel(); updateFilterModel();

View file

@ -76,25 +76,11 @@ void VisualDeckEditorSampleHandWidget::updateDisplay()
QList<ExactCard> VisualDeckEditorSampleHandWidget::getRandomCards(int amountToGet) QList<ExactCard> VisualDeckEditorSampleHandWidget::getRandomCards(int amountToGet)
{ {
QList<ExactCard> mainDeckCards;
QList<ExactCard> randomCards; QList<ExactCard> randomCards;
if (!deckListModel) if (!deckListModel)
return randomCards; return randomCards;
DeckList *decklist = deckListModel->getDeckList();
if (!decklist)
return randomCards;
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes({DECK_ZONE_MAIN}); QList<ExactCard> mainDeckCards = deckListModel->getCardsForZone(DECK_ZONE_MAIN);
// Collect all cards in the main deck, allowing duplicates based on their count
for (auto currentCard : cardsInDeck) {
for (int k = 0; k < currentCard->getNumber(); ++k) {
ExactCard card = CardDatabaseManager::query()->getCard(currentCard->toCardRef());
if (card) {
mainDeckCards.append(card);
}
}
}
if (mainDeckCards.isEmpty()) if (mainDeckCards.isEmpty())
return randomCards; return randomCards;

View file

@ -599,6 +599,16 @@ QList<ExactCard> DeckListModel::getCardsForZone(const QString &zoneName) const
return cards; return cards;
} }
QList<QString> DeckListModel::getCardNames() const
{
auto nodes = deckList->getCardNodes();
QList<QString> names;
std::transform(nodes.cbegin(), nodes.cend(), std::back_inserter(names), [](auto node) { return node->getName(); });
return names;
}
QList<QString> DeckListModel::getZones() const QList<QString> DeckListModel::getZones() const
{ {
auto zoneNodes = deckList->getZoneNodes(); auto zoneNodes = deckList->getZoneNodes();
@ -632,7 +642,6 @@ int maxAllowedForLegality(const FormatRules &format, const QString &legality)
return -1; // unknown legality → treat as illegal return -1; // unknown legality → treat as illegal
} }
bool DeckListModel::isCardQuantityLegalForCurrentFormat(const CardInfoPtr cardInfo, int quantity) bool DeckListModel::isCardQuantityLegalForCurrentFormat(const CardInfoPtr cardInfo, int quantity)
{ {
auto formatRules = CardDatabaseManager::query()->getFormat(deckList->getGameFormat()); auto formatRules = CardDatabaseManager::query()->getFormat(deckList->getGameFormat());

View file

@ -311,6 +311,7 @@ public:
[[nodiscard]] QList<ExactCard> getCards() const; [[nodiscard]] QList<ExactCard> getCards() const;
[[nodiscard]] QList<ExactCard> getCardsForZone(const QString &zoneName) const; [[nodiscard]] QList<ExactCard> getCardsForZone(const QString &zoneName) const;
[[nodiscard]] QList<QString> getCardNames() const;
[[nodiscard]] QList<QString> getZones() const; [[nodiscard]] QList<QString> getZones() const;
bool isCardLegalForCurrentFormat(CardInfoPtr cardInfo); bool isCardLegalForCurrentFormat(CardInfoPtr cardInfo);
bool isCardQuantityLegalForCurrentFormat(CardInfoPtr cardInfo, int quantity); bool isCardQuantityLegalForCurrentFormat(CardInfoPtr cardInfo, int quantity);