From 609e3fc41d30479d622dfb4c109b16ff0f9adf0f Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Mon, 2 Jan 2012 20:20:31 +0100 Subject: [PATCH] converted SetCardAttr attr_name to enum --- cockatrice/src/phasestoolbar.cpp | 3 +- cockatrice/src/player.cpp | 71 +++++++++++---------- cockatrice/src/player.h | 3 +- cockatrice/src/tablezone.cpp | 2 +- common/pb/proto/card_attributes.proto | 9 +++ common/pb/proto/command_set_card_attr.proto | 4 +- common/pb/proto/event_set_card_attr.proto | 3 +- common/server_card.cpp | 36 +++++------ common/server_card.h | 3 +- common/server_player.cpp | 12 ++-- common/server_player.h | 3 +- common/server_protocolhandler.cpp | 2 +- 12 files changed, 83 insertions(+), 68 deletions(-) create mode 100644 common/pb/proto/card_attributes.proto diff --git a/cockatrice/src/phasestoolbar.cpp b/cockatrice/src/phasestoolbar.cpp index 35d2af08..028bd425 100644 --- a/cockatrice/src/phasestoolbar.cpp +++ b/cockatrice/src/phasestoolbar.cpp @@ -236,8 +236,7 @@ void PhasesToolbar::actUntapAll() { Command_SetCardAttr cmd; cmd.set_zone("table"); - cmd.set_card_id(-1); - cmd.set_attr_name("tapped"); + cmd.set_attribute(AttrTapped); cmd.set_attr_value("0"); emit sendGameCommand(cmd, -1); diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index de94fd24..795e7b6e 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -723,8 +723,7 @@ void Player::actUntapAll() { Command_SetCardAttr cmd; cmd.set_zone("table"); - cmd.set_card_id(-1); - cmd.set_attr_name("tapped"); + cmd.set_attribute(AttrTapped); cmd.set_attr_value("0"); sendGameCommand(cmd); @@ -780,30 +779,38 @@ void Player::actSayMessage() sendGameCommand(cmd); } -void Player::setCardAttrHelper(const GameEventContext &context, CardItem *card, const QString &aname, const QString &avalue, bool allCards) +void Player::setCardAttrHelper(const GameEventContext &context, CardItem *card, CardAttribute attribute, const QString &avalue, bool allCards) { bool moveCardContext = context.HasExtension(Context_MoveCard::ext); - if (aname == "tapped") { - bool tapped = avalue == "1"; - if (!(!tapped && card->getDoesntUntap() && allCards)) { - if (!allCards) - emit logSetTapped(this, card, tapped); - card->setTapped(tapped, !moveCardContext); + switch (attribute) { + case AttrTapped: { + bool tapped = avalue == "1"; + if (!(!tapped && card->getDoesntUntap() && allCards)) { + if (!allCards) + emit logSetTapped(this, card, tapped); + card->setTapped(tapped, !moveCardContext); + } + break; + } + case AttrAttacking: card->setAttacking(avalue == "1"); break; + case AttrFaceDown: card->setFaceDown(avalue == "1"); break; + case AttrColor: card->setColor(avalue); break; + case AttrAnnotation: { + emit logSetAnnotation(this, card, avalue); + card->setAnnotation(avalue); + break; + } + case AttrDoesntUntap: { + bool value = (avalue == "1"); + emit logSetDoesntUntap(this, card, value); + card->setDoesntUntap(value); + break; + } + case AttrPT: { + emit logSetPT(this, card, avalue); + card->setPT(avalue); + break; } - } else if (aname == "attacking") - card->setAttacking(avalue == "1"); - else if (aname == "facedown") - card->setFaceDown(avalue == "1"); - else if (aname == "annotation") { - emit logSetAnnotation(this, card, avalue); - card->setAnnotation(avalue); - } else if (aname == "doesnt_untap") { - bool value = (avalue == "1"); - emit logSetDoesntUntap(this, card, value); - card->setDoesntUntap(value); - } else if (aname == "pt") { - emit logSetPT(this, card, avalue); - card->setPT(avalue); } } @@ -875,8 +882,8 @@ void Player::eventSetCardAttr(const Event_SetCardAttr &event, const GameEventCon if (!event.has_card_id()) { const CardList &cards = zone->getCards(); for (int i = 0; i < cards.size(); i++) - setCardAttrHelper(context, cards.at(i), QString::fromStdString(event.attr_name()), QString::fromStdString(event.attr_value()), true); - if (event.attr_name() == "tapped") + setCardAttrHelper(context, cards.at(i), event.attribute(), QString::fromStdString(event.attr_value()), true); + if (event.attribute() == AttrTapped) emit logSetTapped(this, 0, event.attr_value() == "1"); } else { CardItem *card = zone->getCard(event.card_id(), QString()); @@ -884,7 +891,7 @@ void Player::eventSetCardAttr(const Event_SetCardAttr &event, const GameEventCon qDebug() << "Player::eventSetCardAttr: card id=" << event.card_id() << "not found"; return; } - setCardAttrHelper(context, card, QString::fromStdString(event.attr_name()), QString::fromStdString(event.attr_value()), false); + setCardAttrHelper(context, card, event.attribute(), QString::fromStdString(event.attr_value()), false); } } @@ -1485,7 +1492,7 @@ void Player::cardMenuAction(QAction *a) Command_SetCardAttr *cmd = new Command_SetCardAttr; cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_card_id(card->getId()); - cmd->set_attr_name("tapped"); + cmd->set_attribute(AttrTapped); cmd->set_attr_value("1"); commandList.append(cmd); } @@ -1495,7 +1502,7 @@ void Player::cardMenuAction(QAction *a) Command_SetCardAttr *cmd = new Command_SetCardAttr; cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_card_id(card->getId()); - cmd->set_attr_name("tapped"); + cmd->set_attribute(AttrTapped); cmd->set_attr_value("0"); commandList.append(cmd); } @@ -1504,7 +1511,7 @@ void Player::cardMenuAction(QAction *a) Command_SetCardAttr *cmd = new Command_SetCardAttr; cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_card_id(card->getId()); - cmd->set_attr_name("doesnt_untap"); + cmd->set_attribute(AttrDoesntUntap); cmd->set_attr_value(card->getDoesntUntap() ? "1" : "0"); commandList.append(cmd); break; @@ -1600,7 +1607,7 @@ void Player::actIncPT(int deltaP, int deltaT) Command_SetCardAttr *cmd = new Command_SetCardAttr; cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_card_id(card->getId()); - cmd->set_attr_name("pt"); + cmd->set_attribute(AttrPT); cmd->set_attr_value(ptString.toStdString()); commandList.append(cmd); } @@ -1632,7 +1639,7 @@ void Player::actSetPT(QAction * /*a*/) Command_SetCardAttr *cmd = new Command_SetCardAttr; cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_card_id(card->getId()); - cmd->set_attr_name("pt"); + cmd->set_attribute(AttrPT); cmd->set_attr_value(pt.toStdString()); commandList.append(cmd); } @@ -1665,7 +1672,7 @@ void Player::actSetAnnotation(QAction * /*a*/) Command_SetCardAttr *cmd = new Command_SetCardAttr; cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_card_id(card->getId()); - cmd->set_attr_name("annotation"); + cmd->set_attribute(AttrAnnotation); cmd->set_attr_value(annotation.toStdString()); commandList.append(cmd); } diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index 2b96679f..36705782 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -6,6 +6,7 @@ #include #include "abstractgraphicsitem.h" #include "pb/game_event.pb.h" +#include "pb/card_attributes.pb.h" namespace google { namespace protobuf { class Message; } } class CardDatabase; @@ -174,7 +175,7 @@ private: HandZone *hand; PlayerTarget *playerTarget; - void setCardAttrHelper(const GameEventContext &context, CardItem *card, const QString &aname, const QString &avalue, bool allCards); + void setCardAttrHelper(const GameEventContext &context, CardItem *card, CardAttribute attribute, const QString &avalue, bool allCards); QRectF bRect; diff --git a/cockatrice/src/tablezone.cpp b/cockatrice/src/tablezone.cpp index 6a787c19..1154d393 100644 --- a/cockatrice/src/tablezone.cpp +++ b/cockatrice/src/tablezone.cpp @@ -201,7 +201,7 @@ void TableZone::toggleTapped() Command_SetCardAttr *cmd = new Command_SetCardAttr; cmd->set_zone(name.toStdString()); cmd->set_card_id(temp->getId()); - cmd->set_attr_name("tapped"); + cmd->set_attribute(AttrTapped); cmd->set_attr_value(tapAll ? "1" : "0"); cmdList.append(cmd); } diff --git a/common/pb/proto/card_attributes.proto b/common/pb/proto/card_attributes.proto new file mode 100644 index 00000000..d7a9d1e0 --- /dev/null +++ b/common/pb/proto/card_attributes.proto @@ -0,0 +1,9 @@ +enum CardAttribute { + AttrTapped = 1; + AttrAttacking = 2; + AttrFaceDown = 3; + AttrColor = 4; + AttrPT = 5; + AttrAnnotation = 6; + AttrDoesntUntap = 7; +} diff --git a/common/pb/proto/command_set_card_attr.proto b/common/pb/proto/command_set_card_attr.proto index 0c3ee3d1..b8a8fb60 100644 --- a/common/pb/proto/command_set_card_attr.proto +++ b/common/pb/proto/command_set_card_attr.proto @@ -1,10 +1,12 @@ import "game_commands.proto"; +import "card_attributes.proto"; + message Command_SetCardAttr { extend GameCommand { optional Command_SetCardAttr ext = 1013; } optional string zone = 1; optional sint32 card_id = 2 [default = -1]; - optional string attr_name = 3; + optional CardAttribute attribute = 3; optional string attr_value = 4; } diff --git a/common/pb/proto/event_set_card_attr.proto b/common/pb/proto/event_set_card_attr.proto index 5be9bb3b..a41bfc31 100644 --- a/common/pb/proto/event_set_card_attr.proto +++ b/common/pb/proto/event_set_card_attr.proto @@ -1,4 +1,5 @@ import "game_event.proto"; +import "card_attributes.proto"; message Event_SetCardAttr { extend GameEvent { @@ -6,6 +7,6 @@ message Event_SetCardAttr { } optional string zone_name = 1; optional sint32 card_id = 2; - optional string attr_name = 3; + optional CardAttribute attribute = 3; optional string attr_value = 4; } diff --git a/common/server_card.cpp b/common/server_card.cpp index 6cafdd90..e49a29a0 100644 --- a/common/server_card.cpp +++ b/common/server_card.cpp @@ -45,28 +45,22 @@ void Server_Card::resetState() setDoesntUntap(false); } -QString Server_Card::setAttribute(const QString &aname, const QString &avalue, bool allCards) +QString Server_Card::setAttribute(CardAttribute attribute, const QString &avalue, bool allCards) { - if (aname == "tapped") { - bool value = avalue == "1"; - if (!(!value && allCards && doesntUntap)) - setTapped(value); - } else if (aname == "attacking") { - setAttacking(avalue == "1"); - } else if (aname == "facedown") { - setFaceDown(avalue == "1"); - } else if (aname == "color") { - setColor(avalue); - } else if (aname == "pt") { - setPT(avalue); - return getPT(); - } else if (aname == "annotation") { - setAnnotation(avalue); - } else if (aname == "doesnt_untap") { - setDoesntUntap(avalue == "1"); - } else - return QString(); - + switch (attribute) { + case AttrTapped: { + bool value = avalue == "1"; + if (!(!value && allCards && doesntUntap)) + setTapped(value); + break; + } + case AttrAttacking: setAttacking(avalue == "1"); break; + case AttrFaceDown: setFaceDown(avalue == "1"); break; + case AttrColor: setColor(avalue); break; + case AttrPT: setPT(avalue); return getPT(); + case AttrAnnotation: setAnnotation(avalue); break; + case AttrDoesntUntap: setDoesntUntap(avalue == "1"); break; + } return avalue; } diff --git a/common/server_card.h b/common/server_card.h index a908befa..21320798 100644 --- a/common/server_card.h +++ b/common/server_card.h @@ -21,6 +21,7 @@ #define SERVER_CARD_H #include "server_arrowtarget.h" +#include "pb/card_attributes.pb.h" #include #include @@ -86,7 +87,7 @@ public: void removeAttachedCard(Server_Card *card) { attachedCards.removeAt(attachedCards.indexOf(card)); } void resetState(); - QString setAttribute(const QString &aname, const QString &avalue, bool allCards); + QString setAttribute(CardAttribute attribute, const QString &avalue, bool allCards); }; #endif diff --git a/common/server_player.cpp b/common/server_player.cpp index 1536b52e..e2fdfcbe 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -501,10 +501,10 @@ Response::ResponseCode Server_Player::moveCard(GameEventStorage &ges, Server_Car ges.enqueueGameEvent(eventOthers, playerId, GameEventStorageItem::SendToOthers); if (thisCardProperties->tapped()) - setCardAttrHelper(ges, targetzone->getName(), card->getId(), "tapped", "1"); + setCardAttrHelper(ges, targetzone->getName(), card->getId(), AttrTapped, "1"); QString ptString = QString::fromStdString(thisCardProperties->pt()); if (!ptString.isEmpty() && !thisCardProperties->face_down()) - setCardAttrHelper(ges, targetzone->getName(), card->getId(), "pt", ptString); + setCardAttrHelper(ges, targetzone->getName(), card->getId(), AttrPT, ptString); } } if (startzone->hasCoords() && fixFreeSpaces) @@ -532,7 +532,7 @@ void Server_Player::unattachCard(GameEventStorage &ges, Server_Card *card) delete cardToMove; } -Response::ResponseCode Server_Player::setCardAttrHelper(GameEventStorage &ges, const QString &zoneName, int cardId, const QString &attrName, const QString &attrValue) +Response::ResponseCode Server_Player::setCardAttrHelper(GameEventStorage &ges, const QString &zoneName, int cardId, CardAttribute attribute, const QString &attrValue) { QMutexLocker locker(&game->gameMutex); @@ -546,7 +546,7 @@ Response::ResponseCode Server_Player::setCardAttrHelper(GameEventStorage &ges, c if (cardId == -1) { QListIterator CardIterator(zone->cards); while (CardIterator.hasNext()) { - result = CardIterator.next()->setAttribute(attrName, attrValue, true); + result = CardIterator.next()->setAttribute(attribute, attrValue, true); if (result.isNull()) return Response::RespInvalidCommand; } @@ -554,7 +554,7 @@ Response::ResponseCode Server_Player::setCardAttrHelper(GameEventStorage &ges, c Server_Card *card = zone->getCard(cardId); if (!card) return Response::RespNameNotFound; - result = card->setAttribute(attrName, attrValue, false); + result = card->setAttribute(attribute, attrValue, false); if (result.isNull()) return Response::RespInvalidCommand; } @@ -563,7 +563,7 @@ Response::ResponseCode Server_Player::setCardAttrHelper(GameEventStorage &ges, c event.set_zone_name(zone->getName().toStdString()); if (cardId != -1) event.set_card_id(cardId); - event.set_attr_name(attrName.toStdString()); + event.set_attribute(attribute); event.set_attr_value(result.toStdString()); ges.enqueueGameEvent(event, playerId); diff --git a/common/server_player.h b/common/server_player.h index 2342e7c2..4425e8cc 100644 --- a/common/server_player.h +++ b/common/server_player.h @@ -8,6 +8,7 @@ #include #include "pb/response.pb.h" +#include "pb/card_attributes.pb.h" class DeckList; class Server_Game; @@ -86,7 +87,7 @@ public: Response::ResponseCode moveCard(GameEventStorage &ges, const QString &_startZone, const QList &_cards, int _targetPlayer, const QString &_targetZone, int _x, int _y); Response::ResponseCode moveCard(GameEventStorage &ges, Server_CardZone *startzone, const QList &_cards, Server_CardZone *targetzone, int x, int y, bool fixFreeSpaces = true, bool undoingDraw = false); void unattachCard(GameEventStorage &ges, Server_Card *card); - Response::ResponseCode setCardAttrHelper(GameEventStorage &ges, const QString &zone, int cardId, const QString &attrName, const QString &attrValue); + Response::ResponseCode setCardAttrHelper(GameEventStorage &ges, const QString &zone, int cardId, CardAttribute attribute, const QString &attrValue); void sendGameEvent(GameEventContainer *event); }; diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 26b87c22..93fd1103 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -1349,7 +1349,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdSetCardAttr(const Command_SetC if (player->getConceded()) return Response::RespContextError; - return player->setCardAttrHelper(ges, QString::fromStdString(cmd.zone()), cmd.card_id(), QString::fromStdString(cmd.attr_name()), QString::fromStdString(cmd.attr_value())); + return player->setCardAttrHelper(ges, QString::fromStdString(cmd.zone()), cmd.card_id(), cmd.attribute(), QString::fromStdString(cmd.attr_value())); } Response::ResponseCode Server_ProtocolHandler::cmdSetCardCounter(const Command_SetCardCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges)