[DeckListModel] Refactor: Don't access underlying decklist for iteration (#6427)

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

* add docs

* extract method
This commit is contained in:
RickyRister 2025-12-20 04:25:30 -08:00 committed by GitHub
parent 715ee1d6fe
commit 367507e054
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 73 additions and 100 deletions

View file

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

View file

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

View file

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

View file

@ -76,25 +76,11 @@ void VisualDeckEditorSampleHandWidget::updateDisplay()
QList<ExactCard> VisualDeckEditorSampleHandWidget::getRandomCards(int amountToGet)
{
QList<ExactCard> mainDeckCards;
QList<ExactCard> randomCards;
if (!deckListModel)
return randomCards;
DeckList *decklist = deckListModel->getDeckList();
if (!decklist)
return randomCards;
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes({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);
}
}
}
QList<ExactCard> mainDeckCards = deckListModel->getCardsForZone(DECK_ZONE_MAIN);
if (mainDeckCards.isEmpty())
return randomCards;

View file

@ -561,10 +561,8 @@ void DeckListModel::setDeckList(DeckList *_deck)
rebuildTree();
}
QList<ExactCard> DeckListModel::getCards() const
static QList<ExactCard> cardNodesToExactCards(QList<const DecklistCardNode *> nodes)
{
auto nodes = deckList->getCardNodes();
QList<ExactCard> cards;
for (auto node : nodes) {
ExactCard card = CardDatabaseManager::query()->getCard(node->toCardRef());
@ -580,23 +578,26 @@ QList<ExactCard> DeckListModel::getCards() const
return cards;
}
QList<ExactCard> DeckListModel::getCards() const
{
auto nodes = deckList->getCardNodes();
return cardNodesToExactCards(nodes);
}
QList<ExactCard> DeckListModel::getCardsForZone(const QString &zoneName) const
{
auto nodes = deckList->getCardNodes({zoneName});
return cardNodesToExactCards(nodes);
}
QList<ExactCard> cards;
for (auto node : nodes) {
ExactCard card = CardDatabaseManager::query()->getCard(node->toCardRef());
if (card) {
for (int k = 0; k < node->getNumber(); ++k) {
cards.append(card);
}
} else {
qDebug() << "Card not found in database!";
}
}
QList<QString> DeckListModel::getCardNames() const
{
auto nodes = deckList->getCardNodes();
return cards;
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
@ -632,7 +633,6 @@ static int maxAllowedForLegality(const FormatRules &format, const QString &legal
return -1; // unknown legality → treat as illegal
}
bool DeckListModel::isCardQuantityLegalForCurrentFormat(const CardInfoPtr cardInfo, int quantity)
{
auto formatRules = CardDatabaseManager::query()->getFormat(deckList->getGameFormat());

View file

@ -309,9 +309,25 @@ public:
}
void setDeckList(DeckList *_deck);
/**
* @brief Creates a list consisting of the entries of the model mapped into ExactCards (with each entry looked up
* in the card database).
* If a card node has number > 1, it will be added that many times to the list.
* If an entry's card is not found in the card database, that entry will be left out of the list.
* @return An ordered list of ExactCards
*/
[[nodiscard]] QList<ExactCard> getCards() const;
[[nodiscard]] QList<ExactCard> getCardsForZone(const QString &zoneName) const;
/**
* @brief Gets a deduplicated list of all card names that appear in the model
*/
[[nodiscard]] QList<QString> getCardNames() const;
/**
* @brief Gets a list of all zone names that appear in the model
*/
[[nodiscard]] QList<QString> getZones() const;
bool isCardLegalForCurrentFormat(CardInfoPtr cardInfo);
bool isCardQuantityLegalForCurrentFormat(CardInfoPtr cardInfo, int quantity);
void refreshCardFormatLegalities();