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:
parent
ec3690fd29
commit
922e98af67
7 changed files with 222 additions and 12 deletions
|
@ -18,6 +18,7 @@
|
|||
#include <QInputDialog>
|
||||
#include <QSpinBox>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QRadioButton>
|
||||
#include <QDebug>
|
||||
#include "carddatabase.h"
|
||||
#include "dlg_settings.h"
|
||||
|
@ -532,8 +533,26 @@ DeckEditorSettingsPage::DeckEditorSettingsPage()
|
|||
priceTagsCheckBox->setChecked(settingsCache->getPriceTagFeature());
|
||||
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;
|
||||
generalGrid->addWidget(priceTagsCheckBox, 0, 0);
|
||||
generalGrid->addWidget(priceTagSource0, 1, 0);
|
||||
generalGrid->addWidget(priceTagSource1, 2, 0);
|
||||
|
||||
generalGroupBox = new QGroupBox;
|
||||
generalGroupBox->setLayout(generalGrid);
|
||||
|
@ -546,10 +565,26 @@ DeckEditorSettingsPage::DeckEditorSettingsPage()
|
|||
|
||||
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"));
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
aAdd = new QAction(this);
|
||||
|
|
|
@ -15,6 +15,7 @@ class QCheckBox;
|
|||
class QLabel;
|
||||
class QCloseEvent;
|
||||
class QSpinBox;
|
||||
class QRadioButton;
|
||||
|
||||
class AbstractSettingsPage : public QWidget {
|
||||
public:
|
||||
|
@ -100,8 +101,11 @@ class DeckEditorSettingsPage : public AbstractSettingsPage {
|
|||
public:
|
||||
DeckEditorSettingsPage();
|
||||
void retranslateUi();
|
||||
private slots:
|
||||
void radioPriceTagSourceClicked(bool checked);
|
||||
private:
|
||||
QCheckBox *priceTagsCheckBox;
|
||||
QRadioButton *priceTagSource0, *priceTagSource1;
|
||||
QGroupBox *generalGroupBox;
|
||||
};
|
||||
|
||||
|
|
|
@ -4,25 +4,43 @@
|
|||
*/
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "qt-json/json.h"
|
||||
#include "priceupdater.h"
|
||||
|
||||
#if QT_VERSION < 0x050000
|
||||
// for Qt::escape()
|
||||
#include <QtGui/qtextdocument.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param _deck deck.
|
||||
*/
|
||||
PriceUpdater::PriceUpdater(const DeckList *_deck)
|
||||
AbstractPriceUpdater::AbstractPriceUpdater(const DeckList *_deck)
|
||||
{
|
||||
nam = new QNetworkAccessManager(this);
|
||||
deck = _deck;
|
||||
}
|
||||
|
||||
// blacklotusproject.com
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param _deck deck.
|
||||
*/
|
||||
BLPPriceUpdater::BLPPriceUpdater(const DeckList *_deck)
|
||||
: AbstractPriceUpdater(_deck)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the prices of the cards in deckList.
|
||||
*/
|
||||
void PriceUpdater::updatePrices()
|
||||
void BLPPriceUpdater::updatePrices()
|
||||
{
|
||||
QString q = "http://blacklotusproject.com/json/?cards=";
|
||||
QStringList cards = deck->getCardList();
|
||||
|
@ -38,7 +56,7 @@ void PriceUpdater::updatePrices()
|
|||
/**
|
||||
* 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());
|
||||
bool ok;
|
||||
|
@ -82,3 +100,115 @@ void PriceUpdater::downloadFinished()
|
|||
deleteLater();
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -11,18 +11,38 @@ class QNetworkAccessManager;
|
|||
*
|
||||
* @author Marcio Ribeiro <mmr@b1n.org>
|
||||
*/
|
||||
class PriceUpdater : public QObject
|
||||
class AbstractPriceUpdater : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
private:
|
||||
protected:
|
||||
const DeckList *deck;
|
||||
QNetworkAccessManager *nam;
|
||||
signals:
|
||||
void finishedUpdate();
|
||||
private slots:
|
||||
void downloadFinished();
|
||||
protected slots:
|
||||
virtual void downloadFinished() = 0;
|
||||
public:
|
||||
PriceUpdater(const DeckList *deck);
|
||||
void updatePrices();
|
||||
AbstractPriceUpdater(const DeckList *deck);
|
||||
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
|
||||
|
|
|
@ -45,6 +45,7 @@ SettingsCache::SettingsCache()
|
|||
soundPath = settings->value("sound/path").toString();
|
||||
|
||||
priceTagFeature = settings->value("deckeditor/pricetags", false).toBool();
|
||||
priceTagSource = settings->value("deckeditor/pricetagsource", 0).toInt();
|
||||
|
||||
ignoreUnregisteredUsers = settings->value("chat/ignore_unregistered", false).toBool();
|
||||
}
|
||||
|
@ -247,6 +248,12 @@ void SettingsCache::setPriceTagFeature(int _priceTagFeature)
|
|||
emit priceTagFeatureChanged(priceTagFeature);
|
||||
}
|
||||
|
||||
void SettingsCache::setPriceTagSource(int _priceTagSource)
|
||||
{
|
||||
priceTagSource = _priceTagSource;
|
||||
settings->setValue("deckeditor/pricetagsource", priceTagSource);
|
||||
}
|
||||
|
||||
void SettingsCache::setIgnoreUnregisteredUsers(bool _ignoreUnregisteredUsers)
|
||||
{
|
||||
ignoreUnregisteredUsers = _ignoreUnregisteredUsers;
|
||||
|
|
|
@ -53,6 +53,7 @@ private:
|
|||
bool soundEnabled;
|
||||
QString soundPath;
|
||||
bool priceTagFeature;
|
||||
int priceTagSource;
|
||||
bool ignoreUnregisteredUsers;
|
||||
QString picUrl;
|
||||
QString picUrlHq;
|
||||
|
@ -87,6 +88,7 @@ public:
|
|||
bool getSoundEnabled() const { return soundEnabled; }
|
||||
QString getSoundPath() const { return soundPath; }
|
||||
bool getPriceTagFeature() const { return priceTagFeature; }
|
||||
int getPriceTagSource() const { return priceTagSource; }
|
||||
bool getIgnoreUnregisteredUsers() const { return ignoreUnregisteredUsers; }
|
||||
QString getPicUrl() const { return picUrl; }
|
||||
QString getPicUrlHq() const { return picUrlHq; }
|
||||
|
@ -121,6 +123,7 @@ public slots:
|
|||
void setSoundEnabled(int _soundEnabled);
|
||||
void setSoundPath(const QString &_soundPath);
|
||||
void setPriceTagFeature(int _priceTagFeature);
|
||||
void setPriceTagSource(int _priceTagSource);
|
||||
void setIgnoreUnregisteredUsers(bool _ignoreUnregisteredUsers);
|
||||
void setPicUrl(const QString &_picUrl);
|
||||
void setPicUrlHq(const QString &_picUrlHq);
|
||||
|
|
|
@ -644,7 +644,18 @@ void TabDeckEditor::setPriceTagFeatureEnabled(int enabled)
|
|||
void TabDeckEditor::actUpdatePrices()
|
||||
{
|
||||
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()));
|
||||
up->updatePrices();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue