diff --git a/cockatrice/CMakeLists.txt b/cockatrice/CMakeLists.txt index f531bc2c8..dec1b3d62 100644 --- a/cockatrice/CMakeLists.txt +++ b/cockatrice/CMakeLists.txt @@ -19,6 +19,7 @@ set(cockatrice_SOURCES src/client/settings/card_counter_settings.cpp src/client/settings/shortcut_treeview.cpp src/client/settings/shortcuts_settings.cpp + src/interface/deck_loader/card_node_function.cpp src/interface/deck_loader/deck_file_format.cpp src/interface/deck_loader/deck_loader.cpp src/interface/deck_loader/loaded_deck.cpp diff --git a/cockatrice/src/interface/deck_loader/card_node_function.cpp b/cockatrice/src/interface/deck_loader/card_node_function.cpp new file mode 100644 index 000000000..9441fe428 --- /dev/null +++ b/cockatrice/src/interface/deck_loader/card_node_function.cpp @@ -0,0 +1,40 @@ +#include "card_node_function.h" + +#include +#include +#include + +void CardNodeFunction::SetProviderIdToPreferred::operator()(const InnerDecklistNode *node, DecklistCardNode *card) const +{ + Q_UNUSED(node); + PrintingInfo preferredPrinting = CardDatabaseManager::query()->getPreferredPrinting(card->getName()); + QString providerId = preferredPrinting.getUuid(); + QString setShortName = preferredPrinting.getSet()->getShortName(); + QString collectorNumber = preferredPrinting.getProperty("num"); + + card->setCardProviderId(providerId); + card->setCardCollectorNumber(collectorNumber); + card->setCardSetShortName(setShortName); +} + +void CardNodeFunction::ClearPrintingData::operator()(const InnerDecklistNode *node, DecklistCardNode *card) const +{ + Q_UNUSED(node); + card->setCardSetShortName(nullptr); + card->setCardCollectorNumber(nullptr); + card->setCardProviderId(nullptr); +} + +void CardNodeFunction::ResolveProviderId::operator()(const InnerDecklistNode *node, DecklistCardNode *card) const +{ + Q_UNUSED(node); + // Retrieve the providerId based on setName and collectorNumber + QString providerId = + CardDatabaseManager::getInstance() + ->query() + ->getSpecificPrinting(card->getName(), card->getCardSetShortName(), card->getCardCollectorNumber()) + .getUuid(); + + // Set the providerId on the card + card->setCardProviderId(providerId); +} \ No newline at end of file diff --git a/cockatrice/src/interface/deck_loader/card_node_function.h b/cockatrice/src/interface/deck_loader/card_node_function.h new file mode 100644 index 000000000..398dc0c0e --- /dev/null +++ b/cockatrice/src/interface/deck_loader/card_node_function.h @@ -0,0 +1,39 @@ +#ifndef COCKATRICE_DECK_FUNCTION_H +#define COCKATRICE_DECK_FUNCTION_H + +class DecklistCardNode; +class InnerDecklistNode; + +/** + * Functions to be used with DeckList::forEachCard + */ +namespace CardNodeFunction +{ + +/** + * @brief Sets the providerId of the card to the preferred printing. + */ +struct SetProviderIdToPreferred +{ + void operator()(const InnerDecklistNode *node, DecklistCardNode *card) const; +}; + +/** + * @brief Clears all fields on the card related to the printing + */ +struct ClearPrintingData +{ + void operator()(const InnerDecklistNode *node, DecklistCardNode *card) const; +}; + +/** + * @brief Sets the providerId of the card based on its set name and collector number. + */ +struct ResolveProviderId +{ + void operator()(const InnerDecklistNode *node, DecklistCardNode *card) const; +}; + +} // namespace CardNodeFunction + +#endif // COCKATRICE_DECK_FUNCTION_H diff --git a/cockatrice/src/interface/deck_loader/deck_loader.cpp b/cockatrice/src/interface/deck_loader/deck_loader.cpp index e3d1ce965..d866b684b 100644 --- a/cockatrice/src/interface/deck_loader/deck_loader.cpp +++ b/cockatrice/src/interface/deck_loader/deck_loader.cpp @@ -316,104 +316,6 @@ QString DeckLoader::exportDeckToDecklist(const DeckList *deckList, DecklistWebsi return deckString; } -// This struct is here to support the forEachCard function call, defined in decklist. -// It requires a function to be called for each card, and it will set the providerId to the preferred printing. -struct SetProviderIdToPreferred -{ - // Main operator for struct, allowing the foreachcard to work. - SetProviderIdToPreferred() - { - } - - void operator()(const InnerDecklistNode *node, DecklistCardNode *card) const - { - Q_UNUSED(node); - PrintingInfo preferredPrinting = CardDatabaseManager::query()->getPreferredPrinting(card->getName()); - QString providerId = preferredPrinting.getUuid(); - QString setShortName = preferredPrinting.getSet()->getShortName(); - QString collectorNumber = preferredPrinting.getProperty("num"); - - card->setCardProviderId(providerId); - card->setCardCollectorNumber(collectorNumber); - card->setCardSetShortName(setShortName); - } -}; - -/** - * This function iterates through each card in the decklist and sets the providerId - * on each card based on its set name and collector number. - * - * @param deckList The decklist to modify - */ -void DeckLoader::setProviderIdToPreferredPrinting(const DeckList *deckList) -{ - // Set up the struct to call. - SetProviderIdToPreferred setProviderIdToPreferred; - - // Call the forEachCard method for each card in the deck - deckList->forEachCard(setProviderIdToPreferred); -} - -/** - * Sets the providerId on each card in the decklist based on its set name and collector number. - * - * @param deckList The decklist to modify - */ -void DeckLoader::resolveSetNameAndNumberToProviderID(const DeckList *deckList) -{ - auto setProviderId = [](const auto node, const auto card) { - Q_UNUSED(node); - // Retrieve the providerId based on setName and collectorNumber - QString providerId = - CardDatabaseManager::getInstance() - ->query() - ->getSpecificPrinting(card->getName(), card->getCardSetShortName(), card->getCardCollectorNumber()) - .getUuid(); - - // Set the providerId on the card - card->setCardProviderId(providerId); - }; - - deckList->forEachCard(setProviderId); -} - -// This struct is here to support the forEachCard function call, defined in decklist. -// It requires a function to be called for each card, and it will set the providerId. -struct ClearSetNameNumberAndProviderId -{ - // Main operator for struct, allowing the foreachcard to work. - ClearSetNameNumberAndProviderId() - { - } - - void operator()(const InnerDecklistNode *node, DecklistCardNode *card) const - { - Q_UNUSED(node); - // Set the providerId on the card - card->setCardSetShortName(nullptr); - card->setCardCollectorNumber(nullptr); - card->setCardProviderId(nullptr); - } -}; - -/** - * Clears the set name and numbers on each card in the decklist. - * - * @param deckList The decklist to modify - */ -void DeckLoader::clearSetNamesAndNumbers(const DeckList *deckList) -{ - auto clearSetNameAndNumber = [](const auto node, auto card) { - Q_UNUSED(node) - // Set the providerId on the card - card->setCardSetShortName(nullptr); - card->setCardCollectorNumber(nullptr); - card->setCardProviderId(nullptr); - }; - - deckList->forEachCard(clearSetNameAndNumber); -} - void DeckLoader::saveToClipboard(const DeckList *deckList, bool addComments, bool addSetNameAndNumber) { QString buffer; diff --git a/cockatrice/src/interface/deck_loader/deck_loader.h b/cockatrice/src/interface/deck_loader/deck_loader.h index d356f255d..474eee149 100644 --- a/cockatrice/src/interface/deck_loader/deck_loader.h +++ b/cockatrice/src/interface/deck_loader/deck_loader.h @@ -65,8 +65,6 @@ public: return lastLoadInfo.isEmpty(); } - static void clearSetNamesAndNumbers(const DeckList *deckList); - bool loadFromFile(const QString &fileName, DeckFileFormat::Format fmt, bool userRequest = false); bool loadFromFileAsync(const QString &fileName, DeckFileFormat::Format fmt, bool userRequest); bool loadFromRemote(const QString &nativeString, int remoteDeckId); @@ -75,9 +73,6 @@ public: static QString exportDeckToDecklist(const DeckList *deckList, DecklistWebsite website); - static void setProviderIdToPreferredPrinting(const DeckList *deckList); - static void resolveSetNameAndNumberToProviderID(const DeckList *deckList); - static void saveToClipboard(const DeckList *deckList, bool addComments = true, bool addSetNameAndNumber = true); static bool saveToStream_Plain(QTextStream &out, const DeckList *deckList,