Commit 69407072 authored by Max-Wilhelm Bruker's avatar Max-Wilhelm Bruker
Browse files

restructured protocol code

parent 122f8ea9
......@@ -50,6 +50,7 @@ HEADERS += src/counter.h \
src/remotedecklist_treewidget.h \
src/deckview.h \
src/playerlistwidget.h \
../common/serializable_item.h \
../common/decklist.h \
../common/protocol.h \
../common/protocol_items.h \
......@@ -98,6 +99,7 @@ SOURCES += src/counter.cpp \
src/remotedecklist_treewidget.cpp \
src/deckview.cpp \
src/playerlistwidget.cpp \
../common/serializable_item.cpp \
../common/decklist.cpp \
../common/protocol.cpp \
../common/protocol_items.cpp \
......
......@@ -6,7 +6,7 @@
#include "protocol_items.h"
Client::Client(QObject *parent)
: QObject(parent), currentItem(0), status(StatusDisconnected)
: QObject(parent), topLevelItem(0), status(StatusDisconnected)
{
ProtocolItem::initializeHash();
......@@ -58,44 +58,31 @@ void Client::readData()
qDebug() << data;
xmlReader->addData(data);
if (currentItem) {
if (!currentItem->read(xmlReader))
return;
currentItem = 0;
}
if (topLevelItem)
topLevelItem->read(xmlReader);
else {
while (!xmlReader->atEnd()) {
xmlReader->readNext();
if (xmlReader->isStartElement()) {
QString itemType = xmlReader->name().toString();
if (itemType == "cockatrice_server_stream") {
if (xmlReader->isStartElement() && (xmlReader->name().toString() == "cockatrice_server_stream")) {
int serverVersion = xmlReader->attributes().value("version").toString().toInt();
if (serverVersion != ProtocolItem::protocolVersion) {
emit protocolVersionMismatch(ProtocolItem::protocolVersion, serverVersion);
disconnectFromServer();
return;
} else {
}
xmlWriter->writeStartDocument();
xmlWriter->writeStartElement("cockatrice_client_stream");
xmlWriter->writeAttribute("version", QString::number(ProtocolItem::protocolVersion));
topLevelItem = new TopLevelProtocolItem;
connect(topLevelItem, SIGNAL(protocolItemReceived(ProtocolItem *)), this, SLOT(processProtocolItem(ProtocolItem *)));
setStatus(StatusLoggingIn);
Command_Login *cmdLogin = new Command_Login(userName, password);
connect(cmdLogin, SIGNAL(finished(ResponseCode)), this, SLOT(loginResponse(ResponseCode)));
sendCommand(cmdLogin);
continue;
}
}
QString itemName = xmlReader->attributes().value("name").toString();
qDebug() << "parseXml: startElement: " << "type =" << itemType << ", name =" << itemName;
currentItem = ProtocolItem::getNewItem(itemType + itemName);
if (!currentItem)
continue;
if (!currentItem->read(xmlReader))
return;
else {
processProtocolItem(currentItem);
currentItem = 0;
topLevelItem->read(xmlReader);
}
}
}
......@@ -171,7 +158,9 @@ void Client::connectToServer(const QString &hostname, unsigned int port, const Q
void Client::disconnectFromServer()
{
currentItem = 0;
delete topLevelItem;
topLevelItem = 0;
xmlReader->clear();
timer->stop();
......
......@@ -14,6 +14,7 @@ class QXmlStreamWriter;
class ProtocolItem;
class ProtocolResponse;
class TopLevelProtocolItem;
class ChatEvent;
class GameEvent;
class Event_ListGames;
......@@ -56,6 +57,7 @@ private slots:
void slotSocketError(QAbstractSocket::SocketError error);
void ping();
void loginResponse(ResponseCode response);
void processProtocolItem(ProtocolItem *item);
private:
static const int maxTimeout = 10;
......@@ -64,11 +66,10 @@ private:
QTcpSocket *socket;
QXmlStreamReader *xmlReader;
QXmlStreamWriter *xmlWriter;
ProtocolItem *currentItem;
TopLevelProtocolItem *topLevelItem;
ClientStatus status;
QString userName, password;
void setStatus(ClientStatus _status);
void processProtocolItem(ProtocolItem *item);
public:
Client(QObject *parent = 0);
~Client();
......
......@@ -14,7 +14,7 @@
DeckListModel::DeckListModel(QObject *parent)
: QAbstractItemModel(parent)
{
deckList = new DeckList(this);
deckList = new DeckList;
connect(deckList, SIGNAL(deckLoaded()), this, SLOT(rebuildTree()));
root = new InnerDecklistNode;
}
......@@ -22,6 +22,7 @@ DeckListModel::DeckListModel(QObject *parent)
DeckListModel::~DeckListModel()
{
delete root;
delete deckList;
}
......
#include "gamesmodel.h"
#include "protocol_datastructures.h"
GamesModel::~GamesModel()
{
if (!gameList.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, gameList.size() - 1);
for (int i = 0; i < gameList.size(); ++i)
delete gameList[i];
gameList.clear();
endRemoveRows();
}
......@@ -20,13 +23,13 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
if ((index.row() >= gameList.size()) || (index.column() >= columnCount()))
return QVariant();
const ServerGameInfo &g = gameList[index.row()];
ServerInfo_Game *g = gameList[index.row()];
switch (index.column()) {
case 0: return g.getDescription();
case 1: return g.getCreatorName();
case 2: return g.getHasPassword() ? tr("yes") : tr("no");
case 3: return QString("%1/%2").arg(g.getPlayerCount()).arg(g.getMaxPlayers());
case 4: return g.getSpectatorsAllowed() ? QVariant(g.getSpectatorCount()) : QVariant(tr("not allowed"));
case 0: return g->getDescription();
case 1: return g->getCreatorName();
case 2: return g->getHasPassword() ? tr("yes") : tr("no");
case 3: return QString("%1/%2").arg(g->getPlayerCount()).arg(g->getMaxPlayers());
case 4: return g->getSpectatorsAllowed() ? QVariant(g->getSpectatorCount()) : QVariant(tr("not allowed"));
default: return QVariant();
}
}
......@@ -45,30 +48,32 @@ QVariant GamesModel::headerData(int section, Qt::Orientation orientation, int ro
}
}
const ServerGameInfo &GamesModel::getGame(int row)
ServerInfo_Game *GamesModel::getGame(int row)
{
Q_ASSERT(row < gameList.size());
return gameList[row];
}
void GamesModel::updateGameList(const ServerGameInfo &game)
void GamesModel::updateGameList(ServerInfo_Game *_game)
{
ServerInfo_Game *game = new ServerInfo_Game(_game->getGameId(), _game->getDescription(), _game->getHasPassword(), _game->getPlayerCount(), _game->getMaxPlayers(), _game->getCreatorName(), _game->getSpectatorsAllowed(), _game->getSpectatorCount());
for (int i = 0; i < gameList.size(); i++)
if (gameList[i].getGameId() == game.getGameId()) {
if (game.getPlayerCount() == 0) {
if (gameList[i]->getGameId() == game->getGameId()) {
if (game->getPlayerCount() == 0) {
beginRemoveRows(QModelIndex(), i, i);
gameList.removeAt(i);
delete gameList.takeAt(i);
endRemoveRows();
} else {
delete gameList[i];
gameList[i] = game;
emit dataChanged(index(i, 0), index(i, 4));
}
return;
}
if (game.getPlayerCount() == 0)
if (game->getPlayerCount() == 0)
return;
beginInsertRows(QModelIndex(), gameList.size(), gameList.size());
gameList << game;
gameList.append(game);
endInsertRows();
}
......@@ -93,8 +98,8 @@ bool GamesProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &/*sourc
if (!model)
return false;
const ServerGameInfo &game = model->getGame(sourceRow);
if (game.getPlayerCount() == game.getMaxPlayers())
ServerInfo_Game *game = model->getGame(sourceRow);
if (game->getPlayerCount() == game->getMaxPlayers())
return false;
return true;
......
......@@ -4,7 +4,8 @@
#include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include <QList>
#include "protocol_datastructures.h"
class ServerInfo_Game;
class GamesModel : public QAbstractTableModel {
Q_OBJECT
......@@ -16,10 +17,10 @@ public:
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
const ServerGameInfo &getGame(int row);
void updateGameList(const ServerGameInfo &game);
ServerInfo_Game *getGame(int row);
void updateGameList(ServerInfo_Game *game);
private:
QList<ServerGameInfo> gameList;
QList<ServerInfo_Game *> gameList;
};
class GamesProxyModel : public QSortFilterProxyModel {
......
......@@ -373,7 +373,7 @@ void Player::actDrawCards()
void Player::actUntapAll()
{
// client->setCardAttr("table", -1, "tapped", "false");
sendGameCommand(new Command_SetCardAttr(-1, "table", -1, "tapped", "0"));
}
void Player::actRollDie()
......@@ -448,12 +448,15 @@ void Player::eventRollDie(Event_RollDie *event)
emit logRollDie(this, event->getSides(), event->getValue());
}
void Player::eventCreateArrow(Event_CreateArrow *event)
void Player::eventCreateArrows(Event_CreateArrows *event)
{
ArrowItem *arrow = addArrow(event->getArrow());
const QList<ServerInfo_Arrow *> eventArrowList = event->getArrowList();
for (int i = 0; i < eventArrowList.size(); ++i) {
ArrowItem *arrow = addArrow(eventArrowList[i]);
if (!arrow)
return;
emit logCreateArrow(this, arrow->getStartItem()->getOwner(), arrow->getStartItem()->getName(), arrow->getTargetItem()->getOwner(), arrow->getTargetItem()->getName());
}
}
void Player::eventDeleteArrow(Event_DeleteArrow *event)
......@@ -492,9 +495,11 @@ void Player::eventSetCardAttr(Event_SetCardAttr *event)
}
}
void Player::eventCreateCounter(Event_CreateCounter *event)
void Player::eventCreateCounters(Event_CreateCounters *event)
{
addCounter(event->getCounter());
const QList<ServerInfo_Counter *> &eventCounterList = event->getCounterList();
for (int i = 0; i < eventCounterList.size(); ++i)
addCounter(eventCounterList[i]);
}
void Player::eventSetCounter(Event_SetCounter *event)
......@@ -612,11 +617,11 @@ void Player::processGameEvent(GameEvent *event)
case ItemId_Event_ReadyStart: eventReadyStart(qobject_cast<Event_ReadyStart *>(event)); break;
case ItemId_Event_Shuffle: eventShuffle(qobject_cast<Event_Shuffle *>(event)); break;
case ItemId_Event_RollDie: eventRollDie(qobject_cast<Event_RollDie *>(event)); break;
case ItemId_Event_CreateArrow: eventCreateArrow(qobject_cast<Event_CreateArrow *>(event)); break;
case ItemId_Event_CreateArrows: eventCreateArrows(qobject_cast<Event_CreateArrows *>(event)); break;
case ItemId_Event_DeleteArrow: eventDeleteArrow(qobject_cast<Event_DeleteArrow *>(event)); break;
case ItemId_Event_CreateToken: eventCreateToken(qobject_cast<Event_CreateToken *>(event)); break;
case ItemId_Event_SetCardAttr: eventSetCardAttr(qobject_cast<Event_SetCardAttr *>(event)); break;
case ItemId_Event_CreateCounter: eventCreateCounter(qobject_cast<Event_CreateCounter *>(event)); break;
case ItemId_Event_CreateCounters: eventCreateCounters(qobject_cast<Event_CreateCounters *>(event)); break;
case ItemId_Event_SetCounter: eventSetCounter(qobject_cast<Event_SetCounter *>(event)); break;
case ItemId_Event_DelCounter: eventDelCounter(qobject_cast<Event_DelCounter *>(event)); break;
case ItemId_Event_DumpZone: eventDumpZone(qobject_cast<Event_DumpZone *>(event)); break;
......
......@@ -27,11 +27,11 @@ class Event_Say;
class Event_ReadyStart;
class Event_Shuffle;
class Event_RollDie;
class Event_CreateArrow;
class Event_CreateArrows;
class Event_DeleteArrow;
class Event_CreateToken;
class Event_SetCardAttr;
class Event_CreateCounter;
class Event_CreateCounters;
class Event_SetCounter;
class Event_DelCounter;
class Event_DumpZone;
......@@ -118,11 +118,11 @@ private:
void eventReadyStart(Event_ReadyStart *event);
void eventShuffle(Event_Shuffle *event);
void eventRollDie(Event_RollDie *event);
void eventCreateArrow(Event_CreateArrow *event);
void eventCreateArrows(Event_CreateArrows *event);
void eventDeleteArrow(Event_DeleteArrow *event);
void eventCreateToken(Event_CreateToken *event);
void eventSetCardAttr(Event_SetCardAttr *event);
void eventCreateCounter(Event_CreateCounter *event);
void eventCreateCounters(Event_CreateCounters *event);
void eventSetCounter(Event_SetCounter *event);
void eventDelCounter(Event_DelCounter *event);
void eventDumpZone(Event_DumpZone *event);
......
......@@ -54,12 +54,13 @@ void RemoteDeckList_TreeWidget::addFolderToTree(DeckList_Directory *folder, QTre
newItem->setData(0, Qt::UserRole, QString());
}
for (int i = 0; i < folder->size(); ++i) {
DeckList_Directory *subFolder = dynamic_cast<DeckList_Directory *>(folder->at(i));
const QList<DeckList_TreeItem *> &folderItems = folder->getTreeItems();
for (int i = 0; i < folderItems.size(); ++i) {
DeckList_Directory *subFolder = dynamic_cast<DeckList_Directory *>(folderItems[i]);
if (subFolder)
addFolderToTree(subFolder, newItem);
else
addFileToTree(dynamic_cast<DeckList_File *>(folder->at(i)), newItem);
addFileToTree(dynamic_cast<DeckList_File *>(folderItems[i]), newItem);
}
}
......
......@@ -51,9 +51,9 @@ void TabChatChannel::processChatEvent(ChatEvent *event)
void TabChatChannel::processListPlayersEvent(Event_ChatListPlayers *event)
{
const QList<ServerChatUserInfo> &players = event->getPlayerList();
const QList<ServerInfo_ChatUser *> &players = event->getPlayerList();
for (int i = 0; i < players.size(); ++i)
playerList->addItem(players[i].getName());
playerList->addItem(players[i]->getName());
}
void TabChatChannel::processJoinChannelEvent(Event_ChatJoinChannel *event)
......
......@@ -121,7 +121,6 @@ void TabDeckStorage::uploadFinished(ProtocolResponse *r)
if (!resp)
return;
Command_DeckUpload *cmd = static_cast<Command_DeckUpload *>(sender());
delete cmd->getDeck();
QTreeWidgetItemIterator it(serverDirView);
while (*it) {
......
......@@ -324,11 +324,9 @@ void TabGame::deckSelectFinished(ProtocolResponse *r)
Response_DeckDownload *resp = qobject_cast<Response_DeckDownload *>(r);
if (!resp)
return;
Command_DeckSelect *cmd = static_cast<Command_DeckSelect *>(sender());
delete cmd->getDeck();
Deck_PictureCacher::cachePictures(resp->getDeck(), this);
deckView->setDeck(resp->getDeck());
deckView->setDeck(new DeckList(resp->getDeck()));
}
void TabGame::readyStart()
......
......@@ -78,16 +78,16 @@ void GameSelector::actJoin()
QModelIndex ind = gameListView->currentIndex();
if (!ind.isValid())
return;
const ServerGameInfo &game = gameListModel->getGame(ind.data(Qt::UserRole).toInt());
ServerInfo_Game *game = gameListModel->getGame(ind.data(Qt::UserRole).toInt());
QString password;
if (game.getHasPassword()) {
if (game->getHasPassword()) {
bool ok;
password = QInputDialog::getText(this, tr("Join game"), tr("Password:"), QLineEdit::Password, QString(), &ok);
if (!ok)
return;
}
Command_JoinGame *commandJoinGame = new Command_JoinGame(game.getGameId(), password, spectator);
Command_JoinGame *commandJoinGame = new Command_JoinGame(game->getGameId(), password, spectator);
connect(commandJoinGame, SIGNAL(finished(ResponseCode)), this, SLOT(checkResponse(ResponseCode)));
client->sendCommand(commandJoinGame);
......@@ -107,7 +107,7 @@ void GameSelector::retranslateUi()
void GameSelector::processListGamesEvent(Event_ListGames *event)
{
const QList<ServerGameInfo> &gamesToUpdate = event->getGameList();
const QList<ServerInfo_Game *> &gamesToUpdate = event->getGameList();
for (int i = 0; i < gamesToUpdate.size(); ++i)
gameListModel->updateGameList(gamesToUpdate[i]);
}
......@@ -148,26 +148,26 @@ void ChatChannelSelector::retranslateUi()
void ChatChannelSelector::processListChatChannelsEvent(Event_ListChatChannels *event)
{
const QList<ServerChatChannelInfo> &channelsToUpdate = event->getChannelList();
const QList<ServerInfo_ChatChannel *> &channelsToUpdate = event->getChannelList();
for (int i = 0; i < channelsToUpdate.size(); ++i) {
const ServerChatChannelInfo &channel = channelsToUpdate[i];
ServerInfo_ChatChannel *channel = channelsToUpdate[i];
for (int j = 0; j < channelList->topLevelItemCount(); ++j) {
QTreeWidgetItem *twi = channelList->topLevelItem(j);
if (twi->text(0) == channel.getName()) {
twi->setText(1, channel.getDescription());
twi->setText(2, QString::number(channel.getPlayerCount()));
if (twi->text(0) == channel->getName()) {
twi->setText(1, channel->getDescription());
twi->setText(2, QString::number(channel->getPlayerCount()));
return;
}
}
QTreeWidgetItem *twi = new QTreeWidgetItem(QStringList() << channel.getName() << channel.getDescription() << QString::number(channel.getPlayerCount()));
QTreeWidgetItem *twi = new QTreeWidgetItem(QStringList() << channel->getName() << channel->getDescription() << QString::number(channel->getPlayerCount()));
twi->setTextAlignment(2, Qt::AlignRight);
channelList->addTopLevelItem(twi);
channelList->resizeColumnToContents(0);
channelList->resizeColumnToContents(1);
channelList->resizeColumnToContents(2);
if (channel.getAutoJoin())
joinChannel(channel.getName());
if (channel->getAutoJoin())
joinChannel(channel->getName());
}
}
......
......@@ -42,7 +42,7 @@ void ZoneViewZone::initializeCards()
}
}
void ZoneViewZone::zoneDumpReceived(QList<ServerInfo_Card> cards)
/*void ZoneViewZone::zoneDumpReceived(QList<ServerInfo_Card *> cards)
{
for (int i = 0; i < cards.size(); i++) {
CardItem *card = new CardItem(player, cards[i].getName(), i, this);
......@@ -52,7 +52,7 @@ void ZoneViewZone::zoneDumpReceived(QList<ServerInfo_Card> cards)
emit contentsChanged();
reorganizeCards();
}
*/
// Because of boundingRect(), this function must not be called before the zone was added to a scene.
void ZoneViewZone::reorganizeCards()
{
......
......@@ -29,7 +29,7 @@ public:
public slots:
void setSortingEnabled(int _sortingEnabled);
private slots:
void zoneDumpReceived(QList<ServerInfo_Card> cards);
// void zoneDumpReceived(QList<ServerInfo_Card> cards);
protected:
void addCardImpl(CardItem *card, int x, int y);
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
......
......@@ -21,6 +21,18 @@ int AbstractDecklistNode::depth() const
return 0;
}
InnerDecklistNode::InnerDecklistNode(InnerDecklistNode *other, InnerDecklistNode *_parent)
: AbstractDecklistNode(_parent), name(other->getName())
{
for (int i = 0; i < other->size(); ++i) {
InnerDecklistNode *inner = dynamic_cast<InnerDecklistNode *>(other->at(i));
if (inner)
new InnerDecklistNode(inner, this);
else
new DecklistCardNode(dynamic_cast<DecklistCardNode *>(other->at(i)), this);
}
}
InnerDecklistNode::~InnerDecklistNode()
{
clearTree();
......@@ -48,6 +60,11 @@ void InnerDecklistNode::clearTree()
clear();
}
DecklistCardNode::DecklistCardNode(DecklistCardNode *other, InnerDecklistNode *_parent)
: AbstractDecklistCardNode(_parent), name(other->getName()), number(other->getNumber())
{
}
AbstractDecklistNode *InnerDecklistNode::findChild(const QString &name)
{
for (int i = 0; i < size(); i++)
......@@ -178,76 +195,71 @@ const QStringList DeckList::fileNameFilters = QStringList()
<< QObject::tr("Plain text decks (*.dec *.mwDeck)")
<< QObject::tr("All files (*.*)");
DeckList::DeckList(QObject *parent)
: QObject(parent), currentZone(0)
DeckList::DeckList()
: SerializableItem("cockatrice_deck"), currentZone(0)
{
root = new InnerDecklistNode;
}
DeckList::DeckList(DeckList *other)
: SerializableItem("cockatrice_deck"), currentZone(0)
{
root = new InnerDecklistNode(other->getRoot());
}
DeckList::~DeckList()
{
delete root;
}
bool DeckList::readElement(QXmlStreamReader *xml)
void DeckList::readElement(QXmlStreamReader *xml)
{
if (currentZone) {
if (currentZone->readElement(xml))
currentZone = 0;
return false;
}
if (xml->isEndElement()) {
} else if (xml->isEndElement()) {
if (xml->name() == "deckname")
name = currentElementText;
else if (xml->name() == "comments")
comments = currentElementText;
else if (xml->name() == "cockatrice_deck")
return true;
currentElementText.clear();
} else if (xml->isStartElement() && (xml->name() == "zone"))
currentZone = new InnerDecklistNode(xml->attributes().value("name").toString(), root);
else if (xml->isCharacters() && !xml->isWhitespace())
currentElementText = xml->text().toString();
return false;
}
void DeckList::writeElement(QXmlStreamWriter *xml)
{
xml->writeStartElement("cockatrice_deck");
xml->writeAttribute("version", "1");
xml->writeTextElement("deckname", name);
xml->writeTextElement("comments", comments);
for (int i = 0; i < root->size(); i++)
root->at(i)->writeElement(xml);
xml->writeEndElement(); // cockatrice_deck
}
bool DeckList::loadFromXml(QXmlStreamReader *xml)
void DeckList::loadFromXml(QXmlStreamReader *xml)
{
while (!xml->atEnd()) {
xml->readNext();
if (xml->isStartElement()) {
if (xml->name() != "cockatrice_deck")
return false;
return;
while (!xml->atEnd()) {
xml->readNext();
if (readElement(xml))
return true;
readElement(xml);
}
}
}
return false;
}
bool DeckList::loadFromFile_Native(QIODevice *device)
{
QXmlStreamReader xml(device);
return loadFromXml(&xml);
loadFromXml(&xml);
return true;
}
bool DeckList::saveToFile_Native(QIODevice *device)
......@@ -256,7 +268,7 @@ bool DeckList::saveToFile_Native(QIODevice *device)
xml.setAutoFormatting(true);
xml.writeStartDocument();
writeElement(&xml);
write(&xml);
xml.writeEndDocument();
return true;
......
......@@ -5,6 +5,7 @@
#include <QVector>
#include <QPair>
#include <QObject>
#include "serializable_item.h"
class CardDatabase;
class QIODevice;
......@@ -36,6 +37,7 @@ private:
class compareFunctor;
public:
InnerDecklistNode(const QString &_name = QString(), InnerDecklistNode *_parent = 0) : AbstractDecklistNode(_parent), name(_name) { }
InnerDecklistNode(InnerDecklistNode *other, InnerDecklistNode *_parent = 0);
virtual ~InnerDecklistNode();
QString getName() const { return name; }
void setName(const QString &_name) { name = _name; }
......@@ -72,13 +74,14 @@ private:
int number;
public:
DecklistCardNode(const QString &_name = QString(), int _number = 1, InnerDecklistNode *_parent = 0) : AbstractDecklistCardNode(_parent), name(_name), number(_number) { }
DecklistCardNode(DecklistCardNode *other, InnerDecklistNode *_parent);
int getNumber() const { return number; }
void setNumber(int _number) { number = _number; }
QString getName() const { return name; }
void setName(const QString &_name) { name = _name; }
};
class DeckList : public QObject {
class DeckList : public SerializableItem {
Q_OBJECT
public:
enum FileFormat { PlainTextFormat, CockatriceFormat };
......@@ -96,16 +99,17 @@ public slots:
void setComments(const QString &_comments = QString()) { comments = _comments; }
public:
static const QStringList fileNameFilters;
DeckList(QObject *parent = 0);
DeckList();
DeckList(DeckList *other);
~DeckList();
QString getName() const { return name; }
QString getComments() const { return comments; }
QString getLastFileName() const { return lastFileName; }
FileFormat getLastFileFormat() const { return lastFileFormat; }
bool readElement(QXmlStreamReader *xml);
void readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
bool loadFromXml(QXmlStreamReader *xml);
void loadFromXml(QXmlStreamReader *xml);
bool loadFromFile_Native(QIODevice *device);
bool saveToFile_Native(QIODevice *device);
......
This diff is collapsed.
......@@ -23,43 +23,44 @@ enum ItemId {
ItemId_Event_ChatListPlayers = ItemId_Other + 201,
ItemId_Event_ListGames = ItemId_Other + 202,
ItemId_Event_GameStateChanged = ItemId_Other + 203,
ItemId_Event_CreateArrow = ItemId_Other + 204,
ItemId_Event_CreateCounter = ItemId_Other + 205,
ItemId_Event_CreateArrows = ItemId_Other + 204,
ItemId_Event_CreateCounters = ItemId_Other + 205,
ItemId_Event_DrawCards = ItemId_Other + 206,
ItemId_Response_DeckList = ItemId_Other + 300,
ItemId_Response_DeckDownload = ItemId_Other + 301,
ItemId_Response_DeckUpload = ItemId_Other + 302
ItemId_Response_DeckUpload = ItemId_Other + 302,
ItemId_Invalid = ItemId_Other + 1000
};
class ProtocolItem : public QObject {
class ProtocolItem : public SerializableItem_Map {
Q_OBJECT
private:
QString currentElementText;
protected:
typedef ProtocolItem *(*NewItemFunction)();
static QHash<QString, NewItemFunction> itemNameHash;
static void initializeHashAuto();
public:
static const int protocolVersion = 5;
static void initializeHash();
virtual int getItemId() const = 0;
ProtocolItem(const QString &_itemType, const QString &_itemSubType);
};
QString itemName;
QMap<QString, QString> parameters;
void setParameter(const QString &name, const QString &value) { parameters[name] = value; }
void setParameter(const QString &name, bool value) { parameters[name] = (value ? "1" : "0"); }
void setParameter(const QString &name, int value) { parameters[name] = QString::number(value); }
void setParameter(const QString &name, const QColor &value) { parameters[name] = QString::number(ColorConverter::colorToInt(value)); }
virtual void extractParameters() { }
virtual QString getItemType() const = 0;
class ProtocolItem_Invalid : public ProtocolItem {
public:
ProtocolItem_Invalid() : ProtocolItem(QString(), QString()) { }
int getItemId() const { return ItemId_Invalid; }
};
virtual bool readElement(QXmlStreamReader * /*xml*/) { return false; }
virtual void writeElement(QXmlStreamWriter * /*xml*/) { }
class TopLevelProtocolItem : public SerializableItem {
Q_OBJECT
signals:
void protocolItemReceived(ProtocolItem *item);
private:
static void initializeHashAuto();
ProtocolItem *currentItem;
bool readCurrentItem(QXmlStreamReader *xml);
protected:
void readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
public:
static const int protocolVersion = 4;
virtual int getItemId() const = 0;
ProtocolItem(const QString &_itemName);
static void initializeHash();
static ProtocolItem *getNewItem(const QString &name);
bool read(QXmlStreamReader *xml);
void write(QXmlStreamWriter *xml);
TopLevelProtocolItem();
};
// ----------------
......@@ -72,106 +73,59 @@ signals:
void finished(ProtocolResponse *response);
void finished(ResponseCode response);
private:
int cmdId;
int ticks;
static int lastCmdId;
QVariant extraData;
protected:
QString getItemType() const { return "cmd"; }
void extractParameters();
public:
Command(const QString &_itemName = QString(), int _cmdId = -1);
int getCmdId() const { return cmdId; }
int getCmdId() const { return static_cast<SerializableItem_Int *>(itemMap.value("cmd_id"))->getData(); }
int tick() { return ++ticks; }
void processResponse(ProtocolResponse *response);
void setExtraData(const QVariant &_extraData) { extraData = _extraData; }
QVariant getExtraData() const { return extraData; }
};
class InvalidCommand : public Command {
Q_OBJECT
public:
InvalidCommand() : Command() { }
int getItemId() const { return ItemId_Other; }
};
class ChatCommand : public Command {
Q_OBJECT
private:
QString channel;
protected:
void extractParameters()
{
Command::extractParameters();
channel = parameters["channel"];
}
public:
ChatCommand(const QString &_cmdName, const QString &_channel)
: Command(_cmdName), channel(_channel)
: Command(_cmdName)
{
setParameter("channel", channel);
insertItem(new SerializableItem_String("channel", _channel));
}
QString getChannel() const { return channel; }
QString getChannel() const { return static_cast<SerializableItem_String *>(itemMap.value("channel"))->getData(); }
};
class GameCommand : public Command {
Q_OBJECT
private:
int gameId;
protected:
void extractParameters()
{
Command::extractParameters();
gameId = parameters["game_id"].toInt();
}
public:
GameCommand(const QString &_cmdName, int _gameId)
: Command(_cmdName), gameId(_gameId)
{
setParameter("game_id", gameId);
}
int getGameId() const { return gameId; }
void setGameId(int _gameId)
: Command(_cmdName)
{
gameId = _gameId;
setParameter("game_id", gameId);
insertItem(new SerializableItem_Int("game_id", _gameId));
}
int getGameId() const { return static_cast<SerializableItem_Int *>(itemMap.value("game_id"))->getData(); }
void setGameId(int _gameId) { static_cast<SerializableItem_Int *>(itemMap.value("game_id"))->setData(_gameId); }
};
class Command_DeckUpload : public Command {
Q_OBJECT
private:
DeckList *deck;
QString path;
bool readFinished;
protected:
void extractParameters();
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
public:
Command_DeckUpload(DeckList *_deck = 0, const QString &_path = QString());
static ProtocolItem *newItem() { return new Command_DeckUpload; }
static SerializableItem *newItem() { return new Command_DeckUpload; }
int getItemId() const { return ItemId_Command_DeckUpload; }
DeckList *getDeck() const { return deck; }
QString getPath() const { return path; }
DeckList *getDeck() const;
QString getPath() const { return static_cast<SerializableItem_String *>(itemMap.value("path"))->getData(); }
};
class Command_DeckSelect : public GameCommand {
Q_OBJECT
private:
DeckList *deck;
int deckId;
bool readFinished;
protected:
void extractParameters();
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
public:
Command_DeckSelect(int _gameId = -1, DeckList *_deck = 0, int _deckId = -1);
static ProtocolItem *newItem() { return new Command_DeckSelect; }
static SerializableItem *newItem() { return new Command_DeckSelect; }
int getItemId() const { return ItemId_Command_DeckSelect; }
DeckList *getDeck() const { return deck; }
int getDeckId() const { return deckId; }
DeckList *getDeck() const;
int getDeckId() const { return static_cast<SerializableItem_Int *>(itemMap.value("deck_id"))->getData(); }
};
// -----------------
......@@ -181,66 +135,41 @@ public:
class ProtocolResponse : public ProtocolItem {
Q_OBJECT
private:
int cmdId;
ResponseCode responseCode;
static QHash<QString, ResponseCode> responseHash;
protected:
QString getItemType() const { return "resp"; }
void extractParameters();
public:
ProtocolResponse(int _cmdId = -1, ResponseCode _responseCode = RespOk, const QString &_itemName = QString());
int getItemId() const { return ItemId_Other; }
static void initializeHash();
static ProtocolItem *newItem() { return new ProtocolResponse; }
int getCmdId() const { return cmdId; }
ResponseCode getResponseCode() const { return responseCode; }
static SerializableItem *newItem() { return new ProtocolResponse; }
int getCmdId() const { return static_cast<SerializableItem_Int *>(itemMap.value("cmd_id"))->getData(); }
ResponseCode getResponseCode() const { return responseHash.value(static_cast<SerializableItem_String *>(itemMap.value("response_code"))->getData(), RespOk); }
};
class Response_DeckList : public ProtocolResponse {
Q_OBJECT
private:
DeckList_Directory *root;
bool readFinished;
protected:
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
public:
Response_DeckList(int _cmdId = -1, ResponseCode _responseCode = RespOk, DeckList_Directory *_root = 0);
~Response_DeckList();
int getItemId() const { return ItemId_Response_DeckList; }
static ProtocolItem *newItem() { return new Response_DeckList; }
DeckList_Directory *getRoot() const { return root; }
static SerializableItem *newItem() { return new Response_DeckList; }
DeckList_Directory *getRoot() const { return static_cast<DeckList_Directory *>(itemMap.value("directory")); }
};
class Response_DeckDownload : public ProtocolResponse {
Q_OBJECT
private:
DeckList *deck;
bool readFinished;
protected:
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
public:
Response_DeckDownload(int _cmdId = -1, ResponseCode _responseCode = RespOk, DeckList *_deck = 0);
int getItemId() const { return ItemId_Response_DeckDownload; }
static ProtocolItem *newItem() { return new Response_DeckDownload; }
DeckList *getDeck() const { return deck; }
static SerializableItem *newItem() { return new Response_DeckDownload; }
DeckList *getDeck() const;
};
class Response_DeckUpload : public ProtocolResponse {
Q_OBJECT
private:
DeckList_File *file;
bool readFinished;
protected:
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
public:
Response_DeckUpload(int _cmdId = -1, ResponseCode _responseCode = RespOk, DeckList_File *_file = 0);
~Response_DeckUpload();
int getItemId() const { return ItemId_Response_DeckUpload; }
static ProtocolItem *newItem() { return new Response_DeckUpload; }
DeckList_File *getFile() const { return file; }
static SerializableItem *newItem() { return new Response_DeckUpload; }
DeckList_File *getFile() const { return static_cast<DeckList_File *>(itemMap.value("file")); }
};
// --------------
......@@ -249,162 +178,89 @@ public:
class GenericEvent : public ProtocolItem {
Q_OBJECT
protected:
QString getItemType() const { return "generic_event"; }
public:
GenericEvent(const QString &_eventName);
GenericEvent(const QString &_eventName)
: ProtocolItem("generic_event", _eventName) { }
};
class GameEvent : public ProtocolItem {
Q_OBJECT
private:
int gameId;
int playerId;
protected:
QString getItemType() const { return "game_event"; }
void extractParameters();
public:
GameEvent(const QString &_eventName, int _gameId, int _playerId);
int getGameId() const { return gameId; }
int getPlayerId() const { return playerId; }
void setGameId(int _gameId)
{
gameId = _gameId;
setParameter("game_id", gameId);
}
int getGameId() const { return static_cast<SerializableItem_Int *>(itemMap.value("game_id"))->getData(); }
int getPlayerId() const { return static_cast<SerializableItem_Int *>(itemMap.value("player_id"))->getData(); }
void setGameId(int _gameId) { static_cast<SerializableItem_Int *>(itemMap.value("game_id"))->setData(_gameId); }
};
class ChatEvent : public ProtocolItem {
Q_OBJECT
private:
QString channel;
protected:
QString getItemType() const { return "chat_event"; }
void extractParameters();
public:
ChatEvent(const QString &_eventName, const QString &_channel);
QString getChannel() const { return channel; }
QString getChannel() const { return static_cast<SerializableItem_String *>(itemMap.value("channel"))->getData(); }
};
class Event_ListChatChannels : public GenericEvent {
Q_OBJECT
private:
QList<ServerChatChannelInfo> channelList;
public:
Event_ListChatChannels() : GenericEvent("list_chat_channels") { }
Event_ListChatChannels(const QList<ServerInfo_ChatChannel *> &_channelList = QList<ServerInfo_ChatChannel *>());
int getItemId() const { return ItemId_Event_ListChatChannels; }
static ProtocolItem *newItem() { return new Event_ListChatChannels; }
void addChannel(const QString &_name, const QString &_description, int _playerCount, bool _autoJoin)
{
channelList.append(ServerChatChannelInfo(_name, _description, _playerCount, _autoJoin));
}
const QList<ServerChatChannelInfo> &getChannelList() const { return channelList; }
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
static SerializableItem *newItem() { return new Event_ListChatChannels; }
QList<ServerInfo_ChatChannel *> getChannelList() const { return typecastItemList<ServerInfo_ChatChannel *>(); }
};
class Event_ChatListPlayers : public ChatEvent {
Q_OBJECT
private:
QList<ServerChatUserInfo> playerList;
public:
Event_ChatListPlayers(const QString &_channel = QString()) : ChatEvent("chat_list_players", _channel) { }
Event_ChatListPlayers(const QString &_channel = QString(), const QList<ServerInfo_ChatUser *> &_playerList = QList<ServerInfo_ChatUser *>());
int getItemId() const { return ItemId_Event_ChatListPlayers; }
static ProtocolItem *newItem() { return new Event_ChatListPlayers; }
void addPlayer(const QString &_name)
{
playerList.append(ServerChatUserInfo(_name));
}
const QList<ServerChatUserInfo> &getPlayerList() const { return playerList; }
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
static SerializableItem *newItem() { return new Event_ChatListPlayers; }
QList<ServerInfo_ChatUser *> getPlayerList() const { return typecastItemList<ServerInfo_ChatUser *>(); }
};
class Event_ListGames : public GenericEvent {
Q_OBJECT
private:
QList<ServerGameInfo> gameList;
public:
Event_ListGames() : GenericEvent("list_games") { }
Event_ListGames(const QList<ServerInfo_Game *> &_gameList = QList<ServerInfo_Game *>());
int getItemId() const { return ItemId_Event_ListGames; }
static ProtocolItem *newItem() { return new Event_ListGames; }
void addGame(int _gameId, const QString &_description, bool _hasPassword, int _playerCount, int _maxPlayers, const QString &_creatorName, bool _spectatorsAllowed, int _spectatorCount)
{
gameList.append(ServerGameInfo(_gameId, _description, _hasPassword, _playerCount, _maxPlayers, _creatorName, _spectatorsAllowed, _spectatorCount));
}
const QList<ServerGameInfo> &getGameList() const { return gameList; }
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
static SerializableItem *newItem() { return new Event_ListGames; }
QList<ServerInfo_Game *> getGameList() const { return typecastItemList<ServerInfo_Game *>(); }
};
class Event_GameStateChanged : public GameEvent {
Q_OBJECT
private:
SerializableItem *currentItem;
QList<ServerInfo_Player *> playerList;
public:
Event_GameStateChanged(int _gameId = -1, const QList<ServerInfo_Player *> &_playerList = QList<ServerInfo_Player *>());
~Event_GameStateChanged();
const QList<ServerInfo_Player *> &getPlayerList() const { return playerList; }
static ProtocolItem *newItem() { return new Event_GameStateChanged; }
static SerializableItem *newItem() { return new Event_GameStateChanged; }
int getItemId() const { return ItemId_Event_GameStateChanged; }
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
QList<ServerInfo_Player *> getPlayerList() const { return typecastItemList<ServerInfo_Player *>(); }
};
class Event_CreateArrow : public GameEvent {
class Event_CreateArrows : public GameEvent {
Q_OBJECT
private:
ServerInfo_Arrow *arrow;
bool readFinished;
protected:
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
public:
Event_CreateArrow(int _gameId = -1, int _playerId = -1, ServerInfo_Arrow *_arrow = 0);
~Event_CreateArrow();
int getItemId() const { return ItemId_Event_CreateArrow; }
static ProtocolItem *newItem() { return new Event_CreateArrow; }
ServerInfo_Arrow *getArrow() const { return arrow; }
Event_CreateArrows(int _gameId = -1, int _playerId = -1, const QList<ServerInfo_Arrow *> &_arrowList = QList<ServerInfo_Arrow *>());
int getItemId() const { return ItemId_Event_CreateArrows; }
static SerializableItem *newItem() { return new Event_CreateArrows; }
QList<ServerInfo_Arrow *> getArrowList() const { return typecastItemList<ServerInfo_Arrow *>(); }
};
class Event_CreateCounter : public GameEvent {
class Event_CreateCounters : public GameEvent {
Q_OBJECT
private:
ServerInfo_Counter *counter;
bool readFinished;
protected:
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
public:
Event_CreateCounter(int _gameId = -1, int _playerId = -1, ServerInfo_Counter *_counter = 0);
~Event_CreateCounter();
int getItemId() const { return ItemId_Event_CreateCounter; }
static ProtocolItem *newItem() { return new Event_CreateCounter; }
ServerInfo_Counter *getCounter() const { return counter; }
Event_CreateCounters(int _gameId = -1, int _playerId = -1, const QList<ServerInfo_Counter *> &_counterList = QList<ServerInfo_Counter *>());
int getItemId() const { return ItemId_Event_CreateCounters; }
static SerializableItem *newItem() { return new Event_CreateCounters; }
QList<ServerInfo_Counter *> getCounterList() const { return typecastItemList<ServerInfo_Counter *>(); }
};
class Event_DrawCards : public GameEvent {
Q_OBJECT
private:
SerializableItem *currentItem;
int numberCards;
QList<ServerInfo_Card *> cardList;
protected:
void extractParameters();
bool readElement(QXmlStreamReader *xml);
void writeElement(QXmlStreamWriter *xml);
public:
Event_DrawCards(int _gameId = -1, int _playerId = -1, int numberCards = -1, const QList<ServerInfo_Card *> &_cardList = QList<ServerInfo_Card *>());
~Event_DrawCards();
int getItemId() const { return ItemId_Event_DrawCards; }
static ProtocolItem *newItem() { return new Event_DrawCards; }
int getNumberCards() const { return numberCards; }
const QList<ServerInfo_Card *> &getCardList() const { return cardList; }
static SerializableItem *newItem() { return new Event_DrawCards; }
int getNumberCards() const { return static_cast<SerializableItem_Int *>(itemMap.value("number_cards"))->getData(); }
QList<ServerInfo_Card *> getCardList() const { return typecastItemList<ServerInfo_Card *>(); }
};
#endif
......@@ -2,244 +2,154 @@
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
ServerInfo_Player::~ServerInfo_Player()
ServerInfo_ChatChannel::ServerInfo_ChatChannel(const QString &_name, const QString &_description, int _playerCount, bool _autoJoin)
: SerializableItem_Map("chat_channel")
{
for (int i = 0; i < zoneList.size(); ++i)
delete zoneList[i];
for (int i = 0; i < arrowList.size(); ++i)
delete arrowList[i];
for (int i = 0; i < counterList.size(); ++i)
delete counterList[i];
insertItem(new SerializableItem_String("name", _name));
insertItem(new SerializableItem_String("description", _description));
insertItem(new SerializableItem_Int("player_count", _playerCount));
insertItem(new SerializableItem_Bool("auto_join", _autoJoin));
}
ServerInfo_Zone::~ServerInfo_Zone()
ServerInfo_ChatUser::ServerInfo_ChatUser(const QString &_name)
: SerializableItem_Map("chat_user")
{
for (int i = 0; i < cardList.size(); ++i)
delete cardList[i];
insertItem(new SerializableItem_String("name", _name));
}
bool ServerInfo_Arrow::readElement(QXmlStreamReader *xml)
ServerInfo_Game::ServerInfo_Game(int _gameId, const QString &_description, bool _hasPassword, int _playerCount, int _maxPlayers, const QString &_creatorName, bool _spectatorsAllowed, int _spectatorCount)
: SerializableItem_Map("game")
{
if (xml->isStartElement() && (xml->name() == "arrow")) {
id = xml->attributes().value("id").toString().toInt();
startPlayerId = xml->attributes().value("start_player_id").toString().toInt();
startZone = xml->attributes().value("start_zone").toString();
startCardId = xml->attributes().value("start_card_id").toString().toInt();
targetPlayerId = xml->attributes().value("target_player_id").toString().toInt();
targetZone = xml->attributes().value("target_zone").toString();
targetCardId = xml->attributes().value("target_card_id").toString().toInt();
color = ColorConverter::colorFromInt(xml->attributes().value("color").toString().toInt());
} else if (xml->isEndElement() && (xml->name() == "arrow"))
return true;
return false;
insertItem(new SerializableItem_Int("game_id", _gameId));
insertItem(new SerializableItem_String("description", _description));
insertItem(new SerializableItem_Bool("has_password", _hasPassword));
insertItem(new SerializableItem_Int("player_count", _playerCount));
insertItem(new SerializableItem_Int("max_players", _maxPlayers));
insertItem(new SerializableItem_String("creator_name", _creatorName));
insertItem(new SerializableItem_Bool("spectators_allowed", _spectatorsAllowed));
insertItem(new SerializableItem_Int("spectator_count", _spectatorCount));
}
void ServerInfo_Arrow::writeElement(QXmlStreamWriter *xml)
ServerInfo_Card::ServerInfo_Card(int _id, const QString &_name, int _x, int _y, int _counters, bool _tapped, bool _attacking, const QString &_annotation)
: SerializableItem_Map("card")
{
xml->writeStartElement("arrow");
xml->writeAttribute("id", QString::number(id));
xml->writeAttribute("start_player_id", QString::number(startPlayerId));
xml->writeAttribute("start_zone", startZone);
xml->writeAttribute("start_card_id", QString::number(startCardId));
xml->writeAttribute("target_player_id", QString::number(targetPlayerId));
xml->writeAttribute("target_zone", targetZone);
xml->writeAttribute("target_card_id", QString::number(targetCardId));
xml->writeAttribute("color", QString::number(ColorConverter::colorToInt(color)));
xml->writeEndElement();
insertItem(new SerializableItem_Int("id", _id));
insertItem(new SerializableItem_String("name", _name));
insertItem(new SerializableItem_Int("x", _x));
insertItem(new SerializableItem_Int("y", _y));
insertItem(new SerializableItem_Int("counters", _counters));
insertItem(new SerializableItem_Bool("tapped", _tapped));
insertItem(new SerializableItem_Bool("attacking", _attacking));
insertItem(new SerializableItem_String("annotation", _annotation));
}
bool ServerInfo_Counter::readElement(QXmlStreamReader *xml)
ServerInfo_Zone::ServerInfo_Zone(const QString &_name, ZoneType _type, bool _hasCoords, int _cardCount, const QList<ServerInfo_Card *> &_cardList)
: SerializableItem_Map("zone")
{
if (xml->isStartElement() && (xml->name() == "counter")) {
id = xml->attributes().value("id").toString().toInt();
name = xml->attributes().value("name").toString();
color = ColorConverter::colorFromInt(xml->attributes().value("color").toString().toInt());
radius = xml->attributes().value("radius").toString().toInt();
count = xml->attributes().value("count").toString().toInt();
} else if (xml->isEndElement() && (xml->name() == "counter"))
return true;
return false;
}
insertItem(new SerializableItem_String("name", _name));
insertItem(new SerializableItem_String("zone_type", typeToString(_type)));
insertItem(new SerializableItem_Bool("has_coords", _hasCoords));
insertItem(new SerializableItem_Int("card_count", _cardCount));
void ServerInfo_Counter::writeElement(QXmlStreamWriter *xml)
{
xml->writeStartElement("counter");
xml->writeAttribute("id", QString::number(id));
xml->writeAttribute("name", name);
xml->writeAttribute("color", QString::number(ColorConverter::colorToInt(color)));
xml->writeAttribute("radius", QString::number(radius));
xml->writeAttribute("count", QString::number(count));
xml->writeEndElement();
}
bool ServerInfo_Card::readElement(QXmlStreamReader *xml)
{
if (xml->isStartElement() && (xml->name() == "card")) {
id = xml->attributes().value("id").toString().toInt();
name = xml->attributes().value("name").toString();
x = xml->attributes().value("x").toString().toInt();
y = xml->attributes().value("y").toString().toInt();
counters = xml->attributes().value("counters").toString().toInt();
tapped = xml->attributes().value("tapped").toString().toInt();
attacking = xml->attributes().value("attacking").toString().toInt();
annotation = xml->attributes().value("annotation").toString();
} else if (xml->isEndElement() && (xml->name() == "card"))
return true;
return false;
for (int i = 0; i < _cardList.size(); ++i)
itemList.append(_cardList[i]);
}
void ServerInfo_Card::writeElement(QXmlStreamWriter *xml)
ZoneType ServerInfo_Zone::typeFromString(const QString &type) const
{
xml->writeStartElement("card");
xml->writeAttribute("id", QString::number(id));
xml->writeAttribute("name", name);
xml->writeAttribute("x", QString::number(x));
xml->writeAttribute("y", QString::number(y));
xml->writeAttribute("counters", QString::number(counters));
xml->writeAttribute("tapped", tapped ? "1" : "0");
xml->writeAttribute("attacking", attacking ? "1" : "0");
xml->writeAttribute("annotation", annotation);
xml->writeEndElement();
if (type == "private")
return PrivateZone;
else if (type == "hidden")
return HiddenZone;
return PublicZone;
}
bool ServerInfo_Zone::readElement(QXmlStreamReader *xml)
QString ServerInfo_Zone::typeToString(ZoneType type) const
{
if (currentItem) {
if (currentItem->readElement(xml))
currentItem = 0;
return false;
switch (type) {
case PrivateZone: return "private";
case HiddenZone: return "hidden";
default: return "public";
}
if (xml->isStartElement() && (xml->name() == "zone")) {
name = xml->attributes().value("name").toString();
type = (ZoneType) xml->attributes().value("type").toString().toInt();
hasCoords = xml->attributes().value("has_coords").toString().toInt();
cardCount = xml->attributes().value("card_count").toString().toInt();
} else if (xml->isStartElement() && (xml->name() == "card")) {
ServerInfo_Card *card = new ServerInfo_Card;
cardList.append(card);
currentItem = card;
} else if (xml->isEndElement() && (xml->name() == "zone"))
return true;
if (currentItem)
if (currentItem->readElement(xml))
currentItem = 0;
return false;
}
void ServerInfo_Zone::writeElement(QXmlStreamWriter *xml)
QList<ServerInfo_Card *> ServerInfo_Zone::getCardList() const
{
xml->writeStartElement("zone");
xml->writeAttribute("name", name);
QString typeStr;
switch (type) {
case PrivateZone: typeStr = "private"; break;
case HiddenZone: typeStr = "hidden"; break;
case PublicZone: typeStr = "public"; break;
QList<ServerInfo_Card *> result;
for (int i = 0; i < itemList.size(); ++i) {
ServerInfo_Card *card = dynamic_cast<ServerInfo_Card *>(itemList[i]);
if (card)
result.append(card);
}
xml->writeAttribute("type", typeStr);
xml->writeAttribute("has_coords", hasCoords ? "1" : "0");
xml->writeAttribute("card_count", QString::number(cardCount));
for (int i = 0; i < cardList.size(); ++i)
cardList[i]->writeElement(xml);
xml->writeEndElement();
return result;
}
bool ServerInfo_Player::readElement(QXmlStreamReader *xml)
ServerInfo_Counter::ServerInfo_Counter(int _id, const QString &_name, const QColor &_color, int _radius, int _count)
: SerializableItem_Map("counter")
{
if (currentItem) {
if (currentItem->readElement(xml))
currentItem = 0;
return false;
}
if (xml->isStartElement() && (xml->name() == "player")) {
playerId = xml->attributes().value("player_id").toString().toInt();
name = xml->attributes().value("name").toString();
} else if (xml->isStartElement() && (xml->name() == "zone")) {
ServerInfo_Zone *zone = new ServerInfo_Zone;
zoneList.append(zone);
currentItem = zone;
} else if (xml->isStartElement() && (xml->name() == "counter")) {
ServerInfo_Counter *counter = new ServerInfo_Counter;
counterList.append(counter);
currentItem = counter;
} else if (xml->isStartElement() && (xml->name() == "arrow")) {
ServerInfo_Arrow *arrow = new ServerInfo_Arrow;
arrowList.append(arrow);
currentItem = arrow;
} else if (xml->isEndElement() && (xml->name() == "player"))
return true;
if (currentItem)
if (currentItem->readElement(xml))
currentItem = 0;
return false;
insertItem(new SerializableItem_Int("id", _id));
insertItem(new SerializableItem_String("name", _name));
insertItem(new SerializableItem_Color("color", _color));
insertItem(new SerializableItem_Int("radius", _radius));
insertItem(new SerializableItem_Int("count", _count));
}
void ServerInfo_Player::writeElement(QXmlStreamWriter *xml)
ServerInfo_Arrow::ServerInfo_Arrow(int _id, int _startPlayerId, const QString &_startZone, int _startCardId, int _targetPlayerId, const QString &_targetZone, int _targetCardId, const QColor &_color)
: SerializableItem_Map("arrow")
{
xml->writeStartElement("player");
xml->writeAttribute("player_id", QString::number(playerId));
xml->writeAttribute("name", name);
for (int i = 0; i < zoneList.size(); ++i)
zoneList[i]->writeElement(xml);
for (int i = 0; i < counterList.size(); ++i)
counterList[i]->writeElement(xml);
for (int i = 0; i < arrowList.size(); ++i)
arrowList[i]->writeElement(xml);
xml->writeEndElement();
insertItem(new SerializableItem_Int("id", _id));
insertItem(new SerializableItem_Int("start_player_id", _startPlayerId));
insertItem(new SerializableItem_String("start_zone", _startZone));
insertItem(new SerializableItem_Int("start_card_id", _startCardId));
insertItem(new SerializableItem_Int("target_player_id", _targetPlayerId));
insertItem(new SerializableItem_String("target_zone", _targetZone));
insertItem(new SerializableItem_Int("target_card_id", _targetCardId));
insertItem(new SerializableItem_Color("color", _color));
}
bool DeckList_File::readElement(QXmlStreamReader *xml)
ServerInfo_Player::ServerInfo_Player(int _playerId, const QString &_name, const QList<ServerInfo_Zone *> &_zoneList, const QList<ServerInfo_Counter *> &_counterList, const QList<ServerInfo_Arrow *> &_arrowList)
: SerializableItem_Map("player"), zoneList(_zoneList), counterList(_counterList), arrowList(_arrowList)
{
if (xml->isEndElement())
return true;
else
return false;
insertItem(new SerializableItem_Int("player_id", _playerId));
insertItem(new SerializableItem_String("name", _name));
for (int i = 0; i < _zoneList.size(); ++i)
itemList.append(_zoneList[i]);
for (int i = 0; i < _counterList.size(); ++i)
itemList.append(_counterList[i]);
for (int i = 0; i < _arrowList.size(); ++i)
itemList.append(_arrowList[i]);
}
void DeckList_File::writeElement(QXmlStreamWriter *xml)
void ServerInfo_Player::extractData()
{
xml->writeStartElement("file");
xml->writeAttribute("name", name);
xml->writeAttribute("id", QString::number(id));
xml->writeAttribute("upload_time", QString::number(uploadTime.toTime_t()));
xml->writeEndElement();
for (int i = 0; i < itemList.size(); ++i) {
ServerInfo_Zone *zone = dynamic_cast<ServerInfo_Zone *>(itemList[i]);
ServerInfo_Counter *counter = dynamic_cast<ServerInfo_Counter *>(itemList[i]);
ServerInfo_Arrow *arrow = dynamic_cast<ServerInfo_Arrow *>(itemList[i]);
if (zone)
zoneList.append(zone);
else if (counter)
counterList.append(counter);
else if (arrow)
arrowList.append(arrow);
}
}
DeckList_Directory::~DeckList_Directory()
DeckList_TreeItem::DeckList_TreeItem(const QString &_itemType, const QString &_name, int _id)
: SerializableItem_Map(_itemType)
{
for (int i = 0; i < size(); ++i)
delete at(i);
insertItem(new SerializableItem_String("name", _name));
insertItem(new SerializableItem_Int("id", _id));
}
bool DeckList_Directory::readElement(QXmlStreamReader *xml)
DeckList_File::DeckList_File(const QString &_name, int _id, QDateTime _uploadTime)
: DeckList_TreeItem("file", _name, _id)
{
if (currentItem) {
if (currentItem->readElement(xml))
currentItem = 0;
return false;
}
if (xml->isStartElement() && (xml->name() == "directory")) {
DeckList_Directory *newItem = new DeckList_Directory(xml->attributes().value("name").toString());
append(newItem);
currentItem = newItem;
} else if (xml->isStartElement() && (xml->name() == "file")) {
DeckList_File *newItem = new DeckList_File(xml->attributes().value("name").toString(), xml->attributes().value("id").toString().toInt(), QDateTime::fromTime_t(xml->attributes().value("upload_time").toString().toUInt()));
append(newItem);
currentItem = newItem;
} else if (xml->isEndElement() && (xml->name() == "directory"))
return true;
return false;
insertItem(new SerializableItem_DateTime("upload_time", _uploadTime));
}
void DeckList_Directory::writeElement(QXmlStreamWriter *xml)
DeckList_Directory::DeckList_Directory(const QString &_name, int _id)
: DeckList_TreeItem("directory", _name, _id)
{
xml->writeStartElement("directory");
xml->writeAttribute("name", name);
for (int i = 0; i < size(); ++i)
at(i)->writeElement(xml);
xml->writeEndElement();
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment