Fix #45: don't send tokens to deckstats.

This commit is contained in:
arxanas 2014-06-30 01:18:05 -04:00
parent 4d6f46b06e
commit 7cbe410172
8 changed files with 105 additions and 47 deletions

View file

@ -652,41 +652,13 @@ void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml)
}
}
LoadStatus CardDatabase::loadFromFile(const QString &fileName, bool tokens)
LoadStatus CardDatabase::loadFromFile(const QString &fileName)
{
QFile file(fileName);
file.open(QIODevice::ReadOnly);
if (!file.isOpen())
return FileError;
if (tokens) {
QMutableHashIterator<QString, CardInfo *> i(cardHash);
while (i.hasNext()) {
i.next();
if (i.value()->getIsToken()) {
delete i.value();
i.remove();
}
}
} else {
QHashIterator<QString, CardSet *> setIt(setHash);
while (setIt.hasNext()) {
setIt.next();
delete setIt.value();
}
setHash.clear();
QMutableHashIterator<QString, CardInfo *> i(cardHash);
while (i.hasNext()) {
i.next();
if (!i.value()->getIsToken()) {
delete i.value();
i.remove();
}
}
cardHash.clear();
}
QXmlStreamReader xml(&file);
while (!xml.atEnd()) {
if (xml.readNext() == QXmlStreamReader::StartElement) {
@ -738,8 +710,7 @@ bool CardDatabase::saveToFile(const QString &fileName, bool tokens)
QHashIterator<QString, CardInfo *> cardIterator(cardHash);
while (cardIterator.hasNext()) {
CardInfo *card = cardIterator.next().value();
if (card->getIsToken() == tokens)
xml << card;
xml << card;
}
xml.writeEndElement(); // cards
@ -773,7 +744,7 @@ LoadStatus CardDatabase::loadCardDatabase(const QString &path, bool tokens)
{
LoadStatus tempLoadStatus = NotLoaded;
if (!path.isEmpty())
tempLoadStatus = loadFromFile(path, tokens);
tempLoadStatus = loadFromFile(path);
if (tempLoadStatus == Ok) {
SetList allSets;

View file

@ -186,7 +186,7 @@ public:
CardSet *getSet(const QString &setName);
QList<CardInfo *> getCardList() const { return cardHash.values(); }
SetList getSetList() const;
LoadStatus loadFromFile(const QString &fileName, bool tokens = false);
LoadStatus loadFromFile(const QString &fileName);
bool saveToFile(const QString &fileName, bool tokens = false);
QStringList getAllColors() const;
QStringList getAllMainCardTypes() const;

View file

@ -7,8 +7,10 @@
#include <QMessageBox>
#include <QDesktopServices>
DeckStatsInterface::DeckStatsInterface(QObject *parent)
: QObject(parent)
DeckStatsInterface::DeckStatsInterface(
CardDatabase &_cardDatabase,
QObject *parent
) : QObject(parent), cardDatabase(_cardDatabase)
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(queryFinished(QNetworkReply *)));
@ -42,7 +44,11 @@ void DeckStatsInterface::queryFinished(QNetworkReply *reply)
void DeckStatsInterface::analyzeDeck(DeckList *deck)
{
QUrl params;
params.addQueryItem("deck", deck->writeToString_Plain());
DeckList deckWithoutTokens;
copyDeckWithoutTokens(*deck, deckWithoutTokens);
params.addQueryItem("deck", deckWithoutTokens.writeToString_Plain());
QByteArray data;
data.append(params.encodedQuery());
@ -51,3 +57,30 @@ void DeckStatsInterface::analyzeDeck(DeckList *deck)
manager->post(request, data);
}
struct CopyIfNotAToken {
CardDatabase &cardDatabase;
DeckList &destination;
CopyIfNotAToken(
CardDatabase &_cardDatabase,
DeckList &_destination
) : cardDatabase(_cardDatabase), destination(_destination) {};
void operator()(
const InnerDecklistNode *node,
const DecklistCardNode *card
) const {
if (!cardDatabase.getCard(card->getName())->getIsToken()) {
destination.addCard(card->getName(), node->getName());
}
}
};
void DeckStatsInterface::copyDeckWithoutTokens(
const DeckList &source,
DeckList &destination
) {
CopyIfNotAToken copyIfNotAToken(cardDatabase, destination);
source.forEachCard(copyIfNotAToken);
}

View file

@ -1,6 +1,8 @@
#ifndef DECKSTATS_INTERFACE_H
#define DECKSTATS_INTERFACE_H
#include "carddatabase.h"
#include "decklist.h"
#include <QObject>
class QNetworkAccessManager;
@ -11,10 +13,20 @@ class DeckStatsInterface : public QObject {
Q_OBJECT
private:
QNetworkAccessManager *manager;
CardDatabase &cardDatabase;
/**
* Deckstats doesn't recognize token cards, and instead tries to find the
* closest non-token card instead. So we construct a new deck which has no
* tokens.
*/
void copyDeckWithoutTokens(const DeckList &source, DeckList& destination);
private slots:
void queryFinished(QNetworkReply *reply);
public:
DeckStatsInterface(QObject *parent = 0);
DeckStatsInterface(CardDatabase &_cardDatabase, QObject *parent = 0);
void analyzeDeck(DeckList *deck);
};

View file

@ -508,7 +508,10 @@ void TabDeckEditor::actPrintDeck()
void TabDeckEditor::actAnalyzeDeck()
{
DeckStatsInterface *interface = new DeckStatsInterface(this); // it deletes itself when done
DeckStatsInterface *interface = new DeckStatsInterface(
*databaseModel->getDatabase(),
this
); // it deletes itself when done
interface->analyzeDeck(deckModel->getDeckList());
}