Cleaned up decklist code
This commit is contained in:
parent
ab11a40863
commit
16541141bd
11 changed files with 144 additions and 487 deletions
|
@ -87,7 +87,6 @@ HEADERS += src/abstractcounter.h \
|
|||
src/pending_command.h \
|
||||
../common/get_pb_extension.h \
|
||||
../common/color.h \
|
||||
../common/serializable_item.h \
|
||||
../common/decklist.h \
|
||||
../common/rng_abstract.h \
|
||||
../common/rng_sfmt.h \
|
||||
|
@ -171,7 +170,6 @@ SOURCES += src/abstractcounter.cpp \
|
|||
src/priceupdater.cpp \
|
||||
src/soundengine.cpp \
|
||||
../common/get_pb_extension.cpp \
|
||||
../common/serializable_item.cpp \
|
||||
../common/decklist.cpp \
|
||||
../common/rng_abstract.cpp \
|
||||
../common/rng_sfmt.cpp \
|
||||
|
|
|
@ -318,20 +318,19 @@ void DeckViewScene::rebuildTree()
|
|||
}
|
||||
}
|
||||
|
||||
void DeckViewScene::applySideboardPlan(const QList<MoveCardToZone *> &plan)
|
||||
void DeckViewScene::applySideboardPlan(const QList<MoveCard_ToZone> &plan)
|
||||
{
|
||||
for (int i = 0; i < plan.size(); ++i) {
|
||||
MoveCardToZone *m = plan[i];
|
||||
|
||||
DeckViewCardContainer *start = cardContainers.value(m->getStartZone());
|
||||
DeckViewCardContainer *target = cardContainers.value(m->getTargetZone());
|
||||
const MoveCard_ToZone &m = plan[i];
|
||||
DeckViewCardContainer *start = cardContainers.value(QString::fromStdString(m.start_zone()));
|
||||
DeckViewCardContainer *target = cardContainers.value(QString::fromStdString(m.target_zone()));
|
||||
if (!start || !target)
|
||||
continue;
|
||||
|
||||
DeckViewCard *card = 0;
|
||||
const QList<DeckViewCard *> &cardList = start->getCards();
|
||||
for (int j = 0; j < cardList.size(); ++j)
|
||||
if (cardList[j]->getName() == m->getCardName()) {
|
||||
if (cardList[j]->getName() == QString::fromStdString(m.card_name())) {
|
||||
card = cardList[j];
|
||||
break;
|
||||
}
|
||||
|
@ -414,16 +413,21 @@ void DeckViewScene::updateContents()
|
|||
emit sideboardPlanChanged();
|
||||
}
|
||||
|
||||
QList<MoveCardToZone *> DeckViewScene::getSideboardPlan() const
|
||||
QList<MoveCard_ToZone> DeckViewScene::getSideboardPlan() const
|
||||
{
|
||||
QList<MoveCardToZone *> result;
|
||||
QList<MoveCard_ToZone> result;
|
||||
QMapIterator<QString, DeckViewCardContainer *> containerIterator(cardContainers);
|
||||
while (containerIterator.hasNext()) {
|
||||
DeckViewCardContainer *cont = containerIterator.next().value();
|
||||
const QList<DeckViewCard *> cardList = cont->getCards();
|
||||
for (int i = 0; i < cardList.size(); ++i)
|
||||
if (cardList[i]->getOriginZone() != cont->getName())
|
||||
result.append(new MoveCardToZone(cardList[i]->getName(), cardList[i]->getOriginZone(), cont->getName()));
|
||||
if (cardList[i]->getOriginZone() != cont->getName()) {
|
||||
MoveCard_ToZone m;
|
||||
m.set_card_name(cardList[i]->getName().toStdString());
|
||||
m.set_start_zone(cardList[i]->getOriginZone().toStdString());
|
||||
m.set_target_zone(cont->getName().toStdString());
|
||||
result.append(m);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
#include <QPixmap>
|
||||
#include "abstractcarddragitem.h"
|
||||
|
||||
#include "pb/move_card_to_zone.pb.h"
|
||||
|
||||
class DeckList;
|
||||
class InnerDecklistNode;
|
||||
class CardInfo;
|
||||
|
@ -80,7 +82,7 @@ private:
|
|||
QMap<QString, DeckViewCardContainer *> cardContainers;
|
||||
qreal optimalAspectRatio;
|
||||
void rebuildTree();
|
||||
void applySideboardPlan(const QList<MoveCardToZone *> &plan);
|
||||
void applySideboardPlan(const QList<MoveCard_ToZone> &plan);
|
||||
public:
|
||||
DeckViewScene(QObject *parent = 0);
|
||||
~DeckViewScene();
|
||||
|
@ -90,7 +92,7 @@ public:
|
|||
void setOptimalAspectRatio(qreal _optimalAspectRatio) { optimalAspectRatio = _optimalAspectRatio; }
|
||||
void rearrangeItems();
|
||||
void updateContents();
|
||||
QList<MoveCardToZone *> getSideboardPlan() const;
|
||||
QList<MoveCard_ToZone> getSideboardPlan() const;
|
||||
};
|
||||
|
||||
class DeckView : public QGraphicsView {
|
||||
|
@ -108,7 +110,7 @@ public:
|
|||
DeckView(QWidget *parent = 0);
|
||||
void setDeck(DeckList *_deck);
|
||||
void setLocked(bool _locked) { deckViewScene->setLocked(_locked); }
|
||||
QList<MoveCardToZone *> getSideboardPlan() const { return deckViewScene->getSideboardPlan(); }
|
||||
QList<MoveCard_ToZone> getSideboardPlan() const { return deckViewScene->getSideboardPlan(); }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -175,9 +175,9 @@ void DeckViewContainer::readyStart()
|
|||
void DeckViewContainer::sideboardPlanChanged()
|
||||
{
|
||||
Command_SetSideboardPlan cmd;
|
||||
QList<MoveCardToZone *> newPlan = deckView->getSideboardPlan();
|
||||
const QList<MoveCard_ToZone> &newPlan = deckView->getSideboardPlan();
|
||||
for (int i = 0; i < newPlan.size(); ++i)
|
||||
cmd.add_move_list()->CopyFrom(newPlan[i]->toPB());
|
||||
cmd.add_move_list()->CopyFrom(newPlan[i]);
|
||||
static_cast<TabGame *>(parent())->sendGameCommand(cmd, playerId);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,43 +6,64 @@
|
|||
#include <QCryptographicHash>
|
||||
#include "decklist.h"
|
||||
|
||||
MoveCardToZone::MoveCardToZone(const QString &_cardName, const QString &_startZone, const QString &_targetZone)
|
||||
: SerializableItem_Map("move_card_to_zone")
|
||||
SideboardPlan::SideboardPlan(const QString &_name, const QList<MoveCard_ToZone> &_moveList)
|
||||
: name(_name), moveList(_moveList)
|
||||
{
|
||||
insertItem(new SerializableItem_String("card_name", _cardName));
|
||||
insertItem(new SerializableItem_String("start_zone", _startZone));
|
||||
insertItem(new SerializableItem_String("target_zone", _targetZone));
|
||||
}
|
||||
|
||||
MoveCardToZone::MoveCardToZone(MoveCardToZone *other)
|
||||
: SerializableItem_Map("move_card_to_zone")
|
||||
void SideboardPlan::setMoveList(const QList<MoveCard_ToZone> &_moveList)
|
||||
{
|
||||
insertItem(new SerializableItem_String("card_name", other->getCardName()));
|
||||
insertItem(new SerializableItem_String("start_zone", other->getStartZone()));
|
||||
insertItem(new SerializableItem_String("target_zone", other->getTargetZone()));
|
||||
moveList = _moveList;
|
||||
}
|
||||
|
||||
SideboardPlan::SideboardPlan(const QString &_name, const QList<MoveCardToZone *> &_moveList)
|
||||
: SerializableItem_Map("sideboard_plan")
|
||||
bool SideboardPlan::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
insertItem(new SerializableItem_String("name", _name));
|
||||
|
||||
for (int i = 0; i < _moveList.size(); ++i)
|
||||
itemList.append(_moveList[i]);
|
||||
while (!xml->atEnd()) {
|
||||
xml->readNext();
|
||||
const QString childName = xml->name().toString();
|
||||
if (xml->isStartElement()) {
|
||||
if (childName == "name")
|
||||
name = xml->readElementText();
|
||||
else if (childName == "move_card_to_zone") {
|
||||
MoveCard_ToZone m;
|
||||
while (!xml->atEnd()) {
|
||||
xml->readNext();
|
||||
const QString childName2 = xml->name().toString();
|
||||
if (xml->isStartElement()) {
|
||||
if (childName2 == "card_name")
|
||||
m.set_card_name(xml->readElementText().toStdString());
|
||||
else if (childName2 == "start_zone")
|
||||
m.set_start_zone(xml->readElementText().toStdString());
|
||||
else if (childName2 == "target_zone")
|
||||
m.set_target_zone(xml->readElementText().toStdString());
|
||||
} else if (xml->isEndElement() && (childName2 == "move_card_to_zone")) {
|
||||
moveList.append(m);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (xml->isEndElement() && (childName == "sideboard_plan"))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void SideboardPlan::setMoveList(const QList<MoveCardToZone *> &_moveList)
|
||||
void SideboardPlan::write(QXmlStreamWriter *xml)
|
||||
{
|
||||
for (int i = 0; i < itemList.size(); ++i)
|
||||
delete itemList[i];
|
||||
itemList.clear();
|
||||
|
||||
for (int i = 0; i < _moveList.size(); ++i)
|
||||
itemList.append(_moveList[i]);
|
||||
xml->writeStartElement("sideboard_plan");
|
||||
xml->writeTextElement("name", name);
|
||||
for (int i = 0; i < moveList.size(); ++i) {
|
||||
xml->writeStartElement("move_card_to_zone");
|
||||
xml->writeTextElement("card_name", QString::fromStdString(moveList[i].card_name()));
|
||||
xml->writeTextElement("start_zone", QString::fromStdString(moveList[i].start_zone()));
|
||||
xml->writeTextElement("target_zone", QString::fromStdString(moveList[i].target_zone()));
|
||||
xml->writeEndElement();
|
||||
}
|
||||
xml->writeEndElement();
|
||||
}
|
||||
|
||||
AbstractDecklistNode::AbstractDecklistNode(InnerDecklistNode *_parent)
|
||||
: parent(_parent), currentItem(0)
|
||||
: parent(_parent)
|
||||
{
|
||||
if (parent)
|
||||
parent->append(this);
|
||||
|
@ -172,20 +193,22 @@ public:
|
|||
|
||||
bool InnerDecklistNode::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
if (currentItem) {
|
||||
if (currentItem->readElement(xml))
|
||||
currentItem = 0;
|
||||
return false;
|
||||
while (!xml->atEnd()) {
|
||||
xml->readNext();
|
||||
const QString childName = xml->name().toString();
|
||||
if (xml->isStartElement()) {
|
||||
if (childName == "zone") {
|
||||
InnerDecklistNode *newZone = new InnerDecklistNode(xml->attributes().value("name").toString(), this);
|
||||
newZone->readElement(xml);
|
||||
} else if (childName == "card") {
|
||||
float price = (xml->attributes().value("price") != NULL) ? xml->attributes().value("price").toString().toFloat() : 0;
|
||||
DecklistCardNode *newCard = new DecklistCardNode(xml->attributes().value("name").toString(), xml->attributes().value("number").toString().toInt(), price, this);
|
||||
newCard->readElement(xml);
|
||||
}
|
||||
} else if (xml->isEndElement() && (childName == "zone"))
|
||||
return false;
|
||||
}
|
||||
if (xml->isStartElement() && (xml->name() == "zone"))
|
||||
currentItem = new InnerDecklistNode(xml->attributes().value("name").toString(), this);
|
||||
else if (xml->isStartElement() && (xml->name() == "card")) {
|
||||
float price = (xml->attributes().value("price") != NULL) ? xml->attributes().value("price").toString().toFloat() : 0;
|
||||
currentItem = new DecklistCardNode(xml->attributes().value("name").toString(), xml->attributes().value("number").toString().toInt(), price, this);
|
||||
} else if (xml->isEndElement() && (xml->name() == "zone"))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void InnerDecklistNode::writeElement(QXmlStreamWriter *xml)
|
||||
|
@ -199,10 +222,12 @@ void InnerDecklistNode::writeElement(QXmlStreamWriter *xml)
|
|||
|
||||
bool AbstractDecklistCardNode::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
if (xml->isEndElement())
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
while (!xml->atEnd()) {
|
||||
xml->readNext();
|
||||
if (xml->isEndElement() && xml->name() == "card")
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void AbstractDecklistCardNode::writeElement(QXmlStreamWriter *xml)
|
||||
|
@ -245,30 +270,23 @@ const QStringList DeckList::fileNameFilters = QStringList()
|
|||
<< QObject::tr("All files (*.*)");
|
||||
|
||||
DeckList::DeckList()
|
||||
: SerializableItem("cockatrice_deck"), currentZone(0), currentSideboardPlan(0)
|
||||
{
|
||||
root = new InnerDecklistNode;
|
||||
}
|
||||
|
||||
DeckList::DeckList(DeckList *other)
|
||||
: SerializableItem("cockatrice_deck"), currentZone(0), currentSideboardPlan(0)
|
||||
{
|
||||
root = new InnerDecklistNode(other->getRoot());
|
||||
|
||||
QMapIterator<QString, SideboardPlan *> spIterator(other->getSideboardPlans());
|
||||
while (spIterator.hasNext()) {
|
||||
spIterator.next();
|
||||
QList<MoveCardToZone *> newMoveList;
|
||||
QList<MoveCardToZone *> oldMoveList = spIterator.value()->getMoveList();
|
||||
for (int i = 0; i < oldMoveList.size(); ++i)
|
||||
newMoveList.append(new MoveCardToZone(oldMoveList[i]));
|
||||
sideboardPlans.insert(spIterator.key(), new SideboardPlan(spIterator.key(), newMoveList));
|
||||
sideboardPlans.insert(spIterator.key(), new SideboardPlan(spIterator.key(), spIterator.value()->getMoveList()));
|
||||
}
|
||||
updateDeckHash();
|
||||
}
|
||||
|
||||
DeckList::DeckList(const QString &nativeString)
|
||||
: SerializableItem("cockatrice_deck"), currentZone(0), currentSideboardPlan(0)
|
||||
{
|
||||
root = new InnerDecklistNode;
|
||||
|
||||
|
@ -285,16 +303,16 @@ DeckList::~DeckList()
|
|||
delete i.next().value();
|
||||
}
|
||||
|
||||
QList<MoveCardToZone *> DeckList::getCurrentSideboardPlan()
|
||||
QList<MoveCard_ToZone> DeckList::getCurrentSideboardPlan()
|
||||
{
|
||||
SideboardPlan *current = sideboardPlans.value(QString(), 0);
|
||||
if (!current)
|
||||
return QList<MoveCardToZone *>();
|
||||
return QList<MoveCard_ToZone>();
|
||||
else
|
||||
return current->getMoveList();
|
||||
}
|
||||
|
||||
void DeckList::setCurrentSideboardPlan(const QList<MoveCardToZone *> &plan)
|
||||
void DeckList::setCurrentSideboardPlan(const QList<MoveCard_ToZone> &plan)
|
||||
{
|
||||
SideboardPlan *current = sideboardPlans.value(QString(), 0);
|
||||
if (!current) {
|
||||
|
@ -302,45 +320,35 @@ void DeckList::setCurrentSideboardPlan(const QList<MoveCardToZone *> &plan)
|
|||
sideboardPlans.insert(QString(), current);
|
||||
}
|
||||
|
||||
QList<MoveCardToZone *> newList;
|
||||
for (int i = 0; i < plan.size(); ++i)
|
||||
newList.append(new MoveCardToZone(plan[i]));
|
||||
current->setMoveList(newList);
|
||||
current->setMoveList(plan);
|
||||
}
|
||||
|
||||
bool DeckList::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
if (currentZone) {
|
||||
if (currentZone->readElement(xml))
|
||||
currentZone = 0;
|
||||
} else if (currentSideboardPlan) {
|
||||
if (currentSideboardPlan->readElement(xml)) {
|
||||
sideboardPlans.insert(currentSideboardPlan->getName(), currentSideboardPlan);
|
||||
currentSideboardPlan = 0;
|
||||
const QString childName = xml->name().toString();
|
||||
if (xml->isStartElement()) {
|
||||
if (childName == "deckname")
|
||||
name = xml->readElementText();
|
||||
else if (childName == "comments")
|
||||
comments = xml->readElementText();
|
||||
else if (childName == "zone") {
|
||||
InnerDecklistNode *newZone = new InnerDecklistNode(xml->attributes().value("name").toString(), root);
|
||||
newZone->readElement(xml);
|
||||
} else if (childName == "sideboard_plan") {
|
||||
SideboardPlan *newSideboardPlan = new SideboardPlan;
|
||||
if (newSideboardPlan->readElement(xml))
|
||||
sideboardPlans.insert(newSideboardPlan->getName(), newSideboardPlan);
|
||||
else
|
||||
delete newSideboardPlan;
|
||||
}
|
||||
} else if (xml->isEndElement() && (childName == "cockatrice_deck"))
|
||||
return false;
|
||||
} else if (xml->isEndElement()) {
|
||||
if (xml->name() == "deckname")
|
||||
name = currentElementText;
|
||||
else if (xml->name() == "comments")
|
||||
comments = currentElementText;
|
||||
|
||||
currentElementText.clear();
|
||||
} else if (xml->isStartElement() && (xml->name() == "zone"))
|
||||
currentZone = new InnerDecklistNode(xml->attributes().value("name").toString(), root);
|
||||
else if (xml->isStartElement() && (xml->name() == "sideboard_plan")) {
|
||||
currentSideboardPlan = new SideboardPlan;
|
||||
if (currentSideboardPlan->readElement(xml)) {
|
||||
sideboardPlans.insert(currentSideboardPlan->getName(), currentSideboardPlan);
|
||||
currentSideboardPlan = 0;
|
||||
}
|
||||
} else if (xml->isCharacters() && !xml->isWhitespace())
|
||||
currentElementText = xml->text().toString();
|
||||
return SerializableItem::readElement(xml);
|
||||
return true;
|
||||
}
|
||||
|
||||
void DeckList::writeElement(QXmlStreamWriter *xml)
|
||||
void DeckList::write(QXmlStreamWriter *xml)
|
||||
{
|
||||
xml->writeStartElement("cockatrice_deck");
|
||||
xml->writeAttribute("version", "1");
|
||||
xml->writeTextElement("deckname", name);
|
||||
xml->writeTextElement("comments", comments);
|
||||
|
@ -351,6 +359,7 @@ void DeckList::writeElement(QXmlStreamWriter *xml)
|
|||
QMapIterator<QString, SideboardPlan *> i(sideboardPlans);
|
||||
while (i.hasNext())
|
||||
i.next().value()->write(xml);
|
||||
xml->writeEndElement();
|
||||
}
|
||||
|
||||
void DeckList::loadFromXml(QXmlStreamReader *xml)
|
||||
|
@ -362,7 +371,8 @@ void DeckList::loadFromXml(QXmlStreamReader *xml)
|
|||
return;
|
||||
while (!xml->atEnd()) {
|
||||
xml->readNext();
|
||||
readElement(xml);
|
||||
if (!readElement(xml))
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include <QObject>
|
||||
#include <QStringList>
|
||||
#include <QSet>
|
||||
#include "serializable_item.h"
|
||||
#include <QMap>
|
||||
|
||||
#include "pb/move_card_to_zone.pb.h"
|
||||
|
||||
|
@ -19,30 +19,23 @@ class QXmlStreamWriter;
|
|||
|
||||
class InnerDecklistNode;
|
||||
|
||||
class MoveCardToZone : public SerializableItem_Map {
|
||||
class SideboardPlan {
|
||||
private:
|
||||
QString name;
|
||||
QList<MoveCard_ToZone> moveList;
|
||||
public:
|
||||
MoveCardToZone(const QString &_cardName = QString(), const QString &_startZone = QString(), const QString &_targetZone = QString());
|
||||
MoveCardToZone(MoveCardToZone *other);
|
||||
static SerializableItem *newItem() { return new MoveCardToZone; }
|
||||
QString getCardName() const { return static_cast<SerializableItem_String *>(itemMap.value("card_name"))->getData(); }
|
||||
QString getStartZone() const { return static_cast<SerializableItem_String *>(itemMap.value("start_zone"))->getData(); }
|
||||
QString getTargetZone() const { return static_cast<SerializableItem_String *>(itemMap.value("target_zone"))->getData(); }
|
||||
MoveCard_ToZone toPB() { MoveCard_ToZone foo; foo.set_card_name(getCardName().toStdString()); foo.set_start_zone(getStartZone().toStdString()); foo.set_target_zone(getTargetZone().toStdString()); return foo; } // XXX
|
||||
};
|
||||
|
||||
class SideboardPlan : public SerializableItem_Map {
|
||||
public:
|
||||
SideboardPlan(const QString &_name = QString(), const QList<MoveCardToZone *> &_moveList = QList<MoveCardToZone *>());
|
||||
static SerializableItem *newItem() { return new SideboardPlan; }
|
||||
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
|
||||
QList<MoveCardToZone *> getMoveList() const { return typecastItemList<MoveCardToZone *>(); }
|
||||
void setMoveList(const QList<MoveCardToZone *> &_moveList);
|
||||
SideboardPlan(const QString &_name = QString(), const QList<MoveCard_ToZone> &_moveList = QList<MoveCard_ToZone>());
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void write(QXmlStreamWriter *xml);
|
||||
|
||||
QString getName() const { return name; }
|
||||
const QList<MoveCard_ToZone> &getMoveList() const { return moveList; }
|
||||
void setMoveList(const QList<MoveCard_ToZone> &_moveList);
|
||||
};
|
||||
|
||||
class AbstractDecklistNode {
|
||||
protected:
|
||||
InnerDecklistNode *parent;
|
||||
AbstractDecklistNode *currentItem;
|
||||
public:
|
||||
AbstractDecklistNode(InnerDecklistNode *_parent = 0);
|
||||
virtual ~AbstractDecklistNode() { }
|
||||
|
@ -114,7 +107,7 @@ public:
|
|||
void setPrice(const float _price) { price = _price; }
|
||||
};
|
||||
|
||||
class DeckList : public SerializableItem {
|
||||
class DeckList : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum FileFormat { PlainTextFormat, CockatriceFormat };
|
||||
|
@ -125,8 +118,6 @@ private:
|
|||
FileFormat lastFileFormat;
|
||||
QMap<QString, SideboardPlan *> sideboardPlans;
|
||||
InnerDecklistNode *root;
|
||||
InnerDecklistNode *currentZone;
|
||||
SideboardPlan *currentSideboardPlan;
|
||||
QString currentElementText;
|
||||
void getCardListHelper(InnerDecklistNode *node, QSet<QString> &result) const;
|
||||
signals:
|
||||
|
@ -145,12 +136,12 @@ public:
|
|||
QString getComments() const { return comments; }
|
||||
QString getLastFileName() const { return lastFileName; }
|
||||
FileFormat getLastFileFormat() const { return lastFileFormat; }
|
||||
QList<MoveCardToZone *> getCurrentSideboardPlan();
|
||||
void setCurrentSideboardPlan(const QList<MoveCardToZone *> &plan);
|
||||
QList<MoveCard_ToZone> getCurrentSideboardPlan();
|
||||
void setCurrentSideboardPlan(const QList<MoveCard_ToZone> &plan);
|
||||
const QMap<QString, SideboardPlan *> &getSideboardPlans() const { return sideboardPlans; }
|
||||
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
void write(QXmlStreamWriter *xml);
|
||||
void loadFromXml(QXmlStreamReader *xml);
|
||||
QString writeToString_Native();
|
||||
bool loadFromFile_Native(QIODevice *device);
|
||||
|
|
|
@ -1,192 +0,0 @@
|
|||
#include "serializable_item.h"
|
||||
#include <QXmlStreamReader>
|
||||
#include <QXmlStreamWriter>
|
||||
#include <QBuffer>
|
||||
|
||||
QHash<QString, SerializableItem::NewItemFunction> SerializableItem::itemNameHash;
|
||||
|
||||
SerializableItem *SerializableItem::getNewItem(const QString &name)
|
||||
{
|
||||
if (!itemNameHash.contains(name))
|
||||
return 0;
|
||||
return itemNameHash.value(name)();
|
||||
}
|
||||
|
||||
void SerializableItem::registerSerializableItem(const QString &name, NewItemFunction func)
|
||||
{
|
||||
itemNameHash.insert(name, func);
|
||||
}
|
||||
|
||||
bool SerializableItem::read(QXmlStreamReader *xml)
|
||||
{
|
||||
if (!compressed)
|
||||
return readElement(xml);
|
||||
if (xml->isEndElement() && (xml->name() == itemType)) {
|
||||
QByteArray uncompressedData = "<d>" + qUncompress(QByteArray::fromBase64(compressedData)) + "</d>";
|
||||
compressedData.clear();
|
||||
QBuffer compressedBuffer(&uncompressedData);
|
||||
compressedBuffer.open(QIODevice::ReadOnly);
|
||||
QXmlStreamReader *xml2 = new QXmlStreamReader(&compressedBuffer);
|
||||
while (!xml2->atEnd()) {
|
||||
xml2->readNext();
|
||||
if (xml2->name() == "d")
|
||||
continue;
|
||||
readElement(xml2);
|
||||
}
|
||||
delete xml2;
|
||||
compressedBuffer.close();
|
||||
|
||||
return readElement(xml);
|
||||
} else {
|
||||
compressedData.append(xml->text().toString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool SerializableItem::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
if (xml->isEndElement() && (xml->name() == itemType))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void SerializableItem::write(QXmlStreamWriter *xml)
|
||||
{
|
||||
if (isEmpty())
|
||||
return;
|
||||
|
||||
xml->writeStartElement(itemType);
|
||||
if (!itemSubType.isEmpty())
|
||||
xml->writeAttribute("type", itemSubType);
|
||||
if (compressed) {
|
||||
xml->writeAttribute("comp", "1");
|
||||
|
||||
QBuffer compressBuffer;
|
||||
compressBuffer.open(QIODevice::WriteOnly);
|
||||
QXmlStreamWriter *xml2 = new QXmlStreamWriter(&compressBuffer);
|
||||
writeElement(xml2);
|
||||
delete xml2;
|
||||
compressBuffer.close();
|
||||
|
||||
xml->writeCharacters(qCompress(compressBuffer.data()).toBase64());
|
||||
} else
|
||||
writeElement(xml);
|
||||
xml->writeEndElement();
|
||||
}
|
||||
|
||||
SerializableItem_Map::~SerializableItem_Map()
|
||||
{
|
||||
QMapIterator<QString, SerializableItem *> mapIterator(itemMap);
|
||||
while (mapIterator.hasNext())
|
||||
delete mapIterator.next().value();
|
||||
for (int i = 0; i < itemList.size(); ++i)
|
||||
delete itemList[i];
|
||||
}
|
||||
|
||||
bool SerializableItem_Map::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
if (currentItem) {
|
||||
if (currentItem->read(xml))
|
||||
currentItem = 0;
|
||||
return false;
|
||||
} else if (firstItem)
|
||||
firstItem = false;
|
||||
else if (xml->isEndElement() && (xml->name() == itemType))
|
||||
extractData();
|
||||
else if (xml->isStartElement()) {
|
||||
QString childName = xml->name().toString();
|
||||
QString childSubType = xml->attributes().value("type").toString();
|
||||
bool childCompressed = xml->attributes().value("comp").toString().toInt() == 1;
|
||||
currentItem = itemMap.value(childName);
|
||||
if (!currentItem) {
|
||||
currentItem = getNewItem(childName + childSubType);
|
||||
itemList.append(currentItem);
|
||||
if (!currentItem)
|
||||
currentItem = new SerializableItem_Invalid(childName);
|
||||
}
|
||||
currentItem->setCompressed(childCompressed);
|
||||
if (currentItem->read(xml))
|
||||
currentItem = 0;
|
||||
}
|
||||
return SerializableItem::readElement(xml);
|
||||
}
|
||||
|
||||
void SerializableItem_Map::writeElement(QXmlStreamWriter *xml)
|
||||
{
|
||||
QMapIterator<QString, SerializableItem *> mapIterator(itemMap);
|
||||
while (mapIterator.hasNext())
|
||||
mapIterator.next().value()->write(xml);
|
||||
for (int i = 0; i < itemList.size(); ++i)
|
||||
itemList[i]->write(xml);
|
||||
}
|
||||
|
||||
bool SerializableItem_String::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
// This function is sometimes called multiple times if there are
|
||||
// entities in the strings, so we have to make sure the data is
|
||||
// not overwritten but appended to.
|
||||
if (xml->isCharacters() && !xml->isWhitespace())
|
||||
data.append(xml->text().toString());
|
||||
return SerializableItem::readElement(xml);
|
||||
}
|
||||
|
||||
void SerializableItem_String::writeElement(QXmlStreamWriter *xml)
|
||||
{
|
||||
xml->writeCharacters(data);
|
||||
}
|
||||
|
||||
bool SerializableItem_Int::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
if (xml->isCharacters() && !xml->isWhitespace()) {
|
||||
bool ok;
|
||||
data = xml->text().toString().toInt(&ok);
|
||||
if (!ok)
|
||||
data = -1;
|
||||
}
|
||||
return SerializableItem::readElement(xml);
|
||||
}
|
||||
|
||||
void SerializableItem_Int::writeElement(QXmlStreamWriter *xml)
|
||||
{
|
||||
xml->writeCharacters(QString::number(data));
|
||||
}
|
||||
|
||||
bool SerializableItem_Bool::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
if (xml->isCharacters() && !xml->isWhitespace())
|
||||
data = xml->text().toString() == "1";
|
||||
return SerializableItem::readElement(xml);
|
||||
}
|
||||
|
||||
void SerializableItem_Bool::writeElement(QXmlStreamWriter *xml)
|
||||
{
|
||||
xml->writeCharacters(data ? "1" : "0");
|
||||
}
|
||||
|
||||
bool SerializableItem_DateTime::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
if (xml->isCharacters() && !xml->isWhitespace()) {
|
||||
bool ok;
|
||||
unsigned int dateTimeValue = xml->text().toString().toUInt(&ok);
|
||||
data = ok ? QDateTime::fromTime_t(dateTimeValue) : QDateTime();
|
||||
}
|
||||
return SerializableItem::readElement(xml);
|
||||
}
|
||||
|
||||
void SerializableItem_DateTime::writeElement(QXmlStreamWriter *xml)
|
||||
{
|
||||
xml->writeCharacters(QString::number(data.toTime_t()));
|
||||
}
|
||||
|
||||
bool SerializableItem_ByteArray::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
if (xml->isCharacters() && !xml->isWhitespace())
|
||||
data = qUncompress(QByteArray::fromBase64(xml->text().toString().toAscii()));
|
||||
|
||||
return SerializableItem::readElement(xml);
|
||||
}
|
||||
|
||||
void SerializableItem_ByteArray::writeElement(QXmlStreamWriter *xml)
|
||||
{
|
||||
xml->writeCharacters(QString(qCompress(data).toBase64()));
|
||||
}
|
|
@ -1,153 +0,0 @@
|
|||
#ifndef SERIALIZABLE_ITEM_H
|
||||
#define SERIALIZABLE_ITEM_H
|
||||
|
||||
#include <QtCore/QXmlStreamWriter>
|
||||
#include <QObject>
|
||||
#include <QMap>
|
||||
#include <QList>
|
||||
#include <QHash>
|
||||
#include <QDateTime>
|
||||
#include <QStringList>
|
||||
#include "color.h"
|
||||
|
||||
class QXmlStreamReader;
|
||||
class QXmlStreamWriter;
|
||||
|
||||
class SerializableItem : public QObject {
|
||||
Q_OBJECT
|
||||
private:
|
||||
bool compressed;
|
||||
QByteArray compressedData;
|
||||
QXmlStreamReader *compressedReader;
|
||||
protected:
|
||||
typedef SerializableItem *(*NewItemFunction)();
|
||||
static QHash<QString, NewItemFunction> itemNameHash;
|
||||
|
||||
QString itemType, itemSubType;
|
||||
bool firstItem;
|
||||
public:
|
||||
SerializableItem(const QString &_itemType, const QString &_itemSubType = QString())
|
||||
: QObject(), compressed(false), itemType(_itemType), itemSubType(_itemSubType), firstItem(true) { }
|
||||
static void registerSerializableItem(const QString &name, NewItemFunction func);
|
||||
static SerializableItem *getNewItem(const QString &name);
|
||||
const QString &getItemType() const { return itemType; }
|
||||
const QString &getItemSubType() const { return itemSubType; }
|
||||
virtual bool readElement(QXmlStreamReader *xml);
|
||||
virtual void writeElement(QXmlStreamWriter *xml) = 0;
|
||||
virtual bool isEmpty() const = 0;
|
||||
void setCompressed(bool _compressed) { compressed = _compressed; }
|
||||
bool read(QXmlStreamReader *xml);
|
||||
void write(QXmlStreamWriter *xml);
|
||||
};
|
||||
|
||||
class SerializableItem_Invalid : public SerializableItem {
|
||||
public:
|
||||
SerializableItem_Invalid(const QString &_itemType) : SerializableItem(_itemType) { }
|
||||
void writeElement(QXmlStreamWriter * /*xml*/) { }
|
||||
bool isEmpty() const { return true; }
|
||||
};
|
||||
|
||||
class SerializableItem_Map : public SerializableItem {
|
||||
private:
|
||||
SerializableItem *currentItem;
|
||||
protected:
|
||||
QMap<QString, SerializableItem *> itemMap;
|
||||
QList<SerializableItem *> itemList;
|
||||
virtual void extractData() { }
|
||||
void insertItem(SerializableItem *item)
|
||||
{
|
||||
itemMap.insert(item->getItemType(), item);
|
||||
}
|
||||
template<class T> QList<T> typecastItemList() const
|
||||
{
|
||||
QList<T> result;
|
||||
for (int i = 0; i < itemList.size(); ++i) {
|
||||
T item = dynamic_cast<T>(itemList[i]);
|
||||
if (item)
|
||||
result.append(item);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public:
|
||||
SerializableItem_Map(const QString &_itemType, const QString &_itemSubType = QString())
|
||||
: SerializableItem(_itemType, _itemSubType), currentItem(0)
|
||||
{
|
||||
}
|
||||
~SerializableItem_Map();
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
bool isEmpty() const { return itemMap.isEmpty() && itemList.isEmpty(); }
|
||||
void appendItem(SerializableItem *item) { itemList.append(item); }
|
||||
};
|
||||
|
||||
class SerializableItem_String : public SerializableItem {
|
||||
private:
|
||||
QString data;
|
||||
protected:
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
public:
|
||||
SerializableItem_String(const QString &_itemType, const QString &_data = QString())
|
||||
: SerializableItem(_itemType), data(_data) { }
|
||||
const QString &getData() { return data; }
|
||||
void setData(const QString &_data) { data = _data; }
|
||||
bool isEmpty() const { return data.isEmpty(); }
|
||||
};
|
||||
|
||||
class SerializableItem_Int : public SerializableItem {
|
||||
private:
|
||||
int data;
|
||||
protected:
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
public:
|
||||
SerializableItem_Int(const QString &_itemType, int _data = -1)
|
||||
: SerializableItem(_itemType), data(_data) { }
|
||||
int getData() { return data; }
|
||||
void setData(int _data) { data = _data; }
|
||||
bool isEmpty() const { return data == -1; }
|
||||
};
|
||||
|
||||
class SerializableItem_Bool : public SerializableItem {
|
||||
private:
|
||||
bool data;
|
||||
protected:
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
public:
|
||||
SerializableItem_Bool(const QString &_itemType, bool _data = false)
|
||||
: SerializableItem(_itemType), data(_data) { }
|
||||
bool getData() { return data; }
|
||||
void setData(bool _data) { data = _data; }
|
||||
bool isEmpty() const { return data == false; }
|
||||
};
|
||||
|
||||
class SerializableItem_DateTime : public SerializableItem {
|
||||
private:
|
||||
QDateTime data;
|
||||
protected:
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
public:
|
||||
SerializableItem_DateTime(const QString &_itemType, const QDateTime &_data = QDateTime())
|
||||
: SerializableItem(_itemType), data(_data) { }
|
||||
const QDateTime &getData() { return data; }
|
||||
void setData(const QDateTime &_data) { data = _data; }
|
||||
bool isEmpty() const { return data == QDateTime(); }
|
||||
};
|
||||
|
||||
class SerializableItem_ByteArray : public SerializableItem {
|
||||
private:
|
||||
QByteArray data;
|
||||
protected:
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
public:
|
||||
SerializableItem_ByteArray(const QString &_itemType, const QByteArray &_data = QByteArray())
|
||||
: SerializableItem(_itemType), data(_data) { }
|
||||
const QByteArray &getData() { return data; }
|
||||
void setData(const QByteArray &_data) { data = _data; }
|
||||
bool isEmpty() const { return data.isEmpty(); }
|
||||
};
|
||||
|
||||
#endif
|
|
@ -6,6 +6,7 @@
|
|||
#include "server_game.h"
|
||||
#include "server_protocolhandler.h"
|
||||
#include "decklist.h"
|
||||
#include "color.h"
|
||||
#include "pb/response.pb.h"
|
||||
#include "pb/command_move_card.pb.h"
|
||||
#include "pb/serverinfo_user.pb.h"
|
||||
|
@ -133,26 +134,28 @@ void Server_Player::setupZones()
|
|||
}
|
||||
}
|
||||
|
||||
const QList<MoveCardToZone *> &sideboardPlan = deck->getCurrentSideboardPlan();
|
||||
const QList<MoveCard_ToZone> &sideboardPlan = deck->getCurrentSideboardPlan();
|
||||
for (int i = 0; i < sideboardPlan.size(); ++i) {
|
||||
MoveCardToZone *m = sideboardPlan[i];
|
||||
const MoveCard_ToZone &m = sideboardPlan[i];
|
||||
const QString startZone = QString::fromStdString(m.start_zone());
|
||||
const QString targetZone = QString::fromStdString(m.target_zone());
|
||||
|
||||
Server_CardZone *start, *target;
|
||||
if (m->getStartZone() == "main")
|
||||
if (startZone == "main")
|
||||
start = deckZone;
|
||||
else if (m->getStartZone() == "side")
|
||||
else if (startZone == "side")
|
||||
start = sbZone;
|
||||
else
|
||||
continue;
|
||||
if (m->getTargetZone() == "main")
|
||||
if (targetZone == "main")
|
||||
target = deckZone;
|
||||
else if (m->getTargetZone() == "side")
|
||||
else if (targetZone == "side")
|
||||
target = sbZone;
|
||||
else
|
||||
continue;
|
||||
|
||||
for (int j = 0; j < start->cards.size(); ++j)
|
||||
if (start->cards[j]->getName() == m->getCardName()) {
|
||||
if (start->cards[j]->getName() == QString::fromStdString(m.card_name())) {
|
||||
Server_Card *card = start->cards[j];
|
||||
start->cards.removeAt(j);
|
||||
target->cards.append(card);
|
||||
|
|
|
@ -887,15 +887,11 @@ Response::ResponseCode Server_ProtocolHandler::cmdSetSideboardPlan(const Command
|
|||
if (!deck)
|
||||
return Response::RespContextError;
|
||||
|
||||
QList<MoveCardToZone *> sideboardPlan;
|
||||
for (int i = 0; i < cmd.move_list_size(); ++i) {
|
||||
const MoveCard_ToZone &temp = cmd.move_list(i);
|
||||
sideboardPlan.append(new MoveCardToZone(QString::fromStdString(temp.card_name()), QString::fromStdString(temp.start_zone()), QString::fromStdString(temp.target_zone())));
|
||||
}
|
||||
QList<MoveCard_ToZone> sideboardPlan;
|
||||
for (int i = 0; i < cmd.move_list_size(); ++i)
|
||||
sideboardPlan.append(cmd.move_list(i));
|
||||
deck->setCurrentSideboardPlan(sideboardPlan);
|
||||
for (int i = 0; i < sideboardPlan.size(); ++i)
|
||||
delete sideboardPlan[i];
|
||||
// XXX TEMPORARY HACK
|
||||
|
||||
return Response::RespOk;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,7 +22,6 @@ HEADERS += src/main.h \
|
|||
src/serversocketthread.h \
|
||||
src/passwordhasher.h \
|
||||
../common/color.h \
|
||||
../common/serializable_item.h \
|
||||
../common/decklist.h \
|
||||
../common/rng_abstract.h \
|
||||
../common/rng_sfmt.h \
|
||||
|
@ -45,7 +44,6 @@ SOURCES += src/main.cpp \
|
|||
src/server_logger.cpp \
|
||||
src/serversocketthread.cpp \
|
||||
src/passwordhasher.cpp \
|
||||
../common/serializable_item.cpp \
|
||||
../common/decklist.cpp \
|
||||
../common/rng_abstract.cpp \
|
||||
../common/rng_sfmt.cpp \
|
||||
|
|
Loading…
Reference in a new issue