inline structs in forEachCard

This commit is contained in:
RickyRister 2025-03-01 20:26:15 -08:00
parent 9ee575c1a2
commit 5f1fca5da0
4 changed files with 22 additions and 91 deletions

View file

@ -92,16 +92,9 @@ void TappedOutInterface::analyzeDeck(DeckList *deck)
manager->post(request, data); manager->post(request, data);
} }
struct CopyMainOrSide void TappedOutInterface::copyDeckSplitMainAndSide(DeckList &source, DeckList &mainboard, DeckList &sideboard)
{ {
CardDatabase &cardDatabase; auto copyMainOrSide = [this, &mainboard, &sideboard](const auto node, const auto card) {
DeckList &mainboard, &sideboard;
CopyMainOrSide(CardDatabase &_cardDatabase, DeckList &_mainboard, DeckList &_sideboard)
: cardDatabase(_cardDatabase), mainboard(_mainboard), sideboard(_sideboard){};
void operator()(const InnerDecklistNode *node, const DecklistCardNode *card) const
{
CardInfoPtr dbCard = cardDatabase.getCard(card->getName()); CardInfoPtr dbCard = cardDatabase.getCard(card->getName());
if (!dbCard || dbCard->getIsToken()) if (!dbCard || dbCard->getIsToken())
return; return;
@ -112,11 +105,7 @@ struct CopyMainOrSide
else else
addedCard = mainboard.addCard(card->getName(), node->getName()); addedCard = mainboard.addCard(card->getName(), node->getName());
addedCard->setNumber(card->getNumber()); addedCard->setNumber(card->getNumber());
} };
};
void TappedOutInterface::copyDeckSplitMainAndSide(DeckList &source, DeckList &mainboard, DeckList &sideboard)
{
CopyMainOrSide copyMainOrSide(cardDatabase, mainboard, sideboard);
source.forEachCard(copyMainOrSide); source.forEachCard(copyMainOrSide);
} }

View file

@ -316,17 +316,12 @@ QString DeckLoader::exportDeckToDecklist(DecklistWebsite website)
return deckString; 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. * Sets the providerId on each card in the decklist based on its set name and collector number.
struct SetProviderId */
void DeckLoader::resolveSetNameAndNumberToProviderID()
{ {
// Main operator for struct, allowing the foreachcard to work. auto setProviderId = [](const auto node, const auto card) {
SetProviderId()
{
}
void operator()(const InnerDecklistNode *node, DecklistCardNode *card) const
{
Q_UNUSED(node); Q_UNUSED(node);
// Retrieve the providerId based on setName and collectorNumber // Retrieve the providerId based on setName and collectorNumber
QString providerId = QString providerId =
@ -336,50 +331,23 @@ struct SetProviderId
// Set the providerId on the card // Set the providerId on the card
card->setCardProviderId(providerId); card->setCardProviderId(providerId);
} };
};
/**
* This function iterates through each card in the decklist and sets the providerId
* on each card based on its set name and collector number.
*/
void DeckLoader::resolveSetNameAndNumberToProviderID()
{
// Set up the struct to call.
SetProviderId setProviderId;
// Call the forEachCard method for each card in the deck
forEachCard(setProviderId); 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 ClearSetNameAndNumber
{
// Main operator for struct, allowing the foreachcard to work.
ClearSetNameAndNumber()
{
}
void operator()(const InnerDecklistNode *node, DecklistCardNode *card) const
{
Q_UNUSED(node);
// Set the providerId on the card
card->setCardSetShortName(nullptr);
card->setCardCollectorNumber(nullptr);
}
};
/** /**
* This function iterates through each card in the decklist and sets the providerId * Clears the set name and numbers on each card in the decklist.
* on each card based on its set name and collector number.
*/ */
void DeckLoader::clearSetNamesAndNumbers() void DeckLoader::clearSetNamesAndNumbers()
{ {
// Set up the struct to call. auto clearSetNameAndNumber = [](const auto node, auto card) {
ClearSetNameAndNumber clearSetNameAndNumber; Q_UNUSED(node)
// Set the providerId on the card
card->setCardSetShortName(nullptr);
card->setCardCollectorNumber(nullptr);
};
// Call the forEachCard method for each card in the deck
forEachCard(clearSetNameAndNumber); forEachCard(clearSetNameAndNumber);
} }

View file

@ -67,26 +67,15 @@ void DeckStatsInterface::analyzeDeck(DeckList *deck)
manager->post(request, data); manager->post(request, data);
} }
struct CopyIfNotAToken void DeckStatsInterface::copyDeckWithoutTokens(DeckList &source, DeckList &destination)
{ {
CardDatabase &cardDatabase; auto copyIfNotAToken = [this, &destination](const auto node, const auto card) {
DeckList &destination;
CopyIfNotAToken(CardDatabase &_cardDatabase, DeckList &_destination)
: cardDatabase(_cardDatabase), destination(_destination){};
void operator()(const InnerDecklistNode *node, const DecklistCardNode *card) const
{
CardInfoPtr dbCard = cardDatabase.getCard(card->getName()); CardInfoPtr dbCard = cardDatabase.getCard(card->getName());
if (dbCard && !dbCard->getIsToken()) { if (dbCard && !dbCard->getIsToken()) {
DecklistCardNode *addedCard = destination.addCard(card->getName(), node->getName()); DecklistCardNode *addedCard = destination.addCard(card->getName(), node->getName());
addedCard->setNumber(card->getNumber()); addedCard->setNumber(card->getNumber());
} }
} };
};
void DeckStatsInterface::copyDeckWithoutTokens(DeckList &source, DeckList &destination)
{
CopyIfNotAToken copyIfNotAToken(cardDatabase, destination);
source.forEachCard(copyIfNotAToken); source.forEachCard(copyIfNotAToken);
} }

View file

@ -762,20 +762,9 @@ bool DeckList::loadFromFile_Plain(QIODevice *device)
return loadFromStream_Plain(in, false); return loadFromStream_Plain(in, false);
} }
struct WriteToStream bool DeckList::saveToStream_Plain(QTextStream &stream, bool prefixSideboardCards, bool slashTappedOutSplitCards)
{ {
QTextStream &stream; auto writeToStream = [&stream, prefixSideboardCards, slashTappedOutSplitCards](const auto node, const auto card) {
bool prefixSideboardCards;
bool slashTappedOutSplitCards;
WriteToStream(QTextStream &_stream, bool _prefixSideboardCards, bool _slashTappedOutSplitCards)
: stream(_stream), prefixSideboardCards(_prefixSideboardCards),
slashTappedOutSplitCards(_slashTappedOutSplitCards)
{
}
void operator()(const InnerDecklistNode *node, const DecklistCardNode *card)
{
if (prefixSideboardCards && node->getName() == DECK_ZONE_SIDE) { if (prefixSideboardCards && node->getName() == DECK_ZONE_SIDE) {
stream << "SB: "; stream << "SB: ";
} }
@ -784,12 +773,8 @@ struct WriteToStream
} else { } else {
stream << QString("%1 %2\n").arg(card->getNumber()).arg(card->getName().replace("//", "/")); stream << QString("%1 %2\n").arg(card->getNumber()).arg(card->getName().replace("//", "/"));
} }
} };
};
bool DeckList::saveToStream_Plain(QTextStream &out, bool prefixSideboardCards, bool slashTappedOutSplitCards)
{
WriteToStream writeToStream(out, prefixSideboardCards, slashTappedOutSplitCards);
forEachCard(writeToStream); forEachCard(writeToStream);
return true; return true;
} }