Update CardInfo and CardDatabase

This commit is contained in:
RickyRister 2025-07-21 01:07:17 -07:00
parent d3a578b307
commit 3f570de815
4 changed files with 72 additions and 122 deletions

View file

@ -110,11 +110,10 @@ void CardDatabase::removeCard(CardInfoPtr card)
} }
/** /**
* Looks up the generic cardInfo (the CardInfoPtr that does not refer to a specific printing) corresponding to the * Looks up the cardInfo corresponding to the cardName.
* cardName.
* *
* @param cardName The card name to look up * @param cardName The card name to look up
* @return A generic CardInfoPtr, or null if not corresponding CardInfo is found. * @return A CardInfoPtr, or null if not corresponding CardInfo is found.
*/ */
CardInfoPtr CardDatabase::getCardInfo(const QString &cardName) const CardInfoPtr CardDatabase::getCardInfo(const QString &cardName) const
{ {
@ -122,10 +121,10 @@ CardInfoPtr CardDatabase::getCardInfo(const QString &cardName) const
} }
/** /**
* Looks up the generic cardInfos (the CardInfoPtr that does not refer to a specific printing) for a list of card names. * Looks up the cardInfos for a list of card names.
* *
* @param cardNames The card names to look up * @param cardNames The card names to look up
* @return A List of generic CardInfoPtr. Any failed lookups will be ignored and dropped from the resulting list * @return A List of CardInfoPtr. Any failed lookups will be ignored and dropped from the resulting list
*/ */
QList<CardInfoPtr> CardDatabase::getCardInfos(const QStringList &cardNames) const QList<CardInfoPtr> CardDatabase::getCardInfos(const QStringList &cardNames) const
{ {
@ -140,48 +139,46 @@ QList<CardInfoPtr> CardDatabase::getCardInfos(const QStringList &cardNames) cons
} }
/** /**
* Looks up the CardInfoPtrs corresponding to the CardRefs * Looks up the cards corresponding to the CardRefs.
* If the providerId is empty, will default to the preferred printing.
* If providerId is given but not found, the PrintingInfo will be empty.
* *
* @param cardRefs The cards to look up. If providerId is empty 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 default to the preferred printing for
* entry's cardName. * that entry. If providerId is given but not found, the PrintingInfo will be empty for that entry.
* @return A list of specific printings of cards. Any failed lookups will be ignored and dropped from the resulting * @return A list of cards. Any failed lookups will be ignored and dropped from the resulting list.
* list.
*/ */
QList<CardInfoPtr> CardDatabase::getCards(const QList<CardRef> &cardRefs) const QList<ExactCard> CardDatabase::getCards(const QList<CardRef> &cardRefs) const
{ {
QList<CardInfoPtr> cardInfos; QList<ExactCard> cards;
for (const auto &cardRef : cardRefs) { for (const auto &cardRef : cardRefs) {
CardInfoPtr ptr = getCard(cardRef); ExactCard card = getCard(cardRef);
if (ptr) if (card)
cardInfos.append(ptr); cards.append(card);
} }
return cardInfos; return cards;
} }
/** /**
* Looks up the CardInfoPtr corresponding to the CardRef * Looks up the card corresponding to the CardRef.
* If the providerId is empty, will default to the preferred printing.
* If providerId is given but not found, the PrintingInfo will be empty.
* *
* @param cardRef The card to look up. If providerId is empty, will look up the generic CardInfo for the cardName. * @param cardRef The card to look up.
* @return A specific printing of a card, or null if not found. * @return A specific printing of a card, or empty if not found.
*/ */
CardInfoPtr CardDatabase::getCard(const CardRef &cardRef) const ExactCard CardDatabase::getCard(const CardRef &cardRef) const
{ {
auto info = getCardInfo(cardRef.name); auto info = getCardInfo(cardRef.name);
if (cardRef.providerId.isNull() || cardRef.providerId.isEmpty() || info.isNull()) { if (info.isNull()) {
return info;
}
PrintingInfo printing = findPrintingWithId(info, cardRef.providerId);
if (!printing.getSet()) {
return {}; return {};
} }
CardInfoPtr cardFromSpecificSet = info->clone(); if (cardRef.providerId.isEmpty() || cardRef.providerId.isNull()) {
cardFromSpecificSet->setPixmapCacheKey(QLatin1String("card_") + QString(info->getName()) + QString("_") + return ExactCard(info, getPreferredPrinting(info));
QString(printing.getProperty("uuid"))); }
return cardFromSpecificSet;
return ExactCard(info, findPrintingWithId(info, cardRef.providerId));
} }
CardInfoPtr CardDatabase::getCardBySimpleName(const QString &cardName) const CardInfoPtr CardDatabase::getCardBySimpleName(const QString &cardName) const
@ -190,14 +187,16 @@ CardInfoPtr CardDatabase::getCardBySimpleName(const QString &cardName) const
} }
/** /**
* Looks up the CardInfoPtr by CardRef, simplifying the name if required. * Looks up the card by CardRef, simplifying the name if required.
* If the providerId is empty, will default to the preferred printing.
* If providerId is given but not found, the PrintingInfo will be empty.
* *
* @param cardRef The card to look up. If providerId is empty, will look up the generic CardInfo for the cardName. * @param cardRef The card to look up.
* @return A specific printing of a card, or null if not found. * @return A specific printing of a card, or empty if not found.
*/ */
CardInfoPtr CardDatabase::guessCard(const CardRef &cardRef) const ExactCard CardDatabase::guessCard(const CardRef &cardRef) const
{ {
CardInfoPtr temp = getCard(cardRef); CardInfoPtr temp = getCardInfo(cardRef.name);
if (temp == nullptr) { // get card by simple name instead if (temp == nullptr) { // get card by simple name instead
temp = getCardBySimpleName(cardRef.name); temp = getCardBySimpleName(cardRef.name);
@ -206,7 +205,12 @@ CardInfoPtr CardDatabase::guessCard(const CardRef &cardRef) const
temp = getCardBySimpleName(simpleCardName); temp = getCardBySimpleName(simpleCardName);
} }
} }
return temp; // returns nullptr if not found
if (cardRef.providerId.isEmpty() || cardRef.providerId.isNull()) {
return ExactCard(temp, getPreferredPrinting(temp));
}
return ExactCard(temp, findPrintingWithId(temp, cardRef.providerId));
} }
CardSetPtr CardDatabase::getSet(const QString &setName) CardSetPtr CardDatabase::getSet(const QString &setName)
@ -303,8 +307,6 @@ LoadStatus CardDatabase::loadCardDatabases()
// AFTER all the cards have been loaded // AFTER all the cards have been loaded
// Refresh the pixmap cache keys for all cards by setting them to the UUID of the preferred printing
refreshPreferredPrintings();
// resolve the reverse-related tags // resolve the reverse-related tags
refreshCachedReverseRelatedCards(); refreshCachedReverseRelatedCards();
@ -321,12 +323,25 @@ LoadStatus CardDatabase::loadCardDatabases()
return loadStatus; return loadStatus;
} }
void CardDatabase::refreshPreferredPrintings() /**
* Gets the card representing the preferred printing of the cardInfo
*
* @param cardInfo The cardInfo to find the preferred printing for
* @return A specific printing of a card
*/
ExactCard CardDatabase::getPreferredCard(const CardInfoPtr &cardInfo) const
{ {
for (const CardInfoPtr &card : cards) { return ExactCard(cardInfo, getPreferredPrinting(cardInfo));
card->setPixmapCacheKey(QLatin1String("card_") + QString(card->getName()) + QString("_") + }
QString(getPreferredPrintingProviderId(card->getName())));
PrintingInfo CardDatabase::getSpecificPrinting(const CardRef &cardRef) const
{
CardInfoPtr cardInfo = getCardInfo(cardRef.name);
if (!cardInfo) {
return PrintingInfo(nullptr);
} }
return findPrintingWithId(cardInfo, cardRef.providerId);
} }
/** /**
@ -387,16 +402,6 @@ PrintingInfo CardDatabase::getPreferredPrinting(const CardInfoPtr &cardInfo) con
return PrintingInfo(nullptr); return PrintingInfo(nullptr);
} }
PrintingInfo CardDatabase::getSpecificPrinting(const CardRef &cardRef) const
{
CardInfoPtr cardInfo = getCardInfo(cardRef.name);
if (!cardInfo) {
return PrintingInfo(nullptr);
}
return findPrintingWithId(cardInfo, cardRef.providerId);
}
PrintingInfo CardDatabase::getSpecificPrinting(const QString &cardName, PrintingInfo CardDatabase::getSpecificPrinting(const QString &cardName,
const QString &setShortName, const QString &setShortName,
const QString &collectorNumber) const const QString &collectorNumber) const
@ -453,25 +458,6 @@ bool CardDatabase::isPreferredPrinting(const CardRef &cardRef) const
return cardRef.providerId == getPreferredPrintingProviderId(cardRef.name); return cardRef.providerId == getPreferredPrintingProviderId(cardRef.name);
} }
PrintingInfo CardDatabase::getSetInfoForCard(const CardInfoPtr &_card)
{
const SetToPrintingsMap &setMap = _card->getSets();
if (setMap.empty()) {
return PrintingInfo(nullptr);
}
for (const auto &printings : setMap) {
for (const auto &cardInfoForSet : printings) {
if (QLatin1String("card_") + _card->getName() + QString("_") + cardInfoForSet.getUuid() ==
_card->getPixmapCacheKey()) {
return cardInfoForSet;
}
}
}
return PrintingInfo(nullptr);
}
void CardDatabase::refreshCachedReverseRelatedCards() void CardDatabase::refreshCachedReverseRelatedCards()
{ {
for (const CardInfoPtr &card : cards) for (const CardInfoPtr &card : cards)

View file

@ -2,7 +2,7 @@
#define CARDDATABASE_H #define CARDDATABASE_H
#include "../common/card_ref.h" #include "../common/card_ref.h"
#include "card_info.h" #include "exact_card.h"
#include <QBasicMutex> #include <QBasicMutex>
#include <QDate> #include <QDate>
@ -69,8 +69,10 @@ public:
[[nodiscard]] CardInfoPtr getCardInfo(const QString &cardName) const; [[nodiscard]] CardInfoPtr getCardInfo(const QString &cardName) const;
[[nodiscard]] QList<CardInfoPtr> getCardInfos(const QStringList &cardNames) const; [[nodiscard]] QList<CardInfoPtr> getCardInfos(const QStringList &cardNames) const;
QList<CardInfoPtr> getCards(const QList<CardRef> &cardRefs) const; QList<ExactCard> getCards(const QList<CardRef> &cardRefs) const;
[[nodiscard]] CardInfoPtr getCard(const CardRef &cardRef) const; [[nodiscard]] ExactCard getCard(const CardRef &cardRef) const;
[[nodiscard]] ExactCard getPreferredCard(const CardInfoPtr &cardInfo) const;
static PrintingInfo findPrintingWithId(const CardInfoPtr &cardInfo, const QString &providerId); static PrintingInfo findPrintingWithId(const CardInfoPtr &cardInfo, const QString &providerId);
[[nodiscard]] PrintingInfo getPreferredPrinting(const QString &cardName) const; [[nodiscard]] PrintingInfo getPreferredPrinting(const QString &cardName) const;
@ -81,7 +83,7 @@ public:
QString getPreferredPrintingProviderId(const QString &cardName) const; QString getPreferredPrintingProviderId(const QString &cardName) const;
bool isPreferredPrinting(const CardRef &cardRef) const; bool isPreferredPrinting(const CardRef &cardRef) const;
[[nodiscard]] CardInfoPtr guessCard(const CardRef &cardRef) const; [[nodiscard]] ExactCard guessCard(const CardRef &cardRef) const;
/* /*
* Get a card by its simple name. The name will be simplified in this * Get a card by its simple name. The name will be simplified in this
@ -90,7 +92,6 @@ public:
[[nodiscard]] CardInfoPtr getCardBySimpleName(const QString &cardName) const; [[nodiscard]] CardInfoPtr getCardBySimpleName(const QString &cardName) const;
CardSetPtr getSet(const QString &setName); CardSetPtr getSet(const QString &setName);
static PrintingInfo getSetInfoForCard(const CardInfoPtr &_card);
const CardNameMap &getCardList() const const CardNameMap &getCardList() const
{ {
return cards; return cards;
@ -111,7 +112,6 @@ public:
public slots: public slots:
LoadStatus loadCardDatabases(); LoadStatus loadCardDatabases();
void refreshPreferredPrintings();
void addCard(CardInfoPtr card); void addCard(CardInfoPtr card);
void addSet(CardSetPtr set); void addSet(CardSetPtr set);
protected slots: protected slots:

View file

@ -242,17 +242,11 @@ CardInfo::CardInfo(const QString &_name,
reverseRelatedCards(_reverseRelatedCards), setsToPrintings(std::move(_sets)), cipt(_cipt), reverseRelatedCards(_reverseRelatedCards), setsToPrintings(std::move(_sets)), cipt(_cipt),
landscapeOrientation(_landscapeOrientation), tableRow(_tableRow), upsideDownArt(_upsideDownArt) landscapeOrientation(_landscapeOrientation), tableRow(_tableRow), upsideDownArt(_upsideDownArt)
{ {
pixmapCacheKey = QLatin1String("card_") + name;
simpleName = CardInfo::simplifyName(name); simpleName = CardInfo::simplifyName(name);
refreshCachedSetNames(); refreshCachedSetNames();
} }
CardInfo::~CardInfo()
{
PictureLoader::clearPixmapCache(smartThis);
}
CardInfoPtr CardInfo::newInstance(const QString &_name) CardInfoPtr CardInfo::newInstance(const QString &_name)
{ {
return newInstance(_name, QString(), false, QVariantHash(), QList<CardRelation *>(), QList<CardRelation *>(), return newInstance(_name, QString(), false, QVariantHash(), QList<CardRelation *>(), QList<CardRelation *>(),

View file

@ -148,7 +148,7 @@ public:
class PrintingInfo class PrintingInfo
{ {
public: public:
explicit PrintingInfo(const CardSetPtr &_set = QSharedPointer<CardSet>(nullptr)); explicit PrintingInfo(const CardSetPtr &_set = nullptr);
~PrintingInfo() = default; ~PrintingInfo() = default;
bool operator==(const PrintingInfo &other) const bool operator==(const PrintingInfo &other) const
@ -191,8 +191,6 @@ private:
QString name; QString name;
// The name without punctuation or capitalization, for better card name recognition. // The name without punctuation or capitalization, for better card name recognition.
QString simpleName; QString simpleName;
// The key used to identify this card in the cache
QString pixmapCacheKey;
// card text // card text
QString text; QString text;
// whether this is not a "real" card but a token // whether this is not a "real" card but a token
@ -228,14 +226,13 @@ public:
int _tableRow, int _tableRow,
bool _upsideDownArt); bool _upsideDownArt);
CardInfo(const CardInfo &other) CardInfo(const CardInfo &other)
: QObject(other.parent()), name(other.name), simpleName(other.simpleName), pixmapCacheKey(other.pixmapCacheKey), : QObject(other.parent()), name(other.name), simpleName(other.simpleName), text(other.text),
text(other.text), isToken(other.isToken), properties(other.properties), relatedCards(other.relatedCards), isToken(other.isToken), properties(other.properties), relatedCards(other.relatedCards),
reverseRelatedCards(other.reverseRelatedCards), reverseRelatedCardsToMe(other.reverseRelatedCardsToMe), reverseRelatedCards(other.reverseRelatedCards), reverseRelatedCardsToMe(other.reverseRelatedCardsToMe),
setsToPrintings(other.setsToPrintings), setsNames(other.setsNames), cipt(other.cipt), setsToPrintings(other.setsToPrintings), setsNames(other.setsNames), cipt(other.cipt),
landscapeOrientation(other.landscapeOrientation), tableRow(other.tableRow), upsideDownArt(other.upsideDownArt) landscapeOrientation(other.landscapeOrientation), tableRow(other.tableRow), upsideDownArt(other.upsideDownArt)
{ {
} }
~CardInfo() override;
static CardInfoPtr newInstance(const QString &_name); static CardInfoPtr newInstance(const QString &_name);
@ -273,14 +270,6 @@ public:
{ {
return simpleName; return simpleName;
} }
void setPixmapCacheKey(QString _pixmapCacheKey)
{
pixmapCacheKey = _pixmapCacheKey;
}
const QString &getPixmapCacheKey() const
{
return pixmapCacheKey;
}
const QString &getText() const const QString &getText() const
{ {
@ -321,20 +310,6 @@ public:
{ {
return setsNames; return setsNames;
} }
QString getSetProperty(const QString &setName, const QString &propertyName) const
{
if (!setsToPrintings.contains(setName))
return "";
for (const auto &set : setsToPrintings[setName]) {
if (QLatin1String("card_") + this->getName() + QString("_") + QString(set.getProperty("uuid")) ==
this->getPixmapCacheKey()) {
return set.getProperty(propertyName);
}
}
return setsToPrintings[setName][0].getProperty(propertyName);
}
// related cards // related cards
const QList<CardRelation *> &getRelatedCards() const const QList<CardRelation *> &getRelatedCards() const
@ -397,18 +372,9 @@ public:
const QString getPowTough() const; const QString getPowTough() const;
void setPowTough(const QString &value); void setPowTough(const QString &value);
// methods using per-set properties
QString getCustomPicURL(const QString &set) const
{
return getSetProperty(set, "picurl");
}
QString getCorrectedName() const; QString getCorrectedName() const;
void addToSet(const CardSetPtr &_set, PrintingInfo _info = PrintingInfo()); void addToSet(const CardSetPtr &_set, PrintingInfo _info = PrintingInfo());
void combineLegalities(const QVariantHash &props); void combineLegalities(const QVariantHash &props);
void emitPixmapUpdated()
{
emit pixmapUpdated();
}
void refreshCachedSetNames(); void refreshCachedSetNames();
/** /**
@ -418,7 +384,11 @@ public:
static QString simplifyName(const QString &name); static QString simplifyName(const QString &name);
signals: signals:
void pixmapUpdated(); /**
* Emit this when a pixmap for this card finishes loading.
* @param printing The specific printing the pixmap is for.
*/
void pixmapUpdated(const PrintingInfo &printing);
void cardInfoChanged(CardInfoPtr card); void cardInfoChanged(CardInfoPtr card);
}; };