From 388e9255c77faaa7a8529cd6d11fcf07178cf314 Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Mon, 1 Mar 2010 21:19:20 +0100 Subject: [PATCH] cipt code --- cockatrice/src/carditem.cpp | 5 ++- cockatrice/src/tablezone.cpp | 4 +- cockatrice/src/tablezone.h | 4 +- common/protocol.cpp | 17 ++++++--- common/protocol.h | 9 +++-- common/protocol_items.cpp | 3 +- common/protocol_items.dat | 2 +- common/protocol_items.h | 3 +- common/server_protocolhandler.cpp | 62 +++++++++++++++++++------------ common/server_protocolhandler.h | 5 ++- 10 files changed, 73 insertions(+), 41 deletions(-) diff --git a/cockatrice/src/carditem.cpp b/cockatrice/src/carditem.cpp index b16058bf..6b9778ba 100644 --- a/cockatrice/src/carditem.cpp +++ b/cockatrice/src/carditem.cpp @@ -173,8 +173,11 @@ void CardItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) if (tz) tz->toggleTapped(); else { + bool faceDown = event->modifiers().testFlag(Qt::ShiftModifier); + bool tapped = info->getCipt(); + TableZone *table = zone->getPlayer()->getTable(); QPoint gridPoint = table->getFreeGridPoint(info->getTableRow()); - table->handleDropEventByGrid(id, zone, gridPoint, false); + table->handleDropEventByGrid(id, zone, gridPoint, faceDown, tapped); } } diff --git a/cockatrice/src/tablezone.cpp b/cockatrice/src/tablezone.cpp index 0fd4b5af..2c847f5c 100644 --- a/cockatrice/src/tablezone.cpp +++ b/cockatrice/src/tablezone.cpp @@ -66,9 +66,9 @@ void TableZone::handleDropEvent(int cardId, CardZone *startZone, const QPoint &d handleDropEventByGrid(cardId, startZone, mapToGrid(dropPoint), faceDown); } -void TableZone::handleDropEventByGrid(int cardId, CardZone *startZone, const QPoint &gridPoint, bool faceDown) +void TableZone::handleDropEventByGrid(int cardId, CardZone *startZone, const QPoint &gridPoint, bool faceDown, bool tapped) { - player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), cardId, getName(), gridPoint.x(), gridPoint.y(), faceDown)); + player->sendGameCommand(new Command_MoveCard(-1, startZone->getName(), cardId, getName(), gridPoint.x(), gridPoint.y(), faceDown, tapped)); } void TableZone::reorganizeCards() diff --git a/cockatrice/src/tablezone.h b/cockatrice/src/tablezone.h index b94bc0d1..580e383b 100644 --- a/cockatrice/src/tablezone.h +++ b/cockatrice/src/tablezone.h @@ -21,8 +21,8 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void reorganizeCards(); void toggleTapped(); - void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown); - void handleDropEventByGrid(int cardId, CardZone *startZone, const QPoint &gridPoint, bool faceDown); + void handleDropEvent(int cardId, CardZone *startZone, const QPoint &dropPoint, bool faceDown = false); + void handleDropEventByGrid(int cardId, CardZone *startZone, const QPoint &gridPoint, bool faceDown = false, bool tapped = false); CardItem *getCardFromGrid(const QPoint &gridPoint) const; QPointF mapFromGrid(const QPoint &gridPoint) const; QPoint mapToGrid(const QPointF &mapPoint) const; diff --git a/common/protocol.cpp b/common/protocol.cpp index 4b131964..0bb78042 100644 --- a/common/protocol.cpp +++ b/common/protocol.cpp @@ -100,7 +100,7 @@ void Command::processResponse(ProtocolResponse *response) } CommandContainer::CommandContainer(const QList &_commandList, int _cmdId) - : ProtocolItem("container", "cmd"), ticks(0), resp(0), gameEventQueue(0) + : ProtocolItem("container", "cmd"), ticks(0), resp(0), gameEventQueuePublic(0), gameEventQueuePrivate(0) { if (_cmdId == -1) _cmdId = lastCmdId++; @@ -126,11 +126,18 @@ void CommandContainer::setResponse(ProtocolResponse *_resp) resp = _resp; } -void CommandContainer::enqueueGameEvent(GameEvent *event, int gameId) +void CommandContainer::enqueueGameEventPublic(GameEvent *event, int gameId) { - if (!gameEventQueue) - gameEventQueue = new GameEventContainer(QList(), gameId); - gameEventQueue->appendItem(event); + if (!gameEventQueuePublic) + gameEventQueuePublic = new GameEventContainer(QList(), gameId); + gameEventQueuePublic->appendItem(event); +} + +void CommandContainer::enqueueGameEventPrivate(GameEvent *event, int gameId) +{ + if (!gameEventQueuePrivate) + gameEventQueuePrivate = new GameEventContainer(QList(), gameId); + gameEventQueuePrivate->appendItem(event); } Command_DeckUpload::Command_DeckUpload(DeckList *_deck, const QString &_path) diff --git a/common/protocol.h b/common/protocol.h index 4c76d59e..5aa67563 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -99,7 +99,8 @@ private: // These are only for processing inside the server. ProtocolResponse *resp; QList itemQueue; - GameEventContainer *gameEventQueue; + GameEventContainer *gameEventQueuePublic; + GameEventContainer *gameEventQueuePrivate; public: CommandContainer(const QList &_commandList = QList(), int _cmdId = -1); static SerializableItem *newItem() { return new CommandContainer; } @@ -113,8 +114,10 @@ public: void setResponse(ProtocolResponse *_resp); const QList &getItemQueue() const { return itemQueue; } void enqueueItem(ProtocolItem *item) { itemQueue.append(item); } - GameEventContainer *getGameEventQueue() const { return gameEventQueue; } - void enqueueGameEvent(GameEvent *event, int gameId); + GameEventContainer *getGameEventQueuePublic() const { return gameEventQueuePublic; } + void enqueueGameEventPublic(GameEvent *event, int gameId); + GameEventContainer *getGameEventQueuePrivate() const { return gameEventQueuePrivate; } + void enqueueGameEventPrivate(GameEvent *event, int gameId); }; class ChatCommand : public Command { diff --git a/common/protocol_items.cpp b/common/protocol_items.cpp index 0f3ec22a..5721b3ed 100644 --- a/common/protocol_items.cpp +++ b/common/protocol_items.cpp @@ -100,7 +100,7 @@ Command_DrawCards::Command_DrawCards(int _gameId, int _number) { insertItem(new SerializableItem_Int("number", _number)); } -Command_MoveCard::Command_MoveCard(int _gameId, const QString &_startZone, int _cardId, const QString &_targetZone, int _x, int _y, bool _faceDown) +Command_MoveCard::Command_MoveCard(int _gameId, const QString &_startZone, int _cardId, const QString &_targetZone, int _x, int _y, bool _faceDown, bool _tapped) : GameCommand("move_card", _gameId) { insertItem(new SerializableItem_String("start_zone", _startZone)); @@ -109,6 +109,7 @@ Command_MoveCard::Command_MoveCard(int _gameId, const QString &_startZone, int _ insertItem(new SerializableItem_Int("x", _x)); insertItem(new SerializableItem_Int("y", _y)); insertItem(new SerializableItem_Bool("face_down", _faceDown)); + insertItem(new SerializableItem_Bool("tapped", _tapped)); } Command_CreateToken::Command_CreateToken(int _gameId, const QString &_zone, const QString &_cardName, const QString &_pt, int _x, int _y) : GameCommand("create_token", _gameId) diff --git a/common/protocol_items.dat b/common/protocol_items.dat index 9f7a95d8..ea138bab 100644 --- a/common/protocol_items.dat +++ b/common/protocol_items.dat @@ -18,7 +18,7 @@ 2:mulligan 2:roll_die:i,sides 2:draw_cards:i,number -2:move_card:s,start_zone:i,card_id:s,target_zone:i,x:i,y:b,face_down +2:move_card:s,start_zone:i,card_id:s,target_zone:i,x:i,y:b,face_down:b,tapped 2:create_token:s,zone:s,card_name:s,pt:i,x:i,y 2:create_arrow:i,start_player_id:s,start_zone:i,start_card_id:i,target_player_id:s,target_zone:i,target_card_id:c,color 2:delete_arrow:i,arrow_id diff --git a/common/protocol_items.h b/common/protocol_items.h index 540d7a3b..08461645 100644 --- a/common/protocol_items.h +++ b/common/protocol_items.h @@ -165,13 +165,14 @@ public: class Command_MoveCard : public GameCommand { Q_OBJECT public: - Command_MoveCard(int _gameId = -1, const QString &_startZone = QString(), int _cardId = -1, const QString &_targetZone = QString(), int _x = -1, int _y = -1, bool _faceDown = false); + Command_MoveCard(int _gameId = -1, const QString &_startZone = QString(), int _cardId = -1, const QString &_targetZone = QString(), int _x = -1, int _y = -1, bool _faceDown = false, bool _tapped = false); QString getStartZone() const { return static_cast(itemMap.value("start_zone"))->getData(); }; int getCardId() const { return static_cast(itemMap.value("card_id"))->getData(); }; QString getTargetZone() const { return static_cast(itemMap.value("target_zone"))->getData(); }; int getX() const { return static_cast(itemMap.value("x"))->getData(); }; int getY() const { return static_cast(itemMap.value("y"))->getData(); }; bool getFaceDown() const { return static_cast(itemMap.value("face_down"))->getData(); }; + bool getTapped() const { return static_cast(itemMap.value("tapped"))->getData(); }; static SerializableItem *newItem() { return new Command_MoveCard; } int getItemId() const { return ItemId_Command_MoveCard; } }; diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 4b3d32d2..82b69da8 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -142,10 +142,16 @@ void Server_ProtocolHandler::processCommandContainer(CommandContainer *cont) if (!pr) pr = new ProtocolResponse(cont->getCmdId(), finalResponseCode); - GameEventContainer *gQ = cont->getGameEventQueue(); - if (gQ) { - Server_Game *game = games.value(gQ->getGameId()).first; - game->sendGameEventContainer(gQ); + GameEventContainer *gQPublic = cont->getGameEventQueuePublic(); + if (gQPublic) { + Server_Game *game = games.value(gQPublic->getGameId()).first; + Server_Player *player = games.value(gQPublic->getGameId()).second; + GameEventContainer *gQPrivate = cont->getGameEventQueuePrivate(); + if (gQPrivate) { + game->sendGameEventContainer(gQPublic, player); + player->sendProtocolItem(gQPrivate); + } else + game->sendGameEventContainer(gQPublic); } const QList &iQ = cont->getItemQueue(); @@ -391,12 +397,13 @@ ResponseCode Server_ProtocolHandler::cmdMulligan(Command_Mulligan * /*cmd*/, Com Server_CardZone *hand = player->getZones().value("hand"); while (!hand->cards.isEmpty()) - moveCard(game, player, "hand", hand->cards.first()->getId(), "deck", 0, 0, false); + moveCard(game, player, cont, "hand", hand->cards.first()->getId(), "deck", 0, 0, false, false); player->getZones().value("deck")->shuffle(); - game->sendGameEvent(new Event_Shuffle(player->getPlayerId())); + cont->enqueueGameEventPrivate(new Event_Shuffle(player->getPlayerId()), game->getGameId()); + cont->enqueueGameEventPublic(new Event_Shuffle(player->getPlayerId()), game->getGameId()); - drawCards(game, player, number); + drawCards(game, player, cont, number); player->setInitialCards(number - 1); return RespOk; @@ -408,7 +415,7 @@ ResponseCode Server_ProtocolHandler::cmdRollDie(Command_RollDie *cmd, CommandCon return RespOk; } -ResponseCode Server_ProtocolHandler::drawCards(Server_Game *game, Server_Player *player, int number) +ResponseCode Server_ProtocolHandler::drawCards(Server_Game *game, Server_Player *player, CommandContainer *cont, int number) { if (!game->getGameStarted()) return RespGameNotStarted; @@ -425,8 +432,8 @@ ResponseCode Server_ProtocolHandler::drawCards(Server_Game *game, Server_Player cardList.append(new ServerInfo_Card(card->getId(), card->getName())); } - player->sendProtocolItem(GameEventContainer::makeNew(new Event_DrawCards(player->getPlayerId(), cardList.size(), cardList), game->getGameId())); - game->sendGameEvent(new Event_DrawCards(player->getPlayerId(), cardList.size()), player); + cont->enqueueGameEventPrivate(new Event_DrawCards(player->getPlayerId(), cardList.size(), cardList), game->getGameId()); + cont->enqueueGameEventPublic(new Event_DrawCards(player->getPlayerId(), cardList.size()), game->getGameId()); return RespOk; } @@ -434,10 +441,10 @@ ResponseCode Server_ProtocolHandler::drawCards(Server_Game *game, Server_Player ResponseCode Server_ProtocolHandler::cmdDrawCards(Command_DrawCards *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player) { - return drawCards(game, player, cmd->getNumber()); + return drawCards(game, player, cont, cmd->getNumber()); } -ResponseCode Server_ProtocolHandler::moveCard(Server_Game *game, Server_Player *player, const QString &_startZone, int _cardId, const QString &_targetZone, int x, int y, bool faceDown) +ResponseCode Server_ProtocolHandler::moveCard(Server_Game *game, Server_Player *player, CommandContainer *cont, const QString &_startZone, int _cardId, const QString &_targetZone, int x, int y, bool faceDown, bool tapped) { if (!game->getGameStarted()) return RespGameNotStarted; @@ -489,7 +496,7 @@ ResponseCode Server_ProtocolHandler::moveCard(Server_Game *game, Server_Player * int privatePosition = -1; if (startzone->getType() == HiddenZone) privatePosition = position; - player->sendProtocolItem(GameEventContainer::makeNew(new Event_MoveCard(player->getPlayerId(), privateOldCardId, privateCardName, startzone->getName(), privatePosition, targetzone->getName(), x, y, privateNewCardId, faceDown), game->getGameId())); + cont->enqueueGameEventPrivate(new Event_MoveCard(player->getPlayerId(), privateOldCardId, privateCardName, startzone->getName(), privatePosition, targetzone->getName(), x, y, privateNewCardId, faceDown), game->getGameId()); // Other players do not get to see the start and/or target position of the card if the respective // part of the zone is being looked at. The information is not needed anyway because in hidden zones, @@ -503,9 +510,12 @@ ResponseCode Server_ProtocolHandler::moveCard(Server_Game *game, Server_Player * x = -1; if ((startzone->getType() == PublicZone) || (targetzone->getType() == PublicZone)) - game->sendGameEvent(new Event_MoveCard(player->getPlayerId(), oldCardId, publicCardName, startzone->getName(), position, targetzone->getName(), x, y, card->getId(), faceDown), player); + cont->enqueueGameEventPublic(new Event_MoveCard(player->getPlayerId(), oldCardId, publicCardName, startzone->getName(), position, targetzone->getName(), x, y, card->getId(), faceDown), game->getGameId()); else - game->sendGameEvent(new Event_MoveCard(player->getPlayerId(), -1, QString(), startzone->getName(), position, targetzone->getName(), x, y, -1, false), player); + cont->enqueueGameEventPublic(new Event_MoveCard(player->getPlayerId(), -1, QString(), startzone->getName(), position, targetzone->getName(), x, y, -1, false), game->getGameId()); + + if (tapped) + setCardAttrHelper(cont, game, player, targetzone->getName(), card->getId(), "tapped", "1"); // If the card was moved to another zone, delete all arrows from and to the card if (startzone != targetzone) { @@ -528,7 +538,7 @@ ResponseCode Server_ProtocolHandler::moveCard(Server_Game *game, Server_Player * ResponseCode Server_ProtocolHandler::cmdMoveCard(Command_MoveCard *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player) { - return moveCard(game, player, cmd->getStartZone(), cmd->getCardId(), cmd->getTargetZone(), cmd->getX(), cmd->getY(), cmd->getFaceDown()); + return moveCard(game, player, cont, cmd->getStartZone(), cmd->getCardId(), cmd->getTargetZone(), cmd->getX(), cmd->getY(), cmd->getFaceDown(), cmd->getTapped()); } ResponseCode Server_ProtocolHandler::cmdCreateToken(Command_CreateToken *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player) @@ -599,33 +609,39 @@ ResponseCode Server_ProtocolHandler::cmdDeleteArrow(Command_DeleteArrow *cmd, Co return RespOk; } -ResponseCode Server_ProtocolHandler::cmdSetCardAttr(Command_SetCardAttr *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player) +ResponseCode Server_ProtocolHandler::setCardAttrHelper(CommandContainer *cont, Server_Game *game, Server_Player *player, const QString &zoneName, int cardId, const QString &attrName, const QString &attrValue) { if (!game->getGameStarted()) return RespGameNotStarted; // zone, card id, attr name, attr value // card id = -1 => affects all cards in the specified zone - Server_CardZone *zone = player->getZones().value(cmd->getZone()); + Server_CardZone *zone = player->getZones().value(zoneName); if (!zone) return RespNameNotFound; - if (cmd->getCardId() == -1) { + if (cardId == -1) { QListIterator CardIterator(zone->cards); while (CardIterator.hasNext()) - if (!CardIterator.next()->setAttribute(cmd->getAttrName(), cmd->getAttrValue(), true)) + if (!CardIterator.next()->setAttribute(attrName, attrValue, true)) return RespInvalidCommand; } else { - Server_Card *card = zone->getCard(cmd->getCardId(), false); + Server_Card *card = zone->getCard(cardId, false); if (!card) return RespNameNotFound; - if (!card->setAttribute(cmd->getAttrName(), cmd->getAttrValue(), false)) + if (!card->setAttribute(attrName, attrValue, false)) return RespInvalidCommand; } - cont->enqueueGameEvent(new Event_SetCardAttr(player->getPlayerId(), zone->getName(), cmd->getCardId(), cmd->getAttrName(), cmd->getAttrValue()), game->getGameId()); + cont->enqueueGameEventPrivate(new Event_SetCardAttr(player->getPlayerId(), zone->getName(), cardId, attrName, attrValue), game->getGameId()); + cont->enqueueGameEventPublic(new Event_SetCardAttr(player->getPlayerId(), zone->getName(), cardId, attrName, attrValue), game->getGameId()); return RespOk; } +ResponseCode Server_ProtocolHandler::cmdSetCardAttr(Command_SetCardAttr *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player) +{ + return setCardAttrHelper(cont, game, player, cmd->getZone(), cmd->getCardId(), cmd->getAttrName(), cmd->getAttrValue()); +} + ResponseCode Server_ProtocolHandler::cmdIncCounter(Command_IncCounter *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player) { if (!game->getGameStarted()) diff --git a/common/server_protocolhandler.h b/common/server_protocolhandler.h index 05490b63..be1b806a 100644 --- a/common/server_protocolhandler.h +++ b/common/server_protocolhandler.h @@ -56,13 +56,14 @@ private: ResponseCode cmdMulligan(Command_Mulligan *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); ResponseCode cmdRollDie(Command_RollDie *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); // XXX Maybe the following function and others belong into Server_Player - ResponseCode drawCards(Server_Game *game, Server_Player *player, int number); + ResponseCode drawCards(Server_Game *game, Server_Player *player, CommandContainer *cont, int number); ResponseCode cmdDrawCards(Command_DrawCards *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); - ResponseCode moveCard(Server_Game *game, Server_Player *player, const QString &_startZone, int _cardId, const QString &_targetZone, int _x, int _y, bool _faceDown); + ResponseCode moveCard(Server_Game *game, Server_Player *player, CommandContainer *cont, const QString &_startZone, int _cardId, const QString &_targetZone, int _x, int _y, bool _faceDown, bool _tapped); ResponseCode cmdMoveCard(Command_MoveCard *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); ResponseCode cmdCreateToken(Command_CreateToken *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); ResponseCode cmdCreateArrow(Command_CreateArrow *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); ResponseCode cmdDeleteArrow(Command_DeleteArrow *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); + ResponseCode setCardAttrHelper(CommandContainer *cont, Server_Game *game, Server_Player *player, const QString &zone, int cardId, const QString &attrName, const QString &attrValue); ResponseCode cmdSetCardAttr(Command_SetCardAttr *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); ResponseCode cmdIncCounter(Command_IncCounter *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player); ResponseCode cmdCreateCounter(Command_CreateCounter *cmd, CommandContainer *cont, Server_Game *game, Server_Player *player);