local playing bugfixes

This commit is contained in:
Max-Wilhelm Bruker 2010-08-11 04:23:12 +02:00
parent 00077565ab
commit 168d184e8f
21 changed files with 71 additions and 52 deletions

View file

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

View file

@ -1,6 +1,7 @@
#include "abstractclient.h"
#include "protocol.h"
#include "protocol_items.h"
#include <QDebug>
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<Event_ListChatChannels *>(item)); break;
case ItemId_Event_GameJoined: emit gameJoinedEventReceived(qobject_cast<Event_GameJoined *>(item)); break;
}
delete genericEvent;
if (genericEvent->getReceiverMayDelete())
delete genericEvent;
return;
}
GameEventContainer *gameEventContainer = qobject_cast<GameEventContainer *>(item);
if (gameEventContainer) {
emit gameEventContainerReceived(gameEventContainer);
delete gameEventContainer;
if (gameEventContainer->getReceiverMayDelete())
delete gameEventContainer;
return;
}
ChatEvent *chatEvent = qobject_cast<ChatEvent *>(item);
if (chatEvent) {
emit chatEventReceived(chatEvent);
delete chatEvent;
if (chatEvent->getReceiverMayDelete())
delete chatEvent;
return;
}
}

View file

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

View file

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

View file

@ -1,5 +1,6 @@
#include "localserverinterface.h"
#include "localserver.h"
#include <QDebug>
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)

View file

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

View file

@ -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_Say *>(event)); break;
case ItemId_Event_Shuffle: eventShuffle(qobject_cast<Event_Shuffle *>(event)); break;

View file

@ -128,7 +128,7 @@ void ZoneViewZone::removeCard(int position)
return;
CardItem *card = cards.takeAt(position);
delete card;
card->deleteLater();
reorganizeCards();
}

View file

@ -5,7 +5,6 @@
#include <QXmlStreamWriter>
#include <QVariant>
#include "decklist.h"
#include <QDebug>
MoveCardToZone::MoveCardToZone(const QString &_cardName, const QString &_startZone, const QString &_targetZone)
: SerializableItem_Map("move_card_to_zone")

View file

@ -1,12 +1,11 @@
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QDebug>
#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<GameEvent *>(), gameId);
gameEventQueuePublic->appendItem(event);
gameEventQueuePublic->addGameEvent(event);
}
void CommandContainer::enqueueGameEventOmniscient(GameEvent *event, int gameId)
{
if (!gameEventQueueOmniscient)
gameEventQueueOmniscient = new GameEventContainer(QList<GameEvent *>(), gameId);
gameEventQueueOmniscient->appendItem(event);
gameEventQueueOmniscient->addGameEvent(event);
}
void CommandContainer::enqueueGameEventPrivate(GameEvent *event, int gameId)
{
if (!gameEventQueuePrivate)
gameEventQueuePrivate = new GameEventContainer(QList<GameEvent *>(), gameId);
gameEventQueuePrivate->appendItem(event);
gameEventQueuePrivate->addGameEvent(event);
}
Command_DeckUpload::Command_DeckUpload(DeckList *_deck, const QString &_path)
@ -307,6 +306,7 @@ GameEventContainer::GameEventContainer(const QList<GameEvent *> &_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<GameEvent *>() << event, _gameId);

View file

@ -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<GameEvent *> 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<SerializableItem_Int *>(itemMap.value("game_id"))->getData(); }

View file

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

View file

@ -1,7 +1,6 @@
#include "serializable_item.h"
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QDebug>
QHash<QString, SerializableItem::NewItemFunction> SerializableItem::itemNameHash;

View file

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

View file

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

View file

@ -25,6 +25,7 @@
#include "server_cardzone.h"
#include "server_counter.h"
#include <QTimer>
#include <QDebug>
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<int, Server_Player *> 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<int, Server_Player *> 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)

View file

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

View file

@ -70,7 +70,7 @@ public:
void clearZones();
void setupZones();
bool sendProtocolItem(ProtocolItem *item, bool deleteItem = true);
void sendProtocolItem(ProtocolItem *item, bool deleteItem = true);
};
#endif

View file

@ -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<Server_Game *> &gameList = server->getGames();
QList<ServerInfo_Game *> 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<Server_Game *, Server_Player *>(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());

View file

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

View file

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