XML card database
This commit is contained in:
parent
a3f6adddc9
commit
b8bf18801f
5 changed files with 126 additions and 94 deletions
|
@ -13,16 +13,14 @@ CardSet::CardSet(const QString &_shortName, const QString &_longName)
|
||||||
updateSortKey();
|
updateSortKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CardSet::loadFromStream(QDataStream &stream)
|
QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardSet *set)
|
||||||
{
|
{
|
||||||
stream >> shortName >> longName;
|
xml.writeStartElement("set");
|
||||||
updateSortKey();
|
xml.writeTextElement("name", set->getShortName());
|
||||||
qDebug(QString("set loaded: %1, %2").arg(shortName).arg(longName).toLatin1());
|
xml.writeTextElement("longname", set->getLongName());
|
||||||
}
|
xml.writeEndElement();
|
||||||
|
|
||||||
void CardSet::saveToStream(QDataStream &stream)
|
return xml;
|
||||||
{
|
|
||||||
stream << shortName << longName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CardSet::setSortKey(unsigned int _sortKey)
|
void CardSet::setSortKey(unsigned int _sortKey)
|
||||||
|
@ -56,9 +54,11 @@ void SetList::sortByKey()
|
||||||
qSort(begin(), end(), CompareFunctor());
|
qSort(begin(), end(), CompareFunctor());
|
||||||
}
|
}
|
||||||
|
|
||||||
CardInfo::CardInfo(CardDatabase *_db, const QString &_name, const QString &_manacost, const QString &_cardtype, const QString &_powtough, const QStringList &_text)
|
CardInfo::CardInfo(CardDatabase *_db, const QString &_name, const QString &_manacost, const QString &_cardtype, const QString &_powtough, const QString &_text, const SetList &_sets)
|
||||||
: db(_db), name(_name), manacost(_manacost), cardtype(_cardtype), powtough(_powtough), text(_text), pixmap(NULL)
|
: db(_db), name(_name), sets(_sets), manacost(_manacost), cardtype(_cardtype), powtough(_powtough), text(_text), pixmap(NULL)
|
||||||
{
|
{
|
||||||
|
for (int i = 0; i < sets.size(); i++)
|
||||||
|
sets[i]->append(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
CardInfo::~CardInfo()
|
CardInfo::~CardInfo()
|
||||||
|
@ -173,32 +173,23 @@ QPixmap *CardInfo::getPixmap(QSize size)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CardInfo::loadFromStream(QDataStream &stream)
|
QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardInfo *info)
|
||||||
{
|
{
|
||||||
QStringList setNames;
|
xml.writeStartElement("card");
|
||||||
stream >> name
|
xml.writeTextElement("name", info->getName());
|
||||||
>> setNames
|
|
||||||
>> manacost
|
|
||||||
>> cardtype
|
|
||||||
>> powtough
|
|
||||||
>> text;
|
|
||||||
|
|
||||||
for (int i = 0; i < setNames.size(); i++)
|
SetList sets = info->getSets();
|
||||||
addToSet(db->getSet(setNames[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
void CardInfo::saveToStream(QDataStream &stream)
|
|
||||||
{
|
|
||||||
QStringList setNames;
|
|
||||||
for (int i = 0; i < sets.size(); i++)
|
for (int i = 0; i < sets.size(); i++)
|
||||||
setNames << sets[i]->getShortName();
|
xml.writeTextElement("set", sets[i]->getShortName());
|
||||||
|
|
||||||
stream << name
|
xml.writeTextElement("manacost", info->getManaCost());
|
||||||
<< setNames
|
xml.writeTextElement("type", info->getCardType());
|
||||||
<< manacost
|
if (!info->getPowTough().isEmpty())
|
||||||
<< cardtype
|
xml.writeTextElement("pt", info->getPowTough());
|
||||||
<< powtough
|
xml.writeTextElement("text", info->getText());
|
||||||
<< text;
|
xml.writeEndElement(); // card
|
||||||
|
|
||||||
|
return xml;
|
||||||
}
|
}
|
||||||
|
|
||||||
CardDatabase::CardDatabase()
|
CardDatabase::CardDatabase()
|
||||||
|
@ -301,7 +292,7 @@ void CardDatabase::importOracleFile(const QString &fileName, CardSet *set)
|
||||||
if (cardHash.contains(cardname))
|
if (cardHash.contains(cardname))
|
||||||
card = cardHash.value(cardname);
|
card = cardHash.value(cardname);
|
||||||
else {
|
else {
|
||||||
card = new CardInfo(this, cardname, manacost, cardtype, powtough, text);
|
card = new CardInfo(this, cardname, manacost, cardtype, powtough, text.join("\n"));
|
||||||
cardHash.insert(cardname, card);
|
cardHash.insert(cardname, card);
|
||||||
}
|
}
|
||||||
card->addToSet(set);
|
card->addToSet(set);
|
||||||
|
@ -329,64 +320,104 @@ void CardDatabase::importOracleDir()
|
||||||
qDebug(QString("CardDatabase: %1 cards imported").arg(cardHash.size()).toLatin1());
|
qDebug(QString("CardDatabase: %1 cards imported").arg(cardHash.size()).toLatin1());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CardDatabase::loadSetsFromXml(QXmlStreamReader &xml)
|
||||||
|
{
|
||||||
|
while (!xml.atEnd()) {
|
||||||
|
if (xml.readNext() == QXmlStreamReader::EndElement)
|
||||||
|
break;
|
||||||
|
if (xml.name() == "set") {
|
||||||
|
QString shortName, longName;
|
||||||
|
while (!xml.atEnd()) {
|
||||||
|
if (xml.readNext() == QXmlStreamReader::EndElement)
|
||||||
|
break;
|
||||||
|
if (xml.name() == "name")
|
||||||
|
shortName = xml.readElementText();
|
||||||
|
else if (xml.name() == "longname")
|
||||||
|
longName = xml.readElementText();
|
||||||
|
}
|
||||||
|
setHash.insert(shortName, new CardSet(shortName, longName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CardDatabase::loadCardsFromXml(QXmlStreamReader &xml)
|
||||||
|
{
|
||||||
|
while (!xml.atEnd()) {
|
||||||
|
if (xml.readNext() == QXmlStreamReader::EndElement)
|
||||||
|
break;
|
||||||
|
if (xml.name() == "card") {
|
||||||
|
QString name, manacost, type, pt, text;
|
||||||
|
SetList sets;
|
||||||
|
while (!xml.atEnd()) {
|
||||||
|
if (xml.readNext() == QXmlStreamReader::EndElement)
|
||||||
|
break;
|
||||||
|
if (xml.name() == "name")
|
||||||
|
name = xml.readElementText();
|
||||||
|
else if (xml.name() == "manacost")
|
||||||
|
manacost = xml.readElementText();
|
||||||
|
else if (xml.name() == "type")
|
||||||
|
type = xml.readElementText();
|
||||||
|
else if (xml.name() == "pt")
|
||||||
|
pt = xml.readElementText();
|
||||||
|
else if (xml.name() == "text")
|
||||||
|
text = xml.readElementText();
|
||||||
|
else if (xml.name() == "set")
|
||||||
|
sets << getSet(xml.readElementText());
|
||||||
|
}
|
||||||
|
cardHash.insert(name, new CardInfo(this, name, manacost, type, pt, text, sets));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int CardDatabase::loadFromFile(const QString &fileName)
|
int CardDatabase::loadFromFile(const QString &fileName)
|
||||||
{
|
{
|
||||||
QFile file(fileName);
|
QFile file(fileName);
|
||||||
file.open(QIODevice::ReadOnly);
|
file.open(QIODevice::ReadOnly);
|
||||||
QDataStream in(&file);
|
QXmlStreamReader xml(&file);
|
||||||
in.setVersion(QDataStream::Qt_4_4);
|
|
||||||
|
|
||||||
quint32 _magicNumber, _fileVersion, setCount, cardCount;
|
|
||||||
in >> _magicNumber
|
|
||||||
>> _fileVersion
|
|
||||||
>> setCount
|
|
||||||
>> cardCount;
|
|
||||||
|
|
||||||
if (_magicNumber != magicNumber)
|
|
||||||
return -1;
|
|
||||||
if (_fileVersion != fileVersion)
|
|
||||||
return -2;
|
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
setHash.reserve(setCount);
|
while (!xml.atEnd()) {
|
||||||
for (unsigned int i = 0; i < setCount; i++) {
|
if (xml.readNext() == QXmlStreamReader::StartElement) {
|
||||||
CardSet *newSet = new CardSet;
|
if (xml.name() != "cockatrice_carddatabase")
|
||||||
newSet->loadFromStream(in);
|
return false;
|
||||||
setHash.insert(newSet->getShortName(), newSet);
|
while (!xml.atEnd()) {
|
||||||
|
if (xml.readNext() == QXmlStreamReader::EndElement)
|
||||||
|
break;
|
||||||
|
if (xml.name() == "sets")
|
||||||
|
loadSetsFromXml(xml);
|
||||||
|
else if (xml.name() == "cards")
|
||||||
|
loadCardsFromXml(xml);
|
||||||
}
|
}
|
||||||
cardHash.reserve(cardCount);
|
|
||||||
for (unsigned int i = 0; i < cardCount; i++) {
|
|
||||||
CardInfo *newCard = new CardInfo(this);
|
|
||||||
newCard->loadFromStream(in);
|
|
||||||
cardHash.insert(newCard->getName(), newCard);
|
|
||||||
}
|
}
|
||||||
qDebug(QString("%1 cards in %2 sets loaded").arg(cardCount).arg(setHash.size()).toLatin1());
|
}
|
||||||
|
qDebug(QString("%1 cards in %2 sets loaded").arg(cardHash.size()).arg(setHash.size()).toLatin1());
|
||||||
return cardCount;
|
return cardHash.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CardDatabase::saveToFile(const QString &fileName)
|
bool CardDatabase::saveToFile(const QString &fileName)
|
||||||
{
|
{
|
||||||
QFile file(fileName);
|
QFile file(fileName);
|
||||||
file.open(QIODevice::WriteOnly);
|
file.open(QIODevice::WriteOnly);
|
||||||
QDataStream out(&file);
|
QXmlStreamWriter xml(&file);
|
||||||
out.setVersion(QDataStream::Qt_4_4);
|
|
||||||
|
|
||||||
out << (quint32) magicNumber
|
xml.setAutoFormatting(true);
|
||||||
<< (quint32) fileVersion
|
xml.writeStartDocument();
|
||||||
<< (quint32) setHash.size()
|
xml.writeStartElement("cockatrice_carddatabase");
|
||||||
<< (quint32) cardHash.size();
|
xml.writeAttribute("version", "1");
|
||||||
|
|
||||||
QHashIterator<QString, CardSet *> setIt(setHash);
|
xml.writeStartElement("sets");
|
||||||
while (setIt.hasNext()) {
|
QHashIterator<QString, CardSet *> setIterator(setHash);
|
||||||
setIt.next();
|
while (setIterator.hasNext())
|
||||||
setIt.value()->saveToStream(out);
|
xml << setIterator.next().value();
|
||||||
}
|
xml.writeEndElement(); // sets
|
||||||
QHashIterator<QString, CardInfo *> i(cardHash);
|
|
||||||
while (i.hasNext()) {
|
xml.writeStartElement("cards");
|
||||||
i.next();
|
QHashIterator<QString, CardInfo *> cardIterator(cardHash);
|
||||||
i.value()->saveToStream(out);
|
while (cardIterator.hasNext())
|
||||||
}
|
xml << cardIterator.next().value();
|
||||||
|
xml.writeEndElement(); // cards
|
||||||
|
|
||||||
|
xml.writeEndElement(); // cockatrice_carddatabase
|
||||||
|
xml.writeEndDocument();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QDataStream>
|
#include <QDataStream>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
|
#include <QXmlStreamReader>
|
||||||
|
|
||||||
class CardDatabase;
|
class CardDatabase;
|
||||||
class CardInfo;
|
class CardInfo;
|
||||||
|
@ -21,8 +22,6 @@ public:
|
||||||
int getSortKey() const { return sortKey; }
|
int getSortKey() const { return sortKey; }
|
||||||
void setSortKey(unsigned int _sortKey);
|
void setSortKey(unsigned int _sortKey);
|
||||||
void updateSortKey();
|
void updateSortKey();
|
||||||
void loadFromStream(QDataStream &stream);
|
|
||||||
void saveToStream(QDataStream &stream);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class SetList : public QList<CardSet *> {
|
class SetList : public QList<CardSet *> {
|
||||||
|
@ -41,7 +40,7 @@ private:
|
||||||
QString manacost;
|
QString manacost;
|
||||||
QString cardtype;
|
QString cardtype;
|
||||||
QString powtough;
|
QString powtough;
|
||||||
QStringList text;
|
QString text;
|
||||||
QPixmap *pixmap;
|
QPixmap *pixmap;
|
||||||
QMap<int, QPixmap *> scaledPixmapCache;
|
QMap<int, QPixmap *> scaledPixmapCache;
|
||||||
public:
|
public:
|
||||||
|
@ -50,21 +49,20 @@ public:
|
||||||
const QString &_manacost = QString(),
|
const QString &_manacost = QString(),
|
||||||
const QString &_cardtype = QString(),
|
const QString &_cardtype = QString(),
|
||||||
const QString &_powtough = QString(),
|
const QString &_powtough = QString(),
|
||||||
const QStringList &_text = QStringList());
|
const QString &_text = QString(),
|
||||||
|
const SetList &_sets = SetList());
|
||||||
~CardInfo();
|
~CardInfo();
|
||||||
QString getName() const { return name; }
|
QString getName() const { return name; }
|
||||||
SetList getSets() const { return sets; }
|
SetList getSets() const { return sets; }
|
||||||
QString getManacost() const { return manacost; }
|
QString getManaCost() const { return manacost; }
|
||||||
QString getCardType() const { return cardtype; }
|
QString getCardType() const { return cardtype; }
|
||||||
QString getPowTough() const { return powtough; }
|
QString getPowTough() const { return powtough; }
|
||||||
QStringList getText() const { return text; }
|
QString getText() const { return text; }
|
||||||
QString getMainCardType() const;
|
QString getMainCardType() const;
|
||||||
int getTableRow() const;
|
int getTableRow() const;
|
||||||
void addToSet(CardSet *set);
|
void addToSet(CardSet *set);
|
||||||
QPixmap *loadPixmap();
|
QPixmap *loadPixmap();
|
||||||
QPixmap *getPixmap(QSize size);
|
QPixmap *getPixmap(QSize size);
|
||||||
void loadFromStream(QDataStream &stream);
|
|
||||||
void saveToStream(QDataStream &stream);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class CardDatabase {
|
class CardDatabase {
|
||||||
|
@ -74,6 +72,9 @@ private:
|
||||||
CardInfo *noCard;
|
CardInfo *noCard;
|
||||||
static const unsigned int magicNumber = 0x12345678;
|
static const unsigned int magicNumber = 0x12345678;
|
||||||
static const unsigned int fileVersion = 1;
|
static const unsigned int fileVersion = 1;
|
||||||
|
|
||||||
|
void loadCardsFromXml(QXmlStreamReader &xml);
|
||||||
|
void loadSetsFromXml(QXmlStreamReader &xml);
|
||||||
public:
|
public:
|
||||||
CardDatabase();
|
CardDatabase();
|
||||||
~CardDatabase();
|
~CardDatabase();
|
||||||
|
|
|
@ -40,7 +40,7 @@ QVariant CardDatabaseModel::data(const QModelIndex &index, int role) const
|
||||||
setList << sets[i]->getShortName();
|
setList << sets[i]->getShortName();
|
||||||
return setList.join(", ");
|
return setList.join(", ");
|
||||||
}
|
}
|
||||||
case 2: return card->getManacost();
|
case 2: return card->getManaCost();
|
||||||
case 3: return card->getCardType();
|
case 3: return card->getCardType();
|
||||||
case 4: return card->getPowTough();
|
case 4: return card->getPowTough();
|
||||||
default: return QVariant();
|
default: return QVariant();
|
||||||
|
@ -75,7 +75,7 @@ public:
|
||||||
switch (column) {
|
switch (column) {
|
||||||
case 0: result = (a->getName() < b->getName()); break;
|
case 0: result = (a->getName() < b->getName()); break;
|
||||||
case 1: result = (a->getSets().at(0)->getShortName() < b->getSets().at(0)->getShortName()); break;
|
case 1: result = (a->getSets().at(0)->getShortName() < b->getSets().at(0)->getShortName()); break;
|
||||||
case 2: result = (a->getManacost() < b->getManacost()); break;
|
case 2: result = (a->getManaCost() < b->getManaCost()); break;
|
||||||
case 3: result = (a->getCardType() < b->getCardType()); break;
|
case 3: result = (a->getCardType() < b->getCardType()); break;
|
||||||
case 4: result = (a->getPowTough() < b->getPowTough()); break;
|
case 4: result = (a->getPowTough() < b->getPowTough()); break;
|
||||||
default: result = false;
|
default: result = false;
|
||||||
|
|
|
@ -76,10 +76,10 @@ void CardInfoWidget::setCard(CardInfo *card)
|
||||||
cardPicture->setPixmap(*(db->getCard()->getPixmap(QSize(pixmapWidth, pixmapHeight))));
|
cardPicture->setPixmap(*(db->getCard()->getPixmap(QSize(pixmapWidth, pixmapHeight))));
|
||||||
|
|
||||||
nameLabel2->setText(card->getName());
|
nameLabel2->setText(card->getName());
|
||||||
manacostLabel2->setText(card->getManacost());
|
manacostLabel2->setText(card->getManaCost());
|
||||||
cardtypeLabel2->setText(card->getCardType());
|
cardtypeLabel2->setText(card->getCardType());
|
||||||
powtoughLabel2->setText(card->getPowTough());
|
powtoughLabel2->setText(card->getPowTough());
|
||||||
textLabel->setText(card->getText().join("\n"));
|
textLabel->setText(card->getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CardInfoWidget::setCard(const QString &cardName)
|
void CardInfoWidget::setCard(const QString &cardName)
|
||||||
|
|
|
@ -228,9 +228,9 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
QPixmapCache::setCacheLimit(200000);
|
QPixmapCache::setCacheLimit(200000);
|
||||||
|
|
||||||
db = new CardDatabase;
|
db = new CardDatabase;
|
||||||
db->loadFromFile("../cards.dat");
|
db->loadFromFile("../cards.xml");
|
||||||
// db->importOracleDir();
|
// db->importOracleDir();
|
||||||
// db->saveToFile("../cards.dat");
|
// db->saveToFile("../cards.xml");
|
||||||
|
|
||||||
scene = new QGraphicsScene(0, 0, 1096, 1160, this);
|
scene = new QGraphicsScene(0, 0, 1096, 1160, this);
|
||||||
view = new GameView(scene);
|
view = new GameView(scene);
|
||||||
|
|
Loading…
Reference in a new issue