diff --git a/cockatrice/cockatrice.pro b/cockatrice/cockatrice.pro index 16258d50..a6885d71 100644 --- a/cockatrice/cockatrice.pro +++ b/cockatrice/cockatrice.pro @@ -6,13 +6,15 @@ MOC_DIR = build OBJECTS_DIR = build RESOURCES = cockatrice.qrc QT += network svg +CONFIG += debug HEADERS += src/counter.h \ src/dlg_creategame.h \ src/dlg_connect.h \ src/dlg_create_token.h \ src/gamesmodel.h \ - src/client.h \ + src/abstractclient.h \ + src/remoteclient.h \ src/window_main.h \ src/cardzone.h \ src/player.h \ @@ -58,17 +60,33 @@ HEADERS += src/counter.h \ src/playerlistwidget.h \ src/pingpixmapgenerator.h \ src/settingscache.h \ + src/localserver.h \ + src/localserverinterface.h \ + src/localclient.h \ ../common/serializable_item.h \ ../common/decklist.h \ ../common/protocol.h \ ../common/protocol_items.h \ - ../common/protocol_datastructures.h + ../common/protocol_datastructures.h \ + ../common/rng_abstract.h \ + ../common/rng_qt.h \ + ../common/server.h \ + ../common/server_arrow.h \ + ../common/server_card.h \ + ../common/server_cardzone.h \ + ../common/server_chatchannel.h \ + ../common/server_counter.h \ + ../common/server_game.h \ + ../common/server_player.h \ + ../common/server_protocolhandler.h \ + ../common/server_arrowtarget.h SOURCES += src/counter.cpp \ src/dlg_creategame.cpp \ src/dlg_connect.cpp \ src/dlg_create_token.cpp \ - src/client.cpp \ + src/abstractclient.cpp \ + src/remoteclient.cpp \ src/main.cpp \ src/window_main.cpp \ src/gamesmodel.cpp \ @@ -115,12 +133,24 @@ SOURCES += src/counter.cpp \ src/playerlistwidget.cpp \ src/pingpixmapgenerator.cpp \ src/settingscache.cpp \ + src/localserver.cpp \ + src/localserverinterface.cpp \ + src/localclient.cpp \ ../common/serializable_item.cpp \ ../common/decklist.cpp \ ../common/protocol.cpp \ ../common/protocol_items.cpp \ - ../common/protocol_datastructures.cpp - + ../common/protocol_datastructures.cpp \ + ../common/rng_abstract.cpp \ + ../common/rng_qt.cpp \ + ../common/server.cpp \ + ../common/server_card.cpp \ + ../common/server_cardzone.cpp \ + ../common/server_chatchannel.cpp \ + ../common/server_game.cpp \ + ../common/server_player.cpp \ + ../common/server_protocolhandler.cpp + TRANSLATIONS += translations/cockatrice_de.ts translations/cockatrice_en.ts win32 { RC_FILE = cockatrice.rc diff --git a/cockatrice/src/client.cpp b/cockatrice/src/client.cpp deleted file mode 100644 index e5562273..00000000 --- a/cockatrice/src/client.cpp +++ /dev/null @@ -1,194 +0,0 @@ -#include -#include -#include -#include "client.h" -#include "protocol.h" -#include "protocol_items.h" - -Client::Client(QObject *parent) - : QObject(parent), topLevelItem(0), status(StatusDisconnected) -{ - ProtocolItem::initializeHash(); - - timer = new QTimer(this); - timer->setInterval(1000); - connect(timer, SIGNAL(timeout()), this, SLOT(ping())); - - socket = new QTcpSocket(this); - connect(socket, SIGNAL(connected()), this, SLOT(slotConnected())); - connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); - connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotSocketError(QAbstractSocket::SocketError))); - - xmlReader = new QXmlStreamReader; - xmlWriter = new QXmlStreamWriter; - xmlWriter->setAutoFormatting(true); - xmlWriter->setDevice(socket); -} - -Client::~Client() -{ - disconnectFromServer(); -} - -void Client::slotSocketError(QAbstractSocket::SocketError /*error*/) -{ - emit socketError(socket->errorString()); - disconnectFromServer(); -} - -void Client::slotConnected() -{ - timer->start(); - setStatus(StatusAwaitingWelcome); -} - -void Client::loginResponse(ResponseCode response) -{ - if (response == RespOk) - setStatus(StatusLoggedIn); - else { - emit serverError(response); - setStatus(StatusDisconnecting); - } -} - -void Client::readData() -{ - QByteArray data = socket->readAll(); - qDebug() << data; - xmlReader->addData(data); - - while (!xmlReader->atEnd()) { - xmlReader->readNext(); - if (topLevelItem) - topLevelItem->readElement(xmlReader); - else 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; - } - 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); - } - } - if (status == StatusDisconnecting) - disconnectFromServer(); -} - -void Client::processProtocolItem(ProtocolItem *item) -{ - ProtocolResponse *response = qobject_cast(item); - if (response) { - CommandContainer *cmdCont = pendingCommands.value(response->getCmdId(), 0); - if (!cmdCont) - return; - - pendingCommands.remove(cmdCont->getCmdId()); - cmdCont->processResponse(response); - delete response; - delete cmdCont; - - return; - } - - GenericEvent *genericEvent = qobject_cast(item); - if (genericEvent) { - switch (genericEvent->getItemId()) { - case ItemId_Event_ListGames: emit listGamesEventReceived(qobject_cast(item)); break; - case ItemId_Event_ServerMessage: emit serverMessageEventReceived(qobject_cast(item)); break; - case ItemId_Event_ListChatChannels: emit listChatChannelsEventReceived(qobject_cast(item)); break; - case ItemId_Event_GameJoined: emit gameJoinedEventReceived(qobject_cast(item)); break; - } - delete genericEvent; - return; - } - - GameEventContainer *gameEventContainer = qobject_cast(item); - if (gameEventContainer) { - emit gameEventContainerReceived(gameEventContainer); - delete gameEventContainer; - return; - } - - ChatEvent *chatEvent = qobject_cast(item); - if (chatEvent) { - emit chatEventReceived(chatEvent); - delete chatEvent; - return; - } -} - -void Client::setStatus(const ClientStatus _status) -{ - if (_status != status) { - status = _status; - emit statusChanged(_status); - } -} - -void Client::sendCommand(Command *cmd) -{ - sendCommandContainer(new CommandContainer(QList() << cmd)); -} - -void Client::sendCommandContainer(CommandContainer *cont) -{ - cont->write(xmlWriter); - pendingCommands.insert(cont->getCmdId(), cont); -} - -void Client::connectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password) -{ - disconnectFromServer(); - - userName = _userName; - password = _password; - socket->connectToHost(hostname, port); - setStatus(StatusConnecting); -} - -void Client::disconnectFromServer() -{ - delete topLevelItem; - topLevelItem = 0; - - xmlReader->clear(); - - timer->stop(); - - QList pc = pendingCommands.values(); - for (int i = 0; i < pc.size(); i++) - delete pc[i]; - pendingCommands.clear(); - - setStatus(StatusDisconnected); - socket->close(); -} - -void Client::ping() -{ - int maxTime = 0; - QMapIterator i(pendingCommands); - while (i.hasNext()) { - int time = i.next().value()->tick(); - if (time > maxTime) - maxTime = time; - } - emit maxPingTime(maxTime, maxTimeout); - if (maxTime >= maxTimeout) { - emit serverTimeout(); - disconnectFromServer(); - } else - sendCommand(new Command_Ping); -} diff --git a/cockatrice/src/client.h b/cockatrice/src/client.h deleted file mode 100644 index afa000e7..00000000 --- a/cockatrice/src/client.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef CLIENT_H -#define CLIENT_H - -#include -#include -#include -#include -#include "protocol_datastructures.h" - -class QTimer; -class Command; -class CommandContainer; -class QXmlStreamReader; -class QXmlStreamWriter; - -class ProtocolItem; -class ProtocolResponse; -class TopLevelProtocolItem; -class CommandContainer; -class ChatEvent; -class GameEventContainer; -class Event_ListGames; -class Event_ServerMessage; -class Event_ListChatChannels; -class Event_GameJoined; - -enum ClientStatus { - StatusDisconnected, - StatusDisconnecting, - StatusConnecting, - StatusAwaitingWelcome, - StatusLoggingIn, - StatusLoggedIn, -}; - -class Client : public QObject { - Q_OBJECT -signals: - void statusChanged(ClientStatus _status); - void maxPingTime(int seconds, int maxSeconds); - void serverTimeout(); - void socketError(const QString &errorString); - void serverError(ResponseCode resp); - void protocolVersionMismatch(int clientVersion, int serverVersion); - void protocolError(); - - // Chat events - void chatEventReceived(ChatEvent *event); - // Game events - void gameEventContainerReceived(GameEventContainer *event); - // Generic events - void listGamesEventReceived(Event_ListGames *event); - void serverMessageEventReceived(Event_ServerMessage *event); - void listChatChannelsEventReceived(Event_ListChatChannels *event); - void gameJoinedEventReceived(Event_GameJoined *event); - -private slots: - void slotConnected(); - void readData(); - void slotSocketError(QAbstractSocket::SocketError error); - void ping(); - void loginResponse(ResponseCode response); - void processProtocolItem(ProtocolItem *item); -private: - static const int maxTimeout = 10; - - QTimer *timer; - QMap pendingCommands; - QTcpSocket *socket; - QXmlStreamReader *xmlReader; - QXmlStreamWriter *xmlWriter; - TopLevelProtocolItem *topLevelItem; - ClientStatus status; - QString userName, password; - void setStatus(ClientStatus _status); -public: - Client(QObject *parent = 0); - ~Client(); - ClientStatus getStatus() const { return status; } - QString peerName() const { return socket->peerName(); } - - void connectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password); - void disconnectFromServer(); - void sendCommand(Command *cmd); - void sendCommandContainer(CommandContainer *cont); -}; - -#endif diff --git a/cockatrice/src/counter.cpp b/cockatrice/src/counter.cpp index b47e297b..bd6f07c5 100644 --- a/cockatrice/src/counter.cpp +++ b/cockatrice/src/counter.cpp @@ -1,6 +1,5 @@ #include "counter.h" #include "player.h" -#include "client.h" #include "protocol_items.h" #include diff --git a/cockatrice/src/dlg_creategame.cpp b/cockatrice/src/dlg_creategame.cpp index 41406cc5..c613c273 100644 --- a/cockatrice/src/dlg_creategame.cpp +++ b/cockatrice/src/dlg_creategame.cpp @@ -2,7 +2,7 @@ #include "dlg_creategame.h" #include "protocol_items.h" -DlgCreateGame::DlgCreateGame(Client *_client, QWidget *parent) +DlgCreateGame::DlgCreateGame(AbstractClient *_client, QWidget *parent) : QDialog(parent), client(_client) { descriptionLabel = new QLabel(tr("&Description:")); diff --git a/cockatrice/src/dlg_creategame.h b/cockatrice/src/dlg_creategame.h index 529e4b22..af72fe18 100644 --- a/cockatrice/src/dlg_creategame.h +++ b/cockatrice/src/dlg_creategame.h @@ -2,7 +2,7 @@ #define DLG_CREATEGAME_H #include -#include "client.h" +#include "abstractclient.h" class QLabel; class QLineEdit; @@ -14,13 +14,13 @@ class QSpinBox; class DlgCreateGame : public QDialog { Q_OBJECT public: - DlgCreateGame(Client *_client, QWidget *parent = 0); + DlgCreateGame(AbstractClient *_client, QWidget *parent = 0); private slots: void actOK(); void checkResponse(ResponseCode response); void spectatorsAllowedChanged(int state); private: - Client *client; + AbstractClient *client; QGroupBox *spectatorsGroupBox; QLabel *descriptionLabel, *passwordLabel, *maxPlayersLabel; diff --git a/cockatrice/src/dlg_load_remote_deck.cpp b/cockatrice/src/dlg_load_remote_deck.cpp index 8d498cd9..10c38379 100644 --- a/cockatrice/src/dlg_load_remote_deck.cpp +++ b/cockatrice/src/dlg_load_remote_deck.cpp @@ -5,7 +5,7 @@ #include "dlg_load_remote_deck.h" #include "main.h" -DlgLoadRemoteDeck::DlgLoadRemoteDeck(Client *_client, QWidget *parent) +DlgLoadRemoteDeck::DlgLoadRemoteDeck(AbstractClient *_client, QWidget *parent) : QDialog(parent), client(_client) { dirView = new RemoteDeckList_TreeWidget(client); diff --git a/cockatrice/src/dlg_load_remote_deck.h b/cockatrice/src/dlg_load_remote_deck.h index b8c842b9..ae57aaa5 100644 --- a/cockatrice/src/dlg_load_remote_deck.h +++ b/cockatrice/src/dlg_load_remote_deck.h @@ -5,19 +5,19 @@ class RemoteDeckList_TreeWidget; class QModelIndex; -class Client; +class AbstractClient; class QPushButton; class DlgLoadRemoteDeck: public QDialog { Q_OBJECT private: - Client *client; + AbstractClient *client; RemoteDeckList_TreeWidget *dirView; QPushButton *okButton, *cancelButton; private slots: void currentItemChanged(const QModelIndex ¤t, const QModelIndex &previous); public: - DlgLoadRemoteDeck(Client *_client, QWidget *parent = 0); + DlgLoadRemoteDeck(AbstractClient *_client, QWidget *parent = 0); int getDeckId() const; }; diff --git a/cockatrice/src/pilezone.cpp b/cockatrice/src/pilezone.cpp index 8df96a77..dc11b238 100644 --- a/cockatrice/src/pilezone.cpp +++ b/cockatrice/src/pilezone.cpp @@ -1,7 +1,6 @@ #include #include "pilezone.h" #include "player.h" -#include "client.h" #include "carddragitem.h" #include "zoneviewzone.h" #include "protocol_items.h" diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 486fa2ab..a6d5f024 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -1,5 +1,5 @@ #include "player.h" -#include "client.h" +#include "abstractclient.h" #include "cardzone.h" #include "playertarget.h" #include "counter.h" @@ -19,8 +19,9 @@ #include #include #include +#include -Player::Player(const QString &_name, int _id, bool _local, Client *_client, TabGame *_parent) +Player::Player(const QString &_name, int _id, bool _local, AbstractClient *_client, TabGame *_parent) : QObject(_parent), defaultNumberTopCards(3), lastTokenDestroy(true), name(_name), id(_id), active(false), local(_local), mirrored(false), client(_client) { setCacheMode(DeviceCoordinateCache); diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index 0dfa5028..48a64828 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -6,7 +6,7 @@ #include #include "carditem.h" -class Client; +class AbstractClient; class CardDatabase; class QMenu; class QAction; @@ -171,8 +171,8 @@ public: void clearArrows(); PlayerTarget *getPlayerTarget() const { return playerTarget; } - Client *client; - Player(const QString &_name, int _id, bool _local, Client *_client, TabGame *_parent); + AbstractClient *client; + Player(const QString &_name, int _id, bool _local, AbstractClient *_client, TabGame *_parent); ~Player(); void retranslateUi(); QMenu *getPlayerMenu() const { return playerMenu; } diff --git a/cockatrice/src/remotedecklist_treewidget.cpp b/cockatrice/src/remotedecklist_treewidget.cpp index eab93ed9..0244ebc8 100644 --- a/cockatrice/src/remotedecklist_treewidget.cpp +++ b/cockatrice/src/remotedecklist_treewidget.cpp @@ -3,7 +3,7 @@ #include #include "remotedecklist_treewidget.h" #include "protocol_items.h" -#include "client.h" +#include "abstractclient.h" RemoteDeckList_TreeModel::DirectoryNode::DirectoryNode(const QString &_name, RemoteDeckList_TreeModel::DirectoryNode *_parent) : RemoteDeckList_TreeModel::Node(_name, _parent) @@ -72,7 +72,7 @@ RemoteDeckList_TreeModel::FileNode *RemoteDeckList_TreeModel::DirectoryNode::get return 0; } -RemoteDeckList_TreeModel::RemoteDeckList_TreeModel(Client *_client, QObject *parent) +RemoteDeckList_TreeModel::RemoteDeckList_TreeModel(AbstractClient *_client, QObject *parent) : QAbstractItemModel(parent), client(_client) { QFileIconProvider fip; @@ -256,7 +256,7 @@ void RemoteDeckList_TreeModel::deckListFinished(ProtocolResponse *r) emit treeRefreshed(); } -RemoteDeckList_TreeWidget::RemoteDeckList_TreeWidget(Client *_client, QWidget *parent) +RemoteDeckList_TreeWidget::RemoteDeckList_TreeWidget(AbstractClient *_client, QWidget *parent) : QTreeView(parent) { treeModel = new RemoteDeckList_TreeModel(_client, this); diff --git a/cockatrice/src/remotedecklist_treewidget.h b/cockatrice/src/remotedecklist_treewidget.h index a5d718aa..308d4bdb 100644 --- a/cockatrice/src/remotedecklist_treewidget.h +++ b/cockatrice/src/remotedecklist_treewidget.h @@ -6,7 +6,7 @@ #include class ProtocolResponse; -class Client; +class AbstractClient; class QSortFilterProxyModel; class DeckList_File; class DeckList_Directory; @@ -55,7 +55,7 @@ public: return dynamic_cast(static_cast(index.internalPointer())); } private: - Client *client; + AbstractClient *client; DirectoryNode *root; QIcon fileIcon, dirIcon; @@ -66,7 +66,7 @@ signals: private slots: void deckListFinished(ProtocolResponse *r); public: - RemoteDeckList_TreeModel(Client *_client, QObject *parent = 0); + RemoteDeckList_TreeModel(AbstractClient *_client, QObject *parent = 0); ~RemoteDeckList_TreeModel(); int rowCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &/*parent*/ = QModelIndex()) const; @@ -89,7 +89,7 @@ private: RemoteDeckList_TreeModel *treeModel; QSortFilterProxyModel *proxyModel; public: - RemoteDeckList_TreeWidget(Client *_client, QWidget *parent = 0); + RemoteDeckList_TreeWidget(AbstractClient *_client, QWidget *parent = 0); RemoteDeckList_TreeModel::Node *getNode(const QModelIndex &ind) const; RemoteDeckList_TreeModel::Node *getCurrentItem() const; RemoteDeckList_TreeModel::DirectoryNode *getNodeByPath(const QString &path) const; diff --git a/cockatrice/src/tab_chatchannel.cpp b/cockatrice/src/tab_chatchannel.cpp index cd8e9c51..b17e25cc 100644 --- a/cockatrice/src/tab_chatchannel.cpp +++ b/cockatrice/src/tab_chatchannel.cpp @@ -1,9 +1,9 @@ #include #include "tab_chatchannel.h" -#include "client.h" +#include "abstractclient.h" #include "protocol_items.h" -TabChatChannel::TabChatChannel(Client *_client, const QString &_channelName) +TabChatChannel::TabChatChannel(AbstractClient *_client, const QString &_channelName) : Tab(), client(_client), channelName(_channelName) { playerList = new QListWidget; diff --git a/cockatrice/src/tab_chatchannel.h b/cockatrice/src/tab_chatchannel.h index 7552043a..812c689d 100644 --- a/cockatrice/src/tab_chatchannel.h +++ b/cockatrice/src/tab_chatchannel.h @@ -3,7 +3,7 @@ #include "tab.h" -class Client; +class AbstractClient; class QListWidget; class QTextEdit; class QLineEdit; @@ -16,7 +16,7 @@ class Event_ChatSay; class TabChatChannel : public Tab { Q_OBJECT private: - Client *client; + AbstractClient *client; QString channelName; QListWidget *playerList; @@ -35,7 +35,7 @@ private slots: void processLeaveChannelEvent(Event_ChatLeaveChannel *event); void processSayEvent(Event_ChatSay *event); public: - TabChatChannel(Client *_client, const QString &_channelName); + TabChatChannel(AbstractClient *_client, const QString &_channelName); ~TabChatChannel(); void retranslateUi(); void processChatEvent(ChatEvent *event); diff --git a/cockatrice/src/tab_deck_storage.cpp b/cockatrice/src/tab_deck_storage.cpp index 5e0d296f..08741459 100644 --- a/cockatrice/src/tab_deck_storage.cpp +++ b/cockatrice/src/tab_deck_storage.cpp @@ -1,12 +1,12 @@ #include #include "tab_deck_storage.h" #include "remotedecklist_treewidget.h" -#include "client.h" +#include "abstractclient.h" #include "decklist.h" #include "protocol_items.h" #include "window_deckeditor.h" -TabDeckStorage::TabDeckStorage(Client *_client) +TabDeckStorage::TabDeckStorage(AbstractClient *_client) : Tab(), client(_client) { localDirModel = new QFileSystemModel(this); diff --git a/cockatrice/src/tab_deck_storage.h b/cockatrice/src/tab_deck_storage.h index dd47a6c9..43893efb 100644 --- a/cockatrice/src/tab_deck_storage.h +++ b/cockatrice/src/tab_deck_storage.h @@ -4,7 +4,7 @@ #include "tab.h" #include "protocol.h" -class Client; +class AbstractClient; class QTreeView; class QFileSystemModel; class QSortFilterProxyModel; @@ -18,7 +18,7 @@ class RemoteDeckList_TreeWidget; class TabDeckStorage : public Tab { Q_OBJECT private: - Client *client; + AbstractClient *client; QTreeView *localDirView; QFileSystemModel *localDirModel; QSortFilterProxyModel *sortFilter; @@ -45,7 +45,7 @@ private slots: void actDelete(); void deleteFinished(ResponseCode resp); public: - TabDeckStorage(Client *_client); + TabDeckStorage(AbstractClient *_client); void retranslateUi(); QString getTabText() const { return tr("Deck storage"); } }; diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index c588a96b..2f32a020 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -14,7 +14,7 @@ #include "deck_picturecacher.h" #include "protocol_items.h" #include "dlg_load_remote_deck.h" -#include "client.h" +#include "abstractclient.h" #include "carditem.h" #include "arrowitem.h" #include "main.h" @@ -42,7 +42,7 @@ void ReadyStartButton::setReadyStart(bool _readyStart) update(); } -TabGame::TabGame(Client *_client, int _gameId, const QString &_gameDescription, int _localPlayerId, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming) +TabGame::TabGame(AbstractClient *_client, int _gameId, const QString &_gameDescription, int _localPlayerId, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming) : Tab(), client(_client), gameId(_gameId), gameDescription(_gameDescription), localPlayerId(_localPlayerId), spectator(_spectator), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), started(false), resuming(_resuming), currentPhase(-1) { scene = new GameScene(this); diff --git a/cockatrice/src/tab_game.h b/cockatrice/src/tab_game.h index f09692aa..21fb0689 100644 --- a/cockatrice/src/tab_game.h +++ b/cockatrice/src/tab_game.h @@ -5,7 +5,7 @@ #include #include "tab.h" -class Client; +class AbstractClient; class CardDatabase; class GameView; class DeckView; @@ -55,7 +55,7 @@ protected: class TabGame : public Tab { Q_OBJECT private: - Client *client; + AbstractClient *client; int gameId; QString gameDescription; int localPlayerId; @@ -120,7 +120,7 @@ private slots: void actNextPhase(); void actNextTurn(); public: - TabGame(Client *_client, int _gameId, const QString &_gameDescription, int _localPlayerId, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming); + TabGame(AbstractClient *_client, int _gameId, const QString &_gameDescription, int _localPlayerId, bool _spectator, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, bool _resuming); ~TabGame(); void retranslateUi(); const QMap &getPlayers() const { return players; } diff --git a/cockatrice/src/tab_server.cpp b/cockatrice/src/tab_server.cpp index 6c626262..1a93dbb0 100644 --- a/cockatrice/src/tab_server.cpp +++ b/cockatrice/src/tab_server.cpp @@ -2,11 +2,11 @@ #include "tab_server.h" #include "gamesmodel.h" #include "dlg_creategame.h" -#include "client.h" +#include "abstractclient.h" #include "protocol.h" #include "protocol_items.h" -GameSelector::GameSelector(Client *_client, QWidget *parent) +GameSelector::GameSelector(AbstractClient *_client, QWidget *parent) : QGroupBox(parent), client(_client) { gameListView = new QTreeView; @@ -113,7 +113,7 @@ void GameSelector::processListGamesEvent(Event_ListGames *event) gameListModel->updateGameList(gamesToUpdate[i]); } -ChatChannelSelector::ChatChannelSelector(Client *_client, QWidget *parent) +ChatChannelSelector::ChatChannelSelector(AbstractClient *_client, QWidget *parent) : QGroupBox(parent), client(_client) { channelList = new QTreeWidget; @@ -200,7 +200,7 @@ void ChatChannelSelector::joinFinished(ResponseCode resp) emit channelJoined(channelName); } -ServerMessageLog::ServerMessageLog(Client *_client, QWidget *parent) +ServerMessageLog::ServerMessageLog(AbstractClient *_client, QWidget *parent) : QGroupBox(parent) { textEdit = new QTextEdit; @@ -225,7 +225,7 @@ void ServerMessageLog::processServerMessageEvent(Event_ServerMessage *event) textEdit->append(event->getMessage()); } -TabServer::TabServer(Client *_client, QWidget *parent) +TabServer::TabServer(AbstractClient *_client, QWidget *parent) : Tab(parent), client(_client) { gameSelector = new GameSelector(client); diff --git a/cockatrice/src/tab_server.h b/cockatrice/src/tab_server.h index 9ba12b2d..08771582 100644 --- a/cockatrice/src/tab_server.h +++ b/cockatrice/src/tab_server.h @@ -5,7 +5,7 @@ #include "tab.h" #include "protocol_datastructures.h" -class Client; +class AbstractClient; class QTreeView; class QTreeWidget; class QPushButton; @@ -22,7 +22,7 @@ class Event_ServerMessage; class GameSelector : public QGroupBox { Q_OBJECT public: - GameSelector(Client *_client, QWidget *parent = 0); + GameSelector(AbstractClient *_client, QWidget *parent = 0); void retranslateUi(); private slots: void processListGamesEvent(Event_ListGames *event); @@ -33,7 +33,7 @@ private slots: signals: void gameJoined(int gameId); private: - Client *client; + AbstractClient *client; QTreeView *gameListView; GamesModel *gameListModel; @@ -47,7 +47,7 @@ class ChatChannelSelector : public QGroupBox { private: QTreeWidget *channelList; QPushButton *joinButton; - Client *client; + AbstractClient *client; void joinChannel(const QString &channelName); private slots: @@ -57,7 +57,7 @@ private slots: signals: void channelJoined(const QString &channelName); public: - ChatChannelSelector(Client *_client, QWidget *parent = 0); + ChatChannelSelector(AbstractClient *_client, QWidget *parent = 0); void retranslateUi(); }; @@ -68,7 +68,7 @@ private: private slots: void processServerMessageEvent(Event_ServerMessage *event); public: - ServerMessageLog(Client *_client, QWidget *parent = 0); + ServerMessageLog(AbstractClient *_client, QWidget *parent = 0); void retranslateUi(); }; @@ -78,12 +78,12 @@ signals: void chatChannelJoined(const QString &channelName); void gameJoined(int gameId); private: - Client *client; + AbstractClient *client; GameSelector *gameSelector; ChatChannelSelector *chatChannelSelector; ServerMessageLog *serverMessageLog; public: - TabServer(Client *_client, QWidget *parent = 0); + TabServer(AbstractClient *_client, QWidget *parent = 0); void retranslateUi(); QString getTabText() const { return tr("Server"); } }; diff --git a/cockatrice/src/tab_supervisor.cpp b/cockatrice/src/tab_supervisor.cpp index 5c45c301..a5a60e1a 100644 --- a/cockatrice/src/tab_supervisor.cpp +++ b/cockatrice/src/tab_supervisor.cpp @@ -1,12 +1,13 @@ #include #include "tab_supervisor.h" -#include "client.h" +#include "abstractclient.h" #include "tab_server.h" #include "tab_chatchannel.h" #include "tab_game.h" #include "tab_deck_storage.h" #include "protocol_items.h" #include "pingpixmapgenerator.h" +#include TabSupervisor:: TabSupervisor(QWidget *parent) : QTabWidget(parent), client(0), tabServer(0), tabDeckStorage(0) @@ -48,7 +49,7 @@ void TabSupervisor::myAddTab(Tab *tab) addTab(tab, tab->getTabText()); } -void TabSupervisor::start(Client *_client) +void TabSupervisor::start(AbstractClient *_client) { client = _client; connect(client, SIGNAL(chatEventReceived(ChatEvent *)), this, SLOT(processChatEvent(ChatEvent *))); diff --git a/cockatrice/src/tab_supervisor.h b/cockatrice/src/tab_supervisor.h index e3b128df..d36294fe 100644 --- a/cockatrice/src/tab_supervisor.h +++ b/cockatrice/src/tab_supervisor.h @@ -5,7 +5,7 @@ #include class QMenu; -class Client; +class AbstractClient; class Tab; class TabServer; class TabChatChannel; @@ -19,7 +19,7 @@ class TabSupervisor : public QTabWidget { Q_OBJECT private: QIcon *tabChangedIcon; - Client *client; + AbstractClient *client; TabServer *tabServer; TabDeckStorage *tabDeckStorage; QMap chatChannelTabs; @@ -29,7 +29,7 @@ public: TabSupervisor(QWidget *parent = 0); ~TabSupervisor(); void retranslateUi(); - void start(Client *_client); + void start(AbstractClient *_client); void stop(); int getGameCount() const { return gameTabs.size(); } signals: diff --git a/cockatrice/src/tablezone.cpp b/cockatrice/src/tablezone.cpp index 70a5f999..bce8d299 100644 --- a/cockatrice/src/tablezone.cpp +++ b/cockatrice/src/tablezone.cpp @@ -1,7 +1,6 @@ #include #include "tablezone.h" #include "player.h" -#include "client.h" #include "protocol_items.h" #include "settingscache.h" #include "arrowitem.h" diff --git a/cockatrice/src/window_main.cpp b/cockatrice/src/window_main.cpp index ef6c6a07..1427f80e 100644 --- a/cockatrice/src/window_main.cpp +++ b/cockatrice/src/window_main.cpp @@ -24,6 +24,10 @@ #include "dlg_settings.h" #include "window_deckeditor.h" #include "tab_supervisor.h" +#include "remoteclient.h" +#include "localserver.h" +#include "localserverinterface.h" +#include "localclient.h" const QString MainWindow::appName = "Cockatrice"; @@ -74,6 +78,15 @@ void MainWindow::actDisconnect() client->disconnectFromServer(); } +void MainWindow::actSinglePlayer() +{ + LocalServer *ls = new LocalServer(this); + LocalServerInterface *mainLsi = ls->newConnection(); + LocalClient *mainClient = new LocalClient(mainLsi, this); + tabSupervisor->start(mainClient); + +} + void MainWindow::actDeckEditor() { WndDeckEditor *deckEditor = new WndDeckEditor(this); @@ -138,6 +151,7 @@ void MainWindow::retranslateUi() aConnect->setText(tr("&Connect...")); aDisconnect->setText(tr("&Disconnect")); + aSinglePlayer->setText(tr("Start &local game...")); aDeckEditor->setText(tr("&Deck editor")); aFullScreen->setText(tr("&Full screen")); aFullScreen->setShortcut(tr("Ctrl+F")); @@ -156,6 +170,8 @@ void MainWindow::createActions() aDisconnect = new QAction(this); aDisconnect->setEnabled(false); connect(aDisconnect, SIGNAL(triggered()), this, SLOT(actDisconnect())); + aSinglePlayer = new QAction(this); + connect(aSinglePlayer, SIGNAL(triggered()), this, SLOT(actSinglePlayer())); aDeckEditor = new QAction(this); connect(aDeckEditor, SIGNAL(triggered()), this, SLOT(actDeckEditor())); aFullScreen = new QAction(this); @@ -172,6 +188,7 @@ void MainWindow::createMenus() cockatriceMenu = menuBar()->addMenu(QString()); cockatriceMenu->addAction(aConnect); cockatriceMenu->addAction(aDisconnect); + cockatriceMenu->addAction(aSinglePlayer); cockatriceMenu->addSeparator(); cockatriceMenu->addAction(aDeckEditor); cockatriceMenu->addSeparator(); @@ -187,7 +204,7 @@ MainWindow::MainWindow(QWidget *parent) { QPixmapCache::setCacheLimit(200000); - client = new Client(this); + client = new RemoteClient(this); connect(client, SIGNAL(serverError(ResponseCode)), this, SLOT(serverError(ResponseCode))); connect(client, SIGNAL(socketError(const QString &)), this, SLOT(socketError(const QString &))); connect(client, SIGNAL(serverTimeout()), this, SLOT(serverTimeout())); diff --git a/cockatrice/src/window_main.h b/cockatrice/src/window_main.h index d16fd70f..b81d37ab 100644 --- a/cockatrice/src/window_main.h +++ b/cockatrice/src/window_main.h @@ -21,10 +21,11 @@ #define WINDOW_H #include -#include "client.h" +#include "abstractclient.h" #include "protocol_datastructures.h" class TabSupervisor; +class RemoteClient; class MainWindow : public QMainWindow { Q_OBJECT @@ -38,6 +39,7 @@ private slots: void actConnect(); void actDisconnect(); + void actSinglePlayer(); void actDeckEditor(); void actFullScreen(bool checked); void actSettings(); @@ -49,10 +51,10 @@ private: void createActions(); void createMenus(); QMenu *cockatriceMenu, *tabMenu; - QAction *aConnect, *aDisconnect, *aDeckEditor, *aFullScreen, *aSettings, *aExit; + QAction *aConnect, *aDisconnect, *aSinglePlayer, *aDeckEditor, *aFullScreen, *aSettings, *aExit; TabSupervisor *tabSupervisor; - Client *client; + RemoteClient *client; public: MainWindow(QWidget *parent = 0); protected: diff --git a/cockatrice/src/zoneviewwidget.cpp b/cockatrice/src/zoneviewwidget.cpp index 892f9653..759af2e7 100644 --- a/cockatrice/src/zoneviewwidget.cpp +++ b/cockatrice/src/zoneviewwidget.cpp @@ -3,7 +3,6 @@ #include "carditem.h" #include "zoneviewzone.h" #include "player.h" -#include "client.h" #include "gamescene.h" #include "protocol_items.h" #include "settingscache.h" diff --git a/cockatrice/src/zoneviewzone.cpp b/cockatrice/src/zoneviewzone.cpp index 18834386..3acb5096 100644 --- a/cockatrice/src/zoneviewzone.cpp +++ b/cockatrice/src/zoneviewzone.cpp @@ -1,7 +1,6 @@ #include #include "zoneviewzone.h" #include "player.h" -#include "client.h" #include "protocol_items.h" ZoneViewZone::ZoneViewZone(Player *_p, CardZone *_origZone, int _numberCards, QGraphicsItem *parent) diff --git a/common/server.cpp b/common/server.cpp index ef5370c0..51d2982c 100644 --- a/common/server.cpp +++ b/common/server.cpp @@ -92,10 +92,12 @@ void Server::broadcastGameListUpdate(Server_Game *game) eventGameList.append(new ServerInfo_Game(game->getGameId(), QString(), false, 0, game->getMaxPlayers(), QString(), false, 0)); Event_ListGames *event = new Event_ListGames(eventGameList); + bool mayDelete = true; for (int i = 0; i < clients.size(); i++) if (clients[i]->getAcceptsGameListChanges()) - clients[i]->sendProtocolItem(event, false); - delete event; + mayDelete = clients[i]->sendProtocolItem(event, false); + if (mayDelete) + delete event; } void Server::broadcastChannelUpdate() @@ -105,10 +107,12 @@ void Server::broadcastChannelUpdate() eventChannelList.append(new ServerInfo_ChatChannel(channel->getName(), channel->getDescription(), channel->size(), channel->getAutoJoin())); Event_ListChatChannels *event = new Event_ListChatChannels(eventChannelList); + bool mayDelete = true; for (int i = 0; i < clients.size(); ++i) if (clients[i]->getAcceptsChatChannelListChanges()) - clients[i]->sendProtocolItem(event, false); - delete event; + mayDelete = clients[i]->sendProtocolItem(event, false); + if (mayDelete) + delete event; } void Server::gameClosing() diff --git a/common/server_chatchannel.cpp b/common/server_chatchannel.cpp index f88a517e..095fd3d4 100644 --- a/common/server_chatchannel.cpp +++ b/common/server_chatchannel.cpp @@ -36,7 +36,9 @@ void Server_ChatChannel::say(Server_ProtocolHandler *client, const QString &s) void Server_ChatChannel::sendChatEvent(ChatEvent *event) { + bool mayDelete = true; for (int i = 0; i < size(); ++i) - at(i)->sendProtocolItem(event, false); - delete event; + mayDelete = at(i)->sendProtocolItem(event, false); + if (mayDelete) + delete event; } diff --git a/common/server_game.cpp b/common/server_game.cpp index 63abe40d..e4ac5f89 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -24,7 +24,6 @@ #include "server_card.h" #include "server_cardzone.h" #include "server_counter.h" -#include #include Server_Game::Server_Game(Server_ProtocolHandler *_creator, int _gameId, const QString &_description, const QString &_password, int _maxPlayers, bool _spectatorsAllowed, bool _spectatorsNeedPassword, bool _spectatorsCanTalk, bool _spectatorsSeeEverything, QObject *parent) @@ -345,28 +344,34 @@ void Server_Game::sendGameEvent(GameEvent *event, GameEventContext *context, Ser void Server_Game::sendGameEventContainer(GameEventContainer *cont, Server_Player *exclude, bool excludeOmniscient) { + bool mayDelete = true; + cont->setGameId(gameId); QMapIterator playerIterator(players); while (playerIterator.hasNext()) { Server_Player *p = playerIterator.next().value(); if ((p != exclude) && !(excludeOmniscient && p->getSpectator() && spectatorsSeeEverything)) - p->sendProtocolItem(cont, false); + mayDelete = p->sendProtocolItem(cont, false); } - delete cont; + if (mayDelete) + delete cont; } void Server_Game::sendGameEventContainerOmniscient(GameEventContainer *cont, Server_Player *exclude) { + bool mayDelete = true; + cont->setGameId(gameId); QMapIterator playerIterator(players); while (playerIterator.hasNext()) { Server_Player *p = playerIterator.next().value(); if ((p != exclude) && (p->getSpectator() && spectatorsSeeEverything)) - p->sendProtocolItem(cont, false); + mayDelete = p->sendProtocolItem(cont, false); } - - delete cont; + + if (mayDelete) + delete cont; } void Server_Game::sendGameEventToPlayer(Server_Player *player, GameEvent *event) diff --git a/common/server_player.cpp b/common/server_player.cpp index 818433c0..e09141f5 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -196,8 +196,10 @@ bool Server_Player::deleteCounter(int counterId) return true; } -void Server_Player::sendProtocolItem(ProtocolItem *item, bool deleteItem) +bool Server_Player::sendProtocolItem(ProtocolItem *item, bool deleteItem) { if (handler) - handler->sendProtocolItem(item, deleteItem); + return handler->sendProtocolItem(item, deleteItem); + else + return true; } diff --git a/common/server_player.h b/common/server_player.h index 70ed9e4a..9971dcb6 100644 --- a/common/server_player.h +++ b/common/server_player.h @@ -70,7 +70,7 @@ public: void clearZones(); void setupZones(); - void sendProtocolItem(ProtocolItem *item, bool deleteItem = true); + bool sendProtocolItem(ProtocolItem *item, bool deleteItem = true); }; #endif diff --git a/common/server_protocolhandler.h b/common/server_protocolhandler.h index 725920b0..f68af832 100644 --- a/common/server_protocolhandler.h +++ b/common/server_protocolhandler.h @@ -94,7 +94,7 @@ public: const QDateTime &getLastCommandTime() const { return lastCommandTime; } void processCommandContainer(CommandContainer *cont); - virtual void sendProtocolItem(ProtocolItem *item, bool deleteItem = true) = 0; + virtual bool sendProtocolItem(ProtocolItem *item, bool deleteItem = true) = 0; void enqueueProtocolItem(ProtocolItem *item); }; diff --git a/servatrice/src/serversocketinterface.cpp b/servatrice/src/serversocketinterface.cpp index 52a75b54..cf60bba8 100644 --- a/servatrice/src/serversocketinterface.cpp +++ b/servatrice/src/serversocketinterface.cpp @@ -96,6 +96,7 @@ void ServerSocketInterface::sendProtocolItem(ProtocolItem *item, bool deleteItem item->write(xmlWriter); if (deleteItem) delete item; + return true; } int ServerSocketInterface::getDeckPathId(int basePathId, QStringList path)