converted SetCardAttr attr_name to enum

This commit is contained in:
Max-Wilhelm Bruker 2012-01-02 20:20:31 +01:00
parent 4634787b00
commit 609e3fc41d
12 changed files with 83 additions and 68 deletions

View file

@ -236,8 +236,7 @@ void PhasesToolbar::actUntapAll()
{ {
Command_SetCardAttr cmd; Command_SetCardAttr cmd;
cmd.set_zone("table"); cmd.set_zone("table");
cmd.set_card_id(-1); cmd.set_attribute(AttrTapped);
cmd.set_attr_name("tapped");
cmd.set_attr_value("0"); cmd.set_attr_value("0");
emit sendGameCommand(cmd, -1); emit sendGameCommand(cmd, -1);

View file

@ -723,8 +723,7 @@ void Player::actUntapAll()
{ {
Command_SetCardAttr cmd; Command_SetCardAttr cmd;
cmd.set_zone("table"); cmd.set_zone("table");
cmd.set_card_id(-1); cmd.set_attribute(AttrTapped);
cmd.set_attr_name("tapped");
cmd.set_attr_value("0"); cmd.set_attr_value("0");
sendGameCommand(cmd); sendGameCommand(cmd);
@ -780,30 +779,38 @@ void Player::actSayMessage()
sendGameCommand(cmd); 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); bool moveCardContext = context.HasExtension(Context_MoveCard::ext);
if (aname == "tapped") { switch (attribute) {
case AttrTapped: {
bool tapped = avalue == "1"; bool tapped = avalue == "1";
if (!(!tapped && card->getDoesntUntap() && allCards)) { if (!(!tapped && card->getDoesntUntap() && allCards)) {
if (!allCards) if (!allCards)
emit logSetTapped(this, card, tapped); emit logSetTapped(this, card, tapped);
card->setTapped(tapped, !moveCardContext); card->setTapped(tapped, !moveCardContext);
} }
} else if (aname == "attacking") break;
card->setAttacking(avalue == "1"); }
else if (aname == "facedown") case AttrAttacking: card->setAttacking(avalue == "1"); break;
card->setFaceDown(avalue == "1"); case AttrFaceDown: card->setFaceDown(avalue == "1"); break;
else if (aname == "annotation") { case AttrColor: card->setColor(avalue); break;
case AttrAnnotation: {
emit logSetAnnotation(this, card, avalue); emit logSetAnnotation(this, card, avalue);
card->setAnnotation(avalue); card->setAnnotation(avalue);
} else if (aname == "doesnt_untap") { break;
}
case AttrDoesntUntap: {
bool value = (avalue == "1"); bool value = (avalue == "1");
emit logSetDoesntUntap(this, card, value); emit logSetDoesntUntap(this, card, value);
card->setDoesntUntap(value); card->setDoesntUntap(value);
} else if (aname == "pt") { break;
}
case AttrPT: {
emit logSetPT(this, card, avalue); emit logSetPT(this, card, avalue);
card->setPT(avalue); card->setPT(avalue);
break;
}
} }
} }
@ -875,8 +882,8 @@ void Player::eventSetCardAttr(const Event_SetCardAttr &event, const GameEventCon
if (!event.has_card_id()) { if (!event.has_card_id()) {
const CardList &cards = zone->getCards(); const CardList &cards = zone->getCards();
for (int i = 0; i < cards.size(); i++) for (int i = 0; i < cards.size(); i++)
setCardAttrHelper(context, cards.at(i), QString::fromStdString(event.attr_name()), QString::fromStdString(event.attr_value()), true); setCardAttrHelper(context, cards.at(i), event.attribute(), QString::fromStdString(event.attr_value()), true);
if (event.attr_name() == "tapped") if (event.attribute() == AttrTapped)
emit logSetTapped(this, 0, event.attr_value() == "1"); emit logSetTapped(this, 0, event.attr_value() == "1");
} else { } else {
CardItem *card = zone->getCard(event.card_id(), QString()); 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"; qDebug() << "Player::eventSetCardAttr: card id=" << event.card_id() << "not found";
return; 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; Command_SetCardAttr *cmd = new Command_SetCardAttr;
cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd->set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd->set_attr_name("tapped"); cmd->set_attribute(AttrTapped);
cmd->set_attr_value("1"); cmd->set_attr_value("1");
commandList.append(cmd); commandList.append(cmd);
} }
@ -1495,7 +1502,7 @@ void Player::cardMenuAction(QAction *a)
Command_SetCardAttr *cmd = new Command_SetCardAttr; Command_SetCardAttr *cmd = new Command_SetCardAttr;
cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd->set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd->set_attr_name("tapped"); cmd->set_attribute(AttrTapped);
cmd->set_attr_value("0"); cmd->set_attr_value("0");
commandList.append(cmd); commandList.append(cmd);
} }
@ -1504,7 +1511,7 @@ void Player::cardMenuAction(QAction *a)
Command_SetCardAttr *cmd = new Command_SetCardAttr; Command_SetCardAttr *cmd = new Command_SetCardAttr;
cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd->set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd->set_attr_name("doesnt_untap"); cmd->set_attribute(AttrDoesntUntap);
cmd->set_attr_value(card->getDoesntUntap() ? "1" : "0"); cmd->set_attr_value(card->getDoesntUntap() ? "1" : "0");
commandList.append(cmd); commandList.append(cmd);
break; break;
@ -1600,7 +1607,7 @@ void Player::actIncPT(int deltaP, int deltaT)
Command_SetCardAttr *cmd = new Command_SetCardAttr; Command_SetCardAttr *cmd = new Command_SetCardAttr;
cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd->set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd->set_attr_name("pt"); cmd->set_attribute(AttrPT);
cmd->set_attr_value(ptString.toStdString()); cmd->set_attr_value(ptString.toStdString());
commandList.append(cmd); commandList.append(cmd);
} }
@ -1632,7 +1639,7 @@ void Player::actSetPT(QAction * /*a*/)
Command_SetCardAttr *cmd = new Command_SetCardAttr; Command_SetCardAttr *cmd = new Command_SetCardAttr;
cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd->set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd->set_attr_name("pt"); cmd->set_attribute(AttrPT);
cmd->set_attr_value(pt.toStdString()); cmd->set_attr_value(pt.toStdString());
commandList.append(cmd); commandList.append(cmd);
} }
@ -1665,7 +1672,7 @@ void Player::actSetAnnotation(QAction * /*a*/)
Command_SetCardAttr *cmd = new Command_SetCardAttr; Command_SetCardAttr *cmd = new Command_SetCardAttr;
cmd->set_zone(card->getZone()->getName().toStdString()); cmd->set_zone(card->getZone()->getName().toStdString());
cmd->set_card_id(card->getId()); cmd->set_card_id(card->getId());
cmd->set_attr_name("annotation"); cmd->set_attribute(AttrAnnotation);
cmd->set_attr_value(annotation.toStdString()); cmd->set_attr_value(annotation.toStdString());
commandList.append(cmd); commandList.append(cmd);
} }

View file

@ -6,6 +6,7 @@
#include <QMap> #include <QMap>
#include "abstractgraphicsitem.h" #include "abstractgraphicsitem.h"
#include "pb/game_event.pb.h" #include "pb/game_event.pb.h"
#include "pb/card_attributes.pb.h"
namespace google { namespace protobuf { class Message; } } namespace google { namespace protobuf { class Message; } }
class CardDatabase; class CardDatabase;
@ -174,7 +175,7 @@ private:
HandZone *hand; HandZone *hand;
PlayerTarget *playerTarget; 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; QRectF bRect;

View file

@ -201,7 +201,7 @@ void TableZone::toggleTapped()
Command_SetCardAttr *cmd = new Command_SetCardAttr; Command_SetCardAttr *cmd = new Command_SetCardAttr;
cmd->set_zone(name.toStdString()); cmd->set_zone(name.toStdString());
cmd->set_card_id(temp->getId()); cmd->set_card_id(temp->getId());
cmd->set_attr_name("tapped"); cmd->set_attribute(AttrTapped);
cmd->set_attr_value(tapAll ? "1" : "0"); cmd->set_attr_value(tapAll ? "1" : "0");
cmdList.append(cmd); cmdList.append(cmd);
} }

View file

@ -0,0 +1,9 @@
enum CardAttribute {
AttrTapped = 1;
AttrAttacking = 2;
AttrFaceDown = 3;
AttrColor = 4;
AttrPT = 5;
AttrAnnotation = 6;
AttrDoesntUntap = 7;
}

View file

@ -1,10 +1,12 @@
import "game_commands.proto"; import "game_commands.proto";
import "card_attributes.proto";
message Command_SetCardAttr { message Command_SetCardAttr {
extend GameCommand { extend GameCommand {
optional Command_SetCardAttr ext = 1013; optional Command_SetCardAttr ext = 1013;
} }
optional string zone = 1; optional string zone = 1;
optional sint32 card_id = 2 [default = -1]; optional sint32 card_id = 2 [default = -1];
optional string attr_name = 3; optional CardAttribute attribute = 3;
optional string attr_value = 4; optional string attr_value = 4;
} }

View file

@ -1,4 +1,5 @@
import "game_event.proto"; import "game_event.proto";
import "card_attributes.proto";
message Event_SetCardAttr { message Event_SetCardAttr {
extend GameEvent { extend GameEvent {
@ -6,6 +7,6 @@ message Event_SetCardAttr {
} }
optional string zone_name = 1; optional string zone_name = 1;
optional sint32 card_id = 2; optional sint32 card_id = 2;
optional string attr_name = 3; optional CardAttribute attribute = 3;
optional string attr_value = 4; optional string attr_value = 4;
} }

View file

@ -45,28 +45,22 @@ void Server_Card::resetState()
setDoesntUntap(false); 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") { switch (attribute) {
case AttrTapped: {
bool value = avalue == "1"; bool value = avalue == "1";
if (!(!value && allCards && doesntUntap)) if (!(!value && allCards && doesntUntap))
setTapped(value); setTapped(value);
} else if (aname == "attacking") { break;
setAttacking(avalue == "1"); }
} else if (aname == "facedown") { case AttrAttacking: setAttacking(avalue == "1"); break;
setFaceDown(avalue == "1"); case AttrFaceDown: setFaceDown(avalue == "1"); break;
} else if (aname == "color") { case AttrColor: setColor(avalue); break;
setColor(avalue); case AttrPT: setPT(avalue); return getPT();
} else if (aname == "pt") { case AttrAnnotation: setAnnotation(avalue); break;
setPT(avalue); case AttrDoesntUntap: setDoesntUntap(avalue == "1"); break;
return getPT(); }
} else if (aname == "annotation") {
setAnnotation(avalue);
} else if (aname == "doesnt_untap") {
setDoesntUntap(avalue == "1");
} else
return QString();
return avalue; return avalue;
} }

View file

@ -21,6 +21,7 @@
#define SERVER_CARD_H #define SERVER_CARD_H
#include "server_arrowtarget.h" #include "server_arrowtarget.h"
#include "pb/card_attributes.pb.h"
#include <QString> #include <QString>
#include <QMap> #include <QMap>
@ -86,7 +87,7 @@ public:
void removeAttachedCard(Server_Card *card) { attachedCards.removeAt(attachedCards.indexOf(card)); } void removeAttachedCard(Server_Card *card) { attachedCards.removeAt(attachedCards.indexOf(card)); }
void resetState(); void resetState();
QString setAttribute(const QString &aname, const QString &avalue, bool allCards); QString setAttribute(CardAttribute attribute, const QString &avalue, bool allCards);
}; };
#endif #endif

View file

@ -501,10 +501,10 @@ Response::ResponseCode Server_Player::moveCard(GameEventStorage &ges, Server_Car
ges.enqueueGameEvent(eventOthers, playerId, GameEventStorageItem::SendToOthers); ges.enqueueGameEvent(eventOthers, playerId, GameEventStorageItem::SendToOthers);
if (thisCardProperties->tapped()) 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()); QString ptString = QString::fromStdString(thisCardProperties->pt());
if (!ptString.isEmpty() && !thisCardProperties->face_down()) 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) if (startzone->hasCoords() && fixFreeSpaces)
@ -532,7 +532,7 @@ void Server_Player::unattachCard(GameEventStorage &ges, Server_Card *card)
delete cardToMove; 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); QMutexLocker locker(&game->gameMutex);
@ -546,7 +546,7 @@ Response::ResponseCode Server_Player::setCardAttrHelper(GameEventStorage &ges, c
if (cardId == -1) { if (cardId == -1) {
QListIterator<Server_Card *> CardIterator(zone->cards); QListIterator<Server_Card *> CardIterator(zone->cards);
while (CardIterator.hasNext()) { while (CardIterator.hasNext()) {
result = CardIterator.next()->setAttribute(attrName, attrValue, true); result = CardIterator.next()->setAttribute(attribute, attrValue, true);
if (result.isNull()) if (result.isNull())
return Response::RespInvalidCommand; return Response::RespInvalidCommand;
} }
@ -554,7 +554,7 @@ Response::ResponseCode Server_Player::setCardAttrHelper(GameEventStorage &ges, c
Server_Card *card = zone->getCard(cardId); Server_Card *card = zone->getCard(cardId);
if (!card) if (!card)
return Response::RespNameNotFound; return Response::RespNameNotFound;
result = card->setAttribute(attrName, attrValue, false); result = card->setAttribute(attribute, attrValue, false);
if (result.isNull()) if (result.isNull())
return Response::RespInvalidCommand; return Response::RespInvalidCommand;
} }
@ -563,7 +563,7 @@ Response::ResponseCode Server_Player::setCardAttrHelper(GameEventStorage &ges, c
event.set_zone_name(zone->getName().toStdString()); event.set_zone_name(zone->getName().toStdString());
if (cardId != -1) if (cardId != -1)
event.set_card_id(cardId); event.set_card_id(cardId);
event.set_attr_name(attrName.toStdString()); event.set_attribute(attribute);
event.set_attr_value(result.toStdString()); event.set_attr_value(result.toStdString());
ges.enqueueGameEvent(event, playerId); ges.enqueueGameEvent(event, playerId);

View file

@ -8,6 +8,7 @@
#include <QMutex> #include <QMutex>
#include "pb/response.pb.h" #include "pb/response.pb.h"
#include "pb/card_attributes.pb.h"
class DeckList; class DeckList;
class Server_Game; class Server_Game;
@ -86,7 +87,7 @@ public:
Response::ResponseCode moveCard(GameEventStorage &ges, const QString &_startZone, const QList<const CardToMove *> &_cards, int _targetPlayer, const QString &_targetZone, int _x, int _y); Response::ResponseCode moveCard(GameEventStorage &ges, const QString &_startZone, const QList<const CardToMove *> &_cards, int _targetPlayer, const QString &_targetZone, int _x, int _y);
Response::ResponseCode moveCard(GameEventStorage &ges, Server_CardZone *startzone, const QList<const CardToMove *> &_cards, Server_CardZone *targetzone, int x, int y, bool fixFreeSpaces = true, bool undoingDraw = false); Response::ResponseCode moveCard(GameEventStorage &ges, Server_CardZone *startzone, const QList<const CardToMove *> &_cards, Server_CardZone *targetzone, int x, int y, bool fixFreeSpaces = true, bool undoingDraw = false);
void unattachCard(GameEventStorage &ges, Server_Card *card); 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); void sendGameEvent(GameEventContainer *event);
}; };

View file

@ -1349,7 +1349,7 @@ Response::ResponseCode Server_ProtocolHandler::cmdSetCardAttr(const Command_SetC
if (player->getConceded()) if (player->getConceded())
return Response::RespContextError; 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) Response::ResponseCode Server_ProtocolHandler::cmdSetCardCounter(const Command_SetCardCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges)