diff --git a/cockatrice/src/deck/deck_list_model.cpp b/cockatrice/src/deck/deck_list_model.cpp index a70c36532..3658c9800 100644 --- a/cockatrice/src/deck/deck_list_model.cpp +++ b/cockatrice/src/deck/deck_list_model.cpp @@ -324,7 +324,7 @@ DecklistModelCardNode *DeckListModel::findCardNode(const QString &cardName, cons if (uuid.isEmpty()) { return dynamic_cast(typeNode->findChild(cardName)); } - return dynamic_cast(typeNode->findChild(cardName, uuid)); + return dynamic_cast(typeNode->findCardChildByNameAndUUID(cardName, uuid)); } QModelIndex DeckListModel::findCard(const QString &cardName, const QString &zoneName, const QString &uuid) const @@ -366,7 +366,8 @@ QModelIndex DeckListModel::addCard(const QString &cardName, const CardInfoPerSet InnerDecklistNode *cardTypeNode = createNodeIfNeeded(cardType, zoneNode); const QModelIndex parentIndex = nodeToIndex(cardTypeNode); - auto *cardNode = dynamic_cast(cardTypeNode->findChild(cardName, cardInfoSet.getProperty("uuid"))); + auto *cardNode = dynamic_cast( + cardTypeNode->findCardChildByNameAndUUID(cardName, cardInfoSet.getProperty("uuid"))); if (!cardNode) { auto *decklistCard = deckList->addCard( cardInfo->getName(), zoneName, cardInfoSet.getProperty("uuid"), cardInfoSet.getProperty("num")); diff --git a/common/decklist.cpp b/common/decklist.cpp index 09f952152..baddbf0f4 100644 --- a/common/decklist.cpp +++ b/common/decklist.cpp @@ -1,5 +1,7 @@ #include "decklist.h" +#include "../cockatrice/src/game/cards/card_database_manager.h" + #include #include #include @@ -88,8 +90,8 @@ int AbstractDecklistNode::depth() const } InnerDecklistNode::InnerDecklistNode(InnerDecklistNode *other, InnerDecklistNode *_parent) -: AbstractDecklistNode(_parent), name(other->getName()), cardSetCode(other->getCardUuid()), - cardCollectorNumber(other->getCardCollectorNumber()) + : AbstractDecklistNode(_parent), name(other->getName()), cardSetCode(other->getCardUuid()), + cardCollectorNumber(other->getCardCollectorNumber()) { for (int i = 0; i < other->size(); ++i) { auto *inner = dynamic_cast(other->at(i)); @@ -140,8 +142,8 @@ void InnerDecklistNode::clearTree() } DecklistCardNode::DecklistCardNode(DecklistCardNode *other, InnerDecklistNode *_parent) -: AbstractDecklistCardNode(_parent), name(other->getName()), number(other->getNumber()), - cardSetName(other->getCardUuid()), cardSetNumber(other->getCardCollectorNumber()) + : AbstractDecklistCardNode(_parent), name(other->getName()), number(other->getNumber()), + cardSetName(other->getCardUuid()), cardSetNumber(other->getCardCollectorNumber()) { } @@ -155,7 +157,7 @@ AbstractDecklistNode *InnerDecklistNode::findChild(const QString &_name) return nullptr; } -AbstractDecklistNode *InnerDecklistNode::findChild(const QString &_name, const QString &_uuid) +AbstractDecklistNode *InnerDecklistNode::findCardChildByNameAndUUID(const QString &_name, const QString &_uuid) { for (int i = 0; i < size(); i++) { if (at(i)->getName() == _name && at(i)->getCardUuid() == _uuid) { @@ -280,10 +282,39 @@ bool InnerDecklistNode::readElement(QXmlStreamReader *xml) InnerDecklistNode *newZone = new InnerDecklistNode(xml->attributes().value("name").toString(), this); newZone->readElement(xml); } else if (childName == "card") { + QString uuid = xml->attributes().value("uuid").toString(); + QString collectorNumber = xml->attributes().value("collectorNumber").toString(); + if (uuid.isEmpty() || collectorNumber.isEmpty()) { + CardInfoPerSet preferredSetForCard = CardDatabaseManager::getInstance()->getPreferredSetForCard( + xml->attributes().value("name").toString()); + + // If we don't have a UUID and the preferred set has one, we use that as a default. + if (!preferredSetForCard.getProperty("uuid").isEmpty() && uuid.isEmpty()) { + uuid = preferredSetForCard.getProperty("uuid"); + } + + // If we don't have a collectorsNumber, the preferred Set has one, AND we've already set the UUID + // to the preferred Set, we use the one from the preferred set. + if (!preferredSetForCard.getProperty("num").isEmpty() && collectorNumber.isEmpty() && + preferredSetForCard.getProperty("uuid") == uuid) { + collectorNumber = preferredSetForCard.getProperty("num"); + + // Otherwise check which Set has the UUID we've set or reused and use the collectorNumber from + // there. + } else if (!preferredSetForCard.getProperty("num").isEmpty() && collectorNumber.isEmpty()) { + CardInfoPtr card_by_name_and_uuid = CardDatabaseManager::getInstance()->getCardByNameAndUUID( + xml->attributes().value("name").toString(), uuid); + for (auto &set : card_by_name_and_uuid->getSets()) { + if (set.getProperty("uuid") == uuid) { + collectorNumber = set.getProperty("num"); + } + } + } + } + DecklistCardNode *newCard = new DecklistCardNode(xml->attributes().value("name").toString(), - xml->attributes().value("number").toString().toInt(), - this, xml->attributes().value("uuid").toString(), - xml->attributes().value("collectorNumber").toString()); + xml->attributes().value("number").toString().toInt(), + this, uuid, collectorNumber); newCard->readElement(xml); } } else if (xml->isEndElement() && (childName == "zone")) diff --git a/common/decklist.h b/common/decklist.h index 923c2e258..829fe3cab 100644 --- a/common/decklist.h +++ b/common/decklist.h @@ -128,7 +128,7 @@ public: void clearTree(); AbstractDecklistNode *findChild(const QString &_name); - AbstractDecklistNode *findChild(const QString &_name, const QString &_uuid); + AbstractDecklistNode *findCardChildByNameAndUUID(const QString &_name, const QString &_uuid); int height() const override; int recursiveCount(bool countTotalCards = false) const; bool compare(AbstractDecklistNode *other) const override;