diff --git a/common/protocol.cpp b/common/protocol.cpp index d420a282..33c2daa0 100644 --- a/common/protocol.cpp +++ b/common/protocol.cpp @@ -22,7 +22,6 @@ bool ProtocolItem::read(QXmlStreamReader *xml) if (xml->name() == getItemType()) { extractParameters(); qDebug() << "FERTIG"; - deleteLater(); return true; } else { QString tagName = xml->name().toString(); @@ -111,6 +110,7 @@ void ProtocolResponse::extractParameters() void ProtocolResponse::initializeHash() { responseHash.insert("ok", RespOk); + responseHash.insert("invalid_command", RespInvalidCommand); responseHash.insert("name_not_found", RespNameNotFound); responseHash.insert("login_needed", RespLoginNeeded); responseHash.insert("context_error", RespContextError); diff --git a/common/protocol.h b/common/protocol.h index fd3f15bd..0579e833 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -6,6 +6,7 @@ #include #include #include +#include "protocol_item_ids.h" class QXmlStreamReader; class QXmlStreamWriter; @@ -29,6 +30,7 @@ private: static void initializeHashAuto(); public: static const int protocolVersion = 4; + virtual int getItemId() const = 0; ProtocolItem(const QString &_itemName); static void initializeHash(); static ProtocolItem *getNewItem(const QString &name); @@ -45,7 +47,15 @@ protected: QString getItemType() const { return "cmd"; } void extractParameters(); public: - Command(const QString &_itemName, int _cmdId = -1); + Command(const QString &_itemName = QString(), int _cmdId = -1); + int getCmdId() const { return cmdId; } +}; + +class InvalidCommand : public Command { + Q_OBJECT +public: + InvalidCommand() : Command() { } + int getItemId() const { return ItemId_Other; } }; class ChatCommand : public Command { @@ -87,7 +97,7 @@ public: class ProtocolResponse : public ProtocolItem { Q_OBJECT public: - enum ResponseCode { RespOk, RespNameNotFound, RespLoginNeeded, RespContextError, RespWrongPassword, RespSpectatorsNotAllowed }; + enum ResponseCode { RespNothing, RespOk, RespInvalidCommand, RespNameNotFound, RespLoginNeeded, RespContextError, RespWrongPassword, RespSpectatorsNotAllowed }; private: int cmdId; ResponseCode responseCode; @@ -97,6 +107,7 @@ protected: void extractParameters(); public: ProtocolResponse(int _cmdId = -1, ResponseCode _responseCode = RespOk); + int getItemId() const { return ItemId_Other; } static void initializeHash(); static ProtocolItem *newItem() { return new ProtocolResponse; } }; diff --git a/common/protocol_item_ids.h b/common/protocol_item_ids.h new file mode 100644 index 00000000..9d174afe --- /dev/null +++ b/common/protocol_item_ids.h @@ -0,0 +1,55 @@ +enum AutoItemId { +ItemId_Command_Ping = 1001, +ItemId_Command_Login = 1002, +ItemId_Command_ChatListChannels = 1003, +ItemId_Command_ChatJoinChannel = 1004, +ItemId_Command_ChatLeaveChannel = 1005, +ItemId_Command_ChatSay = 1006, +ItemId_Command_ListGames = 1007, +ItemId_Command_CreateGame = 1008, +ItemId_Command_JoinGame = 1009, +ItemId_Command_LeaveGame = 1010, +ItemId_Command_Say = 1011, +ItemId_Command_Shuffle = 1012, +ItemId_Command_RollDie = 1013, +ItemId_Command_DrawCards = 1014, +ItemId_Command_MoveCard = 1015, +ItemId_Command_CreateToken = 1016, +ItemId_Command_CreateArrow = 1017, +ItemId_Command_DeleteArrow = 1018, +ItemId_Command_SetCardAttr = 1019, +ItemId_Command_ReadyStart = 1020, +ItemId_Command_IncCounter = 1021, +ItemId_Command_AddCounter = 1022, +ItemId_Command_SetCounter = 1023, +ItemId_Command_DelCounter = 1024, +ItemId_Command_NextTurn = 1025, +ItemId_Command_SetActivePhase = 1026, +ItemId_Command_DumpZone = 1027, +ItemId_Command_StopDumpZone = 1028, +ItemId_Command_DumpAll = 1029, +ItemId_Command_SubmitDeck = 1030, +ItemId_Event_Say = 1031, +ItemId_Event_Join = 1032, +ItemId_Event_Leave = 1033, +ItemId_Event_GameClosed = 1034, +ItemId_Event_ReadyStart = 1035, +ItemId_Event_SetupZones = 1036, +ItemId_Event_GameStart = 1037, +ItemId_Event_Shuffle = 1038, +ItemId_Event_RollDie = 1039, +ItemId_Event_MoveCard = 1040, +ItemId_Event_CreateToken = 1041, +ItemId_Event_CreateArrow = 1042, +ItemId_Event_DeleteArrow = 1043, +ItemId_Event_SetCardAttr = 1044, +ItemId_Event_AddCounter = 1045, +ItemId_Event_SetCounter = 1046, +ItemId_Event_DelCounter = 1047, +ItemId_Event_SetActivePlayer = 1048, +ItemId_Event_SetActivePhase = 1049, +ItemId_Event_DumpZone = 1050, +ItemId_Event_StopDumpZone = 1051, +ItemId_Event_Welcome = 1052, +ItemId_Other = 1053 +}; diff --git a/common/protocol_items.cpp b/common/protocol_items.cpp index 96c97052..ca536b75 100644 --- a/common/protocol_items.cpp +++ b/common/protocol_items.cpp @@ -137,11 +137,11 @@ void Command_MoveCard::extractParameters() y = parameters["y"].toInt(); faceDown = (parameters["face_down"] == "1"); } -Command_CreateToken::Command_CreateToken(int _gameId, const QString &_zone, const QString &_name, const QString &_pt, int _x, int _y) - : GameCommand("create_token", _gameId), zone(_zone), name(_name), pt(_pt), x(_x), y(_y) +Command_CreateToken::Command_CreateToken(int _gameId, const QString &_zone, const QString &_cardName, const QString &_pt, int _x, int _y) + : GameCommand("create_token", _gameId), zone(_zone), cardName(_cardName), pt(_pt), x(_x), y(_y) { setParameter("zone", zone); - setParameter("name", name); + setParameter("card_name", cardName); setParameter("pt", pt); setParameter("x", x); setParameter("y", y); @@ -150,19 +150,19 @@ void Command_CreateToken::extractParameters() { GameCommand::extractParameters(); zone = parameters["zone"]; - name = parameters["name"]; + cardName = parameters["card_name"]; pt = parameters["pt"]; x = parameters["x"].toInt(); y = parameters["y"].toInt(); } -Command_CreateArrow::Command_CreateArrow(int _gameId, int _startPlayerId, const QString &_startZone, int _startCardId, int _targetPlayerId, const QString &_targetPlayerZone, int _targetCardId, int _color) - : GameCommand("create_arrow", _gameId), startPlayerId(_startPlayerId), startZone(_startZone), startCardId(_startCardId), targetPlayerId(_targetPlayerId), targetPlayerZone(_targetPlayerZone), targetCardId(_targetCardId), color(_color) +Command_CreateArrow::Command_CreateArrow(int _gameId, int _startPlayerId, const QString &_startZone, int _startCardId, int _targetPlayerId, const QString &_targetZone, int _targetCardId, int _color) + : GameCommand("create_arrow", _gameId), startPlayerId(_startPlayerId), startZone(_startZone), startCardId(_startCardId), targetPlayerId(_targetPlayerId), targetZone(_targetZone), targetCardId(_targetCardId), color(_color) { setParameter("start_player_id", startPlayerId); setParameter("start_zone", startZone); setParameter("start_card_id", startCardId); setParameter("target_player_id", targetPlayerId); - setParameter("target_player_zone", targetPlayerZone); + setParameter("target_zone", targetZone); setParameter("target_card_id", targetCardId); setParameter("color", color); } @@ -173,7 +173,7 @@ void Command_CreateArrow::extractParameters() startZone = parameters["start_zone"]; startCardId = parameters["start_card_id"].toInt(); targetPlayerId = parameters["target_player_id"].toInt(); - targetPlayerZone = parameters["target_player_zone"]; + targetZone = parameters["target_zone"]; targetCardId = parameters["target_card_id"].toInt(); color = parameters["color"].toInt(); } diff --git a/common/protocol_items.dat b/common/protocol_items.dat index 96e79b69..5835c909 100644 --- a/common/protocol_items.dat +++ b/common/protocol_items.dat @@ -13,8 +13,8 @@ 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:create_token:s,zone:s,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_player_zone:i,target_card_id:i,color +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:i,color 2:delete_arrow:i,arrow_id 2:set_card_attr:s,zone:i,card_id:s,attr_name:s,attr_value 2:ready_start diff --git a/common/protocol_items.h b/common/protocol_items.h index e1dc3e12..7cc1e337 100644 --- a/common/protocol_items.h +++ b/common/protocol_items.h @@ -9,6 +9,7 @@ private: public: Command_Ping(); static ProtocolItem *newItem() { return new Command_Ping; } + int getItemId() const { return ItemId_Command_Ping; } }; class Command_Login : public Command { Q_OBJECT @@ -20,6 +21,7 @@ public: QString getUsername() const { return username; } QString getPassword() const { return password; } static ProtocolItem *newItem() { return new Command_Login; } + int getItemId() const { return ItemId_Command_Login; } protected: void extractParameters(); }; @@ -29,6 +31,7 @@ private: public: Command_ChatListChannels(); static ProtocolItem *newItem() { return new Command_ChatListChannels; } + int getItemId() const { return ItemId_Command_ChatListChannels; } }; class Command_ChatJoinChannel : public Command { Q_OBJECT @@ -38,6 +41,7 @@ public: Command_ChatJoinChannel(const QString &_channel = QString()); QString getChannel() const { return channel; } static ProtocolItem *newItem() { return new Command_ChatJoinChannel; } + int getItemId() const { return ItemId_Command_ChatJoinChannel; } protected: void extractParameters(); }; @@ -47,6 +51,7 @@ private: public: Command_ChatLeaveChannel(const QString &_channel = QString()); static ProtocolItem *newItem() { return new Command_ChatLeaveChannel; } + int getItemId() const { return ItemId_Command_ChatLeaveChannel; } }; class Command_ChatSay : public ChatCommand { Q_OBJECT @@ -56,6 +61,7 @@ public: Command_ChatSay(const QString &_channel = QString(), const QString &_message = QString()); QString getMessage() const { return message; } static ProtocolItem *newItem() { return new Command_ChatSay; } + int getItemId() const { return ItemId_Command_ChatSay; } protected: void extractParameters(); }; @@ -65,6 +71,7 @@ private: public: Command_ListGames(); static ProtocolItem *newItem() { return new Command_ListGames; } + int getItemId() const { return ItemId_Command_ListGames; } }; class Command_CreateGame : public Command { Q_OBJECT @@ -80,6 +87,7 @@ public: int getMaxPlayers() const { return maxPlayers; } bool getSpectatorsAllowed() const { return spectatorsAllowed; } static ProtocolItem *newItem() { return new Command_CreateGame; } + int getItemId() const { return ItemId_Command_CreateGame; } protected: void extractParameters(); }; @@ -95,6 +103,7 @@ public: QString getPassword() const { return password; } bool getSpectator() const { return spectator; } static ProtocolItem *newItem() { return new Command_JoinGame; } + int getItemId() const { return ItemId_Command_JoinGame; } protected: void extractParameters(); }; @@ -104,6 +113,7 @@ private: public: Command_LeaveGame(int _gameId = -1); static ProtocolItem *newItem() { return new Command_LeaveGame; } + int getItemId() const { return ItemId_Command_LeaveGame; } }; class Command_Say : public GameCommand { Q_OBJECT @@ -113,6 +123,7 @@ public: Command_Say(int _gameId = -1, const QString &_message = QString()); QString getMessage() const { return message; } static ProtocolItem *newItem() { return new Command_Say; } + int getItemId() const { return ItemId_Command_Say; } protected: void extractParameters(); }; @@ -122,6 +133,7 @@ private: public: Command_Shuffle(int _gameId = -1); static ProtocolItem *newItem() { return new Command_Shuffle; } + int getItemId() const { return ItemId_Command_Shuffle; } }; class Command_RollDie : public GameCommand { Q_OBJECT @@ -131,6 +143,7 @@ public: Command_RollDie(int _gameId = -1, int _sides = -1); int getSides() const { return sides; } static ProtocolItem *newItem() { return new Command_RollDie; } + int getItemId() const { return ItemId_Command_RollDie; } protected: void extractParameters(); }; @@ -142,6 +155,7 @@ public: Command_DrawCards(int _gameId = -1, int _number = -1); int getNumber() const { return number; } static ProtocolItem *newItem() { return new Command_DrawCards; } + int getItemId() const { return ItemId_Command_DrawCards; } protected: void extractParameters(); }; @@ -163,6 +177,7 @@ public: int getY() const { return y; } bool getFaceDown() const { return faceDown; } static ProtocolItem *newItem() { return new Command_MoveCard; } + int getItemId() const { return ItemId_Command_MoveCard; } protected: void extractParameters(); }; @@ -170,18 +185,19 @@ class Command_CreateToken : public GameCommand { Q_OBJECT private: QString zone; - QString name; + QString cardName; QString pt; int x; int y; public: - Command_CreateToken(int _gameId = -1, const QString &_zone = QString(), const QString &_name = QString(), const QString &_pt = QString(), int _x = -1, int _y = -1); + Command_CreateToken(int _gameId = -1, const QString &_zone = QString(), const QString &_cardName = QString(), const QString &_pt = QString(), int _x = -1, int _y = -1); QString getZone() const { return zone; } - QString getName() const { return name; } + QString getCardName() const { return cardName; } QString getPt() const { return pt; } int getX() const { return x; } int getY() const { return y; } static ProtocolItem *newItem() { return new Command_CreateToken; } + int getItemId() const { return ItemId_Command_CreateToken; } protected: void extractParameters(); }; @@ -192,19 +208,20 @@ private: QString startZone; int startCardId; int targetPlayerId; - QString targetPlayerZone; + QString targetZone; int targetCardId; int color; public: - Command_CreateArrow(int _gameId = -1, int _startPlayerId = -1, const QString &_startZone = QString(), int _startCardId = -1, int _targetPlayerId = -1, const QString &_targetPlayerZone = QString(), int _targetCardId = -1, int _color = -1); + Command_CreateArrow(int _gameId = -1, int _startPlayerId = -1, const QString &_startZone = QString(), int _startCardId = -1, int _targetPlayerId = -1, const QString &_targetZone = QString(), int _targetCardId = -1, int _color = -1); int getStartPlayerId() const { return startPlayerId; } QString getStartZone() const { return startZone; } int getStartCardId() const { return startCardId; } int getTargetPlayerId() const { return targetPlayerId; } - QString getTargetPlayerZone() const { return targetPlayerZone; } + QString getTargetZone() const { return targetZone; } int getTargetCardId() const { return targetCardId; } int getColor() const { return color; } static ProtocolItem *newItem() { return new Command_CreateArrow; } + int getItemId() const { return ItemId_Command_CreateArrow; } protected: void extractParameters(); }; @@ -216,6 +233,7 @@ public: Command_DeleteArrow(int _gameId = -1, int _arrowId = -1); int getArrowId() const { return arrowId; } static ProtocolItem *newItem() { return new Command_DeleteArrow; } + int getItemId() const { return ItemId_Command_DeleteArrow; } protected: void extractParameters(); }; @@ -233,6 +251,7 @@ public: QString getAttrName() const { return attrName; } QString getAttrValue() const { return attrValue; } static ProtocolItem *newItem() { return new Command_SetCardAttr; } + int getItemId() const { return ItemId_Command_SetCardAttr; } protected: void extractParameters(); }; @@ -242,6 +261,7 @@ private: public: Command_ReadyStart(int _gameId = -1); static ProtocolItem *newItem() { return new Command_ReadyStart; } + int getItemId() const { return ItemId_Command_ReadyStart; } }; class Command_IncCounter : public GameCommand { Q_OBJECT @@ -253,6 +273,7 @@ public: int getCounterId() const { return counterId; } int getDelta() const { return delta; } static ProtocolItem *newItem() { return new Command_IncCounter; } + int getItemId() const { return ItemId_Command_IncCounter; } protected: void extractParameters(); }; @@ -270,6 +291,7 @@ public: int getRadius() const { return radius; } int getValue() const { return value; } static ProtocolItem *newItem() { return new Command_AddCounter; } + int getItemId() const { return ItemId_Command_AddCounter; } protected: void extractParameters(); }; @@ -283,6 +305,7 @@ public: int getCounterId() const { return counterId; } int getValue() const { return value; } static ProtocolItem *newItem() { return new Command_SetCounter; } + int getItemId() const { return ItemId_Command_SetCounter; } protected: void extractParameters(); }; @@ -294,6 +317,7 @@ public: Command_DelCounter(int _gameId = -1, int _counterId = -1); int getCounterId() const { return counterId; } static ProtocolItem *newItem() { return new Command_DelCounter; } + int getItemId() const { return ItemId_Command_DelCounter; } protected: void extractParameters(); }; @@ -303,6 +327,7 @@ private: public: Command_NextTurn(int _gameId = -1); static ProtocolItem *newItem() { return new Command_NextTurn; } + int getItemId() const { return ItemId_Command_NextTurn; } }; class Command_SetActivePhase : public GameCommand { Q_OBJECT @@ -312,6 +337,7 @@ public: Command_SetActivePhase(int _gameId = -1, int _phase = -1); int getPhase() const { return phase; } static ProtocolItem *newItem() { return new Command_SetActivePhase; } + int getItemId() const { return ItemId_Command_SetActivePhase; } protected: void extractParameters(); }; @@ -327,6 +353,7 @@ public: QString getZoneName() const { return zoneName; } int getNumberCards() const { return numberCards; } static ProtocolItem *newItem() { return new Command_DumpZone; } + int getItemId() const { return ItemId_Command_DumpZone; } protected: void extractParameters(); }; @@ -340,6 +367,7 @@ public: int getPlayerId() const { return playerId; } QString getZoneName() const { return zoneName; } static ProtocolItem *newItem() { return new Command_StopDumpZone; } + int getItemId() const { return ItemId_Command_StopDumpZone; } protected: void extractParameters(); }; @@ -349,6 +377,7 @@ private: public: Command_DumpAll(int _gameId = -1); static ProtocolItem *newItem() { return new Command_DumpAll; } + int getItemId() const { return ItemId_Command_DumpAll; } }; class Command_SubmitDeck : public GameCommand { Q_OBJECT @@ -356,6 +385,7 @@ private: public: Command_SubmitDeck(int _gameId = -1); static ProtocolItem *newItem() { return new Command_SubmitDeck; } + int getItemId() const { return ItemId_Command_SubmitDeck; } }; class Event_Say : public GameEvent { Q_OBJECT @@ -365,6 +395,7 @@ public: Event_Say(int _gameId = -1, int _playerId = -1, const QString &_message = QString()); QString getMessage() const { return message; } static ProtocolItem *newItem() { return new Event_Say; } + int getItemId() const { return ItemId_Event_Say; } protected: void extractParameters(); }; @@ -378,6 +409,7 @@ public: QString getPlayerName() const { return playerName; } bool getSpectator() const { return spectator; } static ProtocolItem *newItem() { return new Event_Join; } + int getItemId() const { return ItemId_Event_Join; } protected: void extractParameters(); }; @@ -387,6 +419,7 @@ private: public: Event_Leave(int _gameId = -1, int _playerId = -1); static ProtocolItem *newItem() { return new Event_Leave; } + int getItemId() const { return ItemId_Event_Leave; } }; class Event_GameClosed : public GameEvent { Q_OBJECT @@ -394,6 +427,7 @@ private: public: Event_GameClosed(int _gameId = -1, int _playerId = -1); static ProtocolItem *newItem() { return new Event_GameClosed; } + int getItemId() const { return ItemId_Event_GameClosed; } }; class Event_ReadyStart : public GameEvent { Q_OBJECT @@ -401,6 +435,7 @@ private: public: Event_ReadyStart(int _gameId = -1, int _playerId = -1); static ProtocolItem *newItem() { return new Event_ReadyStart; } + int getItemId() const { return ItemId_Event_ReadyStart; } }; class Event_SetupZones : public GameEvent { Q_OBJECT @@ -412,6 +447,7 @@ public: int getDeckSize() const { return deckSize; } int getSbSize() const { return sbSize; } static ProtocolItem *newItem() { return new Event_SetupZones; } + int getItemId() const { return ItemId_Event_SetupZones; } protected: void extractParameters(); }; @@ -421,6 +457,7 @@ private: public: Event_GameStart(int _gameId = -1, int _playerId = -1); static ProtocolItem *newItem() { return new Event_GameStart; } + int getItemId() const { return ItemId_Event_GameStart; } }; class Event_Shuffle : public GameEvent { Q_OBJECT @@ -428,6 +465,7 @@ private: public: Event_Shuffle(int _gameId = -1, int _playerId = -1); static ProtocolItem *newItem() { return new Event_Shuffle; } + int getItemId() const { return ItemId_Event_Shuffle; } }; class Event_RollDie : public GameEvent { Q_OBJECT @@ -439,6 +477,7 @@ public: int getSides() const { return sides; } int getValue() const { return value; } static ProtocolItem *newItem() { return new Event_RollDie; } + int getItemId() const { return ItemId_Event_RollDie; } protected: void extractParameters(); }; @@ -464,6 +503,7 @@ public: int getY() const { return y; } bool getFaceDown() const { return faceDown; } static ProtocolItem *newItem() { return new Event_MoveCard; } + int getItemId() const { return ItemId_Event_MoveCard; } protected: void extractParameters(); }; @@ -485,6 +525,7 @@ public: int getX() const { return x; } int getY() const { return y; } static ProtocolItem *newItem() { return new Event_CreateToken; } + int getItemId() const { return ItemId_Event_CreateToken; } protected: void extractParameters(); }; @@ -510,6 +551,7 @@ public: int getTargetCardId() const { return targetCardId; } int getColor() const { return color; } static ProtocolItem *newItem() { return new Event_CreateArrow; } + int getItemId() const { return ItemId_Event_CreateArrow; } protected: void extractParameters(); }; @@ -521,6 +563,7 @@ public: Event_DeleteArrow(int _gameId = -1, int _playerId = -1, int _arrowId = -1); int getArrowId() const { return arrowId; } static ProtocolItem *newItem() { return new Event_DeleteArrow; } + int getItemId() const { return ItemId_Event_DeleteArrow; } protected: void extractParameters(); }; @@ -538,6 +581,7 @@ public: QString getAttrName() const { return attrName; } QString getAttrValue() const { return attrValue; } static ProtocolItem *newItem() { return new Event_SetCardAttr; } + int getItemId() const { return ItemId_Event_SetCardAttr; } protected: void extractParameters(); }; @@ -557,6 +601,7 @@ public: int getRadius() const { return radius; } int getValue() const { return value; } static ProtocolItem *newItem() { return new Event_AddCounter; } + int getItemId() const { return ItemId_Event_AddCounter; } protected: void extractParameters(); }; @@ -570,6 +615,7 @@ public: int getCounterId() const { return counterId; } int getValue() const { return value; } static ProtocolItem *newItem() { return new Event_SetCounter; } + int getItemId() const { return ItemId_Event_SetCounter; } protected: void extractParameters(); }; @@ -581,6 +627,7 @@ public: Event_DelCounter(int _gameId = -1, int _playerId = -1, int _counterId = -1); int getCounterId() const { return counterId; } static ProtocolItem *newItem() { return new Event_DelCounter; } + int getItemId() const { return ItemId_Event_DelCounter; } protected: void extractParameters(); }; @@ -592,6 +639,7 @@ public: Event_SetActivePlayer(int _gameId = -1, int _playerId = -1, int _activePlayerId = -1); int getActivePlayerId() const { return activePlayerId; } static ProtocolItem *newItem() { return new Event_SetActivePlayer; } + int getItemId() const { return ItemId_Event_SetActivePlayer; } protected: void extractParameters(); }; @@ -603,6 +651,7 @@ public: Event_SetActivePhase(int _gameId = -1, int _playerId = -1, int _phase = -1); int getPhase() const { return phase; } static ProtocolItem *newItem() { return new Event_SetActivePhase; } + int getItemId() const { return ItemId_Event_SetActivePhase; } protected: void extractParameters(); }; @@ -618,6 +667,7 @@ public: QString getZone() const { return zone; } int getNumberCards() const { return numberCards; } static ProtocolItem *newItem() { return new Event_DumpZone; } + int getItemId() const { return ItemId_Event_DumpZone; } protected: void extractParameters(); }; @@ -631,6 +681,7 @@ public: int getZoneOwnerId() const { return zoneOwnerId; } QString getZone() const { return zone; } static ProtocolItem *newItem() { return new Event_StopDumpZone; } + int getItemId() const { return ItemId_Event_StopDumpZone; } protected: void extractParameters(); }; @@ -642,6 +693,7 @@ public: Event_Welcome(const QString &_message = QString()); QString getMessage() const { return message; } static ProtocolItem *newItem() { return new Event_Welcome; } + int getItemId() const { return ItemId_Event_Welcome; } protected: void extractParameters(); }; diff --git a/common/protocol_mc.pl b/common/protocol_mc.pl index 5a787e75..552db561 100755 --- a/common/protocol_mc.pl +++ b/common/protocol_mc.pl @@ -1,6 +1,11 @@ #!/usr/bin/perl $initializeHash = ''; +$itemId = 1000; + +$headerfileBuffer = ''; + +open(idfile, ">protocol_item_ids.h"); open(headerfile, ">protocol_items.h"); print headerfile "#ifndef PROTOCOL_ITEMS_H\n" @@ -56,7 +61,8 @@ while () { $constructorParamsCpp = ""; } $className = $namePrefix . '_' . $name2; - print headerfile "class $className : public $baseClass {\n" + $itemEnum .= "ItemId_$className = " . ++$itemId . ",\n"; + $headerfileBuffer .= "class $className : public $baseClass {\n" . "\tQ_OBJECT\n" . "private:\n"; $paramStr2 = ''; @@ -92,12 +98,13 @@ while () { } ($prettyVarName2 = $prettyVarName) =~ s/^(.)/\U$1\E/; $paramStr4 .= "\t$dataType get$prettyVarName2() const { return $prettyVarName; }\n"; - print headerfile "\t$dataType $prettyVarName;\n"; + $headerfileBuffer .= "\t$dataType $prettyVarName;\n"; } - print headerfile "public:\n" + $headerfileBuffer .= "public:\n" . "\t$className($constructorParamsH);\n" . $paramStr4 . "\tstatic ProtocolItem *newItem() { return new $className; }\n" + . "\tint getItemId() const { return ItemId_$className; }\n" . ($paramStr5 eq '' ? '' : "protected:\n\tvoid extractParameters();\n") . "};\n"; print cppfile $className . "::$className($constructorParamsCpp)\n" @@ -116,7 +123,14 @@ while () { } close(file); -print headerfile "\n#endif\n"; +print idfile "enum AutoItemId {\n" + . $itemEnum + . "ItemId_Other = " . ++$itemId . "\n" + . "};\n"; +close(idfile); + +print headerfile $headerfileBuffer + . "\n#endif\n"; close(headerfile); print cppfile "void ProtocolItem::initializeHashAuto()\n" diff --git a/common/server_game.cpp b/common/server_game.cpp index 7d2f8d97..5b50f231 100644 --- a/common/server_game.cpp +++ b/common/server_game.cpp @@ -101,17 +101,17 @@ void Server_Game::startGameIfReady() setActivePlayer(0); } -ReturnMessage::ReturnCode Server_Game::checkJoin(const QString &_password, bool spectator) +ProtocolResponse::ResponseCode Server_Game::checkJoin(const QString &_password, bool spectator) { if (_password != password) - return ReturnMessage::ReturnPasswordWrong; + return ProtocolResponse::RespWrongPassword; if (spectator) { if (!spectatorsAllowed) - return ReturnMessage::ReturnSpectatorsNotAllowed; + return ProtocolResponse::RespSpectatorsNotAllowed; } else if (gameStarted || (getPlayerCount() >= getMaxPlayers())) - return ReturnMessage::ReturnContextError; + return ProtocolResponse::RespContextError; - return ReturnMessage::ReturnOk; + return ProtocolResponse::RespOk; } Server_Player *Server_Game::addPlayer(const QString &playerName, bool spectator) diff --git a/common/server_game.h b/common/server_game.h index 9b552dc0..b8df44aa 100644 --- a/common/server_game.h +++ b/common/server_game.h @@ -24,7 +24,7 @@ #include #include #include "server_player.h" -#include "returnmessage.h" +#include "protocol.h" class Server_Game : public QObject { Q_OBJECT @@ -55,7 +55,7 @@ public: int getMaxPlayers() const { return maxPlayers; } bool getSpectatorsAllowed() const { return spectatorsAllowed; } QString getGameListLine() const; - ReturnMessage::ReturnCode checkJoin(const QString &_password, bool spectator); + ProtocolResponse::ResponseCode checkJoin(const QString &_password, bool spectator); Server_Player *addPlayer(const QString &playerName, bool spectator); void removePlayer(Server_Player *player); void startGameIfReady(); diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 9f53d393..124ff66b 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -2,6 +2,13 @@ #include "server_protocolhandler.h" #include "protocol.h" #include "protocol_items.h" +#include "server_chatchannel.h" +#include "server_card.h" +#include "server_arrow.h" +#include "server_cardzone.h" +#include "server_counter.h" +#include "server_game.h" +#include "server_player.h" Server_ProtocolHandler::Server_ProtocolHandler(Server *_server, QObject *parent) : QObject(parent), server(_server), authState(PasswordWrong), acceptsGameListChanges(false) @@ -14,15 +21,70 @@ Server_ProtocolHandler::~Server_ProtocolHandler() void Server_ProtocolHandler::processCommand(Command *command) { + ProtocolResponse::ResponseCode response = ProtocolResponse::RespInvalidCommand; + ChatCommand *chatCommand = qobject_cast(command); GameCommand *gameCommand = qobject_cast(command); if (chatCommand) { qDebug() << "received ChatCommand: channel =" << chatCommand->getChannel(); + Server_ChatChannel *channel = chatChannels.value(chatCommand->getChannel(), 0); + if (!channel) { + sendProtocolItem(new ProtocolResponse(gameCommand->getCmdId(), ProtocolResponse::RespNameNotFound)); + return; + } + switch (command->getItemId()) { + case ItemId_Command_ChatLeaveChannel: response = cmdChatLeaveChannel(qobject_cast(command), channel); break; + case ItemId_Command_ChatSay: response = cmdChatSay(qobject_cast(command), channel); break; + } } else if (gameCommand) { qDebug() << "received GameCommand: game =" << gameCommand->getGameId(); + if (!games.contains(gameCommand->getGameId())) { + sendProtocolItem(new ProtocolResponse(gameCommand->getCmdId(), ProtocolResponse::RespNameNotFound)); + return; + } + QPair gamePair = games.value(gameCommand->getGameId()); + Server_Game *game = gamePair.first; + Server_Player *player = gamePair.second; + + switch (command->getItemId()) { + case ItemId_Command_LeaveGame: response = cmdLeaveGame(qobject_cast(command), game, player); break; + case ItemId_Command_Say: response = cmdSay(qobject_cast(command), game, player); break; + case ItemId_Command_Shuffle: response = cmdShuffle(qobject_cast(command), game, player); break; + case ItemId_Command_RollDie: response = cmdRollDie(qobject_cast(command), game, player); break; + case ItemId_Command_DrawCards: response = cmdDrawCards(qobject_cast(command), game, player); break; + case ItemId_Command_MoveCard: response = cmdMoveCard(qobject_cast(command), game, player); break; + case ItemId_Command_CreateToken: response = cmdCreateToken(qobject_cast(command), game, player); break; + case ItemId_Command_CreateArrow: response = cmdCreateArrow(qobject_cast(command), game, player); break; + case ItemId_Command_DeleteArrow: response = cmdDeleteArrow(qobject_cast(command), game, player); break; + case ItemId_Command_SetCardAttr: response = cmdSetCardAttr(qobject_cast(command), game, player); break; + case ItemId_Command_ReadyStart: response = cmdReadyStart(qobject_cast(command), game, player); break; + case ItemId_Command_IncCounter: response = cmdIncCounter(qobject_cast(command), game, player); break; + case ItemId_Command_AddCounter: response = cmdAddCounter(qobject_cast(command), game, player); break; + case ItemId_Command_SetCounter: response = cmdSetCounter(qobject_cast(command), game, player); break; + case ItemId_Command_DelCounter: response = cmdDelCounter(qobject_cast(command), game, player); break; + case ItemId_Command_NextTurn: response = cmdNextTurn(qobject_cast(command), game, player); break; + case ItemId_Command_SetActivePhase: response = cmdSetActivePhase(qobject_cast(command), game, player); break; + case ItemId_Command_DumpZone: response = cmdDumpZone(qobject_cast(command), game, player); break; + case ItemId_Command_StopDumpZone: response = cmdStopDumpZone(qobject_cast(command), game, player); break; + case ItemId_Command_DumpAll: response = cmdDumpAll(qobject_cast(command), game, player); break; + case ItemId_Command_SubmitDeck: response = cmdSubmitDeck(qobject_cast(command), game, player); break; + } } else { qDebug() << "received generic Command"; + switch (command->getItemId()) { + case ItemId_Command_Ping: response = cmdPing(qobject_cast(command)); break; + case ItemId_Command_Login: response = cmdLogin(qobject_cast(command)); break; + case ItemId_Command_ChatListChannels: response = cmdChatListChannels(qobject_cast(command)); break; + case ItemId_Command_ChatJoinChannel: response = cmdChatJoinChannel(qobject_cast(command)); break; + case ItemId_Command_ListGames: response = cmdListGames(qobject_cast(command)); break; + case ItemId_Command_CreateGame: response = cmdCreateGame(qobject_cast(command)); break; + case ItemId_Command_JoinGame: response = cmdJoinGame(qobject_cast(command)); break; + } } + if (response != ProtocolResponse::RespNothing) + sendProtocolItem(new ProtocolResponse(command->getCmdId(), response)); + + delete command; } QPair Server_ProtocolHandler::getGame(int gameId) const @@ -31,3 +93,438 @@ QPair Server_ProtocolHandler::getGame(int gameId return games.value(gameId); return QPair(0, 0); } + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdPing(Command_Ping *cmd) +{ + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdLogin(Command_Login *cmd) +{ + authState = server->checkUserPassword(cmd->getUsername(), cmd->getPassword()); + if (authState == PasswordWrong) + return ProtocolResponse::RespWrongPassword; + playerName = cmd->getUsername(); + +/* + Mit enqueueProtocolItem() lösen! + + QStringList loginMessage = server->getLoginMessage(); + for (int i = 0; i < loginMessage.size(); ++i) + msg("chat|server_message||" + loginMessage[i]); +*/ + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdChatListChannels(Command_ChatListChannels *cmd) +{ + QMapIterator channelIterator(server->getChatChannels()); + while (channelIterator.hasNext()) { + Server_ChatChannel *c = channelIterator.next().value(); +// msg(c->getChannelListLine()); + } + + acceptsChatChannelListChanges = true; + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdChatJoinChannel(Command_ChatJoinChannel *cmd) +{ + if (chatChannels.contains(cmd->getChannel())) + return ProtocolResponse::RespContextError; + + QMap allChannels = server->getChatChannels(); + Server_ChatChannel *c = allChannels.value(cmd->getChannel(), 0); + if (!c) + return ProtocolResponse::RespNameNotFound; + + // hier wird irgendwo ein Event erzeugt, das nach dem ok kommen muss -> evtl mit enqueue + c->addClient(this); + chatChannels.insert(cmd->getChannel(), c); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdChatLeaveChannel(Command_ChatLeaveChannel *cmd, Server_ChatChannel *channel) +{ + chatChannels.remove(channel->getName()); + channel->removeClient(this); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdChatSay(Command_ChatSay *cmd, Server_ChatChannel *channel) +{ + channel->say(this, cmd->getMessage()); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdListGames(Command_ListGames *cmd) +{ + const QList &gameList = server->getGames(); + for (int i = 0; i < gameList.size(); ++i) { +// msg(gameList[i]->getGameListLine()); + } + acceptsGameListChanges = true; + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdCreateGame(Command_CreateGame *cmd) +{ + Server_Game *game = server->createGame(cmd->getDescription(), cmd->getPassword(), cmd->getMaxPlayers(), cmd->getSpectatorsAllowed(), playerName); + games.insert(game->getGameId(), QPair(game, game->getCreator())); + + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdJoinGame(Command_JoinGame *cmd) +{ + Server_Game *g = server->getGame(cmd->getGameId()); + if (!g) + return ProtocolResponse::RespNameNotFound; + + ProtocolResponse::ResponseCode result = g->checkJoin(cmd->getPassword(), cmd->getSpectator()); + if (result == ProtocolResponse::RespOk) { + Server_Player *player = g->addPlayer(playerName, cmd->getSpectator()); + games.insert(cmd->getGameId(), QPair(g, player)); + } + return result; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdLeaveGame(Command_LeaveGame *cmd, Server_Game *game, Server_Player *player) +{ + game->removePlayer(player); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdSay(Command_Say *cmd, Server_Game *game, Server_Player *player) +{ + // XXX + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdShuffle(Command_Shuffle *cmd, Server_Game *game, Server_Player *player) +{ + player->getZones().value("deck")->shuffle(); +// game->broadcastEvent("shuffle", player); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdRollDie(Command_RollDie *cmd, Server_Game *game, Server_Player *player) +{ +// game->broadcastEvent(QString("roll_die|%1|%2").arg(sides).arg(rng->getNumber(1, sides)), player); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdDrawCards(Command_DrawCards *cmd, Server_Game *game, Server_Player *player) +{ + Server_CardZone *deck = player->getZones().value("deck"); + Server_CardZone *hand = player->getZones().value("hand"); + int number = cmd->getNumber(); + if (deck->cards.size() < number) + number = deck->cards.size(); + + for (int i = 0; i < number; ++i) { + Server_Card *card = deck->cards.takeFirst(); + hand->cards.append(card); +// player->privateEvent(QString("draw|%1|%2").arg(card->getId()).arg(card->getName())); + } + +// game->broadcastEvent(QString("draw|%1").arg(number), player); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdMoveCard(Command_MoveCard *cmd, Server_Game *game, Server_Player *player) +{ + // ID Karte, Startzone, Zielzone, Koordinaten X, Y, Facedown + Server_CardZone *startzone = player->getZones().value(cmd->getStartZone()); + Server_CardZone *targetzone = player->getZones().value(cmd->getTargetZone()); + if ((!startzone) || (!targetzone)) + return ProtocolResponse::RespNameNotFound; + + int position = -1; + Server_Card *card = startzone->getCard(cmd->getCardId(), true, &position); + if (!card) + return ProtocolResponse::RespNameNotFound; + int x = cmd->getX(); + if (x == -1) + x = targetzone->cards.size(); + int y = 0; + if (targetzone->hasCoords()) + y = cmd->getY(); + bool facedown = cmd->getFaceDown(); + + targetzone->insertCard(card, x, y); + + bool targetBeingLookedAt = (targetzone->getType() != Server_CardZone::HiddenZone) || (targetzone->getCardsBeingLookedAt() > x) || (targetzone->getCardsBeingLookedAt() == -1); + bool sourceBeingLookedAt = (startzone->getType() != Server_CardZone::HiddenZone) || (startzone->getCardsBeingLookedAt() > position) || (startzone->getCardsBeingLookedAt() == -1); + + bool targetHiddenToPlayer = facedown || !targetBeingLookedAt; + bool targetHiddenToOthers = facedown || (targetzone->getType() != Server_CardZone::PublicZone); + bool sourceHiddenToPlayer = card->getFaceDown() || !sourceBeingLookedAt; + bool sourceHiddenToOthers = card->getFaceDown() || (startzone->getType() != Server_CardZone::PublicZone); + + QString privateCardName, publicCardName; + if (!(sourceHiddenToPlayer && targetHiddenToPlayer)) + privateCardName = card->getName(); + if (!(sourceHiddenToOthers && targetHiddenToOthers)) + publicCardName = card->getName(); + + if (facedown) + card->setId(player->newCardId()); + card->setFaceDown(facedown); + + // The player does not get to see which card he moved if it moves between two parts of hidden zones which + // are not being looked at. + QString privateCardId = QString::number(card->getId()); + if (!targetBeingLookedAt && !sourceBeingLookedAt) { + privateCardId = QString(); + privateCardName = QString(); + } +/* player->privateEvent(QString("move_card|%1|%2|%3|%4|%5|%6|%7|%8").arg(privateCardId) + .arg(privateCardName) + .arg(startzone->getName()) + .arg(position) + .arg(targetzone->getName()) + .arg(x) + .arg(y) + .arg(facedown ? 1 : 0)); +*/ + // 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, + // all cards are equal. + if ((startzone->getType() == Server_CardZone::HiddenZone) && ((startzone->getCardsBeingLookedAt() > position) || (startzone->getCardsBeingLookedAt() == -1))) + position = -1; + if ((targetzone->getType() == Server_CardZone::HiddenZone) && ((targetzone->getCardsBeingLookedAt() > x) || (targetzone->getCardsBeingLookedAt() == -1))) + x = -1; + +/* if ((startzone->getType() == Server_CardZone::PublicZone) || (targetzone->getType() == Server_CardZone::PublicZone)) + game->broadcastEvent(QString("move_card|%1|%2|%3|%4|%5|%6|%7|%8").arg(card->getId()) + .arg(publicCardName) + .arg(startzone->getName()) + .arg(position) + .arg(targetzone->getName()) + .arg(x) + .arg(y) + .arg(facedown ? 1 : 0), player); + else + game->broadcastEvent(QString("move_card|||%1|%2|%3|%4|%5|0").arg(startzone->getName()) + .arg(position) + .arg(targetzone->getName()) + .arg(x) + .arg(y), player); +*/ + // If the card was moved to another zone, delete all arrows from and to the card + if (startzone != targetzone) { + const QList &players = game->getPlayers(); + for (int i = 0; i < players.size(); ++i) { + QList arrowsToDelete; + QMapIterator arrowIterator(players[i]->getArrows()); + while (arrowIterator.hasNext()) { + Server_Arrow *arrow = arrowIterator.next().value(); + if ((arrow->getStartCard() == card) || (arrow->getTargetCard() == card)) + arrowsToDelete.append(arrow->getId()); + } + for (int j = 0; j < arrowsToDelete.size(); ++j) + players[i]->deleteArrow(arrowsToDelete[j]); + } + } + + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdCreateToken(Command_CreateToken *cmd, Server_Game *game, Server_Player *player) +{ + // zone, cardname, powtough, x, y + // powtough wird erst mal ignoriert + Server_CardZone *zone = player->getZones().value(cmd->getZone()); + if (!zone) + return ProtocolResponse::RespNameNotFound; + + Server_Card *card = new Server_Card(cmd->getCardName(), player->newCardId(), cmd->getX(), cmd->getY()); + zone->insertCard(card, cmd->getX(), cmd->getY()); +/* game->broadcastEvent(QString("create_token|%1|%2|%3|%4|%5|%6").arg(zone->getName()) + .arg(cardid) + .arg(cardname) + .arg(powtough) + .arg(x) + .arg(y), player); +*/ return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdCreateArrow(Command_CreateArrow *cmd, Server_Game *game, Server_Player *player) +{ + Server_Player *startPlayer = game->getPlayer(cmd->getStartPlayerId()); + Server_Player *targetPlayer = game->getPlayer(cmd->getTargetPlayerId()); + if (!startPlayer || !targetPlayer) + return ProtocolResponse::RespNameNotFound; + Server_CardZone *startZone = startPlayer->getZones().value(cmd->getStartZone()); + Server_CardZone *targetZone = targetPlayer->getZones().value(cmd->getTargetZone()); + if (!startZone || !targetZone) + return ProtocolResponse::RespNameNotFound; + Server_Card *startCard = startZone->getCard(cmd->getStartCardId(), false); + Server_Card *targetCard = targetZone->getCard(cmd->getTargetCardId(), false); + if (!startCard || !targetCard || (startCard == targetCard)) + return ProtocolResponse::RespContextError; + QMapIterator arrowIterator(player->getArrows()); + while (arrowIterator.hasNext()) { + Server_Arrow *temp = arrowIterator.next().value(); + if ((temp->getStartCard() == startCard) && (temp->getTargetCard() == targetCard)) + return ProtocolResponse::RespContextError; + } + + Server_Arrow *arrow = new Server_Arrow(player->newArrowId(), startCard, targetCard, cmd->getColor()); + player->addArrow(arrow); +/* game->broadcastEvent(QString("create_arrow|%1|%2|%3|%4|%5|%6|%7|%8") + .arg(arrow->getId()) + .arg(startPlayer->getPlayerId()) + .arg(startZone->getName()) + .arg(startCard->getId()) + .arg(targetPlayer->getPlayerId()) + .arg(targetZone->getName()) + .arg(targetCard->getId()) + .arg(cmd->getColor()), player + ); +*/ return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdDeleteArrow(Command_DeleteArrow *cmd, Server_Game *game, Server_Player *player) +{ + if (!player->deleteArrow(cmd->getArrowId())) + return ProtocolResponse::RespNameNotFound; + +// game->broadcastEvent(QString("delete_arrow|%1").arg(arrowId), player); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdSetCardAttr(Command_SetCardAttr *cmd, Server_Game *game, Server_Player *player) +{ + // 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()); + if (!zone) + return ProtocolResponse::RespNameNotFound; + + if (cmd->getCardId() == -1) { + QListIterator CardIterator(zone->cards); + while (CardIterator.hasNext()) + if (!CardIterator.next()->setAttribute(cmd->getAttrName(), cmd->getAttrValue(), true)) + return ProtocolResponse::RespInvalidCommand; + } else { + Server_Card *card = zone->getCard(cmd->getCardId(), false); + if (!card) + return ProtocolResponse::RespNameNotFound; + if (!card->setAttribute(cmd->getAttrName(), cmd->getAttrValue(), false)) + return ProtocolResponse::RespInvalidCommand; + } +// game->broadcastEvent(QString("set_card_attr|%1|%2|%3|%4").arg(zone->getName()).arg(cardid).arg(aname).arg(avalue), player); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdReadyStart(Command_ReadyStart *cmd, Server_Game *game, Server_Player *player) +{ + player->setStatus(StatusReadyStart); +// game->broadcastEvent(QString("ready_start"), player); + game->startGameIfReady(); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdIncCounter(Command_IncCounter *cmd, Server_Game *game, Server_Player *player) +{ + const QMap counters = player->getCounters(); + Server_Counter *c = counters.value(cmd->getCounterId(), 0); + if (!c) + return ProtocolResponse::RespNameNotFound; + + c->setCount(c->getCount() + cmd->getDelta()); +// game->broadcastEvent(QString("set_counter|%1|%2").arg(c->getId()).arg(c->getCount()), player); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdAddCounter(Command_AddCounter *cmd, Server_Game *game, Server_Player *player) +{ + Server_Counter *c = new Server_Counter(player->newCounterId(), cmd->getCounterName(), cmd->getColor(), cmd->getRadius(), cmd->getValue()); + player->addCounter(c); +// game->broadcastEvent(QString("add_counter|%1|%2|%3|%4|%5").arg(c->getId()).arg(c->getName()).arg(color).arg(radius).arg(count), player); + + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdSetCounter(Command_SetCounter *cmd, Server_Game *game, Server_Player *player) +{ + Server_Counter *c = player->getCounters().value(cmd->getCounterId(), 0);; + if (!c) + return ProtocolResponse::RespNameNotFound; + + c->setCount(cmd->getValue()); +// game->broadcastEvent(QString("set_counter|%1|%2").arg(c->getId()).arg(count), player); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdDelCounter(Command_DelCounter *cmd, Server_Game *game, Server_Player *player) +{ + if (!player->deleteCounter(cmd->getCounterId())) + return ProtocolResponse::RespNameNotFound; +// game->broadcastEvent(QString("del_counter|%1").arg(counterId), player); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdNextTurn(Command_NextTurn * /*cmd*/, Server_Game *game, Server_Player * /*player*/) +{ + int activePlayer = game->getActivePlayer(); + if (++activePlayer == game->getPlayerCount()) + activePlayer = 0; + game->setActivePlayer(activePlayer); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdSetActivePhase(Command_SetActivePhase *cmd, Server_Game *game, Server_Player *player) +{ + if (game->getActivePlayer() != player->getPlayerId()) + return ProtocolResponse::RespContextError; + game->setActivePhase(cmd->getPhase()); + + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdDumpZone(Command_DumpZone *cmd, Server_Game *game, Server_Player *player) +{ + Server_Player *otherPlayer = game->getPlayer(cmd->getPlayerId()); + if (!otherPlayer) + return ProtocolResponse::RespNameNotFound; + Server_CardZone *zone = otherPlayer->getZones().value(cmd->getZoneName()); + if (!zone) + return ProtocolResponse::RespNameNotFound; + if (!((zone->getType() == Server_CardZone::PublicZone) || (player == otherPlayer))) + return ProtocolResponse::RespContextError; + + if (zone->getType() == Server_CardZone::HiddenZone) { +// game->broadcastEvent(QString("dump_zone|%1|%2|%3").arg(player_id).arg(zone->getName()).arg(number_cards), player); + } +// remsg->sendList(dumpZoneHelper(otherPlayer, zone, number_cards)); + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdStopDumpZone(Command_StopDumpZone *cmd, Server_Game *game, Server_Player *player) +{ + Server_Player *otherPlayer = game->getPlayer(cmd->getPlayerId()); + if (!otherPlayer) + return ProtocolResponse::RespNameNotFound; + Server_CardZone *zone = otherPlayer->getZones().value(cmd->getZoneName()); + if (!zone) + return ProtocolResponse::RespNameNotFound; + + if (zone->getType() == Server_CardZone::HiddenZone) { + zone->setCardsBeingLookedAt(0); +// game->broadcastEvent(QString("stop_dump_zone|%1|%2").arg(otherPlayer->getPlayerId()).arg(zone->getName()), player); + } + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdDumpAll(Command_DumpAll *cmd, Server_Game *game, Server_Player *player) +{ + return ProtocolResponse::RespOk; +} + +ProtocolResponse::ResponseCode Server_ProtocolHandler::cmdSubmitDeck(Command_SubmitDeck *cmd, Server_Game *game, Server_Player *player) +{ + return ProtocolResponse::RespOk; +} diff --git a/common/server_protocolhandler.h b/common/server_protocolhandler.h index b8eccba3..bdce286f 100644 --- a/common/server_protocolhandler.h +++ b/common/server_protocolhandler.h @@ -4,10 +4,10 @@ #include #include #include "server.h" +#include "protocol.h" +#include "protocol_items.h" class Server_Player; -class Command; -class ProtocolItem; class Server_ProtocolHandler : public QObject { Q_OBJECT @@ -23,6 +23,40 @@ private: AuthenticationResult authState; bool acceptsGameListChanges; bool acceptsChatChannelListChanges; + + ProtocolResponse::ResponseCode cmdPing(Command_Ping *cmd); + ProtocolResponse::ResponseCode cmdLogin(Command_Login *cmd); + ProtocolResponse::ResponseCode cmdChatListChannels(Command_ChatListChannels *cmd); + ProtocolResponse::ResponseCode cmdChatJoinChannel(Command_ChatJoinChannel *cmd); + ProtocolResponse::ResponseCode cmdChatLeaveChannel(Command_ChatLeaveChannel *cmd, Server_ChatChannel *channel); + ProtocolResponse::ResponseCode cmdChatSay(Command_ChatSay *cmd, Server_ChatChannel *channel); + ProtocolResponse::ResponseCode cmdListGames(Command_ListGames *cmd); + ProtocolResponse::ResponseCode cmdCreateGame(Command_CreateGame *cmd); + ProtocolResponse::ResponseCode cmdJoinGame(Command_JoinGame *cmd); + ProtocolResponse::ResponseCode cmdLeaveGame(Command_LeaveGame *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdSay(Command_Say *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdShuffle(Command_Shuffle *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdRollDie(Command_RollDie *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdDrawCards(Command_DrawCards *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdMoveCard(Command_MoveCard *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdCreateToken(Command_CreateToken *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdCreateArrow(Command_CreateArrow *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdDeleteArrow(Command_DeleteArrow *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdSetCardAttr(Command_SetCardAttr *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdReadyStart(Command_ReadyStart *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdIncCounter(Command_IncCounter *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdAddCounter(Command_AddCounter *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdSetCounter(Command_SetCounter *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdDelCounter(Command_DelCounter *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdNextTurn(Command_NextTurn *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdSetActivePhase(Command_SetActivePhase *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdDumpZone(Command_DumpZone *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdStopDumpZone(Command_StopDumpZone *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdDumpAll(Command_DumpAll *cmd, Server_Game *game, Server_Player *player); + ProtocolResponse::ResponseCode cmdSubmitDeck(Command_SubmitDeck *cmd, Server_Game *game, Server_Player *player); + + QList itemQueue; + void enqueueProtocolItem(ProtocolItem *item); public: Server_ProtocolHandler(Server *_server, QObject *parent = 0); ~Server_ProtocolHandler(); diff --git a/servatrice/servatrice.pro b/servatrice/servatrice.pro index c705c8ef..914e585f 100755 --- a/servatrice/servatrice.pro +++ b/servatrice/servatrice.pro @@ -15,7 +15,6 @@ QT -= gui HEADERS += src/servatrice.h \ src/serversocketinterface.h \ - src/version.h \ ../common/protocol.h \ ../common/protocol_items.h \ ../common/rng_abstract.h \ diff --git a/servatrice/src/returnmessage.cpp b/servatrice/src/returnmessage.cpp deleted file mode 100644 index 0d3c92f5..00000000 --- a/servatrice/src/returnmessage.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "returnmessage.h" -#include "serversocket.h" - -bool ReturnMessage::send(ReturnCode code) -{ - ServerSocket *s = qobject_cast(parent()); - if (!s) - return false; - QString returnCodeString; - switch (code) { - case ReturnNothing: return true; - case ReturnOk: returnCodeString = "ok"; break; - case ReturnNameNotFound: returnCodeString = "name_not_found"; break; - case ReturnLoginNeeded: returnCodeString = "login_needed"; break; - case ReturnSyntaxError: returnCodeString = "syntax"; break; - case ReturnContextError: returnCodeString = "context"; break; - case ReturnPasswordWrong: returnCodeString = "password"; break; - case ReturnSpectatorsNotAllowed: returnCodeString = "spectators_not_allowed"; break; - } - s->msg(QString("resp|%1|%2").arg(msg_id) - .arg(returnCodeString)); - return (code == ReturnOk); -} - -bool ReturnMessage::sendList(const QStringList &args, const QString &prefix) -{ - ServerSocket *s = qobject_cast(parent()); - if (!s) - return false; - - QString arg1 = prefix.isEmpty() ? cmd : prefix; - for (int i = 0; i < args.size(); i++) - s->msg(QString("%1|%2|%3").arg(arg1) - .arg(msg_id) - .arg(args[i])); - return true; -} diff --git a/servatrice/src/returnmessage.h b/servatrice/src/returnmessage.h deleted file mode 100644 index 12f2b015..00000000 --- a/servatrice/src/returnmessage.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef RETURNMESSAGE_H -#define RETURNMESSAGE_H - -#include - -class ReturnMessage : public QObject { - Q_OBJECT -private: - unsigned int msg_id; - QString cmd; -public: - enum ReturnCode { ReturnNothing, ReturnOk, ReturnNameNotFound, ReturnLoginNeeded, ReturnSyntaxError, ReturnContextError, ReturnPasswordWrong, ReturnSpectatorsNotAllowed }; - ReturnMessage(QObject *parent = 0) : QObject(parent), msg_id(0) { } - unsigned int getMsgId() const { return msg_id; } - void setMsgId(unsigned int _msg_id) { msg_id = _msg_id; } - void setCmd(const QString &_cmd) { cmd = _cmd; } - bool send(ReturnCode code); - bool sendList(const QStringList &args, const QString &prefix = QString()); -}; - -#endif diff --git a/servatrice/src/servatrice.cpp b/servatrice/src/servatrice.cpp index 10f25bfa..91e7deba 100644 --- a/servatrice/src/servatrice.cpp +++ b/servatrice/src/servatrice.cpp @@ -22,10 +22,13 @@ #include "servatrice.h" #include "server_chatchannel.h" #include "serversocketinterface.h" +#include "protocol.h" Servatrice::Servatrice(QObject *parent) : Server(parent) { + ProtocolItem::initializeHash(); + tcpServer = new QTcpServer(this); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection())); tcpServer->listen(QHostAddress::Any, 4747); // XXX make customizable diff --git a/servatrice/src/serversocketinterface.cpp b/servatrice/src/serversocketinterface.cpp index 4d797edb..6d0dfff2 100644 --- a/servatrice/src/serversocketinterface.cpp +++ b/servatrice/src/serversocketinterface.cpp @@ -26,7 +26,7 @@ #include "protocol_items.h" ServerSocketInterface::ServerSocketInterface(Server *_server, QTcpSocket *_socket, QObject *parent) - : Server_ProtocolHandler(_server, parent), socket(_socket) + : Server_ProtocolHandler(_server, parent), socket(_socket), currentItem(0) { xmlWriter = new QXmlStreamWriter; xmlWriter->setDevice(socket); @@ -39,7 +39,7 @@ ServerSocketInterface::ServerSocketInterface(Server *_server, QTcpSocket *_socke connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(catchSocketError(QAbstractSocket::SocketError))); xmlWriter->writeStartDocument(); - xmlWriter->writeStartElement("cockatrice_communication"); + xmlWriter->writeStartElement("cockatrice_server_stream"); xmlWriter->writeAttribute("version", QString::number(ProtocolItem::protocolVersion)); sendProtocolItem(new Event_Welcome(Servatrice::versionString)); @@ -64,32 +64,38 @@ ServerSocketInterface::~ServerSocketInterface() void ServerSocketInterface::readClient() { + qDebug() << "readClient"; xmlReader->addData(socket->readAll()); - while (canReadLine()) { - QString line = QString(readLine()).trimmed(); - if (line.isNull()) - break; - - qDebug(QString("<<< %1").arg(line).toLatin1()); -/* switch (PlayerStatus) { - case StatusNormal: - case StatusReadyStart: - case StatusPlaying: - parseCommand(line); - break; - case StatusSubmitDeck: - QString card = line; - if (card == ".") { - PlayerStatus = StatusNormal; - remsg->send(ReturnMessage::ReturnOk); - } else if (card.startsWith("SB:")) - SideboardList << card.mid(3); + if (currentItem) { + if (!currentItem->read(xmlReader)) + return; + currentItem = 0; + } + while (!xmlReader->atEnd()) { + xmlReader->readNext(); + if (xmlReader->isStartElement()) { + QString itemType = xmlReader->name().toString(); + if (itemType == "cockatrice_client_stream") + continue; + QString itemName = xmlReader->attributes().value("name").toString(); + qDebug() << "parseXml: startElement: " << "type =" << itemType << ", name =" << itemName; + currentItem = ProtocolItem::getNewItem(itemType + itemName); + if (!currentItem) + currentItem = new InvalidCommand; + if (!currentItem->read(xmlReader)) + return; + else { + Command *command = qobject_cast(currentItem); + if (qobject_cast(command)) + sendProtocolItem(new ProtocolResponse(command->getCmdId(), ProtocolResponse::RespInvalidCommand)); else - DeckList << card; + processCommand(command); + currentItem = 0; + } } } -*/} +} void ServerSocketInterface::catchSocketError(QAbstractSocket::SocketError socketError) { diff --git a/servatrice/src/serversocketinterface.h b/servatrice/src/serversocketinterface.h index c496e36b..b5a044cf 100644 --- a/servatrice/src/serversocketinterface.h +++ b/servatrice/src/serversocketinterface.h @@ -21,7 +21,7 @@ #define SERVERSOCKETINTERFACE_H #include -#include +#include "server_protocolhandler.h" class QTcpSocket; class Server; @@ -38,6 +38,7 @@ private: QTcpSocket *socket; QXmlStreamWriter *xmlWriter; QXmlStreamReader *xmlReader; + ProtocolItem *currentItem; public: ServerSocketInterface(Server *_server, QTcpSocket *_socket, QObject *parent = 0); ~ServerSocketInterface(); diff --git a/servatrice/src/version.h b/servatrice/src/version.h deleted file mode 100644 index 65088a48..00000000 --- a/servatrice/src/version.h +++ /dev/null @@ -1,22 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2008 by Max-Wilhelm Bruker * - * brukie@laptop * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - ***************************************************************************/ - -static const int PROTOCOL_VERSION = 2; -static const char *VERSION_STRING = "Servatrice 0.20091017";