mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-29 18:13:55 -07:00
[DeckList] Refactor and cleanup methods that iterate over nodes (#6407)
* remove helpers * create getZoneNodes method * replace direct calls to getRoot and forEachCard * remove more non-const uses of forEachCard * make node getter return const lists * one more usage * address comment * address comment again * fix hash * fix hashes (for real this time)
This commit is contained in:
parent
a390c8ada7
commit
2e2682aad4
16 changed files with 125 additions and 202 deletions
|
|
@ -118,12 +118,13 @@ static void setupParserRules()
|
||||||
|
|
||||||
return [=](const DeckPreviewWidget *deck, const ExtraDeckSearchInfo &) -> bool {
|
return [=](const DeckPreviewWidget *deck, const ExtraDeckSearchInfo &) -> bool {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
deck->deckLoader->getDeckList()->forEachCard([&](InnerDecklistNode *, const DecklistCardNode *node) {
|
auto cardNodes = deck->deckLoader->getDeckList()->getCardNodes();
|
||||||
|
for (auto node : cardNodes) {
|
||||||
auto cardInfoPtr = CardDatabaseManager::query()->getCardInfo(node->getName());
|
auto cardInfoPtr = CardDatabaseManager::query()->getCardInfo(node->getName());
|
||||||
if (!cardInfoPtr.isNull() && cardFilter.check(cardInfoPtr)) {
|
if (!cardInfoPtr.isNull() && cardFilter.check(cardInfoPtr)) {
|
||||||
count += node->getNumber();
|
count += node->getNumber();
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
return numberMatcher(count);
|
return numberMatcher(count);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -343,10 +343,7 @@ void DeckViewScene::rebuildTree()
|
||||||
if (!deck)
|
if (!deck)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
InnerDecklistNode *listRoot = deck->getRoot();
|
for (auto *currentZone : deck->getZoneNodes()) {
|
||||||
for (int i = 0; i < listRoot->size(); i++) {
|
|
||||||
auto *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
|
|
||||||
|
|
||||||
DeckViewCardContainer *container = cardContainers.value(currentZone->getName(), 0);
|
DeckViewCardContainer *container = cardContainers.value(currentZone->getName(), 0);
|
||||||
if (!container) {
|
if (!container) {
|
||||||
container = new DeckViewCardContainer(currentZone->getName());
|
container = new DeckViewCardContainer(currentZone->getName());
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include "../player_actions.h"
|
#include "../player_actions.h"
|
||||||
#include "player_menu.h"
|
#include "player_menu.h"
|
||||||
|
|
||||||
|
#include <libcockatrice/deck_list/tree/deck_list_card_node.h>
|
||||||
#include <libcockatrice/deck_list/tree/inner_deck_list_node.h>
|
#include <libcockatrice/deck_list/tree/inner_deck_list_node.h>
|
||||||
|
|
||||||
UtilityMenu::UtilityMenu(Player *_player, QMenu *playerMenu) : QMenu(playerMenu), player(_player)
|
UtilityMenu::UtilityMenu(Player *_player, QMenu *playerMenu) : QMenu(playerMenu), player(_player)
|
||||||
|
|
@ -65,15 +66,13 @@ void UtilityMenu::populatePredefinedTokensMenu()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
InnerDecklistNode *tokenZone =
|
auto tokenCardNodes = _deck->getDeckList()->getCardNodes({DECK_ZONE_TOKENS});
|
||||||
dynamic_cast<InnerDecklistNode *>(_deck->getDeckList()->getRoot()->findChild(DECK_ZONE_TOKENS));
|
|
||||||
|
|
||||||
if (tokenZone) {
|
if (!tokenCardNodes.isEmpty()) {
|
||||||
if (!tokenZone->empty())
|
setEnabled(true);
|
||||||
setEnabled(true);
|
|
||||||
|
|
||||||
for (int i = 0; i < tokenZone->size(); ++i) {
|
for (int i = 0; i < tokenCardNodes.size(); ++i) {
|
||||||
const QString tokenName = tokenZone->at(i)->getName();
|
const QString tokenName = tokenCardNodes[i]->getName();
|
||||||
predefinedTokens.append(tokenName);
|
predefinedTokens.append(tokenName);
|
||||||
QAction *a = addAction(tokenName);
|
QAction *a = addAction(tokenName);
|
||||||
if (i < 10) {
|
if (i < 10) {
|
||||||
|
|
|
||||||
|
|
@ -269,6 +269,20 @@ static QString toDecklistExportString(const DecklistCardNode *card)
|
||||||
return cardString;
|
return cardString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts all cards in the list to their decklist export string and joins them into one string
|
||||||
|
*/
|
||||||
|
static QString toDecklistExportString(const QList<const DecklistCardNode *> &cardNodes)
|
||||||
|
{
|
||||||
|
QString result;
|
||||||
|
|
||||||
|
for (auto cardNode : cardNodes) {
|
||||||
|
result += toDecklistExportString(cardNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export deck to decklist function, called to format the deck in a way to be sent to a server
|
* Export deck to decklist function, called to format the deck in a way to be sent to a server
|
||||||
*
|
*
|
||||||
|
|
@ -279,29 +293,11 @@ QString DeckLoader::exportDeckToDecklist(const DeckList *deckList, DecklistWebsi
|
||||||
{
|
{
|
||||||
// Add the base url
|
// Add the base url
|
||||||
QString deckString = "https://" + getDomainForWebsite(website) + "/?";
|
QString deckString = "https://" + getDomainForWebsite(website) + "/?";
|
||||||
// Create two strings to pass to function
|
|
||||||
QString mainBoardCards, sideBoardCards;
|
|
||||||
|
|
||||||
// Set up the function to call
|
// export all cards in zone
|
||||||
auto formatDeckListForExport = [&mainBoardCards, &sideBoardCards](const auto *node, const auto *card) {
|
QString mainBoardCards = toDecklistExportString(deckList->getCardNodes({DECK_ZONE_MAIN}));
|
||||||
// Get the card name
|
QString sideBoardCards = toDecklistExportString(deckList->getCardNodes({DECK_ZONE_SIDE}));
|
||||||
CardInfoPtr dbCard = CardDatabaseManager::query()->getCardInfo(card->getName());
|
|
||||||
if (!dbCard || dbCard->getIsToken()) {
|
|
||||||
// If it's a token, we don't care about the card.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if it's a sideboard card.
|
|
||||||
if (node->getName() == DECK_ZONE_SIDE) {
|
|
||||||
sideBoardCards += toDecklistExportString(card);
|
|
||||||
} else {
|
|
||||||
// If it's a mainboard card, do the same thing, but for the mainboard card string
|
|
||||||
mainBoardCards += toDecklistExportString(card);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// call our struct function for each card in the deck
|
|
||||||
deckList->forEachCard(formatDeckListForExport);
|
|
||||||
// Remove the extra return at the end of the last cards
|
// Remove the extra return at the end of the last cards
|
||||||
mainBoardCards.chop(3);
|
mainBoardCards.chop(3);
|
||||||
sideBoardCards.chop(3);
|
sideBoardCards.chop(3);
|
||||||
|
|
@ -335,9 +331,7 @@ bool DeckLoader::saveToStream_Plain(QTextStream &out,
|
||||||
}
|
}
|
||||||
|
|
||||||
// loop zones
|
// loop zones
|
||||||
for (int i = 0; i < deckList->getRoot()->size(); i++) {
|
for (auto zoneNode : deckList->getZoneNodes()) {
|
||||||
const auto *zoneNode = dynamic_cast<InnerDecklistNode *>(deckList->getRoot()->at(i));
|
|
||||||
|
|
||||||
saveToStream_DeckZone(out, zoneNode, addComments, addSetNameAndNumber);
|
saveToStream_DeckZone(out, zoneNode, addComments, addSetNameAndNumber);
|
||||||
|
|
||||||
// end of zone
|
// end of zone
|
||||||
|
|
@ -514,7 +508,7 @@ QString DeckLoader::getCompleteCardName(const QString &cardName)
|
||||||
return cardName;
|
return cardName;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeckLoader::printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node)
|
void DeckLoader::printDeckListNode(QTextCursor *cursor, const InnerDecklistNode *node)
|
||||||
{
|
{
|
||||||
const int totalColumns = 2;
|
const int totalColumns = 2;
|
||||||
|
|
||||||
|
|
@ -597,12 +591,11 @@ void DeckLoader::printDeckList(QPrinter *printer, const DeckList *deckList)
|
||||||
cursor.insertText(deckList->getComments());
|
cursor.insertText(deckList->getComments());
|
||||||
cursor.insertBlock(headerBlockFormat, headerCharFormat);
|
cursor.insertBlock(headerBlockFormat, headerCharFormat);
|
||||||
|
|
||||||
for (int i = 0; i < deckList->getRoot()->size(); i++) {
|
for (auto zoneNode : deckList->getZoneNodes()) {
|
||||||
cursor.insertHtml("<br><img src=theme:hr.jpg>");
|
cursor.insertHtml("<br><img src=theme:hr.jpg>");
|
||||||
// cursor.insertHtml("<hr>");
|
|
||||||
cursor.insertBlock(headerBlockFormat, headerCharFormat);
|
cursor.insertBlock(headerBlockFormat, headerCharFormat);
|
||||||
|
|
||||||
printDeckListNode(&cursor, dynamic_cast<InnerDecklistNode *>(deckList->getRoot()->at(i)));
|
printDeckListNode(&cursor, zoneNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
doc.print(printer);
|
doc.print(printer);
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void printDeckListNode(QTextCursor *cursor, InnerDecklistNode *node);
|
static void printDeckListNode(QTextCursor *cursor, const InnerDecklistNode *node);
|
||||||
static void saveToStream_DeckHeader(QTextStream &out, const DeckList *deckList);
|
static void saveToStream_DeckHeader(QTextStream &out, const DeckList *deckList);
|
||||||
|
|
||||||
static void saveToStream_DeckZone(QTextStream &out,
|
static void saveToStream_DeckZone(QTextStream &out,
|
||||||
|
|
|
||||||
|
|
@ -337,7 +337,7 @@ void DeckEditorDeckDockWidget::updateBannerCardComboBox()
|
||||||
|
|
||||||
// Collect unique (name, providerId) pairs
|
// Collect unique (name, providerId) pairs
|
||||||
QSet<QPair<QString, QString>> bannerCardSet;
|
QSet<QPair<QString, QString>> bannerCardSet;
|
||||||
QList<DecklistCardNode *> cardsInDeck = deckModel->getDeckList()->getCardNodes();
|
QList<const DecklistCardNode *> cardsInDeck = deckModel->getDeckList()->getCardNodes();
|
||||||
|
|
||||||
for (auto currentCard : cardsInDeck) {
|
for (auto currentCard : cardsInDeck) {
|
||||||
if (!CardDatabaseManager::query()->getCard(currentCard->toCardRef())) {
|
if (!CardDatabaseManager::query()->getCard(currentCard->toCardRef())) {
|
||||||
|
|
|
||||||
|
|
@ -228,7 +228,7 @@ QMap<QString, int> DlgSelectSetForCards::getSetsForCards()
|
||||||
if (!decklist)
|
if (!decklist)
|
||||||
return setCounts;
|
return setCounts;
|
||||||
|
|
||||||
QList<DecklistCardNode *> cardsInDeck = decklist->getCardNodes();
|
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes();
|
||||||
|
|
||||||
for (auto currentCard : cardsInDeck) {
|
for (auto currentCard : cardsInDeck) {
|
||||||
CardInfoPtr infoPtr = CardDatabaseManager::query()->getCardInfo(currentCard->getName());
|
CardInfoPtr infoPtr = CardDatabaseManager::query()->getCardInfo(currentCard->getName());
|
||||||
|
|
@ -271,7 +271,7 @@ void DlgSelectSetForCards::updateCardLists()
|
||||||
if (!decklist)
|
if (!decklist)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QList<DecklistCardNode *> cardsInDeck = decklist->getCardNodes();
|
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes();
|
||||||
|
|
||||||
for (auto currentCard : cardsInDeck) {
|
for (auto currentCard : cardsInDeck) {
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
|
@ -360,7 +360,7 @@ QMap<QString, QStringList> DlgSelectSetForCards::getCardsForSets()
|
||||||
if (!decklist)
|
if (!decklist)
|
||||||
return setCards;
|
return setCards;
|
||||||
|
|
||||||
QList<DecklistCardNode *> cardsInDeck = decklist->getCardNodes();
|
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes();
|
||||||
|
|
||||||
for (auto currentCard : cardsInDeck) {
|
for (auto currentCard : cardsInDeck) {
|
||||||
CardInfoPtr infoPtr = CardDatabaseManager::query()->getCardInfo(currentCard->getName());
|
CardInfoPtr infoPtr = CardDatabaseManager::query()->getCardInfo(currentCard->getName());
|
||||||
|
|
|
||||||
|
|
@ -311,7 +311,7 @@ int CardAmountWidget::countCardsInZone(const QString &deckZone)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<DecklistCardNode *> cardsInDeck = decklist->getCardNodes({deckZone});
|
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes({deckZone});
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (auto currentCard : cardsInDeck) {
|
for (auto currentCard : cardsInDeck) {
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ void VisualDatabaseDisplayNameFilterWidget::actLoadFromDeck()
|
||||||
if (!decklist)
|
if (!decklist)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
QList<DecklistCardNode *> cardsInDeck = decklist->getCardNodes();
|
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes();
|
||||||
|
|
||||||
for (auto currentCard : cardsInDeck) {
|
for (auto currentCard : cardsInDeck) {
|
||||||
createNameFilter(currentCard->getName());
|
createNameFilter(currentCard->getName());
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@ QList<ExactCard> VisualDeckEditorSampleHandWidget::getRandomCards(int amountToGe
|
||||||
if (!decklist)
|
if (!decklist)
|
||||||
return randomCards;
|
return randomCards;
|
||||||
|
|
||||||
QList<DecklistCardNode *> cardsInDeck = decklist->getCardNodes({DECK_ZONE_MAIN});
|
QList<const DecklistCardNode *> cardsInDeck = decklist->getCardNodes({DECK_ZONE_MAIN});
|
||||||
|
|
||||||
// Collect all cards in the main deck, allowing duplicates based on their count
|
// Collect all cards in the main deck, allowing duplicates based on their count
|
||||||
for (auto currentCard : cardsInDeck) {
|
for (auto currentCard : cardsInDeck) {
|
||||||
|
|
|
||||||
|
|
@ -235,7 +235,7 @@ void DeckPreviewWidget::updateBannerCardComboBox()
|
||||||
// Prepare the new items with deduplication
|
// Prepare the new items with deduplication
|
||||||
QSet<QPair<QString, QString>> bannerCardSet;
|
QSet<QPair<QString, QString>> bannerCardSet;
|
||||||
|
|
||||||
QList<DecklistCardNode *> cardsInDeck = deckLoader->getDeckList()->getCardNodes();
|
QList<const DecklistCardNode *> cardsInDeck = deckLoader->getDeckList()->getCardNodes();
|
||||||
|
|
||||||
for (auto currentCard : cardsInDeck) {
|
for (auto currentCard : cardsInDeck) {
|
||||||
for (int k = 0; k < currentCard->getNumber(); ++k) {
|
for (int k = 0; k < currentCard->getNumber(); ++k) {
|
||||||
|
|
|
||||||
|
|
@ -523,56 +523,34 @@ void DeckList::cleanList(bool preserveMetadata)
|
||||||
refreshDeckHash();
|
refreshDeckHash();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeckList::getCardListHelper(InnerDecklistNode *item, QSet<QString> &result)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < item->size(); ++i) {
|
|
||||||
auto *node = dynamic_cast<DecklistCardNode *>(item->at(i));
|
|
||||||
|
|
||||||
if (node) {
|
|
||||||
result.insert(node->getName());
|
|
||||||
} else {
|
|
||||||
getCardListHelper(dynamic_cast<InnerDecklistNode *>(item->at(i)), result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void DeckList::getCardRefListHelper(InnerDecklistNode *item, QList<CardRef> &result)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < item->size(); ++i) {
|
|
||||||
auto *node = dynamic_cast<DecklistCardNode *>(item->at(i));
|
|
||||||
|
|
||||||
if (node) {
|
|
||||||
result.append(node->toCardRef());
|
|
||||||
} else {
|
|
||||||
getCardRefListHelper(dynamic_cast<InnerDecklistNode *>(item->at(i)), result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QStringList DeckList::getCardList() const
|
QStringList DeckList::getCardList() const
|
||||||
{
|
{
|
||||||
QSet<QString> result;
|
auto nodes = getCardNodes();
|
||||||
getCardListHelper(root, result);
|
|
||||||
return result.values();
|
QStringList result;
|
||||||
|
std::transform(nodes.cbegin(), nodes.cend(), std::back_inserter(result), [](auto node) { return node->getName(); });
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<CardRef> DeckList::getCardRefList() const
|
QList<CardRef> DeckList::getCardRefList() const
|
||||||
{
|
{
|
||||||
|
auto nodes = getCardNodes();
|
||||||
|
|
||||||
QList<CardRef> result;
|
QList<CardRef> result;
|
||||||
getCardRefListHelper(root, result);
|
std::transform(nodes.cbegin(), nodes.cend(), std::back_inserter(result),
|
||||||
|
[](auto node) { return node->toCardRef(); });
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<DecklistCardNode *> DeckList::getCardNodes(const QStringList &restrictToZones) const
|
QList<const DecklistCardNode *> DeckList::getCardNodes(const QStringList &restrictToZones) const
|
||||||
{
|
{
|
||||||
QList<DecklistCardNode *> result;
|
QList<const DecklistCardNode *> result;
|
||||||
|
|
||||||
for (auto *node : *root) {
|
auto zoneNodes = getZoneNodes();
|
||||||
auto *zoneNode = dynamic_cast<InnerDecklistNode *>(node);
|
for (auto *zoneNode : zoneNodes) {
|
||||||
if (zoneNode == nullptr) {
|
if (!restrictToZones.isEmpty() && !restrictToZones.contains(zoneNode->getName())) {
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!restrictToZones.isEmpty() && !restrictToZones.contains(node->getName())) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for (auto *cardNode : *zoneNode) {
|
for (auto *cardNode : *zoneNode) {
|
||||||
|
|
@ -586,20 +564,28 @@ QList<DecklistCardNode *> DeckList::getCardNodes(const QStringList &restrictToZo
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QList<const InnerDecklistNode *> DeckList::getZoneNodes() const
|
||||||
|
{
|
||||||
|
QList<const InnerDecklistNode *> zones;
|
||||||
|
for (auto *node : *root) {
|
||||||
|
InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(node);
|
||||||
|
if (!currentZone)
|
||||||
|
continue;
|
||||||
|
zones.append(currentZone);
|
||||||
|
}
|
||||||
|
|
||||||
|
return zones;
|
||||||
|
}
|
||||||
|
|
||||||
int DeckList::getSideboardSize() const
|
int DeckList::getSideboardSize() const
|
||||||
{
|
{
|
||||||
int size = 0;
|
auto cards = getCardNodes({DECK_ZONE_SIDE});
|
||||||
for (int i = 0; i < root->size(); ++i) {
|
|
||||||
auto *node = dynamic_cast<InnerDecklistNode *>(root->at(i));
|
|
||||||
if (node->getName() != DECK_ZONE_SIDE) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int j = 0; j < node->size(); j++) {
|
int size = 0;
|
||||||
auto *card = dynamic_cast<DecklistCardNode *>(node->at(j));
|
for (auto card : cards) {
|
||||||
size += card->getNumber();
|
size += card->getNumber();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -665,26 +651,23 @@ bool DeckList::deleteNode(AbstractDecklistNode *node, InnerDecklistNode *rootNod
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QString computeDeckHash(const InnerDecklistNode *root)
|
static QString computeDeckHash(const DeckList &deckList)
|
||||||
{
|
{
|
||||||
QStringList cardList;
|
auto mainDeckNodes = deckList.getCardNodes({DECK_ZONE_MAIN});
|
||||||
QSet<QString> hashZones, optionalZones;
|
auto sideDeckNodes = deckList.getCardNodes({DECK_ZONE_SIDE});
|
||||||
|
|
||||||
hashZones << DECK_ZONE_MAIN << DECK_ZONE_SIDE; // Zones in deck to be included in hashing process
|
static auto nodesToCardList = [](const QList<const DecklistCardNode *> &nodes, const QString &prefix = {}) {
|
||||||
optionalZones << DECK_ZONE_TOKENS; // Optional zones in deck not included in hashing process
|
QStringList result;
|
||||||
|
for (auto node : nodes) {
|
||||||
for (int i = 0; i < root->size(); i++) {
|
for (int i = 0; i < node->getNumber(); ++i) {
|
||||||
auto *node = dynamic_cast<InnerDecklistNode *>(root->at(i));
|
result.append(prefix + node->getName().toLower());
|
||||||
for (int j = 0; j < node->size(); j++) {
|
|
||||||
if (hashZones.contains(node->getName())) // Mainboard or Sideboard
|
|
||||||
{
|
|
||||||
auto *card = dynamic_cast<DecklistCardNode *>(node->at(j));
|
|
||||||
for (int k = 0; k < card->getNumber(); ++k) {
|
|
||||||
cardList.append((node->getName() == DECK_ZONE_SIDE ? "SB:" : "") + card->getName().toLower());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
QStringList cardList = nodesToCardList(mainDeckNodes) + nodesToCardList(sideDeckNodes, "SB:");
|
||||||
|
|
||||||
cardList.sort();
|
cardList.sort();
|
||||||
QByteArray deckHashArray = QCryptographicHash::hash(cardList.join(";").toUtf8(), QCryptographicHash::Sha1);
|
QByteArray deckHashArray = QCryptographicHash::hash(cardList.join(";").toUtf8(), QCryptographicHash::Sha1);
|
||||||
quint64 number = (((quint64)(unsigned char)deckHashArray[0]) << 32) +
|
quint64 number = (((quint64)(unsigned char)deckHashArray[0]) << 32) +
|
||||||
|
|
@ -706,7 +689,7 @@ QString DeckList::getDeckHash() const
|
||||||
return cachedDeckHash;
|
return cachedDeckHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
cachedDeckHash = computeDeckHash(root);
|
cachedDeckHash = computeDeckHash(*this);
|
||||||
return cachedDeckHash;
|
return cachedDeckHash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -148,8 +148,6 @@ private:
|
||||||
mutable QString cachedDeckHash;
|
mutable QString cachedDeckHash;
|
||||||
|
|
||||||
// Helpers for traversing the tree
|
// Helpers for traversing the tree
|
||||||
static void getCardListHelper(InnerDecklistNode *node, QSet<QString> &result);
|
|
||||||
static void getCardRefListHelper(InnerDecklistNode *item, QList<CardRef> &result);
|
|
||||||
InnerDecklistNode *getZoneObjFromName(const QString &zoneName);
|
InnerDecklistNode *getZoneObjFromName(const QString &zoneName);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
@ -267,7 +265,8 @@ public:
|
||||||
}
|
}
|
||||||
QStringList getCardList() const;
|
QStringList getCardList() const;
|
||||||
QList<CardRef> getCardRefList() const;
|
QList<CardRef> getCardRefList() const;
|
||||||
QList<DecklistCardNode *> getCardNodes(const QStringList &restrictToZones = QStringList()) const;
|
QList<const DecklistCardNode *> getCardNodes(const QStringList &restrictToZones = QStringList()) const;
|
||||||
|
QList<const InnerDecklistNode *> getZoneNodes() const;
|
||||||
int getSideboardSize() const;
|
int getSideboardSize() const;
|
||||||
InnerDecklistNode *getRoot() const
|
InnerDecklistNode *getRoot() const
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -550,86 +550,49 @@ void DeckListModel::setDeckList(DeckList *_deck)
|
||||||
|
|
||||||
QList<ExactCard> DeckListModel::getCards() const
|
QList<ExactCard> DeckListModel::getCards() const
|
||||||
{
|
{
|
||||||
QList<ExactCard> cards;
|
auto nodes = deckList->getCardNodes();
|
||||||
DeckList *decklist = getDeckList();
|
|
||||||
if (!decklist) {
|
|
||||||
return cards;
|
|
||||||
}
|
|
||||||
InnerDecklistNode *listRoot = decklist->getRoot();
|
|
||||||
if (!listRoot)
|
|
||||||
return cards;
|
|
||||||
|
|
||||||
for (int i = 0; i < listRoot->size(); i++) {
|
QList<ExactCard> cards;
|
||||||
InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
|
for (auto node : nodes) {
|
||||||
if (!currentZone)
|
ExactCard card = CardDatabaseManager::query()->getCard(node->toCardRef());
|
||||||
continue;
|
if (card) {
|
||||||
for (int j = 0; j < currentZone->size(); j++) {
|
for (int k = 0; k < node->getNumber(); ++k) {
|
||||||
DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
|
cards.append(card);
|
||||||
if (!currentCard)
|
|
||||||
continue;
|
|
||||||
for (int k = 0; k < currentCard->getNumber(); ++k) {
|
|
||||||
ExactCard card = CardDatabaseManager::query()->getCard(currentCard->toCardRef());
|
|
||||||
if (card) {
|
|
||||||
cards.append(card);
|
|
||||||
} else {
|
|
||||||
qDebug() << "Card not found in database!";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
qDebug() << "Card not found in database!";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cards;
|
return cards;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<ExactCard> DeckListModel::getCardsForZone(const QString &zoneName) const
|
QList<ExactCard> DeckListModel::getCardsForZone(const QString &zoneName) const
|
||||||
{
|
{
|
||||||
QList<ExactCard> cards;
|
auto nodes = deckList->getCardNodes({zoneName});
|
||||||
DeckList *decklist = getDeckList();
|
|
||||||
if (!decklist) {
|
|
||||||
return cards;
|
|
||||||
}
|
|
||||||
InnerDecklistNode *listRoot = decklist->getRoot();
|
|
||||||
if (!listRoot)
|
|
||||||
return cards;
|
|
||||||
|
|
||||||
for (int i = 0; i < listRoot->size(); i++) {
|
QList<ExactCard> cards;
|
||||||
InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
|
for (auto node : nodes) {
|
||||||
if (!currentZone)
|
ExactCard card = CardDatabaseManager::query()->getCard(node->toCardRef());
|
||||||
continue;
|
if (card) {
|
||||||
if (currentZone->getName() == zoneName) {
|
for (int k = 0; k < node->getNumber(); ++k) {
|
||||||
for (int j = 0; j < currentZone->size(); j++) {
|
cards.append(card);
|
||||||
DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
|
|
||||||
if (!currentCard)
|
|
||||||
continue;
|
|
||||||
for (int k = 0; k < currentCard->getNumber(); ++k) {
|
|
||||||
ExactCard card = CardDatabaseManager::query()->getCard(currentCard->toCardRef());
|
|
||||||
if (card) {
|
|
||||||
cards.append(card);
|
|
||||||
} else {
|
|
||||||
qDebug() << "Card not found in database!";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
qDebug() << "Card not found in database!";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cards;
|
return cards;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> *DeckListModel::getZones() const
|
QList<QString> DeckListModel::getZones() const
|
||||||
{
|
{
|
||||||
QList<QString> *zones = new QList<QString>();
|
auto zoneNodes = deckList->getZoneNodes();
|
||||||
DeckList *decklist = getDeckList();
|
|
||||||
if (!decklist) {
|
QList<QString> zones;
|
||||||
return zones;
|
std::transform(zoneNodes.cbegin(), zoneNodes.cend(), std::back_inserter(zones),
|
||||||
}
|
[](auto zoneNode) { return zoneNode->getName(); });
|
||||||
InnerDecklistNode *listRoot = decklist->getRoot();
|
|
||||||
if (!listRoot)
|
|
||||||
return zones;
|
|
||||||
|
|
||||||
for (int i = 0; i < listRoot->size(); i++) {
|
|
||||||
InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
|
|
||||||
if (!currentZone)
|
|
||||||
continue;
|
|
||||||
zones->append(currentZone->getName());
|
|
||||||
}
|
|
||||||
return zones;
|
return zones;
|
||||||
}
|
}
|
||||||
|
|
@ -300,7 +300,7 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] QList<ExactCard> getCards() const;
|
[[nodiscard]] QList<ExactCard> getCards() const;
|
||||||
[[nodiscard]] QList<ExactCard> getCardsForZone(const QString &zoneName) const;
|
[[nodiscard]] QList<ExactCard> getCardsForZone(const QString &zoneName) const;
|
||||||
[[nodiscard]] QList<QString> *getZones() const;
|
[[nodiscard]] QList<QString> getZones() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Sets the criteria used to group cards in the model.
|
* @brief Sets the criteria used to group cards in the model.
|
||||||
|
|
|
||||||
|
|
@ -102,28 +102,16 @@ void Server_Player::setupZones()
|
||||||
// ------------------------------------------------------------------
|
// ------------------------------------------------------------------
|
||||||
|
|
||||||
// Assign card ids and create deck from deck list
|
// Assign card ids and create deck from deck list
|
||||||
InnerDecklistNode *listRoot = deck->getRoot();
|
auto insertCardsIntoZone = [this](auto cards, auto *zone) {
|
||||||
for (int i = 0; i < listRoot->size(); ++i) {
|
for (auto card : cards) {
|
||||||
auto *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
|
for (int k = 0; k < card->getNumber(); ++k) {
|
||||||
Server_CardZone *z;
|
zone->insertCard(new Server_Card(card->toCardRef(), nextCardId++, 0, 0, zone), -1, 0);
|
||||||
if (currentZone->getName() == DECK_ZONE_MAIN) {
|
}
|
||||||
z = deckZone;
|
|
||||||
} else if (currentZone->getName() == DECK_ZONE_SIDE) {
|
|
||||||
z = sbZone;
|
|
||||||
} else {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
for (int j = 0; j < currentZone->size(); ++j) {
|
insertCardsIntoZone(deck->getCardNodes({DECK_ZONE_MAIN}), deckZone);
|
||||||
auto *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
|
insertCardsIntoZone(deck->getCardNodes({DECK_ZONE_SIDE}), sbZone);
|
||||||
if (!currentCard) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (int k = 0; k < currentCard->getNumber(); ++k) {
|
|
||||||
z->insertCard(new Server_Card(currentCard->toCardRef(), nextCardId++, 0, 0, z), -1, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const QList<MoveCard_ToZone> &sideboardPlan = deck->getCurrentSideboardPlan();
|
const QList<MoveCard_ToZone> &sideboardPlan = deck->getCurrentSideboardPlan();
|
||||||
for (const auto &m : sideboardPlan) {
|
for (const auto &m : sideboardPlan) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue