Add pricing from deckbrew.com

* the previous PriceUpdater class has become abstract
* BLPPriceUpdater inherits the old code for blacklotusproject.com
* DBPriceUpdater is a new implementation for deckbrew.com
* add a setting to choose between the two
This commit is contained in:
Fabio Bas 2014-06-29 23:30:32 +02:00
parent ec3690fd29
commit 922e98af67
7 changed files with 222 additions and 12 deletions

View file

@ -18,6 +18,7 @@
#include <QInputDialog> #include <QInputDialog>
#include <QSpinBox> #include <QSpinBox>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QRadioButton>
#include <QDebug> #include <QDebug>
#include "carddatabase.h" #include "carddatabase.h"
#include "dlg_settings.h" #include "dlg_settings.h"
@ -531,9 +532,27 @@ DeckEditorSettingsPage::DeckEditorSettingsPage()
priceTagsCheckBox = new QCheckBox; priceTagsCheckBox = new QCheckBox;
priceTagsCheckBox->setChecked(settingsCache->getPriceTagFeature()); priceTagsCheckBox->setChecked(settingsCache->getPriceTagFeature());
connect(priceTagsCheckBox, SIGNAL(stateChanged(int)), settingsCache, SLOT(setPriceTagFeature(int))); connect(priceTagsCheckBox, SIGNAL(stateChanged(int)), settingsCache, SLOT(setPriceTagFeature(int)));
priceTagSource0 = new QRadioButton;
priceTagSource1 = new QRadioButton;
switch(settingsCache->getPriceTagSource())
{
case 0:
priceTagSource0->setChecked(true);
break;
case 1:
priceTagSource1->setChecked(true);
break;
}
connect(priceTagSource0, SIGNAL(toggled(bool)), this, SLOT(radioPriceTagSourceClicked(bool)));
connect(priceTagSource1, SIGNAL(toggled(bool)), this, SLOT(radioPriceTagSourceClicked(bool)));
QGridLayout *generalGrid = new QGridLayout; QGridLayout *generalGrid = new QGridLayout;
generalGrid->addWidget(priceTagsCheckBox, 0, 0); generalGrid->addWidget(priceTagsCheckBox, 0, 0);
generalGrid->addWidget(priceTagSource0, 1, 0);
generalGrid->addWidget(priceTagSource1, 2, 0);
generalGroupBox = new QGroupBox; generalGroupBox = new QGroupBox;
generalGroupBox->setLayout(generalGrid); generalGroupBox->setLayout(generalGrid);
@ -546,10 +565,26 @@ DeckEditorSettingsPage::DeckEditorSettingsPage()
void DeckEditorSettingsPage::retranslateUi() void DeckEditorSettingsPage::retranslateUi()
{ {
priceTagsCheckBox->setText(tr("Enable &price tag feature (using data from blacklotusproject.com)")); priceTagsCheckBox->setText(tr("Enable &price tag feature"));
priceTagSource0->setText(tr("using data from blacklotusproject.com"));
priceTagSource1->setText(tr("using data from deckbrew.com"));
generalGroupBox->setTitle(tr("General")); generalGroupBox->setTitle(tr("General"));
} }
void DeckEditorSettingsPage::radioPriceTagSourceClicked(bool checked)
{
if(!checked)
return;
int source=0;
if(priceTagSource0->isChecked())
source=0;
if(priceTagSource1->isChecked())
source=1;
QMetaObject::invokeMethod( settingsCache, "setPriceTagSource", Qt::QueuedConnection, Q_ARG(int, source));
}
MessagesSettingsPage::MessagesSettingsPage() MessagesSettingsPage::MessagesSettingsPage()
{ {
aAdd = new QAction(this); aAdd = new QAction(this);

View file

@ -15,6 +15,7 @@ class QCheckBox;
class QLabel; class QLabel;
class QCloseEvent; class QCloseEvent;
class QSpinBox; class QSpinBox;
class QRadioButton;
class AbstractSettingsPage : public QWidget { class AbstractSettingsPage : public QWidget {
public: public:
@ -100,8 +101,11 @@ class DeckEditorSettingsPage : public AbstractSettingsPage {
public: public:
DeckEditorSettingsPage(); DeckEditorSettingsPage();
void retranslateUi(); void retranslateUi();
private slots:
void radioPriceTagSourceClicked(bool checked);
private: private:
QCheckBox *priceTagsCheckBox; QCheckBox *priceTagsCheckBox;
QRadioButton *priceTagSource0, *priceTagSource1;
QGroupBox *generalGroupBox; QGroupBox *generalGroupBox;
}; };

View file

@ -4,25 +4,43 @@
*/ */
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkReply> #include <QNetworkReply>
#include <QMessageBox>
#include "qt-json/json.h" #include "qt-json/json.h"
#include "priceupdater.h" #include "priceupdater.h"
#if QT_VERSION < 0x050000
// for Qt::escape()
#include <QtGui/qtextdocument.h>
#endif
/** /**
* Constructor. * Constructor.
* *
* @param _deck deck. * @param _deck deck.
*/ */
PriceUpdater::PriceUpdater(const DeckList *_deck) AbstractPriceUpdater::AbstractPriceUpdater(const DeckList *_deck)
{ {
nam = new QNetworkAccessManager(this); nam = new QNetworkAccessManager(this);
deck = _deck; deck = _deck;
} }
// blacklotusproject.com
/**
* Constructor.
*
* @param _deck deck.
*/
BLPPriceUpdater::BLPPriceUpdater(const DeckList *_deck)
: AbstractPriceUpdater(_deck)
{
}
/** /**
* Update the prices of the cards in deckList. * Update the prices of the cards in deckList.
*/ */
void PriceUpdater::updatePrices() void BLPPriceUpdater::updatePrices()
{ {
QString q = "http://blacklotusproject.com/json/?cards="; QString q = "http://blacklotusproject.com/json/?cards=";
QStringList cards = deck->getCardList(); QStringList cards = deck->getCardList();
@ -38,7 +56,7 @@ void PriceUpdater::updatePrices()
/** /**
* Called when the download of the json file with the prices is finished. * Called when the download of the json file with the prices is finished.
*/ */
void PriceUpdater::downloadFinished() void BLPPriceUpdater::downloadFinished()
{ {
QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
bool ok; bool ok;
@ -82,3 +100,115 @@ void PriceUpdater::downloadFinished()
deleteLater(); deleteLater();
emit finishedUpdate(); emit finishedUpdate();
} }
// deckbrew.com
/**
* Constructor.
*
* @param _deck deck.
*/
DBPriceUpdater::DBPriceUpdater(const DeckList *_deck)
: AbstractPriceUpdater(_deck)
{
}
/**
* Update the prices of the cards in deckList.
*/
void DBPriceUpdater::updatePrices()
{
QString q = "https://api.deckbrew.com/mtg/cards";
QStringList cards = deck->getCardList();
for (int i = 0; i < cards.size(); ++i) {
q += (i ? "&" : "?") + QString("name=") + cards[i].toLower();
}
QUrl url(q.replace(' ', '+'));
QNetworkReply *reply = nam->get(QNetworkRequest(url));
connect(reply, SIGNAL(finished()), this, SLOT(downloadFinished()));
}
/**
* Called when the download of the json file with the prices is finished.
*/
void DBPriceUpdater::downloadFinished()
{
QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
bool ok;
QString tmp = QString(reply->readAll());
// Errors are incapsulated in an object, check for them first
QVariantMap resultMap = QtJson::Json::parse(tmp, ok).toMap();
if (!ok) {
QMessageBox::critical(this, tr("Error"), tr("A problem has occured while fetching card prices."));
reply->deleteLater();
deleteLater();
return;
}
if(resultMap.contains("errors"))
{
QMessageBox::critical(this, tr("Error"), tr("A problem has occured while fetching card prices:") +
"<br/>" +
#if QT_VERSION < 0x050000
Qt::escape(resultMap["errors"].toList().first().toString())
#else
resultMap["errors"].toList().first().toString().toHtmlEscaped()
#endif
);
reply->deleteLater();
deleteLater();
return;
}
// Good results are a list
QVariantList resultList = QtJson::Json::parse(tmp, ok).toList();
if (!ok) {
QMessageBox::critical(this, tr("Error"), tr("A problem has occured while fetching card prices."));
reply->deleteLater();
deleteLater();
return;
}
QMap<QString, float> cardsPrice;
QListIterator<QVariant> it(resultList);
while (it.hasNext()) {
QVariantMap map = it.next().toMap();
QString name = map.value("name").toString().toLower();
QList<QVariant> editions = map.value("editions").toList();
foreach (QVariant ed, editions)
{
QVariantMap edition = ed.toMap();
QString set = edition.value("set_id").toString();
// Prices are in USD cents
float price = edition.value("price").toMap().value("median").toString().toFloat() / 100;
//qDebug() << "card " << name << " set " << set << " price " << price << endl;
/**
* Make sure Masters Edition (MED) isn't the set, as it doesn't
* physically exist. Also check the price to see that the cheapest set
* ends up as the final price.
*/
if (set != "MED" && (!cardsPrice.contains(name) || cardsPrice.value(name) > price))
cardsPrice.insert(name, price);
}
}
InnerDecklistNode *listRoot = deck->getRoot();
for (int i = 0; i < listRoot->size(); i++) {
InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
for (int j = 0; j < currentZone->size(); j++) {
DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
if (!currentCard)
continue;
currentCard->setPrice(cardsPrice[currentCard->getName().toLower()]);
}
}
reply->deleteLater();
deleteLater();
emit finishedUpdate();
}

View file

@ -11,18 +11,38 @@ class QNetworkAccessManager;
* *
* @author Marcio Ribeiro <mmr@b1n.org> * @author Marcio Ribeiro <mmr@b1n.org>
*/ */
class PriceUpdater : public QObject class AbstractPriceUpdater : public QWidget
{ {
Q_OBJECT Q_OBJECT
private: protected:
const DeckList *deck; const DeckList *deck;
QNetworkAccessManager *nam; QNetworkAccessManager *nam;
signals: signals:
void finishedUpdate(); void finishedUpdate();
private slots: protected slots:
void downloadFinished(); virtual void downloadFinished() = 0;
public: public:
PriceUpdater(const DeckList *deck); AbstractPriceUpdater(const DeckList *deck);
void updatePrices(); virtual void updatePrices() = 0;
};
class BLPPriceUpdater : public AbstractPriceUpdater
{
Q_OBJECT
protected:
virtual void downloadFinished();
public:
BLPPriceUpdater(const DeckList *deck);
virtual void updatePrices();
};
class DBPriceUpdater : public AbstractPriceUpdater
{
Q_OBJECT
protected:
virtual void downloadFinished();
public:
DBPriceUpdater(const DeckList *deck);
virtual void updatePrices();
}; };
#endif #endif

View file

@ -45,6 +45,7 @@ SettingsCache::SettingsCache()
soundPath = settings->value("sound/path").toString(); soundPath = settings->value("sound/path").toString();
priceTagFeature = settings->value("deckeditor/pricetags", false).toBool(); priceTagFeature = settings->value("deckeditor/pricetags", false).toBool();
priceTagSource = settings->value("deckeditor/pricetagsource", 0).toInt();
ignoreUnregisteredUsers = settings->value("chat/ignore_unregistered", false).toBool(); ignoreUnregisteredUsers = settings->value("chat/ignore_unregistered", false).toBool();
} }
@ -247,6 +248,12 @@ void SettingsCache::setPriceTagFeature(int _priceTagFeature)
emit priceTagFeatureChanged(priceTagFeature); emit priceTagFeatureChanged(priceTagFeature);
} }
void SettingsCache::setPriceTagSource(int _priceTagSource)
{
priceTagSource = _priceTagSource;
settings->setValue("deckeditor/pricetagsource", priceTagSource);
}
void SettingsCache::setIgnoreUnregisteredUsers(bool _ignoreUnregisteredUsers) void SettingsCache::setIgnoreUnregisteredUsers(bool _ignoreUnregisteredUsers)
{ {
ignoreUnregisteredUsers = _ignoreUnregisteredUsers; ignoreUnregisteredUsers = _ignoreUnregisteredUsers;

View file

@ -53,6 +53,7 @@ private:
bool soundEnabled; bool soundEnabled;
QString soundPath; QString soundPath;
bool priceTagFeature; bool priceTagFeature;
int priceTagSource;
bool ignoreUnregisteredUsers; bool ignoreUnregisteredUsers;
QString picUrl; QString picUrl;
QString picUrlHq; QString picUrlHq;
@ -87,6 +88,7 @@ public:
bool getSoundEnabled() const { return soundEnabled; } bool getSoundEnabled() const { return soundEnabled; }
QString getSoundPath() const { return soundPath; } QString getSoundPath() const { return soundPath; }
bool getPriceTagFeature() const { return priceTagFeature; } bool getPriceTagFeature() const { return priceTagFeature; }
int getPriceTagSource() const { return priceTagSource; }
bool getIgnoreUnregisteredUsers() const { return ignoreUnregisteredUsers; } bool getIgnoreUnregisteredUsers() const { return ignoreUnregisteredUsers; }
QString getPicUrl() const { return picUrl; } QString getPicUrl() const { return picUrl; }
QString getPicUrlHq() const { return picUrlHq; } QString getPicUrlHq() const { return picUrlHq; }
@ -121,6 +123,7 @@ public slots:
void setSoundEnabled(int _soundEnabled); void setSoundEnabled(int _soundEnabled);
void setSoundPath(const QString &_soundPath); void setSoundPath(const QString &_soundPath);
void setPriceTagFeature(int _priceTagFeature); void setPriceTagFeature(int _priceTagFeature);
void setPriceTagSource(int _priceTagSource);
void setIgnoreUnregisteredUsers(bool _ignoreUnregisteredUsers); void setIgnoreUnregisteredUsers(bool _ignoreUnregisteredUsers);
void setPicUrl(const QString &_picUrl); void setPicUrl(const QString &_picUrl);
void setPicUrlHq(const QString &_picUrlHq); void setPicUrlHq(const QString &_picUrlHq);

View file

@ -644,7 +644,18 @@ void TabDeckEditor::setPriceTagFeatureEnabled(int enabled)
void TabDeckEditor::actUpdatePrices() void TabDeckEditor::actUpdatePrices()
{ {
aUpdatePrices->setDisabled(true); aUpdatePrices->setDisabled(true);
PriceUpdater *up = new PriceUpdater(deckModel->getDeckList()); AbstractPriceUpdater *up;
switch(settingsCache->getPriceTagSource())
{
case 0:
up = new BLPPriceUpdater(deckModel->getDeckList());
break;
case 1:
up = new DBPriceUpdater(deckModel->getDeckList());
break;
}
connect(up, SIGNAL(finishedUpdate()), this, SLOT(finishedUpdatingPrices())); connect(up, SIGNAL(finishedUpdate()), this, SLOT(finishedUpdatingPrices()));
up->updatePrices(); up->updatePrices();
} }