extra files
This commit is contained in:
parent
6226e381de
commit
4444a0c16c
3 changed files with 109 additions and 0 deletions
BIN
cockatrice/resources/icon_update.png
Normal file
BIN
cockatrice/resources/icon_update.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.1 KiB |
81
cockatrice/src/priceupdater.cpp
Normal file
81
cockatrice/src/priceupdater.cpp
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
/**
|
||||||
|
* @author Marcio Ribeiro <mmr@b1n.org>
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
#include <QNetworkAccessManager>
|
||||||
|
#include <QNetworkReply>
|
||||||
|
|
||||||
|
#include <QScriptEngine>
|
||||||
|
#include <QScriptValueIterator>
|
||||||
|
#include "priceupdater.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param _deck deck.
|
||||||
|
*/
|
||||||
|
PriceUpdater::PriceUpdater(const DeckList *_deck)
|
||||||
|
{
|
||||||
|
nam = new QNetworkAccessManager(this);
|
||||||
|
deck = _deck;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the prices of the cards in deckList.
|
||||||
|
*/
|
||||||
|
void PriceUpdater::updatePrices()
|
||||||
|
{
|
||||||
|
QString q = "http://blacklotusproject.com/json/?cards=";
|
||||||
|
QStringList cards = deck->getCardList();
|
||||||
|
for (int i = 0; i < cards.size(); ++i) {
|
||||||
|
q += cards[i] + "|";
|
||||||
|
}
|
||||||
|
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 PriceUpdater::downloadFinished()
|
||||||
|
{
|
||||||
|
QMap<QString, DecklistCardNode *> cmap;
|
||||||
|
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;
|
||||||
|
cmap.insert(currentCard->getName().toLower(), currentCard);
|
||||||
|
currentCard->setPrice(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
|
||||||
|
QByteArray result = reply->readAll();
|
||||||
|
QScriptValue sc;
|
||||||
|
QScriptEngine engine;
|
||||||
|
sc = engine.evaluate("value = " + result);
|
||||||
|
|
||||||
|
if (sc.property("cards").isArray()) {
|
||||||
|
QScriptValueIterator it(sc.property("cards"));
|
||||||
|
while (it.hasNext()) {
|
||||||
|
it.next();
|
||||||
|
QString name = it.value().property("name").toString().toLower();
|
||||||
|
float price = it.value().property("average").toString().toFloat();
|
||||||
|
DecklistCardNode *c = cmap[name];
|
||||||
|
if (!c)
|
||||||
|
continue;
|
||||||
|
if (c->getPrice() == 0 || c->getPrice() > price) {
|
||||||
|
c->setPrice(price);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reply->deleteLater();
|
||||||
|
deleteLater();
|
||||||
|
emit finishedUpdate();
|
||||||
|
}
|
28
cockatrice/src/priceupdater.h
Normal file
28
cockatrice/src/priceupdater.h
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#ifndef PRICEUPDATER_H
|
||||||
|
#define PRICEUPDATER_H
|
||||||
|
|
||||||
|
#include <QNetworkReply>
|
||||||
|
#include "decklist.h"
|
||||||
|
|
||||||
|
class QNetworkAccessManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Price Updater.
|
||||||
|
*
|
||||||
|
* @author Marcio Ribeiro <mmr@b1n.org>
|
||||||
|
*/
|
||||||
|
class PriceUpdater : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
private:
|
||||||
|
const DeckList *deck;
|
||||||
|
QNetworkAccessManager *nam;
|
||||||
|
signals:
|
||||||
|
void finishedUpdate();
|
||||||
|
private slots:
|
||||||
|
void downloadFinished();
|
||||||
|
public:
|
||||||
|
PriceUpdater(const DeckList *deck);
|
||||||
|
void updatePrices();
|
||||||
|
};
|
||||||
|
#endif
|
Loading…
Reference in a new issue