DeckList now tries to assign reasonable defaults for UUID and collectorNumber if none are found in loaded DeckLists.

Rename overloaded DeckListModel findChild() function to findCardChildByNameAndUUID() for clarity.
This commit is contained in:
Lukas Brübach 2024-11-13 13:52:35 +01:00
parent 963ff64cb6
commit edbc57edc7
3 changed files with 43 additions and 11 deletions

View file

@ -324,7 +324,7 @@ DecklistModelCardNode *DeckListModel::findCardNode(const QString &cardName, cons
if (uuid.isEmpty()) {
return dynamic_cast<DecklistModelCardNode *>(typeNode->findChild(cardName));
}
return dynamic_cast<DecklistModelCardNode *>(typeNode->findChild(cardName, uuid));
return dynamic_cast<DecklistModelCardNode *>(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<DecklistModelCardNode *>(cardTypeNode->findChild(cardName, cardInfoSet.getProperty("uuid")));
auto *cardNode = dynamic_cast<DecklistModelCardNode *>(
cardTypeNode->findCardChildByNameAndUUID(cardName, cardInfoSet.getProperty("uuid")));
if (!cardNode) {
auto *decklistCard = deckList->addCard(
cardInfo->getName(), zoneName, cardInfoSet.getProperty("uuid"), cardInfoSet.getProperty("num"));

View file

@ -1,5 +1,7 @@
#include "decklist.h"
#include "../cockatrice/src/game/cards/card_database_manager.h"
#include <QCryptographicHash>
#include <QDebug>
#include <QFile>
@ -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<InnerDecklistNode *>(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"))

View file

@ -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;