Import data from mtgjson's allsets.xml
* add multiverse id to cards.xml * remove pictures urls from cards.xml TBD: * fix/clean oracle’s gui * add support in cockatrice
This commit is contained in:
parent
0d3ec71e8f
commit
f3a57d5506
5 changed files with 193 additions and 268 deletions
|
@ -406,12 +406,27 @@ QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardInfo *info)
|
||||||
xml.writeTextElement("name", info->getName());
|
xml.writeTextElement("name", info->getName());
|
||||||
|
|
||||||
const SetList &sets = info->getSets();
|
const SetList &sets = info->getSets();
|
||||||
|
QString tmpString;
|
||||||
|
QString tmpSet;
|
||||||
for (int i = 0; i < sets.size(); i++) {
|
for (int i = 0; i < sets.size(); i++) {
|
||||||
xml.writeStartElement("set");
|
xml.writeStartElement("set");
|
||||||
xml.writeAttribute("picURL", info->getPicURL(sets[i]->getShortName()));
|
|
||||||
xml.writeAttribute("picURLHq", info->getPicURLHq(sets[i]->getShortName()));
|
tmpSet=sets[i]->getShortName();
|
||||||
xml.writeAttribute("picURLSt", info->getPicURLSt(sets[i]->getShortName()));
|
xml.writeAttribute("muId", QString::number(info->getMuId(tmpSet)));
|
||||||
xml.writeCharacters(sets[i]->getShortName());
|
|
||||||
|
tmpString = info->getPicURL(tmpSet);
|
||||||
|
if(!tmpString.isEmpty())
|
||||||
|
xml.writeAttribute("picURL", tmpString);
|
||||||
|
|
||||||
|
tmpString = info->getPicURLHq(tmpSet);
|
||||||
|
if(!tmpString.isEmpty())
|
||||||
|
xml.writeAttribute("picURLHq", tmpString);
|
||||||
|
|
||||||
|
tmpString = info->getPicURLSt(tmpSet);
|
||||||
|
if(!tmpString.isEmpty())
|
||||||
|
xml.writeAttribute("picURLSt", tmpString);
|
||||||
|
|
||||||
|
xml.writeCharacters(tmpSet);
|
||||||
xml.writeEndElement();
|
xml.writeEndElement();
|
||||||
}
|
}
|
||||||
const QStringList &colors = info->getColors();
|
const QStringList &colors = info->getColors();
|
||||||
|
|
|
@ -100,6 +100,7 @@ private:
|
||||||
QStringList colors;
|
QStringList colors;
|
||||||
int loyalty;
|
int loyalty;
|
||||||
QMap<QString, QString> picURLs, picURLsHq, picURLsSt;
|
QMap<QString, QString> picURLs, picURLsHq, picURLsSt;
|
||||||
|
QMap<QString, int> muIds;
|
||||||
bool cipt;
|
bool cipt;
|
||||||
int tableRow;
|
int tableRow;
|
||||||
QPixmap *pixmap;
|
QPixmap *pixmap;
|
||||||
|
@ -139,6 +140,7 @@ public:
|
||||||
QString getPicURL(const QString &set) const { return picURLs.value(set); }
|
QString getPicURL(const QString &set) const { return picURLs.value(set); }
|
||||||
QString getPicURLHq(const QString &set) const { return picURLsHq.value(set); }
|
QString getPicURLHq(const QString &set) const { return picURLsHq.value(set); }
|
||||||
QString getPicURLSt(const QString &set) const { return picURLsSt.value(set); }
|
QString getPicURLSt(const QString &set) const { return picURLsSt.value(set); }
|
||||||
|
int getMuId(const QString &set) const { return muIds.value(set); }
|
||||||
QString getPicURL() const;
|
QString getPicURL() const;
|
||||||
const QMap<QString, QString> &getPicURLs() const { return picURLs; }
|
const QMap<QString, QString> &getPicURLs() const { return picURLs; }
|
||||||
QString getMainCardType() const;
|
QString getMainCardType() const;
|
||||||
|
@ -149,6 +151,7 @@ public:
|
||||||
void setPicURL(const QString &_set, const QString &_picURL) { picURLs.insert(_set, _picURL); }
|
void setPicURL(const QString &_set, const QString &_picURL) { picURLs.insert(_set, _picURL); }
|
||||||
void setPicURLHq(const QString &_set, const QString &_picURL) { picURLsHq.insert(_set, _picURL); }
|
void setPicURLHq(const QString &_set, const QString &_picURL) { picURLsHq.insert(_set, _picURL); }
|
||||||
void setPicURLSt(const QString &_set, const QString &_picURL) { picURLsSt.insert(_set, _picURL); }
|
void setPicURLSt(const QString &_set, const QString &_picURL) { picURLsSt.insert(_set, _picURL); }
|
||||||
|
void setMuId(const QString &_set, const int &_muId) { muIds.insert(_set, _muId); }
|
||||||
void addToSet(CardSet *set);
|
void addToSet(CardSet *set);
|
||||||
QPixmap *loadPixmap();
|
QPixmap *loadPixmap();
|
||||||
QPixmap *getPixmap(QSize size);
|
QPixmap *getPixmap(QSize size);
|
||||||
|
|
|
@ -1,162 +1,138 @@
|
||||||
#include "oracleimporter.h"
|
#include "oracleimporter.h"
|
||||||
#include <QtGui>
|
#include <QtGui>
|
||||||
#include <QtNetwork>
|
|
||||||
#include <QXmlStreamReader>
|
|
||||||
#include <QDomDocument>
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
#include "qt-json/json.h"
|
#include "qt-json/json.h"
|
||||||
|
|
||||||
OracleImporter::OracleImporter(const QString &_dataDir, QObject *parent)
|
OracleImporter::OracleImporter(const QString &_dataDir, QObject *parent)
|
||||||
: CardDatabase(parent), dataDir(_dataDir), setIndex(-1)
|
: CardDatabase(parent), dataDir(_dataDir)
|
||||||
{
|
{
|
||||||
buffer = new QBuffer(this);
|
|
||||||
http = new QHttp(this);
|
|
||||||
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool)));
|
|
||||||
connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
|
|
||||||
connect(http, SIGNAL(dataReadProgress(int, int)), this, SIGNAL(dataReadProgress(int, int)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OracleImporter::readSetsFromFile(const QString &fileName)
|
bool OracleImporter::readSetsFromFile(const QString &fileName)
|
||||||
{
|
{
|
||||||
QFile setsFile(fileName);
|
QFile setsFile(fileName);
|
||||||
if (!setsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
if (!setsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
QMessageBox::critical(0, tr("Error"), tr("Cannot open file '%1'.").arg(fileName));
|
QMessageBox::critical(0, tr("Error"), tr("Cannot open file '%1'.").arg(fileName));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QXmlStreamReader xml(&setsFile);
|
return readSetsFromByteArray(setsFile.readAll());
|
||||||
return readSetsFromXml(xml);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OracleImporter::readSetsFromByteArray(const QByteArray &data)
|
bool OracleImporter::readSetsFromByteArray(const QByteArray &data)
|
||||||
{
|
{
|
||||||
QXmlStreamReader xml(data);
|
QList<SetToDownload> newSetList;
|
||||||
return readSetsFromXml(xml);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OracleImporter::readSetsFromXml(QXmlStreamReader &xml)
|
|
||||||
{
|
|
||||||
QList<SetToDownload> newSetList;
|
|
||||||
|
|
||||||
QString edition;
|
|
||||||
QString editionLong;
|
|
||||||
QString editionURL;
|
|
||||||
while (!xml.atEnd()) {
|
|
||||||
if (xml.readNext() == QXmlStreamReader::EndElement)
|
|
||||||
break;
|
|
||||||
if (xml.name() == "set") {
|
|
||||||
bool import = xml.attributes().value("import").toString().toInt();
|
|
||||||
while (!xml.atEnd()) {
|
|
||||||
if (xml.readNext() == QXmlStreamReader::EndElement)
|
|
||||||
break;
|
|
||||||
if (xml.name() == "name")
|
|
||||||
edition = xml.readElementText();
|
|
||||||
else if (xml.name() == "longname")
|
|
||||||
editionLong = xml.readElementText();
|
|
||||||
else if (xml.name() == "url")
|
|
||||||
editionURL = xml.readElementText();
|
|
||||||
}
|
|
||||||
newSetList.append(SetToDownload(edition, editionLong, editionURL, import));
|
|
||||||
edition = editionLong = editionURL = QString();
|
|
||||||
} else if (xml.name() == "picture_url")
|
|
||||||
pictureUrl = xml.readElementText();
|
|
||||||
else if (xml.name() == "picture_url_hq")
|
|
||||||
pictureUrlHq = xml.readElementText();
|
|
||||||
else if (xml.name() == "picture_url_st")
|
|
||||||
pictureUrlSt = xml.readElementText();
|
|
||||||
else if (xml.name() == "set_url")
|
|
||||||
setUrl = xml.readElementText();
|
|
||||||
}
|
|
||||||
if (newSetList.isEmpty())
|
|
||||||
return false;
|
|
||||||
allSets = newSetList;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
CardInfo *OracleImporter::addCard(const QString &setName,
|
|
||||||
QString cardName,
|
|
||||||
bool isToken,
|
|
||||||
int cardId,
|
|
||||||
const QString &cardCost,
|
|
||||||
const QString &cardType,
|
|
||||||
const QString &cardPT,
|
|
||||||
int cardLoyalty,
|
|
||||||
const QStringList &cardText)
|
|
||||||
{
|
|
||||||
QString fullCardText = cardText.join("\n");
|
|
||||||
bool splitCard = false;
|
|
||||||
if (cardName.contains('(')) {
|
|
||||||
cardName.remove(QRegExp(" \\(.*\\)"));
|
|
||||||
splitCard = true;
|
|
||||||
}
|
|
||||||
// Workaround for card name weirdness
|
|
||||||
if (cardName.contains("XX"))
|
|
||||||
cardName.remove("XX");
|
|
||||||
cardName = cardName.replace("Æ", "AE");
|
|
||||||
cardName = cardName.replace("’", "'");
|
|
||||||
|
|
||||||
CardInfo *card;
|
|
||||||
if (cardHash.contains(cardName)) {
|
|
||||||
card = cardHash.value(cardName);
|
|
||||||
if (splitCard && !card->getText().contains(fullCardText))
|
|
||||||
card->setText(card->getText() + "\n---\n" + fullCardText);
|
|
||||||
} else {
|
|
||||||
bool mArtifact = false;
|
|
||||||
if (cardType.endsWith("Artifact"))
|
|
||||||
for (int i = 0; i < cardText.size(); ++i)
|
|
||||||
if (cardText[i].contains("{T}") && cardText[i].contains("to your mana pool"))
|
|
||||||
mArtifact = true;
|
|
||||||
|
|
||||||
QStringList colors;
|
|
||||||
QStringList allColors = QStringList() << "W" << "U" << "B" << "R" << "G";
|
|
||||||
for (int i = 0; i < allColors.size(); i++)
|
|
||||||
if (cardCost.contains(allColors[i]))
|
|
||||||
colors << allColors[i];
|
|
||||||
|
|
||||||
if (cardText.contains(cardName + " is white."))
|
|
||||||
colors << "W";
|
|
||||||
if (cardText.contains(cardName + " is blue."))
|
|
||||||
colors << "U";
|
|
||||||
if (cardText.contains(cardName + " is black."))
|
|
||||||
colors << "B";
|
|
||||||
if (cardText.contains(cardName + " is red."))
|
|
||||||
colors << "R";
|
|
||||||
if (cardText.contains(cardName + " is green."))
|
|
||||||
colors << "G";
|
|
||||||
|
|
||||||
bool cipt = (cardText.contains(cardName + " enters the battlefield tapped."));
|
|
||||||
|
|
||||||
card = new CardInfo(this, cardName, isToken, cardCost, cardType, cardPT, fullCardText, colors, cardLoyalty, cipt);
|
|
||||||
int tableRow = 1;
|
|
||||||
QString mainCardType = card->getMainCardType();
|
|
||||||
if ((mainCardType == "Land") || mArtifact)
|
|
||||||
tableRow = 0;
|
|
||||||
else if ((mainCardType == "Sorcery") || (mainCardType == "Instant"))
|
|
||||||
tableRow = 3;
|
|
||||||
else if (mainCardType == "Creature")
|
|
||||||
tableRow = 2;
|
|
||||||
card->setTableRow(tableRow);
|
|
||||||
|
|
||||||
cardHash.insert(cardName, card);
|
|
||||||
}
|
|
||||||
card->setPicURL(setName, getPictureUrl(pictureUrl, cardId, cardName, setName));
|
|
||||||
card->setPicURLHq(setName, getPictureUrl(pictureUrlHq, cardId, cardName, setName));
|
|
||||||
card->setPicURLSt(setName, getPictureUrl(pictureUrlSt, cardId, cardName, setName));
|
|
||||||
return card;
|
|
||||||
}
|
|
||||||
|
|
||||||
int OracleImporter::importTextSpoiler(CardSet *set, const QByteArray &data)
|
|
||||||
{
|
|
||||||
int cards = 0;
|
|
||||||
bool ok;
|
bool ok;
|
||||||
QVariantMap resultMap = QtJson::Json::parse(QString(data), ok).toMap();
|
setsMap = QtJson::Json::parse(QString(data), ok).toMap();
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
qDebug() << "error: QtJson::Json::parse()";
|
qDebug() << "error: QtJson::Json::parse()";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
QListIterator<QVariant> it(resultMap.value("cards").toList());
|
QListIterator<QVariant> it(setsMap.values());
|
||||||
|
QVariantMap map;
|
||||||
|
|
||||||
|
QString edition;
|
||||||
|
QString editionLong;
|
||||||
|
QVariant editionCards;
|
||||||
|
bool import;
|
||||||
|
|
||||||
|
while (it.hasNext()) {
|
||||||
|
map = it.next().toMap();
|
||||||
|
edition = map.value("code").toString();
|
||||||
|
editionLong = map.value("name").toString();
|
||||||
|
editionCards = map.value("cards");
|
||||||
|
|
||||||
|
// core and expansion sets are marked to be imported by default
|
||||||
|
import = (0 == QString::compare(map.value("type").toString(), QString("core"), Qt::CaseInsensitive) ||
|
||||||
|
0 == QString::compare(map.value("type").toString(), QString("expansion"), Qt::CaseInsensitive));
|
||||||
|
|
||||||
|
newSetList.append(SetToDownload(edition, editionLong, editionCards, import));
|
||||||
|
}
|
||||||
|
if (newSetList.isEmpty())
|
||||||
|
return false;
|
||||||
|
allSets = newSetList;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
CardInfo *OracleImporter::addCard(const QString &setName,
|
||||||
|
QString cardName,
|
||||||
|
bool isToken,
|
||||||
|
int cardId,
|
||||||
|
const QString &cardCost,
|
||||||
|
const QString &cardType,
|
||||||
|
const QString &cardPT,
|
||||||
|
int cardLoyalty,
|
||||||
|
const QStringList &cardText)
|
||||||
|
{
|
||||||
|
QString fullCardText = cardText.join("\n");
|
||||||
|
bool splitCard = false;
|
||||||
|
if (cardName.contains('(')) {
|
||||||
|
cardName.remove(QRegExp(" \\(.*\\)"));
|
||||||
|
splitCard = true;
|
||||||
|
}
|
||||||
|
// Workaround for card name weirdness
|
||||||
|
if (cardName.contains("XX"))
|
||||||
|
cardName.remove("XX");
|
||||||
|
cardName = cardName.replace("Æ", "AE");
|
||||||
|
cardName = cardName.replace("’", "'");
|
||||||
|
|
||||||
|
CardInfo *card;
|
||||||
|
if (cardHash.contains(cardName)) {
|
||||||
|
card = cardHash.value(cardName);
|
||||||
|
if (splitCard && !card->getText().contains(fullCardText))
|
||||||
|
card->setText(card->getText() + "\n---\n" + fullCardText);
|
||||||
|
} else {
|
||||||
|
bool mArtifact = false;
|
||||||
|
if (cardType.endsWith("Artifact"))
|
||||||
|
for (int i = 0; i < cardText.size(); ++i)
|
||||||
|
if (cardText[i].contains("{T}") && cardText[i].contains("to your mana pool"))
|
||||||
|
mArtifact = true;
|
||||||
|
|
||||||
|
QStringList colors;
|
||||||
|
QStringList allColors = QStringList() << "W" << "U" << "B" << "R" << "G";
|
||||||
|
for (int i = 0; i < allColors.size(); i++)
|
||||||
|
if (cardCost.contains(allColors[i]))
|
||||||
|
colors << allColors[i];
|
||||||
|
|
||||||
|
if (cardText.contains(cardName + " is white."))
|
||||||
|
colors << "W";
|
||||||
|
if (cardText.contains(cardName + " is blue."))
|
||||||
|
colors << "U";
|
||||||
|
if (cardText.contains(cardName + " is black."))
|
||||||
|
colors << "B";
|
||||||
|
if (cardText.contains(cardName + " is red."))
|
||||||
|
colors << "R";
|
||||||
|
if (cardText.contains(cardName + " is green."))
|
||||||
|
colors << "G";
|
||||||
|
|
||||||
|
bool cipt = (cardText.contains(cardName + " enters the battlefield tapped."));
|
||||||
|
|
||||||
|
card = new CardInfo(this, cardName, isToken, cardCost, cardType, cardPT, fullCardText, colors, cardLoyalty, cipt);
|
||||||
|
int tableRow = 1;
|
||||||
|
QString mainCardType = card->getMainCardType();
|
||||||
|
if ((mainCardType == "Land") || mArtifact)
|
||||||
|
tableRow = 0;
|
||||||
|
else if ((mainCardType == "Sorcery") || (mainCardType == "Instant"))
|
||||||
|
tableRow = 3;
|
||||||
|
else if (mainCardType == "Creature")
|
||||||
|
tableRow = 2;
|
||||||
|
card->setTableRow(tableRow);
|
||||||
|
|
||||||
|
cardHash.insert(cardName, card);
|
||||||
|
}
|
||||||
|
card->setMuId(setName, cardId);
|
||||||
|
|
||||||
|
return card;
|
||||||
|
}
|
||||||
|
|
||||||
|
int OracleImporter::importTextSpoiler(CardSet *set, const QVariant &data)
|
||||||
|
{
|
||||||
|
int cards = 0;
|
||||||
|
|
||||||
|
QListIterator<QVariant> it(data.toList());
|
||||||
QVariantMap map;
|
QVariantMap map;
|
||||||
QString cardName;
|
QString cardName;
|
||||||
QString cardCost;
|
QString cardCost;
|
||||||
|
@ -237,111 +213,52 @@ int OracleImporter::importTextSpoiler(CardSet *set, const QByteArray &data)
|
||||||
|
|
||||||
return cards;
|
return cards;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
QString OracleImporter::getPictureUrl(QString url, int cardId, QString name, const QString &setName) const
|
QString OracleImporter::getPictureUrl(QString url, int cardId, QString name, const QString &setName) const
|
||||||
{
|
{
|
||||||
if ((name == "Island") || (name == "Swamp") || (name == "Mountain") || (name == "Plains") || (name == "Forest"))
|
if ((name == "Island") || (name == "Swamp") || (name == "Mountain") || (name == "Plains") || (name == "Forest"))
|
||||||
name.append("1");
|
name.append("1");
|
||||||
return url.replace("!cardid!", QString::number(cardId)).replace("!set!", setName).replace("!name!", name
|
return url.replace("!cardid!", QString::number(cardId)).replace("!set!", setName).replace("!name!", name
|
||||||
.replace("ö", "o")
|
.replace("ö", "o")
|
||||||
// .remove('\'')
|
// .remove('\'')
|
||||||
.remove(" // ")
|
.remove(" // ")
|
||||||
// .remove(',')
|
// .remove(',')
|
||||||
// .remove(':')
|
// .remove(':')
|
||||||
// .remove('.')
|
// .remove('.')
|
||||||
.remove(QRegExp("\\(.*\\)"))
|
.remove(QRegExp("\\(.*\\)"))
|
||||||
.simplified()
|
.simplified()
|
||||||
// .replace(' ', '_')
|
// .replace(' ', '_')
|
||||||
// .replace('-', '_')
|
// .replace('-', '_')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
int OracleImporter::startDownload()
|
int OracleImporter::startImport()
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
|
|
||||||
setsToDownload.clear();
|
int setCards = 0, setIndex= 0;
|
||||||
for (int i = 0; i < allSets.size(); ++i)
|
QListIterator<SetToDownload> it(allSets);
|
||||||
if (allSets[i].getImport())
|
const SetToDownload * curSet;
|
||||||
setsToDownload.append(allSets[i]);
|
|
||||||
|
while (it.hasNext())
|
||||||
if (setsToDownload.isEmpty())
|
{
|
||||||
return 0;
|
curSet = & it.next();
|
||||||
setIndex = 0;
|
|
||||||
emit setIndexChanged(0, 0, setsToDownload[0].getLongName());
|
emit setIndexChanged(0, 0, curSet->getLongName());
|
||||||
|
|
||||||
downloadNextFile();
|
CardSet *set = new CardSet(curSet->getShortName(), curSet->getLongName());
|
||||||
return setsToDownload.size();
|
if (!setHash.contains(set->getShortName()))
|
||||||
}
|
setHash.insert(set->getShortName(), set);
|
||||||
|
|
||||||
void OracleImporter::downloadNextFile()
|
int setCards = importTextSpoiler(set, curSet->getCards());
|
||||||
{
|
|
||||||
QString urlString = setsToDownload[setIndex].getUrl();
|
++setIndex;
|
||||||
if (urlString.isEmpty())
|
|
||||||
urlString = setUrl;
|
emit setIndexChanged(setCards, setIndex, curSet->getLongName());
|
||||||
urlString = urlString.replace("!name!", setsToDownload[setIndex].getShortName());
|
}
|
||||||
|
|
||||||
if (urlString.startsWith("http://")) {
|
emit setIndexChanged(setCards, setIndex, QString());
|
||||||
QUrl url(urlString);
|
|
||||||
http->setHost(url.host(), QHttp::ConnectionModeHttp, url.port() == -1 ? 0 : url.port());
|
// total number of sets
|
||||||
QString path = QUrl::toPercentEncoding(urlString.mid(url.host().size() + 7).replace(' ', '+'), "?!$&'()*+,;=:@/");
|
return setIndex;
|
||||||
|
|
||||||
buffer->close();
|
|
||||||
buffer->setData(QByteArray());
|
|
||||||
buffer->open(QIODevice::ReadWrite | QIODevice::Text);
|
|
||||||
reqId = http->get(path, buffer);
|
|
||||||
} else {
|
|
||||||
QFile file(dataDir + "/" + urlString);
|
|
||||||
file.open(QIODevice::ReadOnly | QIODevice::Text);
|
|
||||||
|
|
||||||
buffer->close();
|
|
||||||
buffer->setData(file.readAll());
|
|
||||||
buffer->open(QIODevice::ReadWrite | QIODevice::Text);
|
|
||||||
reqId = 0;
|
|
||||||
httpRequestFinished(reqId, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OracleImporter::httpRequestFinished(int requestId, bool error)
|
|
||||||
{
|
|
||||||
if (error) {
|
|
||||||
QMessageBox::information(0, tr("HTTP"), tr("Error."));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (requestId != reqId)
|
|
||||||
return;
|
|
||||||
|
|
||||||
CardSet *set = new CardSet(setsToDownload[setIndex].getShortName(), setsToDownload[setIndex].getLongName());
|
|
||||||
if (!setHash.contains(set->getShortName()))
|
|
||||||
setHash.insert(set->getShortName(), set);
|
|
||||||
|
|
||||||
buffer->seek(0);
|
|
||||||
buffer->close();
|
|
||||||
int cards = importTextSpoiler(set, buffer->data());
|
|
||||||
if (cards > 0)
|
|
||||||
++setIndex;
|
|
||||||
|
|
||||||
if (setIndex == setsToDownload.size()) {
|
|
||||||
emit setIndexChanged(cards, setIndex, QString());
|
|
||||||
setIndex = -1;
|
|
||||||
} else {
|
|
||||||
downloadNextFile();
|
|
||||||
emit setIndexChanged(cards, setIndex, setsToDownload[setIndex].getLongName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OracleImporter::readResponseHeader(const QHttpResponseHeader &responseHeader)
|
|
||||||
{
|
|
||||||
switch (responseHeader.statusCode()) {
|
|
||||||
case 200:
|
|
||||||
case 301:
|
|
||||||
case 302:
|
|
||||||
case 303:
|
|
||||||
case 307:
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
QMessageBox::information(0, tr("HTTP"), tr("Download failed: %1.").arg(responseHeader.reasonPhrase()));
|
|
||||||
http->abort();
|
|
||||||
deleteLater();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,44 +1,34 @@
|
||||||
#ifndef ORACLEIMPORTER_H
|
#ifndef ORACLEIMPORTER_H
|
||||||
#define ORACLEIMPORTER_H
|
#define ORACLEIMPORTER_H
|
||||||
|
|
||||||
#include <carddatabase.h>
|
#include <QMap>
|
||||||
#include <QHttp>
|
|
||||||
|
|
||||||
class QBuffer;
|
#include <carddatabase.h>
|
||||||
class QXmlStreamReader;
|
|
||||||
|
|
||||||
class SetToDownload {
|
class SetToDownload {
|
||||||
private:
|
private:
|
||||||
QString shortName, longName, url;
|
QString shortName, longName;
|
||||||
bool import;
|
bool import;
|
||||||
|
QVariant cards;
|
||||||
public:
|
public:
|
||||||
const QString &getShortName() const { return shortName; }
|
const QString &getShortName() const { return shortName; }
|
||||||
const QString &getLongName() const { return longName; }
|
const QString &getLongName() const { return longName; }
|
||||||
const QString &getUrl() const { return url; }
|
const QVariant &getCards() const { return cards; }
|
||||||
bool getImport() const { return import; }
|
bool getImport() const { return import; }
|
||||||
void setImport(bool _import) { import = _import; }
|
void setImport(bool _import) { import = _import; }
|
||||||
SetToDownload(const QString &_shortName, const QString &_longName, const QString &_url, bool _import)
|
SetToDownload(const QString &_shortName, const QString &_longName, const QVariant &_cards, bool _import)
|
||||||
: shortName(_shortName), longName(_longName), url(_url), import(_import) { }
|
: shortName(_shortName), longName(_longName), cards(_cards), import(_import) { }
|
||||||
};
|
};
|
||||||
|
|
||||||
class OracleImporter : public CardDatabase {
|
class OracleImporter : public CardDatabase {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private:
|
private:
|
||||||
QList<SetToDownload> allSets, setsToDownload;
|
QList<SetToDownload> allSets;
|
||||||
QString pictureUrl, pictureUrlHq, pictureUrlSt, setUrl;
|
QVariantMap setsMap;
|
||||||
QString dataDir;
|
QString dataDir;
|
||||||
int setIndex;
|
|
||||||
int reqId;
|
|
||||||
QBuffer *buffer;
|
|
||||||
QHttp *http;
|
|
||||||
QString getPictureUrl(QString url, int cardId, QString name, const QString &setName) const;
|
|
||||||
|
|
||||||
void downloadNextFile();
|
void downloadNextFile();
|
||||||
bool readSetsFromXml(QXmlStreamReader &xml);
|
|
||||||
CardInfo *addCard(const QString &setName, QString cardName, bool isToken, int cardId, const QString &cardCost, const QString &cardType, const QString &cardPT, int cardLoyalty, const QStringList &cardText);
|
CardInfo *addCard(const QString &setName, QString cardName, bool isToken, int cardId, const QString &cardCost, const QString &cardType, const QString &cardPT, int cardLoyalty, const QStringList &cardText);
|
||||||
private slots:
|
|
||||||
void httpRequestFinished(int requestId, bool error);
|
|
||||||
void readResponseHeader(const QHttpResponseHeader &responseHeader);
|
|
||||||
signals:
|
signals:
|
||||||
void setIndexChanged(int cardsImported, int setIndex, const QString &nextSetName);
|
void setIndexChanged(int cardsImported, int setIndex, const QString &nextSetName);
|
||||||
void dataReadProgress(int bytesRead, int totalBytes);
|
void dataReadProgress(int bytesRead, int totalBytes);
|
||||||
|
@ -46,8 +36,8 @@ public:
|
||||||
OracleImporter(const QString &_dataDir, QObject *parent = 0);
|
OracleImporter(const QString &_dataDir, QObject *parent = 0);
|
||||||
bool readSetsFromByteArray(const QByteArray &data);
|
bool readSetsFromByteArray(const QByteArray &data);
|
||||||
bool readSetsFromFile(const QString &fileName);
|
bool readSetsFromFile(const QString &fileName);
|
||||||
int startDownload();
|
int startImport();
|
||||||
int importTextSpoiler(CardSet *set, const QByteArray &data);
|
int importTextSpoiler(CardSet *set, const QVariant &data);
|
||||||
QList<SetToDownload> &getSets() { return allSets; }
|
QList<SetToDownload> &getSets() { return allSets; }
|
||||||
const QString &getDataDir() const { return dataDir; }
|
const QString &getDataDir() const { return dataDir; }
|
||||||
};
|
};
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
#include "window_main.h"
|
#include "window_main.h"
|
||||||
#include "oracleimporter.h"
|
#include "oracleimporter.h"
|
||||||
|
|
||||||
const QString WindowMain::defaultSetsUrl = QString("http://www.woogerworks.com/files/sets.xml");
|
const QString WindowMain::defaultSetsUrl = QString("http://mtgjson.com/json/AllSets.json");
|
||||||
|
|
||||||
WindowMain::WindowMain(QWidget *parent)
|
WindowMain::WindowMain(QWidget *parent)
|
||||||
: QMainWindow(parent)
|
: QMainWindow(parent)
|
||||||
|
@ -45,7 +45,7 @@ WindowMain::WindowMain(QWidget *parent)
|
||||||
checkAllButtonLayout->addWidget(checkAllButton);
|
checkAllButtonLayout->addWidget(checkAllButton);
|
||||||
checkAllButtonLayout->addWidget(uncheckAllButton);
|
checkAllButtonLayout->addWidget(uncheckAllButton);
|
||||||
|
|
||||||
startButton = new QPushButton(tr("&Start download"));
|
startButton = new QPushButton(tr("&Start import"));
|
||||||
connect(startButton, SIGNAL(clicked()), this, SLOT(actStart()));
|
connect(startButton, SIGNAL(clicked()), this, SLOT(actStart()));
|
||||||
|
|
||||||
QVBoxLayout *settingsLayout = new QVBoxLayout;
|
QVBoxLayout *settingsLayout = new QVBoxLayout;
|
||||||
|
@ -130,7 +130,7 @@ void WindowMain::actLoadSetsFile()
|
||||||
{
|
{
|
||||||
QFileDialog dialog(this, tr("Load sets file"));
|
QFileDialog dialog(this, tr("Load sets file"));
|
||||||
dialog.setFileMode(QFileDialog::ExistingFile);
|
dialog.setFileMode(QFileDialog::ExistingFile);
|
||||||
dialog.setNameFilter("Sets XML file (*.xml)");
|
dialog.setNameFilter("Sets JSON file (*.json)");
|
||||||
if (!dialog.exec())
|
if (!dialog.exec())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -224,7 +224,7 @@ void WindowMain::actUncheckAll()
|
||||||
|
|
||||||
void WindowMain::actStart()
|
void WindowMain::actStart()
|
||||||
{
|
{
|
||||||
int setsCount = importer->startDownload();
|
int setsCount = importer->startImport();
|
||||||
if (!setsCount) {
|
if (!setsCount) {
|
||||||
QMessageBox::critical(this, tr("Error"), tr("No sets to download selected."));
|
QMessageBox::critical(this, tr("Error"), tr("No sets to download selected."));
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in a new issue