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

View file

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

View file

@ -6,6 +6,7 @@
#include <QMap>
#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;

View file

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

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

View file

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

View file

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

View file

@ -21,6 +21,7 @@
#define SERVER_CARD_H
#include "server_arrowtarget.h"
#include "pb/card_attributes.pb.h"
#include <QString>
#include <QMap>
@ -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

View file

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

View file

@ -8,6 +8,7 @@
#include <QMutex>
#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<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);
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);
};

View file

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