From 168d184e8fd1558c9de629d6449e15cc91ab564f Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Wed, 11 Aug 2010 04:23:12 +0200 Subject: [PATCH] local playing bugfixes --- cockatrice/src/abstractcarddragitem.h | 3 ++- cockatrice/src/abstractclient.cpp | 15 ++++++++++----- cockatrice/src/carditem.cpp | 4 ++-- cockatrice/src/localclient.cpp | 3 +++ cockatrice/src/localserverinterface.cpp | 7 +++++-- cockatrice/src/localserverinterface.h | 2 +- cockatrice/src/player.cpp | 1 + cockatrice/src/zoneviewzone.cpp | 2 +- common/decklist.cpp | 1 - common/protocol.cpp | 16 +++++++++++----- common/protocol.h | 4 ++++ common/protocol_datastructures.cpp | 3 +++ common/serializable_item.cpp | 1 - common/server.cpp | 12 ++++-------- common/server_chatchannel.cpp | 6 ++---- common/server_game.cpp | 15 +++++---------- common/server_player.cpp | 6 ++---- common/server_player.h | 2 +- common/server_protocolhandler.cpp | 17 +++++++++++++---- common/server_protocolhandler.h | 2 +- servatrice/src/serversocketinterface.cpp | 1 - 21 files changed, 71 insertions(+), 52 deletions(-) diff --git a/cockatrice/src/abstractcarddragitem.h b/cockatrice/src/abstractcarddragitem.h index c136b890..62f36436 100644 --- a/cockatrice/src/abstractcarddragitem.h +++ b/cockatrice/src/abstractcarddragitem.h @@ -7,7 +7,8 @@ class QGraphicsScene; class CardZone; class CardInfo; -class AbstractCardDragItem : public QGraphicsItem { +class AbstractCardDragItem : public QObject, public QGraphicsItem { + Q_OBJECT protected: AbstractCardItem *item; QPointF hotSpot; diff --git a/cockatrice/src/abstractclient.cpp b/cockatrice/src/abstractclient.cpp index bac38052..d5eaa9c0 100644 --- a/cockatrice/src/abstractclient.cpp +++ b/cockatrice/src/abstractclient.cpp @@ -1,6 +1,7 @@ #include "abstractclient.h" #include "protocol.h" #include "protocol_items.h" +#include AbstractClient::AbstractClient(QObject *parent) : QObject(parent), status(StatusDisconnected) @@ -21,8 +22,9 @@ void AbstractClient::processProtocolItem(ProtocolItem *item) pendingCommands.remove(cmdCont->getCmdId()); cmdCont->processResponse(response); - delete response; - delete cmdCont; + if (response->getReceiverMayDelete()) + delete response; + cmdCont->deleteLater(); return; } @@ -35,21 +37,24 @@ void AbstractClient::processProtocolItem(ProtocolItem *item) case ItemId_Event_ListChatChannels: emit listChatChannelsEventReceived(qobject_cast(item)); break; case ItemId_Event_GameJoined: emit gameJoinedEventReceived(qobject_cast(item)); break; } - delete genericEvent; + if (genericEvent->getReceiverMayDelete()) + delete genericEvent; return; } GameEventContainer *gameEventContainer = qobject_cast(item); if (gameEventContainer) { emit gameEventContainerReceived(gameEventContainer); - delete gameEventContainer; + if (gameEventContainer->getReceiverMayDelete()) + delete gameEventContainer; return; } ChatEvent *chatEvent = qobject_cast(item); if (chatEvent) { emit chatEventReceived(chatEvent); - delete chatEvent; + if (chatEvent->getReceiverMayDelete()) + delete chatEvent; return; } } diff --git a/cockatrice/src/carditem.cpp b/cockatrice/src/carditem.cpp index bc9a3162..c998bde7 100644 --- a/cockatrice/src/carditem.cpp +++ b/cockatrice/src/carditem.cpp @@ -284,7 +284,7 @@ CardDragItem *CardItem::createDragItem(int _id, const QPointF &_pos, const QPoin void CardItem::deleteDragItem() { - delete dragItem; + dragItem->deleteLater(); dragItem = NULL; } @@ -376,7 +376,7 @@ void CardItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) cardMenu->exec(event->screenPos()); } else if ((event->button() == Qt::LeftButton) && !settingsCache->getDoubleClickToPlay()) playCard(event); - + setCursor(Qt::OpenHandCursor); } diff --git a/cockatrice/src/localclient.cpp b/cockatrice/src/localclient.cpp index ad0dbb8d..e81d0e16 100644 --- a/cockatrice/src/localclient.cpp +++ b/cockatrice/src/localclient.cpp @@ -6,6 +6,7 @@ LocalClient::LocalClient(LocalServerInterface *_lsi, QObject *parent) : AbstractClient(parent), lsi(_lsi) { connect(lsi, SIGNAL(itemToClient(ProtocolItem *)), this, SLOT(itemFromServer(ProtocolItem *))); + sendCommand(new Command_Login("Player", QString())); } LocalClient::~LocalClient() @@ -14,6 +15,8 @@ LocalClient::~LocalClient() void LocalClient::sendCommandContainer(CommandContainer *cont) { + cont->setReceiverMayDelete(false); + pendingCommands.insert(cont->getCmdId(), cont); lsi->itemFromClient(cont); } diff --git a/cockatrice/src/localserverinterface.cpp b/cockatrice/src/localserverinterface.cpp index 16960651..b4e0918d 100644 --- a/cockatrice/src/localserverinterface.cpp +++ b/cockatrice/src/localserverinterface.cpp @@ -1,5 +1,6 @@ #include "localserverinterface.h" #include "localserver.h" +#include LocalServerInterface::LocalServerInterface(LocalServer *_server) : Server_ProtocolHandler(_server, _server) @@ -10,10 +11,12 @@ LocalServerInterface::~LocalServerInterface() { } -bool LocalServerInterface::sendProtocolItem(ProtocolItem *item, bool deleteItem) +void LocalServerInterface::sendProtocolItem(ProtocolItem *item, bool deleteItem) { + item->setReceiverMayDelete(false); emit itemToClient(item); - return false; + if (deleteItem) + delete item; } void LocalServerInterface::itemFromClient(ProtocolItem *item) diff --git a/cockatrice/src/localserverinterface.h b/cockatrice/src/localserverinterface.h index a057e487..0f954844 100644 --- a/cockatrice/src/localserverinterface.h +++ b/cockatrice/src/localserverinterface.h @@ -20,7 +20,7 @@ public: LocalServerInterface(LocalServer *_server); ~LocalServerInterface(); - bool sendProtocolItem(ProtocolItem *item, bool deleteItem = true); + void sendProtocolItem(ProtocolItem *item, bool deleteItem = true); signals: void itemToClient(ProtocolItem *item); diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index 3da0406b..d2c7e791 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -739,6 +739,7 @@ void Player::eventDrawCards(Event_DrawCards *event) void Player::processGameEvent(GameEvent *event, GameEventContext *context) { + qDebug() << "player event: id=" << event->getItemId(); switch (event->getItemId()) { case ItemId_Event_Say: eventSay(qobject_cast(event)); break; case ItemId_Event_Shuffle: eventShuffle(qobject_cast(event)); break; diff --git a/cockatrice/src/zoneviewzone.cpp b/cockatrice/src/zoneviewzone.cpp index 3acb5096..c2153cfa 100644 --- a/cockatrice/src/zoneviewzone.cpp +++ b/cockatrice/src/zoneviewzone.cpp @@ -128,7 +128,7 @@ void ZoneViewZone::removeCard(int position) return; CardItem *card = cards.takeAt(position); - delete card; + card->deleteLater(); reorganizeCards(); } diff --git a/common/decklist.cpp b/common/decklist.cpp index 4e120a0d..eb11c3a7 100644 --- a/common/decklist.cpp +++ b/common/decklist.cpp @@ -5,7 +5,6 @@ #include #include #include "decklist.h" -#include MoveCardToZone::MoveCardToZone(const QString &_cardName, const QString &_startZone, const QString &_targetZone) : SerializableItem_Map("move_card_to_zone") diff --git a/common/protocol.cpp b/common/protocol.cpp index 23fd6676..a4495186 100644 --- a/common/protocol.cpp +++ b/common/protocol.cpp @@ -1,12 +1,11 @@ #include #include -#include #include "protocol.h" #include "protocol_items.h" #include "decklist.h" ProtocolItem::ProtocolItem(const QString &_itemType, const QString &_itemSubType) - : SerializableItem_Map(_itemType, _itemSubType) + : SerializableItem_Map(_itemType, _itemSubType), receiverMayDelete(true) { } @@ -135,21 +134,21 @@ void CommandContainer::enqueueGameEventPublic(GameEvent *event, int gameId) { if (!gameEventQueuePublic) gameEventQueuePublic = new GameEventContainer(QList(), gameId); - gameEventQueuePublic->appendItem(event); + gameEventQueuePublic->addGameEvent(event); } void CommandContainer::enqueueGameEventOmniscient(GameEvent *event, int gameId) { if (!gameEventQueueOmniscient) gameEventQueueOmniscient = new GameEventContainer(QList(), gameId); - gameEventQueueOmniscient->appendItem(event); + gameEventQueueOmniscient->addGameEvent(event); } void CommandContainer::enqueueGameEventPrivate(GameEvent *event, int gameId) { if (!gameEventQueuePrivate) gameEventQueuePrivate = new GameEventContainer(QList(), gameId); - gameEventQueuePrivate->appendItem(event); + gameEventQueuePrivate->addGameEvent(event); } Command_DeckUpload::Command_DeckUpload(DeckList *_deck, const QString &_path) @@ -307,6 +306,7 @@ GameEventContainer::GameEventContainer(const QList &_eventList, int if (_context) itemList.append(_context); + eventList = _eventList; for (int i = 0; i < _eventList.size(); ++i) itemList.append(_eventList[i]); } @@ -337,6 +337,12 @@ void GameEventContainer::setContext(GameEventContext *_context) context = _context; } +void GameEventContainer::addGameEvent(GameEvent *event) +{ + appendItem(event); + eventList.append(event); +} + GameEventContainer *GameEventContainer::makeNew(GameEvent *event, int _gameId) { return new GameEventContainer(QList() << event, _gameId); diff --git a/common/protocol.h b/common/protocol.h index edf51c03..b5420b14 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -46,10 +46,13 @@ class ProtocolItem : public SerializableItem_Map { Q_OBJECT private: static void initializeHashAuto(); + bool receiverMayDelete; public: static const int protocolVersion = 7; static void initializeHash(); virtual int getItemId() const = 0; + bool getReceiverMayDelete() const { return receiverMayDelete; } + void setReceiverMayDelete(bool _receiverMayDelete) { receiverMayDelete = _receiverMayDelete; } ProtocolItem(const QString &_itemType, const QString &_itemSubType); }; @@ -269,6 +272,7 @@ public: QList getEventList() const { return eventList; } GameEventContext *getContext() const { return context; } void setContext(GameEventContext *_context); + void addGameEvent(GameEvent *event); static GameEventContainer *makeNew(GameEvent *event, int _gameId); int getGameId() const { return static_cast(itemMap.value("game_id"))->getData(); } diff --git a/common/protocol_datastructures.cpp b/common/protocol_datastructures.cpp index 0aa7575f..f5d606ac 100644 --- a/common/protocol_datastructures.cpp +++ b/common/protocol_datastructures.cpp @@ -148,10 +148,13 @@ ServerInfo_Player::ServerInfo_Player(ServerInfo_PlayerProperties *_properties, D else insertItem(new DeckList(_deck)); + zoneList = _zoneList; for (int i = 0; i < _zoneList.size(); ++i) itemList.append(_zoneList[i]); + counterList = _counterList; for (int i = 0; i < _counterList.size(); ++i) itemList.append(_counterList[i]); + arrowList = _arrowList; for (int i = 0; i < _arrowList.size(); ++i) itemList.append(_arrowList[i]); } diff --git a/common/serializable_item.cpp b/common/serializable_item.cpp index 912bf133..7815259b 100644 --- a/common/serializable_item.cpp +++ b/common/serializable_item.cpp @@ -1,7 +1,6 @@ #include "serializable_item.h" #include #include -#include QHash SerializableItem::itemNameHash; diff --git a/common/server.cpp b/common/server.cpp index 51d2982c..25e476d8 100644 --- a/common/server.cpp +++ b/common/server.cpp @@ -92,12 +92,10 @@ 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()) - mayDelete = clients[i]->sendProtocolItem(event, false); - if (mayDelete) - delete event; + clients[i]->sendProtocolItem(event, false); + delete event; } void Server::broadcastChannelUpdate() @@ -107,12 +105,10 @@ 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()) - mayDelete = clients[i]->sendProtocolItem(event, false); - if (mayDelete) - delete event; + clients[i]->sendProtocolItem(event, false); + delete event; } void Server::gameClosing() diff --git a/common/server_chatchannel.cpp b/common/server_chatchannel.cpp index 095fd3d4..f88a517e 100644 --- a/common/server_chatchannel.cpp +++ b/common/server_chatchannel.cpp @@ -36,9 +36,7 @@ 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) - mayDelete = at(i)->sendProtocolItem(event, false); - if (mayDelete) - delete event; + at(i)->sendProtocolItem(event, false); + delete event; } diff --git a/common/server_game.cpp b/common/server_game.cpp index 438f4711..ae668d29 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -25,6 +25,7 @@ #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) : QObject(parent), gameStarted(false), gameId(_gameId), description(_description), password(_password), maxPlayers(_maxPlayers), activePlayer(-1), activePhase(-1), spectatorsAllowed(_spectatorsAllowed), spectatorsNeedPassword(_spectatorsNeedPassword), spectatorsCanTalk(_spectatorsCanTalk), spectatorsSeeEverything(_spectatorsSeeEverything), inactivityCounter(0) @@ -344,34 +345,28 @@ 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)) - mayDelete = p->sendProtocolItem(cont, false); + p->sendProtocolItem(cont, false); } - if (mayDelete) - delete cont; + 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)) - mayDelete = p->sendProtocolItem(cont, false); + p->sendProtocolItem(cont, false); } - if (mayDelete) - delete cont; + delete cont; } void Server_Game::sendGameEventToPlayer(Server_Player *player, GameEvent *event) diff --git a/common/server_player.cpp b/common/server_player.cpp index e09141f5..818433c0 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -196,10 +196,8 @@ bool Server_Player::deleteCounter(int counterId) return true; } -bool Server_Player::sendProtocolItem(ProtocolItem *item, bool deleteItem) +void Server_Player::sendProtocolItem(ProtocolItem *item, bool deleteItem) { if (handler) - return handler->sendProtocolItem(item, deleteItem); - else - return true; + handler->sendProtocolItem(item, deleteItem); } diff --git a/common/server_player.h b/common/server_player.h index 9971dcb6..70ed9e4a 100644 --- a/common/server_player.h +++ b/common/server_player.h @@ -70,7 +70,7 @@ public: void clearZones(); void setupZones(); - bool sendProtocolItem(ProtocolItem *item, bool deleteItem = true); + void sendProtocolItem(ProtocolItem *item, bool deleteItem = true); }; #endif diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 6251c631..9a0c9d8f 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -145,7 +145,7 @@ void Server_ProtocolHandler::processCommandContainer(CommandContainer *cont) ProtocolResponse *pr = cont->getResponse(); if (!pr) pr = new ProtocolResponse(cont->getCmdId(), finalResponseCode); - + GameEventContainer *gQPublic = cont->getGameEventQueuePublic(); if (gQPublic) { Server_Game *game = games.value(gQPublic->getGameId()).first; @@ -169,10 +169,11 @@ void Server_ProtocolHandler::processCommandContainer(CommandContainer *cont) sendProtocolItem(pr); - delete cont; - while (!itemQueue.isEmpty()) sendProtocolItem(itemQueue.takeFirst()); + + if (cont->getReceiverMayDelete()) + delete cont; } void Server_ProtocolHandler::pingClockTimeout() @@ -281,6 +282,9 @@ ResponseCode Server_ProtocolHandler::cmdChatSay(Command_ChatSay *cmd, CommandCon ResponseCode Server_ProtocolHandler::cmdListGames(Command_ListGames * /*cmd*/, CommandContainer *cont) { + if (authState == PasswordWrong) + return RespLoginNeeded; + const QList &gameList = server->getGames(); QList eventGameList; for (int i = 0; i < gameList.size(); ++i) { @@ -305,6 +309,9 @@ ResponseCode Server_ProtocolHandler::cmdListGames(Command_ListGames * /*cmd*/, C ResponseCode Server_ProtocolHandler::cmdCreateGame(Command_CreateGame *cmd, CommandContainer *cont) { + if (authState == PasswordWrong) + return RespLoginNeeded; + Server_Game *game = server->createGame(cmd->getDescription(), cmd->getPassword(), cmd->getMaxPlayers(), cmd->getSpectatorsAllowed(), cmd->getSpectatorsNeedPassword(), cmd->getSpectatorsCanTalk(), cmd->getSpectatorsSeeEverything(), this); Server_Player *creator = game->getCreator(); games.insert(game->getGameId(), QPair(game, creator)); @@ -316,6 +323,9 @@ ResponseCode Server_ProtocolHandler::cmdCreateGame(Command_CreateGame *cmd, Comm ResponseCode Server_ProtocolHandler::cmdJoinGame(Command_JoinGame *cmd, CommandContainer *cont) { + if (authState == PasswordWrong) + return RespLoginNeeded; + if (games.contains(cmd->getGameId())) return RespContextError; @@ -485,7 +495,6 @@ ResponseCode Server_ProtocolHandler::drawCards(Server_Game *game, Server_Player cardListPrivate.append(new ServerInfo_Card(card->getId(), card->getName())); cardListOmniscient.append(new ServerInfo_Card(card->getId(), card->getName())); } - cont->enqueueGameEventPrivate(new Event_DrawCards(player->getPlayerId(), cardListPrivate.size(), cardListPrivate), game->getGameId()); cont->enqueueGameEventOmniscient(new Event_DrawCards(player->getPlayerId(), cardListOmniscient.size(), cardListOmniscient), game->getGameId()); cont->enqueueGameEventPublic(new Event_DrawCards(player->getPlayerId(), cardListPrivate.size()), game->getGameId()); diff --git a/common/server_protocolhandler.h b/common/server_protocolhandler.h index f68af832..725920b0 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 bool sendProtocolItem(ProtocolItem *item, bool deleteItem = true) = 0; + virtual void sendProtocolItem(ProtocolItem *item, bool deleteItem = true) = 0; void enqueueProtocolItem(ProtocolItem *item); }; diff --git a/servatrice/src/serversocketinterface.cpp b/servatrice/src/serversocketinterface.cpp index cf60bba8..52a75b54 100644 --- a/servatrice/src/serversocketinterface.cpp +++ b/servatrice/src/serversocketinterface.cpp @@ -96,7 +96,6 @@ void ServerSocketInterface::sendProtocolItem(ProtocolItem *item, bool deleteItem item->write(xmlWriter); if (deleteItem) delete item; - return true; } int ServerSocketInterface::getDeckPathId(int basePathId, QStringList path)