From 70b4843bc4fb8ad86beda2f60c3172c92eb99609 Mon Sep 17 00:00:00 2001 From: RickyRister <42636155+RickyRister@users.noreply.github.com> Date: Wed, 16 Jul 2025 03:33:38 -0700 Subject: [PATCH] Refactor: clean up CardDatabase pt2 (#6042) * findPrintingWithId * remove a param * cleanup up usage of getCardInfo --- ...ponse_commander_details_display_widget.cpp | 2 +- .../widgets/cards/card_info_frame_widget.cpp | 5 +- .../ui/widgets/cards/card_info_frame_widget.h | 2 +- .../src/dialogs/dlg_select_set_for_cards.cpp | 4 +- cockatrice/src/game/cards/card_database.cpp | 64 ++++++++++--------- cockatrice/src/game/cards/card_database.h | 1 + 6 files changed, 39 insertions(+), 39 deletions(-) diff --git a/cockatrice/src/client/tabs/api/edhrec/display/commander/edhrec_api_response_commander_details_display_widget.cpp b/cockatrice/src/client/tabs/api/edhrec/display/commander/edhrec_api_response_commander_details_display_widget.cpp index 6dbb25ac6..0d37dad69 100644 --- a/cockatrice/src/client/tabs/api/edhrec/display/commander/edhrec_api_response_commander_details_display_widget.cpp +++ b/cockatrice/src/client/tabs/api/edhrec/display/commander/edhrec_api_response_commander_details_display_widget.cpp @@ -15,7 +15,7 @@ EdhrecCommanderResponseCommanderDetailsDisplayWidget::EdhrecCommanderResponseCom setLayout(layout); commanderPicture = new CardInfoPictureWidget(this); - commanderPicture->setCard(CardDatabaseManager::getInstance()->getCardInfo(commanderDetails.getName())); + commanderPicture->setCard(CardDatabaseManager::getInstance()->getCard({commanderDetails.getName()})); QWidget *currentParent = parentWidget(); TabEdhRecMain *parentTab = nullptr; diff --git a/cockatrice/src/client/ui/widgets/cards/card_info_frame_widget.cpp b/cockatrice/src/client/ui/widgets/cards/card_info_frame_widget.cpp index 23a6fda46..f72f7a7f5 100644 --- a/cockatrice/src/client/ui/widgets/cards/card_info_frame_widget.cpp +++ b/cockatrice/src/client/ui/widgets/cards/card_info_frame_widget.cpp @@ -11,7 +11,7 @@ #include #include -CardInfoFrameWidget::CardInfoFrameWidget(const QString &cardName, QWidget *parent) +CardInfoFrameWidget::CardInfoFrameWidget(QWidget *parent) : QTabWidget(parent), info(nullptr), viewTransformationButton(nullptr), cardTextOnly(false) { setContentsMargins(3, 3, 3, 3); @@ -60,9 +60,6 @@ CardInfoFrameWidget::CardInfoFrameWidget(const QString &cardName, QWidget *paren tab3->setLayout(tab3Layout); setViewMode(SettingsCache::instance().getCardInfoViewMode()); - - // TODO: Change this to be by UUID - setCard(CardDatabaseManager::getInstance()->getCardInfo(cardName)); } void CardInfoFrameWidget::retranslateUi() diff --git a/cockatrice/src/client/ui/widgets/cards/card_info_frame_widget.h b/cockatrice/src/client/ui/widgets/cards/card_info_frame_widget.h index 6c521aff4..e12cab33c 100644 --- a/cockatrice/src/client/ui/widgets/cards/card_info_frame_widget.h +++ b/cockatrice/src/client/ui/widgets/cards/card_info_frame_widget.h @@ -37,7 +37,7 @@ public: ImageAndTextView }; - explicit CardInfoFrameWidget(const QString &cardName = QString(), QWidget *parent = nullptr); + explicit CardInfoFrameWidget(QWidget *parent = nullptr); CardInfoPtr getInfo() { return info; diff --git a/cockatrice/src/dialogs/dlg_select_set_for_cards.cpp b/cockatrice/src/dialogs/dlg_select_set_for_cards.cpp index 036994c05..3d0780906 100644 --- a/cockatrice/src/dialogs/dlg_select_set_for_cards.cpp +++ b/cockatrice/src/dialogs/dlg_select_set_for_cards.cpp @@ -227,8 +227,8 @@ QMap DlgSelectSetForCards::getSetsForCards() continue; SetToPrintingsMap setMap = infoPtr->getSets(); - for (auto it = setMap.begin(); it != setMap.end(); ++it) { - setCounts[it.key()]++; + for (auto &setName : setMap.keys()) { + setCounts[setName]++; } } } diff --git a/cockatrice/src/game/cards/card_database.cpp b/cockatrice/src/game/cards/card_database.cpp index cb0827c8b..8353e335a 100644 --- a/cockatrice/src/game/cards/card_database.cpp +++ b/cockatrice/src/game/cards/card_database.cpp @@ -142,7 +142,7 @@ QList CardDatabase::getCardInfos(const QStringList &cardNames) cons /** * Looks up the CardInfoPtrs corresponding to the CardRefs * - * @param cardRefs The cards to look up. If providerId is null for an entry, will look up the generic CardInfo for that + * @param cardRefs The cards to look up. If providerId is empty for an entry, will look up the generic CardInfo for that * entry's cardName. * @return A list of specific printings of cards. Any failed lookups will be ignored and dropped from the resulting * list. @@ -162,7 +162,7 @@ QList CardDatabase::getCards(const QList &cardRefs) const /** * Looks up the CardInfoPtr corresponding to the CardRef * - * @param cardRef The card to look up. If providerId is null, will look up the generic CardInfo for the cardName. + * @param cardRef The card to look up. If providerId is empty, will look up the generic CardInfo for the cardName. * @return A specific printing of a card, or null if not found. */ CardInfoPtr CardDatabase::getCard(const CardRef &cardRef) const @@ -172,17 +172,16 @@ CardInfoPtr CardDatabase::getCard(const CardRef &cardRef) const return info; } - for (const auto &printings : info->getSets()) { - for (const auto &printing : printings) { - if (printing.getProperty("uuid") == cardRef.providerId) { - CardInfoPtr cardFromSpecificSet = info->clone(); - cardFromSpecificSet->setPixmapCacheKey(QLatin1String("card_") + QString(info->getName()) + - QString("_") + QString(printing.getProperty("uuid"))); - return cardFromSpecificSet; - } - } + PrintingInfo printing = findPrintingWithId(info, cardRef.providerId); + + if (!printing.getSet()) { + return {}; } - return {}; + + CardInfoPtr cardFromSpecificSet = info->clone(); + cardFromSpecificSet->setPixmapCacheKey(QLatin1String("card_") + QString(info->getName()) + QString("_") + + QString(printing.getProperty("uuid"))); + return cardFromSpecificSet; } CardInfoPtr CardDatabase::getCardBySimpleName(const QString &cardName) const @@ -193,7 +192,7 @@ CardInfoPtr CardDatabase::getCardBySimpleName(const QString &cardName) const /** * Looks up the CardInfoPtr by CardRef, simplifying the name if required. * - * @param cardRef The card to look up. If providerId is null, will look up the generic CardInfo for the cardName. + * @param cardRef The card to look up. If providerId is empty, will look up the generic CardInfo for the cardName. * @return A specific printing of a card, or null if not found. */ CardInfoPtr CardDatabase::guessCard(const CardRef &cardRef) const @@ -330,6 +329,26 @@ void CardDatabase::refreshPreferredPrintings() } } +/** + * Finds the PrintingInfo in the cardInfo that has the given uuid field. + * + * @param cardInfo The CardInfo to search + * @param providerId The uuid to look for + * @return The PrintingInfo, or a default-constructed PrintingInfo if not found. + */ +PrintingInfo CardDatabase::findPrintingWithId(const CardInfoPtr &cardInfo, const QString &providerId) +{ + for (const auto &printings : cardInfo->getSets()) { + for (const auto &printing : printings) { + if (printing.getProperty("uuid") == providerId) { + return printing; + } + } + } + + return PrintingInfo(); +} + PrintingInfo CardDatabase::getPreferredPrinting(const QString &cardName) const { CardInfoPtr cardInfo = getCardInfo(cardName); @@ -375,24 +394,7 @@ PrintingInfo CardDatabase::getSpecificPrinting(const CardRef &cardRef) const return PrintingInfo(nullptr); } - SetToPrintingsMap setMap = cardInfo->getSets(); - if (setMap.empty()) { - return PrintingInfo(nullptr); - } - - for (const auto &printings : setMap) { - for (auto &cardInfoForSet : printings) { - if (cardInfoForSet.getProperty("uuid") == cardRef.providerId) { - return cardInfoForSet; - } - } - } - - if (cardRef.providerId.isNull()) { - return getPreferredPrinting(cardRef.name); - } - - return PrintingInfo(nullptr); + return findPrintingWithId(cardInfo, cardRef.providerId); } PrintingInfo CardDatabase::getSpecificPrinting(const QString &cardName, diff --git a/cockatrice/src/game/cards/card_database.h b/cockatrice/src/game/cards/card_database.h index 68624a740..975cda4bb 100644 --- a/cockatrice/src/game/cards/card_database.h +++ b/cockatrice/src/game/cards/card_database.h @@ -72,6 +72,7 @@ public: QList getCards(const QList &cardRefs) const; [[nodiscard]] CardInfoPtr getCard(const CardRef &cardRef) const; + static PrintingInfo findPrintingWithId(const CardInfoPtr &cardInfo, const QString &providerId); [[nodiscard]] PrintingInfo getPreferredPrinting(const QString &cardName) const; [[nodiscard]] PrintingInfo getPreferredPrinting(const CardInfoPtr &cardInfo) const; [[nodiscard]] PrintingInfo getSpecificPrinting(const CardRef &cardRef) const;