restructured protocol code

This commit is contained in:
Max-Wilhelm Bruker 2009-11-29 03:07:28 +01:00
parent 122f8ea916
commit 694070724c
32 changed files with 1202 additions and 2081 deletions

View file

@ -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;
}
while (!xmlReader->atEnd()) {
xmlReader->readNext();
if (xmlReader->isStartElement()) {
QString itemType = xmlReader->name().toString();
if (itemType == "cockatrice_server_stream") {
if (topLevelItem)
topLevelItem->read(xmlReader);
else {
while (!xmlReader->atEnd()) {
xmlReader->readNext();
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));
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;
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);
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();

View file

@ -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();

View file

@ -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;
}

View file

@ -1,9 +1,12 @@
#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;

View file

@ -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 {

View file

@ -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());
if (!arrow)
return;
emit logCreateArrow(this, arrow->getStartItem()->getOwner(), arrow->getStartItem()->getName(), arrow->getTargetItem()->getOwner(), arrow->getTargetItem()->getName());
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;

View file

@ -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);

View file

@ -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);
}
}

View file

@ -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)

View file

@ -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) {

View file

@ -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()

View file

@ -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());
}
}

View file

@ -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()
{

View file

@ -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;