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
* cardName.
* Looks up the cardInfo corresponding to the cardName.
*
* @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
{
@ -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
* @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
{
@ -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
* entry's cardName.
* @return A list of specific printings of cards. Any failed lookups will be ignored and dropped from the resulting
* list.
* @param cardRefs The cards to look up. If providerId is empty for an entry, will default to the preferred printing for
* that entry. If providerId is given but not found, the PrintingInfo will be empty for that entry.
* @return A list of cards. Any failed lookups will be ignored and dropped from the resulting 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) {
CardInfoPtr ptr = getCard(cardRef);
if (ptr)
cardInfos.append(ptr);
ExactCard card = getCard(cardRef);
if (card)
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.
* @return A specific printing of a card, or null if not found.
* @param cardRef The card to look up.
* @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);
if (cardRef.providerId.isNull() || cardRef.providerId.isEmpty() || info.isNull()) {
return info;
}
PrintingInfo printing = findPrintingWithId(info, cardRef.providerId);
if (!printing.getSet()) {
if (info.isNull()) {
return {};
}
CardInfoPtr cardFromSpecificSet = info->clone();
cardFromSpecificSet->setPixmapCacheKey(QLatin1String("card_") + QString(info->getName()) + QString("_") +
QString(printing.getProperty("uuid")));
return cardFromSpecificSet;
if (cardRef.providerId.isEmpty() || cardRef.providerId.isNull()) {
return ExactCard(info, getPreferredPrinting(info));
}
return ExactCard(info, findPrintingWithId(info, cardRef.providerId));
}
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.
* @return A specific printing of a card, or null if not found.
* @param cardRef The card to look up.
* @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
temp = getCardBySimpleName(cardRef.name);
@ -206,7 +205,12 @@ CardInfoPtr CardDatabase::guessCard(const CardRef &cardRef) const
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)
@ -303,8 +307,6 @@ LoadStatus CardDatabase::loadCardDatabases()
// 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
refreshCachedReverseRelatedCards();
@ -321,12 +323,25 @@ LoadStatus CardDatabase::loadCardDatabases()
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) {
card->setPixmapCacheKey(QLatin1String("card_") + QString(card->getName()) + QString("_") +
QString(getPreferredPrintingProviderId(card->getName())));
return ExactCard(cardInfo, getPreferredPrinting(cardInfo));
}
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);
}
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,
const QString &setShortName,
const QString &collectorNumber) const
@ -453,25 +458,6 @@ bool CardDatabase::isPreferredPrinting(const CardRef &cardRef) const
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()
{
for (const CardInfoPtr &card : cards)

View file

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

View file

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

View file

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