response codes

This commit is contained in:
Max-Wilhelm Bruker 2009-10-26 17:05:08 +01:00
parent 3461b60183
commit bd2855cb95
7 changed files with 186 additions and 92 deletions

View file

@ -4,14 +4,14 @@
#include "protocol.h" #include "protocol.h"
#include "protocol_commands.h" #include "protocol_commands.h"
QHash<QString, Command::NewCommandFunction> Command::commandHash; QHash<QString, ProtocolItem::NewItemFunction> ProtocolItem::itemNameHash;
Command::Command(const QString &_cmdName) ProtocolItem::ProtocolItem(const QString &_itemName)
: cmdName(_cmdName) : itemName(_itemName)
{ {
} }
bool Command::read(QXmlStreamReader &xml) bool ProtocolItem::read(QXmlStreamReader &xml)
{ {
while (!xml.atEnd()) { while (!xml.atEnd()) {
xml.readNext(); xml.readNext();
@ -19,7 +19,7 @@ bool Command::read(QXmlStreamReader &xml)
qDebug() << "startElement: " << xml.name().toString(); qDebug() << "startElement: " << xml.name().toString();
} else if (xml.isEndElement()) { } else if (xml.isEndElement()) {
qDebug() << "endElement: " << xml.name().toString(); qDebug() << "endElement: " << xml.name().toString();
if (xml.name() == cmdName) { if (xml.name() == getItemType()) {
extractParameters(); extractParameters();
qDebug() << "FERTIG"; qDebug() << "FERTIG";
deleteLater(); deleteLater();
@ -39,9 +39,11 @@ bool Command::read(QXmlStreamReader &xml)
return false; return false;
} }
void Command::write(QXmlStreamWriter &xml) void ProtocolItem::write(QXmlStreamWriter &xml)
{ {
xml.writeStartElement(cmdName); xml.writeStartElement(getItemType());
if (!itemName.isEmpty())
xml.writeAttribute("name", itemName);
QMapIterator<QString, QString> i(parameters); QMapIterator<QString, QString> i(parameters);
while (i.hasNext()) { while (i.hasNext()) {
@ -52,9 +54,63 @@ void Command::write(QXmlStreamWriter &xml)
xml.writeEndElement(); xml.writeEndElement();
} }
Command *Command::getNewCommand(const QString &name) ProtocolItem *ProtocolItem::getNewItem(const QString &name)
{ {
if (!commandHash.contains(name)) if (!itemNameHash.contains(name))
return 0; return 0;
return commandHash.value(name)(); return itemNameHash.value(name)();
}
void ProtocolItem::initializeHash()
{
initializeHashAuto();
itemNameHash.insert("resp", ProtocolResponse::newItem);
ProtocolResponse::initializeHash();
}
int Command::lastCmdId = 0;
Command::Command(const QString &_itemName, int _cmdId)
: ProtocolItem(_itemName), cmdId(_cmdId)
{
if (cmdId == -1)
cmdId = lastCmdId++;
setParameter("cmd_id", cmdId);
}
void Command::extractParameters()
{
bool ok;
cmdId = parameters["cmd_id"].toInt(&ok);
if (!ok)
cmdId = -1;
}
QHash<QString, ProtocolResponse::ResponseCode> ProtocolResponse::responseHash;
ProtocolResponse::ProtocolResponse(int _cmdId, ResponseCode _responseCode)
: ProtocolItem(QString()), cmdId(_cmdId), responseCode(_responseCode)
{
setParameter("cmd_id", cmdId);
setParameter("response_code", responseHash.key(responseCode));
}
void ProtocolResponse::extractParameters()
{
bool ok;
cmdId = parameters["cmd_id"].toInt(&ok);
if (!ok)
cmdId = -1;
responseCode = responseHash.value(parameters["response_code"], RespOk);
}
void ProtocolResponse::initializeHash()
{
responseHash.insert("ok", RespOk);
responseHash.insert("name_not_found", RespNameNotFound);
responseHash.insert("login_needed", RespLoginNeeded);
responseHash.insert("context_error", RespContextError);
responseHash.insert("wrong_password", RespWrongPassword);
responseHash.insert("spectators_not_allowed", RespSpectatorsNotAllowed);
} }

View file

@ -9,28 +9,43 @@
class QXmlStreamReader; class QXmlStreamReader;
class QXmlStreamWriter; class QXmlStreamWriter;
class QXmlStreamAttributes;
class Command : public QObject { class ProtocolItem : public QObject {
Q_OBJECT Q_OBJECT
protected: protected:
typedef Command *(*NewCommandFunction)(); typedef ProtocolItem *(*NewItemFunction)();
static QHash<QString, NewCommandFunction> commandHash; static QHash<QString, NewItemFunction> itemNameHash;
QString cmdName; QString itemName;
QMap<QString, QString> parameters; QMap<QString, QString> parameters;
QString currentElementText; QString currentElementText;
void setParameter(const QString &name, const QString &value) { parameters[name] = value; } void setParameter(const QString &name, const QString &value) { parameters[name] = value; }
void setParameter(const QString &name, bool value) { parameters[name] = (value ? "1" : "0"); } void setParameter(const QString &name, bool value) { parameters[name] = (value ? "1" : "0"); }
void setParameter(const QString &name, int value) { parameters[name] = QString::number(value); } void setParameter(const QString &name, int value) { parameters[name] = QString::number(value); }
virtual void extractParameters() { }; virtual void extractParameters() { };
virtual QString getItemType() const = 0;
private:
static void initializeHashAuto();
public: public:
Command(const QString &_cmdName); ProtocolItem(const QString &_itemName);
static void initializeHash(); static void initializeHash();
static Command *getNewCommand(const QString &name); static ProtocolItem *getNewItem(const QString &name);
virtual bool read(QXmlStreamReader &xml); virtual bool read(QXmlStreamReader &xml);
virtual void write(QXmlStreamWriter &xml); virtual void write(QXmlStreamWriter &xml);
}; };
class Command : public ProtocolItem {
private:
int cmdId;
static int lastCmdId;
protected:
QString getItemType() const { return "cmd"; }
void extractParameters();
public:
Command(const QString &_itemName, int _cmdId = -1);
};
class ChatCommand : public Command { class ChatCommand : public Command {
Q_OBJECT Q_OBJECT
private: private:
@ -67,4 +82,21 @@ public:
int getGameId() const { return gameId; } int getGameId() const { return gameId; }
}; };
class ProtocolResponse : public ProtocolItem {
Q_OBJECT
public:
enum ResponseCode { RespOk, RespNameNotFound, RespLoginNeeded, RespContextError, RespWrongPassword, RespSpectatorsNotAllowed };
private:
int cmdId;
ResponseCode responseCode;
static QHash<QString, ResponseCode> responseHash;
protected:
QString getItemType() const { return "resp"; }
void extractParameters();
public:
ProtocolResponse(int _cmdId = -1, ResponseCode _responseCode = RespOk);
static void initializeHash();
static ProtocolItem *newItem() { return new ProtocolResponse; }
};
#endif #endif

View file

@ -305,36 +305,36 @@ Command_SubmitDeck::Command_SubmitDeck(int _gameId)
: GameCommand("submit_deck", _gameId) : GameCommand("submit_deck", _gameId)
{ {
} }
void Command::initializeHash() void ProtocolItem::initializeHashAuto()
{ {
commandHash.insert("ping", Command_Ping::newCommand); itemNameHash.insert("cmdping", Command_Ping::newItem);
commandHash.insert("login", Command_Login::newCommand); itemNameHash.insert("cmdlogin", Command_Login::newItem);
commandHash.insert("chat_list_channels", Command_ChatListChannels::newCommand); itemNameHash.insert("cmdchat_list_channels", Command_ChatListChannels::newItem);
commandHash.insert("chat_join_channel", Command_ChatJoinChannel::newCommand); itemNameHash.insert("cmdchat_join_channel", Command_ChatJoinChannel::newItem);
commandHash.insert("chat_leave_channel", Command_ChatLeaveChannel::newCommand); itemNameHash.insert("cmdchat_leave_channel", Command_ChatLeaveChannel::newItem);
commandHash.insert("chat_say", Command_ChatSay::newCommand); itemNameHash.insert("cmdchat_say", Command_ChatSay::newItem);
commandHash.insert("list_games", Command_ListGames::newCommand); itemNameHash.insert("cmdlist_games", Command_ListGames::newItem);
commandHash.insert("create_game", Command_CreateGame::newCommand); itemNameHash.insert("cmdcreate_game", Command_CreateGame::newItem);
commandHash.insert("join_game", Command_JoinGame::newCommand); itemNameHash.insert("cmdjoin_game", Command_JoinGame::newItem);
commandHash.insert("leave_game", Command_LeaveGame::newCommand); itemNameHash.insert("cmdleave_game", Command_LeaveGame::newItem);
commandHash.insert("say", Command_Say::newCommand); itemNameHash.insert("cmdsay", Command_Say::newItem);
commandHash.insert("shuffle", Command_Shuffle::newCommand); itemNameHash.insert("cmdshuffle", Command_Shuffle::newItem);
commandHash.insert("roll_die", Command_RollDie::newCommand); itemNameHash.insert("cmdroll_die", Command_RollDie::newItem);
commandHash.insert("draw_cards", Command_DrawCards::newCommand); itemNameHash.insert("cmddraw_cards", Command_DrawCards::newItem);
commandHash.insert("move_card", Command_MoveCard::newCommand); itemNameHash.insert("cmdmove_card", Command_MoveCard::newItem);
commandHash.insert("create_token", Command_CreateToken::newCommand); itemNameHash.insert("cmdcreate_token", Command_CreateToken::newItem);
commandHash.insert("create_arrow", Command_CreateArrow::newCommand); itemNameHash.insert("cmdcreate_arrow", Command_CreateArrow::newItem);
commandHash.insert("delete_arrow", Command_DeleteArrow::newCommand); itemNameHash.insert("cmddelete_arrow", Command_DeleteArrow::newItem);
commandHash.insert("set_card_attr", Command_SetCardAttr::newCommand); itemNameHash.insert("cmdset_card_attr", Command_SetCardAttr::newItem);
commandHash.insert("ready_start", Command_ReadyStart::newCommand); itemNameHash.insert("cmdready_start", Command_ReadyStart::newItem);
commandHash.insert("inc_counter", Command_IncCounter::newCommand); itemNameHash.insert("cmdinc_counter", Command_IncCounter::newItem);
commandHash.insert("add_counter", Command_AddCounter::newCommand); itemNameHash.insert("cmdadd_counter", Command_AddCounter::newItem);
commandHash.insert("set_counter", Command_SetCounter::newCommand); itemNameHash.insert("cmdset_counter", Command_SetCounter::newItem);
commandHash.insert("del_counter", Command_DelCounter::newCommand); itemNameHash.insert("cmddel_counter", Command_DelCounter::newItem);
commandHash.insert("next_turn", Command_NextTurn::newCommand); itemNameHash.insert("cmdnext_turn", Command_NextTurn::newItem);
commandHash.insert("set_active_phase", Command_SetActivePhase::newCommand); itemNameHash.insert("cmdset_active_phase", Command_SetActivePhase::newItem);
commandHash.insert("dump_zone", Command_DumpZone::newCommand); itemNameHash.insert("cmddump_zone", Command_DumpZone::newItem);
commandHash.insert("stop_dump_zone", Command_StopDumpZone::newCommand); itemNameHash.insert("cmdstop_dump_zone", Command_StopDumpZone::newItem);
commandHash.insert("dump_all", Command_DumpAll::newCommand); itemNameHash.insert("cmddump_all", Command_DumpAll::newItem);
commandHash.insert("submit_deck", Command_SubmitDeck::newCommand); itemNameHash.insert("cmdsubmit_deck", Command_SubmitDeck::newItem);
} }

View file

@ -8,7 +8,7 @@ class Command_Ping : public Command {
private: private:
public: public:
Command_Ping(); Command_Ping();
static Command *newCommand() { return new Command_Ping; } static ProtocolItem *newItem() { return new Command_Ping; }
}; };
class Command_Login : public Command { class Command_Login : public Command {
Q_OBJECT Q_OBJECT
@ -19,7 +19,7 @@ public:
Command_Login(const QString &_username = QString(), const QString &_password = QString()); Command_Login(const QString &_username = QString(), const QString &_password = QString());
QString getUsername() const { return username; } QString getUsername() const { return username; }
QString getPassword() const { return password; } QString getPassword() const { return password; }
static Command *newCommand() { return new Command_Login; } static ProtocolItem *newItem() { return new Command_Login; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -28,7 +28,7 @@ class Command_ChatListChannels : public Command {
private: private:
public: public:
Command_ChatListChannels(); Command_ChatListChannels();
static Command *newCommand() { return new Command_ChatListChannels; } static ProtocolItem *newItem() { return new Command_ChatListChannels; }
}; };
class Command_ChatJoinChannel : public Command { class Command_ChatJoinChannel : public Command {
Q_OBJECT Q_OBJECT
@ -37,7 +37,7 @@ private:
public: public:
Command_ChatJoinChannel(const QString &_channel = QString()); Command_ChatJoinChannel(const QString &_channel = QString());
QString getChannel() const { return channel; } QString getChannel() const { return channel; }
static Command *newCommand() { return new Command_ChatJoinChannel; } static ProtocolItem *newItem() { return new Command_ChatJoinChannel; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -46,7 +46,7 @@ class Command_ChatLeaveChannel : public ChatCommand {
private: private:
public: public:
Command_ChatLeaveChannel(const QString &_channel = QString()); Command_ChatLeaveChannel(const QString &_channel = QString());
static Command *newCommand() { return new Command_ChatLeaveChannel; } static ProtocolItem *newItem() { return new Command_ChatLeaveChannel; }
}; };
class Command_ChatSay : public ChatCommand { class Command_ChatSay : public ChatCommand {
Q_OBJECT Q_OBJECT
@ -55,7 +55,7 @@ private:
public: public:
Command_ChatSay(const QString &_channel = QString(), const QString &_message = QString()); Command_ChatSay(const QString &_channel = QString(), const QString &_message = QString());
QString getMessage() const { return message; } QString getMessage() const { return message; }
static Command *newCommand() { return new Command_ChatSay; } static ProtocolItem *newItem() { return new Command_ChatSay; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -64,7 +64,7 @@ class Command_ListGames : public Command {
private: private:
public: public:
Command_ListGames(); Command_ListGames();
static Command *newCommand() { return new Command_ListGames; } static ProtocolItem *newItem() { return new Command_ListGames; }
}; };
class Command_CreateGame : public Command { class Command_CreateGame : public Command {
Q_OBJECT Q_OBJECT
@ -79,7 +79,7 @@ public:
QString getPassword() const { return password; } QString getPassword() const { return password; }
int getMaxPlayers() const { return maxPlayers; } int getMaxPlayers() const { return maxPlayers; }
bool getSpectatorsAllowed() const { return spectatorsAllowed; } bool getSpectatorsAllowed() const { return spectatorsAllowed; }
static Command *newCommand() { return new Command_CreateGame; } static ProtocolItem *newItem() { return new Command_CreateGame; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -94,7 +94,7 @@ public:
int getGameId() const { return gameId; } int getGameId() const { return gameId; }
QString getPassword() const { return password; } QString getPassword() const { return password; }
bool getSpectator() const { return spectator; } bool getSpectator() const { return spectator; }
static Command *newCommand() { return new Command_JoinGame; } static ProtocolItem *newItem() { return new Command_JoinGame; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -103,7 +103,7 @@ class Command_LeaveGame : public GameCommand {
private: private:
public: public:
Command_LeaveGame(int _gameId = -1); Command_LeaveGame(int _gameId = -1);
static Command *newCommand() { return new Command_LeaveGame; } static ProtocolItem *newItem() { return new Command_LeaveGame; }
}; };
class Command_Say : public GameCommand { class Command_Say : public GameCommand {
Q_OBJECT Q_OBJECT
@ -112,7 +112,7 @@ private:
public: public:
Command_Say(int _gameId = -1, const QString &_message = QString()); Command_Say(int _gameId = -1, const QString &_message = QString());
QString getMessage() const { return message; } QString getMessage() const { return message; }
static Command *newCommand() { return new Command_Say; } static ProtocolItem *newItem() { return new Command_Say; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -121,7 +121,7 @@ class Command_Shuffle : public GameCommand {
private: private:
public: public:
Command_Shuffle(int _gameId = -1); Command_Shuffle(int _gameId = -1);
static Command *newCommand() { return new Command_Shuffle; } static ProtocolItem *newItem() { return new Command_Shuffle; }
}; };
class Command_RollDie : public GameCommand { class Command_RollDie : public GameCommand {
Q_OBJECT Q_OBJECT
@ -130,7 +130,7 @@ private:
public: public:
Command_RollDie(int _gameId = -1, int _sides = -1); Command_RollDie(int _gameId = -1, int _sides = -1);
int getSides() const { return sides; } int getSides() const { return sides; }
static Command *newCommand() { return new Command_RollDie; } static ProtocolItem *newItem() { return new Command_RollDie; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -141,7 +141,7 @@ private:
public: public:
Command_DrawCards(int _gameId = -1, int _number = -1); Command_DrawCards(int _gameId = -1, int _number = -1);
int getNumber() const { return number; } int getNumber() const { return number; }
static Command *newCommand() { return new Command_DrawCards; } static ProtocolItem *newItem() { return new Command_DrawCards; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -162,7 +162,7 @@ public:
int getX() const { return x; } int getX() const { return x; }
int getY() const { return y; } int getY() const { return y; }
bool getFaceDown() const { return faceDown; } bool getFaceDown() const { return faceDown; }
static Command *newCommand() { return new Command_MoveCard; } static ProtocolItem *newItem() { return new Command_MoveCard; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -181,7 +181,7 @@ public:
QString getPt() const { return pt; } QString getPt() const { return pt; }
int getX() const { return x; } int getX() const { return x; }
int getY() const { return y; } int getY() const { return y; }
static Command *newCommand() { return new Command_CreateToken; } static ProtocolItem *newItem() { return new Command_CreateToken; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -204,7 +204,7 @@ public:
QString getTargetPlayerZone() const { return targetPlayerZone; } QString getTargetPlayerZone() const { return targetPlayerZone; }
int getTargetCardId() const { return targetCardId; } int getTargetCardId() const { return targetCardId; }
int getColor() const { return color; } int getColor() const { return color; }
static Command *newCommand() { return new Command_CreateArrow; } static ProtocolItem *newItem() { return new Command_CreateArrow; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -215,7 +215,7 @@ private:
public: public:
Command_DeleteArrow(int _gameId = -1, int _arrowId = -1); Command_DeleteArrow(int _gameId = -1, int _arrowId = -1);
int getArrowId() const { return arrowId; } int getArrowId() const { return arrowId; }
static Command *newCommand() { return new Command_DeleteArrow; } static ProtocolItem *newItem() { return new Command_DeleteArrow; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -232,7 +232,7 @@ public:
int getCardId() const { return cardId; } int getCardId() const { return cardId; }
QString getAttrName() const { return attrName; } QString getAttrName() const { return attrName; }
QString getAttrValue() const { return attrValue; } QString getAttrValue() const { return attrValue; }
static Command *newCommand() { return new Command_SetCardAttr; } static ProtocolItem *newItem() { return new Command_SetCardAttr; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -241,7 +241,7 @@ class Command_ReadyStart : public GameCommand {
private: private:
public: public:
Command_ReadyStart(int _gameId = -1); Command_ReadyStart(int _gameId = -1);
static Command *newCommand() { return new Command_ReadyStart; } static ProtocolItem *newItem() { return new Command_ReadyStart; }
}; };
class Command_IncCounter : public GameCommand { class Command_IncCounter : public GameCommand {
Q_OBJECT Q_OBJECT
@ -252,7 +252,7 @@ public:
Command_IncCounter(int _gameId = -1, int _counterId = -1, int _delta = -1); Command_IncCounter(int _gameId = -1, int _counterId = -1, int _delta = -1);
int getCounterId() const { return counterId; } int getCounterId() const { return counterId; }
int getDelta() const { return delta; } int getDelta() const { return delta; }
static Command *newCommand() { return new Command_IncCounter; } static ProtocolItem *newItem() { return new Command_IncCounter; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -269,7 +269,7 @@ public:
int getColor() const { return color; } int getColor() const { return color; }
int getRadius() const { return radius; } int getRadius() const { return radius; }
int getValue() const { return value; } int getValue() const { return value; }
static Command *newCommand() { return new Command_AddCounter; } static ProtocolItem *newItem() { return new Command_AddCounter; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -282,7 +282,7 @@ public:
Command_SetCounter(int _gameId = -1, int _counterId = -1, int _value = -1); Command_SetCounter(int _gameId = -1, int _counterId = -1, int _value = -1);
int getCounterId() const { return counterId; } int getCounterId() const { return counterId; }
int getValue() const { return value; } int getValue() const { return value; }
static Command *newCommand() { return new Command_SetCounter; } static ProtocolItem *newItem() { return new Command_SetCounter; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -293,7 +293,7 @@ private:
public: public:
Command_DelCounter(int _gameId = -1, int _counterId = -1); Command_DelCounter(int _gameId = -1, int _counterId = -1);
int getCounterId() const { return counterId; } int getCounterId() const { return counterId; }
static Command *newCommand() { return new Command_DelCounter; } static ProtocolItem *newItem() { return new Command_DelCounter; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -302,7 +302,7 @@ class Command_NextTurn : public GameCommand {
private: private:
public: public:
Command_NextTurn(int _gameId = -1); Command_NextTurn(int _gameId = -1);
static Command *newCommand() { return new Command_NextTurn; } static ProtocolItem *newItem() { return new Command_NextTurn; }
}; };
class Command_SetActivePhase : public GameCommand { class Command_SetActivePhase : public GameCommand {
Q_OBJECT Q_OBJECT
@ -311,7 +311,7 @@ private:
public: public:
Command_SetActivePhase(int _gameId = -1, int _phase = -1); Command_SetActivePhase(int _gameId = -1, int _phase = -1);
int getPhase() const { return phase; } int getPhase() const { return phase; }
static Command *newCommand() { return new Command_SetActivePhase; } static ProtocolItem *newItem() { return new Command_SetActivePhase; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -326,7 +326,7 @@ public:
int getPlayerId() const { return playerId; } int getPlayerId() const { return playerId; }
QString getZoneName() const { return zoneName; } QString getZoneName() const { return zoneName; }
int getNumberCards() const { return numberCards; } int getNumberCards() const { return numberCards; }
static Command *newCommand() { return new Command_DumpZone; } static ProtocolItem *newItem() { return new Command_DumpZone; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -339,7 +339,7 @@ public:
Command_StopDumpZone(int _gameId = -1, int _playerId = -1, const QString &_zoneName = QString()); Command_StopDumpZone(int _gameId = -1, int _playerId = -1, const QString &_zoneName = QString());
int getPlayerId() const { return playerId; } int getPlayerId() const { return playerId; }
QString getZoneName() const { return zoneName; } QString getZoneName() const { return zoneName; }
static Command *newCommand() { return new Command_StopDumpZone; } static ProtocolItem *newItem() { return new Command_StopDumpZone; }
protected: protected:
void extractParameters(); void extractParameters();
}; };
@ -348,14 +348,14 @@ class Command_DumpAll : public GameCommand {
private: private:
public: public:
Command_DumpAll(int _gameId = -1); Command_DumpAll(int _gameId = -1);
static Command *newCommand() { return new Command_DumpAll; } static ProtocolItem *newItem() { return new Command_DumpAll; }
}; };
class Command_SubmitDeck : public GameCommand { class Command_SubmitDeck : public GameCommand {
Q_OBJECT Q_OBJECT
private: private:
public: public:
Command_SubmitDeck(int _gameId = -1); Command_SubmitDeck(int _gameId = -1);
static Command *newCommand() { return new Command_SubmitDeck; } static ProtocolItem *newItem() { return new Command_SubmitDeck; }
}; };
#endif #endif

View file

@ -78,7 +78,7 @@ while (<file>) {
print headerfile "public:\n" print headerfile "public:\n"
. "\t$className($constructorParamsH);\n" . "\t$className($constructorParamsH);\n"
. $paramStr4 . $paramStr4
. "\tstatic Command *newCommand() { return new $className; }\n" . "\tstatic ProtocolItem *newItem() { return new $className; }\n"
. ($paramStr5 eq '' ? '' : "protected:\n\tvoid extractParameters();\n") . ($paramStr5 eq '' ? '' : "protected:\n\tvoid extractParameters();\n")
. "};\n"; . "};\n";
print cppfile $className . "::$className($constructorParamsCpp)\n" print cppfile $className . "::$className($constructorParamsCpp)\n"
@ -93,14 +93,14 @@ while (<file>) {
. $paramStr5 . $paramStr5
. "}\n"; . "}\n";
} }
$initializeHash .= "\tcommandHash.insert(\"$name1\", $className" . "::newCommand);\n"; $initializeHash .= "\titemNameHash.insert(\"cmd$name1\", $className" . "::newItem);\n";
} }
close(file); close(file);
print headerfile "\n#endif\n"; print headerfile "\n#endif\n";
close(headerfile); close(headerfile);
print cppfile "void Command::initializeHash()\n" print cppfile "void ProtocolItem::initializeHashAuto()\n"
. "{\n" . "{\n"
. $initializeHash . $initializeHash
. "}\n"; . "}\n";

View file

@ -5,7 +5,7 @@
#include "protocol_commands.h" #include "protocol_commands.h"
Widget::Widget() Widget::Widget()
: QMainWindow(), currentCommand(0) : QMainWindow()
{ {
edit1 = new QTextEdit; edit1 = new QTextEdit;
start = new QPushButton; start = new QPushButton;
@ -26,12 +26,14 @@ Widget::Widget()
central->setLayout(vbox); central->setLayout(vbox);
setCentralWidget(central); setCentralWidget(central);
resize(400, 500);
Command::initializeHash(); Command::initializeHash();
} }
void Widget::startClicked() void Widget::startClicked()
{ {
currentCommand = 0; currentItem = 0;
xmlWriter.writeStartDocument(); xmlWriter.writeStartDocument();
xmlWriter.writeStartElement("cockatrice_communication"); xmlWriter.writeStartElement("cockatrice_communication");
xmlWriter.writeAttribute("version", "4"); xmlWriter.writeAttribute("version", "4");
@ -44,15 +46,18 @@ void Widget::startClicked()
Command *test3 = new Command_ChatSay("foobar", "Hallo, dies ist ein Test"); Command *test3 = new Command_ChatSay("foobar", "Hallo, dies ist ein Test");
test3->write(xmlWriter); test3->write(xmlWriter);
ProtocolResponse *test4 = new ProtocolResponse(123, ProtocolResponse::RespContextError);
test4->write(xmlWriter);
} }
bool Widget::readCurrentCommand() bool Widget::readCurrentCommand()
{ {
if (!currentCommand) if (!currentItem)
return false; return false;
if (currentCommand->read(xmlReader)) { if (currentItem->read(xmlReader)) {
qDebug() << "setting to 0"; qDebug() << "setting to 0";
currentCommand = 0; currentItem = 0;
} }
return true; return true;
} }
@ -65,11 +70,12 @@ void Widget::parseXml()
while (!xmlReader.atEnd()) { while (!xmlReader.atEnd()) {
xmlReader.readNext(); xmlReader.readNext();
if (xmlReader.isStartElement()) { if (xmlReader.isStartElement()) {
QString cmdStr = xmlReader.name().toString(); QString itemType = xmlReader.name().toString();
qDebug() << "parseXml: startElement: " << cmdStr; QString itemName = xmlReader.attributes().value("name").toString();
currentCommand = Command::getNewCommand(cmdStr); qDebug() << "parseXml: startElement: " << "type =" << itemType << ", name =" << itemName;
if (!currentCommand) currentItem = ProtocolItem::getNewItem(itemType + itemName);
qDebug() << "unrecognized command"; if (!currentItem)
qDebug() << "unrecognized item";
readCurrentCommand(); readCurrentCommand();
} }
} }

View file

@ -8,7 +8,7 @@
class QTextEdit; class QTextEdit;
class QPushButton; class QPushButton;
class QBuffer; class QBuffer;
class Command; class ProtocolItem;
class Widget : public QMainWindow { class Widget : public QMainWindow {
Q_OBJECT Q_OBJECT
@ -19,7 +19,7 @@ private:
QXmlStreamReader xmlReader; QXmlStreamReader xmlReader;
QXmlStreamWriter xmlWriter; QXmlStreamWriter xmlWriter;
Command *currentCommand; ProtocolItem *currentItem;
bool readCurrentCommand(); bool readCurrentCommand();
void parseBuffer(); void parseBuffer();
void parseXml(); void parseXml();