diff --git a/common/pb/proto/event_create_arrow.proto b/common/pb/proto/event_create_arrow.proto new file mode 100644 index 00000000..6cbd04f3 --- /dev/null +++ b/common/pb/proto/event_create_arrow.proto @@ -0,0 +1,9 @@ +import "game_event.proto"; +import "serverinfo_arrow.proto"; + +message Event_CreateArrow { + extend GameEvent { + optional Event_CreateArrow ext = 2000; + } + optional ServerInfo_Arrow arrow_info = 1; +} diff --git a/common/pb/proto/event_create_arrows.proto b/common/pb/proto/event_create_arrows.proto deleted file mode 100644 index 219bb9d0..00000000 --- a/common/pb/proto/event_create_arrows.proto +++ /dev/null @@ -1,9 +0,0 @@ -import "game_event.proto"; -import "serverinfo_arrow.proto"; - -message Event_CreateArrows { - extend GameEvent { - optional Event_CreateArrows ext = 2000; - } - repeated ServerInfo_Arrow arrow_list = 1; -} diff --git a/common/pb/proto/event_create_counter.proto b/common/pb/proto/event_create_counter.proto new file mode 100644 index 00000000..09499964 --- /dev/null +++ b/common/pb/proto/event_create_counter.proto @@ -0,0 +1,9 @@ +import "game_event.proto"; +import "serverinfo_counter.proto"; + +message Event_CreateCounter { + extend GameEvent { + optional Event_CreateCounter ext = 2002; + } + optional ServerInfo_Counter counter_info = 1; +} diff --git a/common/pb/proto/event_create_counters.proto b/common/pb/proto/event_create_counters.proto deleted file mode 100644 index 904a5dbe..00000000 --- a/common/pb/proto/event_create_counters.proto +++ /dev/null @@ -1,9 +0,0 @@ -import "game_event.proto"; -import "serverinfo_counter.proto"; - -message Event_CreateCounters { - extend GameEvent { - optional Event_CreateCounters ext = 2002; - } - repeated ServerInfo_Counter counter_list = 1; -} diff --git a/common/pb/proto/game_event.proto b/common/pb/proto/game_event.proto index 59aa740c..d0d165b4 100644 --- a/common/pb/proto/game_event.proto +++ b/common/pb/proto/game_event.proto @@ -10,9 +10,9 @@ message GameEvent { PLAYER_PROPERTIES_CHANGED = 1007; PING = 1008; GAME_SAY = 1009; - CREATE_ARROWS = 2000; + CREATE_ARROW = 2000; DELETE_ARROW = 2001; - CREATE_COUNTERS = 2002; + CREATE_COUNTER = 2002; SET_COUNTER = 2003; DEL_COUNTER = 2004; DRAW_CARDS = 2005; diff --git a/common/pb/proto/response_deck_download.proto b/common/pb/proto/response_deck_download.proto new file mode 100644 index 00000000..804ba47f --- /dev/null +++ b/common/pb/proto/response_deck_download.proto @@ -0,0 +1,8 @@ +import "response.proto"; + +message Response_DeckDownload { + extend Response { + optional Response_DeckDownload ext = 1007; + } + optional string deck = 1; +} diff --git a/common/pb/proto/server_message.proto b/common/pb/proto/server_message.proto index 43ed7142..b1905598 100644 --- a/common/pb/proto/server_message.proto +++ b/common/pb/proto/server_message.proto @@ -10,8 +10,10 @@ message ServerMessage { GAME_EVENT_CONTAINER = 2; ROOM_EVENT = 3; } - optional Response response = 1; - optional SessionEvent session_event = 2; - optional GameEventContainer game_event_container = 3; - optional RoomEvent room_event = 4; + optional MessageType message_type = 1; + + optional Response response = 2; + optional SessionEvent session_event = 3; + optional GameEventContainer game_event_container = 4; + optional RoomEvent room_event = 5; } diff --git a/common/pb/proto/serverinfo_card.proto b/common/pb/proto/serverinfo_card.proto index 7777bd46..b628a271 100644 --- a/common/pb/proto/serverinfo_card.proto +++ b/common/pb/proto/serverinfo_card.proto @@ -1,10 +1,10 @@ import "serverinfo_cardcounter.proto"; message ServerInfo_Card { - optional sint32 id = 1; + optional sint32 id = 1 [default = -1]; optional string name = 2; - optional sint32 x = 3; - optional sint32 y = 4; + optional sint32 x = 3 [default = -1]; + optional sint32 y = 4 [default = -1]; optional bool face_down = 5; optional bool tapped = 6; optional bool attacking = 7; @@ -14,7 +14,7 @@ message ServerInfo_Card { optional bool destroy_on_zone_change = 11; optional bool doesnt_untap = 12; repeated ServerInfo_CardCounter counter_list = 13; - optional sint32 attach_player_id = 14; + optional sint32 attach_player_id = 14 [default = -1]; optional string attach_zone = 15; - optional sint32 attach_card_id = 16; + optional sint32 attach_card_id = 16 [default = -1]; } diff --git a/common/pb/proto/serverinfo_zone.proto b/common/pb/proto/serverinfo_zone.proto index 70c007f4..805a7d05 100644 --- a/common/pb/proto/serverinfo_zone.proto +++ b/common/pb/proto/serverinfo_zone.proto @@ -2,6 +2,15 @@ import "serverinfo_card.proto"; message ServerInfo_Zone { enum ZoneType { + // PrivateZone: Contents of the zone are always visible to the owner, + // but not to anyone else. + // PublicZone: Contents of the zone are always visible to anyone. + // HiddenZone: Contents of the zone are never visible to anyone. + // However, the owner of the zone can issue a dump_zone command, + // setting beingLookedAt to true. + // Cards in a zone with the type HiddenZone are referenced by their + // list index, whereas cards in any other zone are referenced by their ids. + PrivateZone = 0; PublicZone = 1; HiddenZone = 2; diff --git a/common/protocol.h b/common/protocol.h index 3bb09d5d..5c58aad8 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -6,7 +6,11 @@ #include #include #include -#include "protocol_datastructures.h" +#include "serializable_item.h" +#include + +#include +#include "pb/server_message.pb.h" class QXmlStreamReader; class QXmlStreamWriter; @@ -22,11 +26,9 @@ class MoveCardToZone; class ProtocolItem : public SerializableItem_Map { Q_OBJECT private: - static void initializeHashAuto(); bool receiverMayDelete; public: static const int protocolVersion = 13; - static void initializeHash(); virtual int getItemId() const = 0; bool getReceiverMayDelete() const { return receiverMayDelete; } void setReceiverMayDelete(bool _receiverMayDelete) { receiverMayDelete = _receiverMayDelete; } @@ -38,32 +40,49 @@ public: // --- COMMANDS --- // ---------------- -class BlaContainer : public ProtocolItem { - Q_OBJECT +class GameEventStorage { private: - ProtocolResponse *resp; - QList itemQueue; - GameEventContext *gameEventContext; + ::google::protobuf::Message *gameEventContext; GameEventContainer *gameEventQueuePublic; GameEventContainer *gameEventQueueOmniscient; GameEventContainer *gameEventQueuePrivate; int privatePlayerId; public: - BlaContainer(); - int getItemId() const { return 102332456; } + GameEventStorage(); + ~GameEventStorage(); + + void setGameEventContext(::google::protobuf::Message *_gameEventContext) { gameEventContext = _gameEventContext; } + ::google::protobuf::Message *getGameEventContext() const { return gameEventContext; } - ProtocolResponse *getResponse() const { return resp; } - void setResponse(ProtocolResponse *_resp); - const QList &getItemQueue() const { return itemQueue; } - void enqueueItem(ProtocolItem *item) { itemQueue.append(item); } GameEventContainer *getGameEventQueuePublic() const { return gameEventQueuePublic; } - void enqueueGameEventPublic(GameEvent *event, int gameId, GameEventContext *context = 0); + void enqueueGameEventPublic(const ::google::protobuf::Message &event, int playerId); + GameEventContainer *getGameEventQueueOmniscient() const { return gameEventQueueOmniscient; } - void enqueueGameEventOmniscient(GameEvent *event, int gameId, GameEventContext *context = 0); + void enqueueGameEventOmniscient(const ::google::protobuf::Message &event, int playerId); + GameEventContainer *getGameEventQueuePrivate() const { return gameEventQueuePrivate; } - void enqueueGameEventPrivate(GameEvent *event, int gameId, int playerId = -1, GameEventContext *context = 0); + void enqueueGameEventPrivate(const ::google::protobuf::Message &event, int playerId); + // XXX - DRAN DENKEN, dass privatePlayerId gesetzt wird int getPrivatePlayerId() const { return privatePlayerId; } + + void enqueueGameEvent(const ::google::protobuf::Message &event, int playerId); }; + +class ResponseContainer { +private: + ::google::protobuf::Message *responseExtension; + QList > preResponseQueue, postResponseQueue; +public: + ResponseContainer() : responseExtension(0) { } + ~ResponseContainer() { /* XXX responseExtension und Inhalt beider Listen löschen */ } + void setResponseExtension(::google::protobuf::Message *_responseExtension) { responseExtension = _responseExtension; } + ::google::protobuf::Message *getResponseExtension() const { return responseExtension; } + void enqueuePreResponseItem(ServerMessage::MessageType type, ::google::protobuf::Message *item) { preResponseQueue.append(qMakePair(type, item)); } + void enqueuePostResponseItem(ServerMessage::MessageType type, ::google::protobuf::Message *item) { postResponseQueue.append(qMakePair(type, item)); } + const QList > &getPreResponseQueue() const { return preResponseQueue; } + const QList > &getPostResponseQueue() const { return postResponseQueue; } +}; + /* * XXX * diff --git a/common/protocol_datastructures.cpp b/common/protocol_datastructures.cpp deleted file mode 100644 index 5c8e4302..00000000 --- a/common/protocol_datastructures.cpp +++ /dev/null @@ -1,283 +0,0 @@ -#include "protocol_datastructures.h" -#include "decklist.h" -#include -#include - -ServerInfo_User::ServerInfo_User(const QString &_name, int _userLevel, const QString &_address, const QString &_realName, Gender _gender, const QString &_country, const QByteArray &_avatarBmp) - : SerializableItem_Map("user") -{ - insertItem(new SerializableItem_String("name", _name)); - insertItem(new SerializableItem_Int("userlevel", _userLevel)); - insertItem(new SerializableItem_String("address", _address)); - insertItem(new SerializableItem_String("real_name", _realName)); - insertItem(new SerializableItem_Int("gender", _gender)); - insertItem(new SerializableItem_String("country", _country)); - insertItem(new SerializableItem_ByteArray("avatar_bmp", _avatarBmp)); -} - -ServerInfo_User::ServerInfo_User(const ServerInfo_User *other, bool complete, bool moderatorInfo) - : SerializableItem_Map("user") -{ - insertItem(new SerializableItem_String("name", other->getName())); - insertItem(new SerializableItem_Int("userlevel", other->getUserLevel())); - insertItem(new SerializableItem_String("address", moderatorInfo ? other->getAddress() : QString())); - insertItem(new SerializableItem_String("real_name", other->getRealName())); - insertItem(new SerializableItem_Int("gender", other->getGender())); - insertItem(new SerializableItem_String("country", other->getCountry())); - insertItem(new SerializableItem_ByteArray("avatar_bmp", complete ? other->getAvatarBmp() : QByteArray())); -} - -ServerInfo_UserList::ServerInfo_UserList(const QString &_itemType, const QList &_userList) - : SerializableItem_Map(_itemType) -{ - for (int i = 0; i < _userList.size(); ++i) - itemList.append(_userList[i]); -} - -ServerInfo_Game::ServerInfo_Game(int _roomId, int _gameId, const QString &_description, bool _hasPassword, int _playerCount, int _maxPlayers, bool _started, const QList &_gameTypes, ServerInfo_User *_creatorInfo, bool _onlyBuddies, bool _onlyRegistered, bool _spectatorsAllowed, bool _spectatorsNeedPassword, int _spectatorCount) - : SerializableItem_Map("game") -{ - insertItem(new SerializableItem_Int("room_id", _roomId)); - insertItem(new SerializableItem_Int("game_id", _gameId)); - insertItem(new SerializableItem_String("description", _description)); - insertItem(new SerializableItem_Bool("has_password", _hasPassword)); - insertItem(new SerializableItem_Int("player_count", _playerCount)); - insertItem(new SerializableItem_Int("max_players", _maxPlayers)); - insertItem(new SerializableItem_Bool("started", _started)); - if (!_creatorInfo) - _creatorInfo = new ServerInfo_User; - insertItem(_creatorInfo); - insertItem(new SerializableItem_Bool("only_buddies", _onlyBuddies)); - insertItem(new SerializableItem_Bool("only_registered", _onlyRegistered)); - insertItem(new SerializableItem_Bool("spectators_allowed", _spectatorsAllowed)); - insertItem(new SerializableItem_Bool("spectators_need_password", _spectatorsNeedPassword)); - insertItem(new SerializableItem_Int("spectator_count", _spectatorCount)); - - for (int i = 0; i < _gameTypes.size(); ++i) - itemList.append(_gameTypes[i]); -} - -ServerInfo_GameType::ServerInfo_GameType(int _gameTypeId, const QString &_description) - : SerializableItem_Map("game_type") -{ - insertItem(new SerializableItem_Int("game_type_id", _gameTypeId)); - insertItem(new SerializableItem_String("description", _description)); -} - -ServerInfo_Room::ServerInfo_Room(int _roomId, const QString &_name, const QString &_description, int _gameCount, int _playerCount, bool _autoJoin, const QList &_gameList, const QList &_userList, const QList &_gameTypeList) - : SerializableItem_Map("room") -{ - insertItem(new SerializableItem_Int("room_id", _roomId)); - insertItem(new SerializableItem_String("name", _name)); - insertItem(new SerializableItem_String("description", _description)); - insertItem(new SerializableItem_Int("game_count", _gameCount)); - insertItem(new SerializableItem_Int("player_count", _playerCount)); - insertItem(new SerializableItem_Bool("auto_join", _autoJoin)); - - gameList = _gameList; - for (int i = 0; i < _gameList.size(); ++i) - itemList.append(_gameList[i]); - userList = _userList; - for (int i = 0; i < _userList.size(); ++i) - itemList.append(_userList[i]); - gameTypeList = _gameTypeList; - for (int i = 0; i < _gameTypeList.size(); ++i) - itemList.append(_gameTypeList[i]); -} - -void ServerInfo_Room::extractData() -{ - for (int i = 0; i < itemList.size(); ++i) { - ServerInfo_User *user = dynamic_cast(itemList[i]); - if (user) { - userList.append(user); - continue; - } - ServerInfo_Game *game = dynamic_cast(itemList[i]); - if (game) { - gameList.append(game); - continue; - } - ServerInfo_GameType *gameType = dynamic_cast(itemList[i]); - if (gameType) { - gameTypeList.append(gameType); - continue; - } - } -} - -ServerInfo_CardCounter::ServerInfo_CardCounter(int _id, int _value) - : SerializableItem_Map("card_counter") -{ - insertItem(new SerializableItem_Int("id", _id)); - insertItem(new SerializableItem_Int("value", _value)); -} - -ServerInfo_Card::ServerInfo_Card(int _id, const QString &_name, int _x, int _y, bool _faceDown, bool _tapped, bool _attacking, const QString &_color, const QString &_pt, const QString &_annotation, bool _destroyOnZoneChange, bool _doesntUntap, const QList &_counters, int _attachPlayerId, const QString &_attachZone, int _attachCardId) - : SerializableItem_Map("card") -{ - insertItem(new SerializableItem_Int("id", _id)); - insertItem(new SerializableItem_String("name", _name)); - insertItem(new SerializableItem_Int("x", _x)); - insertItem(new SerializableItem_Int("y", _y)); - insertItem(new SerializableItem_Bool("facedown", _faceDown)); - insertItem(new SerializableItem_Bool("tapped", _tapped)); - insertItem(new SerializableItem_Bool("attacking", _attacking)); - insertItem(new SerializableItem_String("color", _color)); - insertItem(new SerializableItem_String("pt", _pt)); - insertItem(new SerializableItem_String("annotation", _annotation)); - insertItem(new SerializableItem_Bool("destroy_on_zone_change", _destroyOnZoneChange)); - insertItem(new SerializableItem_Bool("doesnt_untap", _doesntUntap)); - insertItem(new SerializableItem_Int("attach_player_id", _attachPlayerId)); - insertItem(new SerializableItem_String("attach_zone", _attachZone)); - insertItem(new SerializableItem_Int("attach_card_id", _attachCardId)); - - for (int i = 0; i < _counters.size(); ++i) - itemList.append(_counters[i]); -} - -ServerInfo_Zone::ServerInfo_Zone(const QString &_name, ZoneType _type, bool _hasCoords, int _cardCount, const QList &_cardList) - : SerializableItem_Map("zone") -{ - insertItem(new SerializableItem_String("name", _name)); - insertItem(new SerializableItem_String("zone_type", typeToString(_type))); - insertItem(new SerializableItem_Bool("has_coords", _hasCoords)); - insertItem(new SerializableItem_Int("card_count", _cardCount)); - - for (int i = 0; i < _cardList.size(); ++i) - itemList.append(_cardList[i]); -} - -ZoneType ServerInfo_Zone::typeFromString(const QString &type) const -{ - if (type == "private") - return PrivateZone; - else if (type == "hidden") - return HiddenZone; - return PublicZone; -} - -QString ServerInfo_Zone::typeToString(ZoneType type) const -{ - switch (type) { - case PrivateZone: return "private"; - case HiddenZone: return "hidden"; - default: return "public"; - } -} - -QList ServerInfo_Zone::getCardList() const -{ - QList result; - for (int i = 0; i < itemList.size(); ++i) { - ServerInfo_Card *card = dynamic_cast(itemList[i]); - if (card) - result.append(card); - } - return result; -} - -ServerInfo_Counter::ServerInfo_Counter(int _id, const QString &_name, const Color &_color, int _radius, int _count) - : SerializableItem_Map("counter") -{ - insertItem(new SerializableItem_Int("id", _id)); - insertItem(new SerializableItem_String("name", _name)); - insertItem(new SerializableItem_Color("color", _color)); - insertItem(new SerializableItem_Int("radius", _radius)); - insertItem(new SerializableItem_Int("count", _count)); -} - -ServerInfo_Arrow::ServerInfo_Arrow(int _id, int _startPlayerId, const QString &_startZone, int _startCardId, int _targetPlayerId, const QString &_targetZone, int _targetCardId, const Color &_color) - : SerializableItem_Map("arrow") -{ - insertItem(new SerializableItem_Int("id", _id)); - insertItem(new SerializableItem_Int("start_player_id", _startPlayerId)); - insertItem(new SerializableItem_String("start_zone", _startZone)); - insertItem(new SerializableItem_Int("start_card_id", _startCardId)); - insertItem(new SerializableItem_Int("target_player_id", _targetPlayerId)); - insertItem(new SerializableItem_String("target_zone", _targetZone)); - insertItem(new SerializableItem_Int("target_card_id", _targetCardId)); - insertItem(new SerializableItem_Color("color", _color)); -} - -ServerInfo_PlayerProperties::ServerInfo_PlayerProperties(int _playerId, ServerInfo_User *_userInfo, bool _spectator, bool _conceded, bool _readyStart, const QString &_deckHash) - : SerializableItem_Map("player_properties") -{ - insertItem(new SerializableItem_Int("player_id", _playerId)); - if (!_userInfo) - _userInfo = new ServerInfo_User; - insertItem(_userInfo); - insertItem(new SerializableItem_Bool("spectator", _spectator)); - insertItem(new SerializableItem_Bool("conceded", _conceded)); - insertItem(new SerializableItem_Bool("ready_start", _readyStart)); - insertItem(new SerializableItem_String("deck_hash", _deckHash)); -} - -ServerInfo_Player::ServerInfo_Player(ServerInfo_PlayerProperties *_properties, DeckList *_deck, const QList &_zoneList, const QList &_counterList, const QList &_arrowList) - : SerializableItem_Map("player"), zoneList(_zoneList), counterList(_counterList), arrowList(_arrowList) -{ - if (!_properties) - insertItem(new ServerInfo_PlayerProperties); - else - insertItem(_properties); - - if (!_deck) - insertItem(new DeckList); - else - insertItem(new DeckList(_deck)); - - zoneList = _zoneList; - for (int i = 0; i < _zoneList.size(); ++i) - itemList.append(_zoneList[i]); - counterList = _counterList; - for (int i = 0; i < _counterList.size(); ++i) - itemList.append(_counterList[i]); - arrowList = _arrowList; - for (int i = 0; i < _arrowList.size(); ++i) - itemList.append(_arrowList[i]); -} - -void ServerInfo_Player::extractData() -{ - for (int i = 0; i < itemList.size(); ++i) { - ServerInfo_Zone *zone = dynamic_cast(itemList[i]); - ServerInfo_Counter *counter = dynamic_cast(itemList[i]); - ServerInfo_Arrow *arrow = dynamic_cast(itemList[i]); - if (zone) - zoneList.append(zone); - else if (counter) - counterList.append(counter); - else if (arrow) - arrowList.append(arrow); - } -} - -DeckList *ServerInfo_Player::getDeck() const -{ - return static_cast(itemMap.value("cockatrice_deck")); -} - -ServerInfo_PlayerPing::ServerInfo_PlayerPing(int _playerId, int _pingTime) - : SerializableItem_Map("player_ping") -{ - insertItem(new SerializableItem_Int("player_id", _playerId)); - insertItem(new SerializableItem_Int("ping_time", _pingTime)); -} - -DeckList_TreeItem::DeckList_TreeItem(const QString &_itemType, const QString &_name, int _id) - : SerializableItem_Map(_itemType) -{ - insertItem(new SerializableItem_String("name", _name)); - insertItem(new SerializableItem_Int("id", _id)); -} - -DeckList_File::DeckList_File(const QString &_name, int _id, QDateTime _uploadTime) - : DeckList_TreeItem("file", _name, _id) -{ - insertItem(new SerializableItem_DateTime("upload_time", _uploadTime)); -} - -DeckList_Directory::DeckList_Directory(const QString &_name, int _id) - : DeckList_TreeItem("directory", _name, _id) -{ -} diff --git a/common/protocol_datastructures.h b/common/protocol_datastructures.h deleted file mode 100644 index b26d5f1a..00000000 --- a/common/protocol_datastructures.h +++ /dev/null @@ -1,240 +0,0 @@ -#ifndef PROTOCOL_DATASTRUCTURES_H -#define PROTOCOL_DATASTRUCTURES_H - -#include -#include -#include "serializable_item.h" -#include "color.h" - -class DeckList; - -enum ResponseCode { RespNothing, RespOk, RespNotInRoom, RespInternalError, RespInvalidCommand, RespInvalidData, RespNameNotFound, RespLoginNeeded, RespFunctionNotAllowed, RespGameNotStarted, RespGameFull, RespContextError, RespWrongPassword, RespSpectatorsNotAllowed, RespOnlyBuddies, RespUserLevelTooLow, RespInIgnoreList, RespWouldOverwriteOldSession, RespChatFlood }; - -// PrivateZone: Contents of the zone are always visible to the owner, -// but not to anyone else. -// PublicZone: Contents of the zone are always visible to anyone. -// HiddenZone: Contents of the zone are never visible to anyone. -// However, the owner of the zone can issue a dump_zone command, -// setting beingLookedAt to true. -// Cards in a zone with the type HiddenZone are referenced by their -// list index, whereas cards in any other zone are referenced by their ids. -enum ZoneType { PrivateZone, PublicZone, HiddenZone }; - -class GameTypeId : public SerializableItem_Int { -public: -GameTypeId(int _gameTypeId = -1) : SerializableItem_Int("game_type_id", _gameTypeId) { } -static SerializableItem *newItem() { return new GameTypeId; } -}; - - - -class ServerInfo_User : public SerializableItem_Map { -public: - enum UserLevelFlags { - IsNothing = 0x00, - IsUser = 0x01, - IsRegistered = 0x02, - IsModerator = 0x04, - IsAdmin = 0x08 - }; - enum Gender { - GenderUnknown = -1, - Male = 0, - Female = 1 - }; - ServerInfo_User(const QString &_name = QString(), int _userLevel = IsNothing, const QString &_address = QString(), const QString &_realName = QString(), Gender _gender = GenderUnknown, const QString &_country = QString(), const QByteArray &_avatarBmp = QByteArray()); - ServerInfo_User(const ServerInfo_User *other, bool complete = true, bool moderatorInfo = false); - static SerializableItem *newItem() { return new ServerInfo_User; } - QString getName() const { return static_cast(itemMap.value("name"))->getData(); } - void setName(const QString &_name) { static_cast(itemMap.value("name"))->setData(_name); } - int getUserLevel() const { return static_cast(itemMap.value("userlevel"))->getData(); } - void setUserLevel(int _userLevel) { static_cast(itemMap.value("userlevel"))->setData(_userLevel); } - QString getAddress() const { return static_cast(itemMap.value("address"))->getData(); } - void setAddress(const QString &_address) { static_cast(itemMap.value("address"))->setData(_address); } - QString getRealName() const { return static_cast(itemMap.value("real_name"))->getData(); } - Gender getGender() const { return static_cast(static_cast(itemMap.value("gender"))->getData()); } - QString getCountry() const { return static_cast(itemMap.value("country"))->getData(); } - QByteArray getAvatarBmp() const { return static_cast(itemMap.value("avatar_bmp"))->getData(); } -}; - -class ServerInfo_UserList : public SerializableItem_Map { -public: - ServerInfo_UserList(const QString &_itemType, const QList &_userList = QList()); - QList getUserList() const { return typecastItemList(); } -}; - -class ServerInfo_Game : public SerializableItem_Map { -public: - ServerInfo_Game(int _roomId = -1, int _gameId = -1, const QString &_description = QString(), bool _hasPassword = false, int _playerCount = -1, int _maxPlayers = -1, bool _started = false, const QList &_gameTypes = QList(), ServerInfo_User *creatorInfo = 0, bool _onlyBuddies = false, bool _onlyRegistered = false, bool _spectatorsAllowed = false, bool _spectatorsNeedPassword = false, int _spectatorCount = -1); - static SerializableItem *newItem() { return new ServerInfo_Game; } - int getRoomId() const { return static_cast(itemMap.value("room_id"))->getData(); } - int getGameId() const { return static_cast(itemMap.value("game_id"))->getData(); } - QString getDescription() const { return static_cast(itemMap.value("description"))->getData(); } - bool getHasPassword() const { return static_cast(itemMap.value("has_password"))->getData(); } - int getPlayerCount() const { return static_cast(itemMap.value("player_count"))->getData(); } - int getMaxPlayers() const { return static_cast(itemMap.value("max_players"))->getData(); } - bool getStarted() const { return static_cast(itemMap.value("started"))->getData(); } - QList getGameTypes() const { return typecastItemList(); } - ServerInfo_User *getCreatorInfo() const { return static_cast(itemMap.value("user")); } - bool getOnlyBuddies() const { return static_cast(itemMap.value("only_buddies"))->getData(); } - bool getOnlyRegistered() const { return static_cast(itemMap.value("only_registered"))->getData(); } - bool getSpectatorsAllowed() const { return static_cast(itemMap.value("spectators_allowed"))->getData(); } - bool getSpectatorsNeedPassword() const { return static_cast(itemMap.value("spectators_need_password"))->getData(); } - int getSpectatorCount() const { return static_cast(itemMap.value("spectator_count"))->getData(); } -}; - -class ServerInfo_GameType : public SerializableItem_Map { -public: - ServerInfo_GameType(int _gameTypeId = -1, const QString &_description = QString()); - static SerializableItem *newItem() { return new ServerInfo_GameType; } - int getGameTypeId() const { return static_cast(itemMap.value("game_type_id"))->getData(); } - QString getDescription() const { return static_cast(itemMap.value("description"))->getData(); } -}; - -class ServerInfo_Room : public SerializableItem_Map { -private: - QList gameList; - QList userList; - QList gameTypeList; -protected: - void extractData(); -public: - ServerInfo_Room(int _id = -1, const QString &_name = QString(), const QString &_description = QString(), int _gameCount = -1, int _playerCount = -1, bool _autoJoin = false, const QList &_gameList = QList(), const QList &_userList = QList(), const QList &_gameTypeList = QList()); - static SerializableItem *newItem() { return new ServerInfo_Room; } - int getRoomId() const { return static_cast(itemMap.value("room_id"))->getData(); } - QString getName() const { return static_cast(itemMap.value("name"))->getData(); } - QString getDescription() const { return static_cast(itemMap.value("description"))->getData(); } - int getGameCount() const { return static_cast(itemMap.value("game_count"))->getData(); } - int getPlayerCount() const { return static_cast(itemMap.value("player_count"))->getData(); } - bool getAutoJoin() const { return static_cast(itemMap.value("auto_join"))->getData(); } - const QList &getGameList() const { return gameList; } - const QList &getUserList() const { return userList; } - const QList &getGameTypeList() const { return gameTypeList; } -}; - -class ServerInfo_CardCounter : public SerializableItem_Map { -public: - ServerInfo_CardCounter(int _id = -1, int _value = 0); - static SerializableItem *newItem() { return new ServerInfo_CardCounter; } - int getId() const { return static_cast(itemMap.value("id"))->getData(); } - int getValue() const { return static_cast(itemMap.value("value"))->getData(); } -}; - -class ServerInfo_Card : public SerializableItem_Map { -public: - ServerInfo_Card(int _id = -1, const QString &_name = QString(), int _x = -1, int _y = -1, bool _faceDown = false, bool _tapped = false, bool _attacking = false, const QString &_color = QString(), const QString &_pt = QString(), const QString &_annotation = QString(), bool _destroyOnZoneChange = false, bool _doesntUntap = false, const QList &_counterList = QList(), int attachPlayerId = -1, const QString &_attachZone = QString(), int attachCardId = -1); - static SerializableItem *newItem() { return new ServerInfo_Card; } - int getId() const { return static_cast(itemMap.value("id"))->getData(); } - QString getName() const { return static_cast(itemMap.value("name"))->getData(); } - int getX() const { return static_cast(itemMap.value("x"))->getData(); } - int getY() const { return static_cast(itemMap.value("y"))->getData(); } - bool getFaceDown() const { return static_cast(itemMap.value("facedown"))->getData(); } - bool getTapped() const { return static_cast(itemMap.value("tapped"))->getData(); } - bool getAttacking() const { return static_cast(itemMap.value("attacking"))->getData(); } - QString getColor() const { return static_cast(itemMap.value("color"))->getData(); } - QString getPT() const { return static_cast(itemMap.value("pt"))->getData(); } - QString getAnnotation() const { return static_cast(itemMap.value("annotation"))->getData(); } - bool getDestroyOnZoneChange() const { return static_cast(itemMap.value("destroy_on_zone_change"))->getData(); } - bool getDoesntUntap() const { return static_cast(itemMap.value("doesnt_untap"))->getData(); } - QList getCounters() const { return typecastItemList(); } - int getAttachPlayerId() const { return static_cast(itemMap.value("attach_player_id"))->getData(); } - QString getAttachZone() const { return static_cast(itemMap.value("attach_zone"))->getData(); } - int getAttachCardId() const { return static_cast(itemMap.value("attach_card_id"))->getData(); } -}; - -class ServerInfo_Zone : public SerializableItem_Map { -private: - ZoneType typeFromString(const QString &type) const; - QString typeToString(ZoneType type) const; -public: - ServerInfo_Zone(const QString &_name = QString(), ZoneType _type = PrivateZone, bool _hasCoords = false, int _cardCount = -1, const QList &_cardList = QList()); - static SerializableItem *newItem() { return new ServerInfo_Zone; } - QString getName() const { return static_cast(itemMap.value("name"))->getData(); } - ZoneType getType() const { return typeFromString(static_cast(itemMap.value("type"))->getData()); } - bool getHasCoords() const { return static_cast(itemMap.value("has_coords"))->getData(); } - int getCardCount() const { return static_cast(itemMap.value("card_count"))->getData(); } - QList getCardList() const; -}; - -class ServerInfo_Counter : public SerializableItem_Map { -public: - ServerInfo_Counter(int _id = -1, const QString &_name = QString(), const Color &_color = Color(), int _radius = -1, int _count = -1); - static SerializableItem *newItem() { return new ServerInfo_Counter; } - int getId() const { return static_cast(itemMap.value("id"))->getData(); } - QString getName() const { return static_cast(itemMap.value("name"))->getData(); } - Color getColor() const { return static_cast(itemMap.value("color"))->getData(); } - int getRadius() const { return static_cast(itemMap.value("radius"))->getData(); } - int getCount() const { return static_cast(itemMap.value("count"))->getData(); } -}; - -class ServerInfo_Arrow : public SerializableItem_Map { -public: - ServerInfo_Arrow(int _id = -1, int _startPlayerId = -1, const QString &_startZone = QString(), int _startCardId = -1, int _targetPlayerId = -1, const QString &_targetZone = QString(), int _targetCardId = -1, const Color &_color = Color()); - static SerializableItem *newItem() { return new ServerInfo_Arrow; } - int getId() const { return static_cast(itemMap.value("id"))->getData(); } - int getStartPlayerId() const { return static_cast(itemMap.value("start_player_id"))->getData(); } - QString getStartZone() const { return static_cast(itemMap.value("start_zone"))->getData(); } - int getStartCardId() const { return static_cast(itemMap.value("start_card_id"))->getData(); } - int getTargetPlayerId() const { return static_cast(itemMap.value("target_player_id"))->getData(); } - QString getTargetZone() const { return static_cast(itemMap.value("target_zone"))->getData(); } - int getTargetCardId() const { return static_cast(itemMap.value("target_card_id"))->getData(); } - Color getColor() const { return static_cast(itemMap.value("color"))->getData(); } -}; - -class ServerInfo_PlayerProperties : public SerializableItem_Map { -public: - ServerInfo_PlayerProperties(int _playerId = -1, ServerInfo_User *_userInfo = 0, bool _spectator = false, bool _conceded = false, bool _readyStart = false, const QString &_deckHash = QString()); - static SerializableItem *newItem() { return new ServerInfo_PlayerProperties; } - int getPlayerId() const { return static_cast(itemMap.value("player_id"))->getData(); } - ServerInfo_User *getUserInfo() const { return static_cast(itemMap.value("user")); } - bool getSpectator() const { return static_cast(itemMap.value("spectator"))->getData(); } - bool getConceded() const { return static_cast(itemMap.value("conceded"))->getData(); } - bool getReadyStart() const { return static_cast(itemMap.value("ready_start"))->getData(); } - QString getDeckHash() const { return static_cast(itemMap.value("deck_hash"))->getData(); } -}; - -class ServerInfo_Player : public SerializableItem_Map { -private: - QList zoneList; - QList counterList; - QList arrowList; -protected: - void extractData(); -public: - ServerInfo_Player(ServerInfo_PlayerProperties *_properties = 0, DeckList *_deck = 0, const QList &_zoneList = QList(), const QList &_counterList = QList(), const QList &_arrowList = QList()); - static SerializableItem *newItem() { return new ServerInfo_Player; } - ServerInfo_PlayerProperties *getProperties() const { return static_cast(itemMap.value("player_properties")); } - DeckList *getDeck() const; - const QList &getZoneList() const { return zoneList; } - const QList &getCounterList() const { return counterList; } - const QList &getArrowList() const { return arrowList; } -}; - -class ServerInfo_PlayerPing : public SerializableItem_Map { -public: - ServerInfo_PlayerPing(int _playerId = -1, int _pingTime = -1); - static SerializableItem *newItem() { return new ServerInfo_PlayerPing; } - int getPlayerId() const { return static_cast(itemMap.value("player_id"))->getData(); } - int getPingTime() const { return static_cast(itemMap.value("ping_time"))->getData(); } -}; - -class DeckList_TreeItem : public SerializableItem_Map { -public: - DeckList_TreeItem(const QString &_itemType, const QString &_name, int _id); - QString getName() const { return static_cast(itemMap.value("name"))->getData(); } - int getId() const { return static_cast(itemMap.value("id"))->getData(); } -}; -class DeckList_File : public DeckList_TreeItem { -public: - DeckList_File(const QString &_name = QString(), int _id = -1, QDateTime _uploadTime = QDateTime()); - static SerializableItem *newItem() { return new DeckList_File; } - QDateTime getUploadTime() const { return static_cast(itemMap.value("upload_time"))->getData(); } -}; -class DeckList_Directory : public DeckList_TreeItem { -public: - DeckList_Directory(const QString &_name = QString(), int _id = 0); - static SerializableItem *newItem() { return new DeckList_Directory; } - QList getTreeItems() const { return typecastItemList(); } -}; - -#endif diff --git a/common/server_cardzone.h b/common/server_cardzone.h index 1744d52f..ee0a7f3a 100644 --- a/common/server_cardzone.h +++ b/common/server_cardzone.h @@ -22,22 +22,22 @@ #include #include -#include "protocol_datastructures.h" +#include "pb/serverinfo_zone.pb.h" class Server_Card; class Server_Player; class Server_Game; -class BlaContainer; +class GameEventStorage; class Server_CardZone { private: Server_Player *player; QString name; bool has_coords; - ZoneType type; + ServerInfo_Zone::ZoneType type; int cardsBeingLookedAt; public: - Server_CardZone(Server_Player *_player, const QString &_name, bool _has_coords, ZoneType _type); + Server_CardZone(Server_Player *_player, const QString &_name, bool _has_coords, ServerInfo_Zone::ZoneType _type); ~Server_CardZone(); int removeCard(Server_Card *card); @@ -46,15 +46,15 @@ public: int getCardsBeingLookedAt() const { return cardsBeingLookedAt; } void setCardsBeingLookedAt(int _cardsBeingLookedAt) { cardsBeingLookedAt = _cardsBeingLookedAt; } bool hasCoords() const { return has_coords; } - ZoneType getType() const { return type; } + ServerInfo_Zone::ZoneType getType() const { return type; } QString getName() const { return name; } Server_Player *getPlayer() const { return player; } int getFreeGridColumn(int x, int y, const QString &cardName) const; bool isColumnEmpty(int x, int y) const; bool isColumnStacked(int x, int y) const; - void fixFreeSpaces(BlaContainer *bla); - void moveCard(BlaContainer *bla, QMap &coordMap, Server_Card *card, int x, int y); + void fixFreeSpaces(GameEventStorage &ges); + void moveCard(GameEventStorage &ges, QMap &coordMap, Server_Card *card, int x, int y); QList cards; void insertCard(Server_Card *card, int x, int y); void shuffle(); diff --git a/common/server_game.h b/common/server_game.h index 0b1ecc8e..ab1f93e9 100644 --- a/common/server_game.h +++ b/common/server_game.h @@ -26,8 +26,11 @@ #include #include "server_player.h" #include "protocol.h" +#include "pb/response.pb.h" +#include "pb/serverinfo_player.pb.h" class QTimer; +class GameEventContainer; class Server_Room; class ServerInfo_User; @@ -78,7 +81,7 @@ public: bool getSpectatorsNeedPassword() const { return spectatorsNeedPassword; } bool getSpectatorsCanTalk() const { return spectatorsCanTalk; } bool getSpectatorsSeeEverything() const { return spectatorsSeeEverything; } - ResponseCode checkJoin(ServerInfo_User *user, const QString &_password, bool spectator, bool overrideRestrictions); + Response::ResponseCode checkJoin(ServerInfo_User *user, const QString &_password, bool spectator, bool overrideRestrictions); bool containsUser(const QString &userName) const; Server_Player *addPlayer(Server_ProtocolHandler *handler, bool spectator, bool broadcastUpdate = true); void removePlayer(Server_Player *player); @@ -93,8 +96,13 @@ public: void nextTurn(); void postConnectionStatusUpdate(Server_Player *player, bool connectionStatus); - QList getGameState(Server_Player *playerWhosAsking) const; - void sendGameEvent(GameEvent *event, GameEventContext *context = 0, Server_Player *exclude = 0); + QList getGameState(Server_Player *playerWhosAsking) const; + + GameEventContainer *prepareGameEvent(const ::google::protobuf::Message &gameEvent, int playerId); + GameEventContainer *prepareGameEvent(const ::google::protobuf::Message &gameEvent, int playerId, const GameEventContext &context); + GameEventContext prepareGameEventContext(const ::google::protobuf::Message &gameEventContext); + +// void sendGameEvent(GameEvent *event, GameEventContext *context = 0, Server_Player *exclude = 0); void sendGameEventContainer(GameEventContainer *cont, Server_Player *exclude = 0, bool excludeOmniscient = false); void sendGameEventContainerOmniscient(GameEventContainer *cont, Server_Player *exclude = 0); void sendGameEventToPlayer(Server_Player *player, GameEvent *event); diff --git a/common/server_player.cpp b/common/server_player.cpp index 3f4a0b4b..ee9c1c0b 100644 --- a/common/server_player.cpp +++ b/common/server_player.cpp @@ -184,7 +184,7 @@ void Server_Player::clearZones() lastDrawList.clear(); } -ServerInfo_PlayerProperties *Server_Player::getProperties() +ServerInfo_PlayerProperties Server_Player::getProperties() { QMutexLocker locker(&game->gameMutex); diff --git a/common/server_player.h b/common/server_player.h index f211ae7f..95cbd254 100644 --- a/common/server_player.h +++ b/common/server_player.h @@ -6,7 +6,6 @@ #include #include #include -#include "protocol_datastructures.h" class DeckList; class Server_Game; @@ -20,7 +19,7 @@ class ServerInfo_User; class ServerInfo_PlayerProperties; class CommandContainer; class CardToMove; -class BlaContainer; +class GameEventStorage; class Server_Player : public Server_ArrowTarget { Q_OBJECT @@ -66,7 +65,7 @@ public: const QMap &getCounters() const { return counters; } const QMap &getArrows() const { return arrows; } - ServerInfo_PlayerProperties *getProperties(); + ServerInfo_PlayerProperties getProperties(); int newCardId(); int newCounterId() const; @@ -81,12 +80,12 @@ public: void clearZones(); void setupZones(); - ResponseCode drawCards(BlaContainer *bla, int number); - ResponseCode undoDraw(BlaContainer *bla); - ResponseCode moveCard(BlaContainer *bla, const QString &_startZone, const QList &_cards, int _targetPlayer, const QString &_targetZone, int _x, int _y); - ResponseCode moveCard(BlaContainer *bla, Server_CardZone *startzone, const QList &_cards, Server_CardZone *targetzone, int x, int y, bool fixFreeSpaces = true, bool undoingDraw = false); - void unattachCard(BlaContainer *bla, Server_Card *card); - ResponseCode setCardAttrHelper(BlaContainer *bla, const QString &zone, int cardId, const QString &attrName, const QString &attrValue); + Response::ResponseCode drawCards(GameEventStorage &ges, int number); + Response::ResponseCode undoDraw(GameEventStorage &ges); + Response::ResponseCode moveCard(GameEventStorage &ges, const QString &_startZone, const QList &_cards, int _targetPlayer, const QString &_targetZone, int _x, int _y); + Response::ResponseCode moveCard(GameEventStorage &ges, Server_CardZone *startzone, const QList &_cards, Server_CardZone *targetzone, int x, int y, bool fixFreeSpaces = true, bool undoingDraw = false); + void unattachCard(GameEventStorage &ges, Server_Card *card); + Response::ResponseCode setCardAttrHelper(GameEventStorage &ges, const QString &zone, int cardId, const QString &attrName, const QString &attrValue); void sendProtocolItem(ProtocolItem *item, bool deleteItem = true); }; diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index aa47ad44..2734c004 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -2,7 +2,6 @@ #include "rng_abstract.h" #include "server_protocolhandler.h" #include "protocol.h" -#include "protocol_items.h" #include "server_room.h" #include "server_card.h" #include "server_arrow.h" @@ -12,6 +11,7 @@ #include "server_player.h" #include "decklist.h" #include +#include "pb/serverinfo_zone.pb.h" #include "pb/commands.pb.h" #include "pb/command_attach_card.pb.h" #include "pb/command_concede.pb.h" @@ -48,6 +48,39 @@ #include "pb/command_deck_new_dir.pb.h" #include "pb/command_deck_del_dir.pb.h" #include "pb/command_deck_del.pb.h" +#include "pb/response.pb.h" +#include "pb/response_login.pb.h" +#include "pb/response_list_users.pb.h" +#include "pb/response_get_games_of_user.pb.h" +#include "pb/response_get_user_info.pb.h" +#include "pb/response_join_room.pb.h" +#include "pb/response_deck_download.pb.h" +#include "pb/response_dump_zone.pb.h" +#include "pb/event_list_rooms.pb.h" +#include "pb/event_server_message.pb.h" +#include "pb/event_user_message.pb.h" +#include "pb/event_game_say.pb.h" +#include "pb/event_game_joined.pb.h" +#include "pb/event_game_state_changed.pb.h" +#include "pb/event_shuffle.pb.h" +#include "pb/event_roll_die.pb.h" +#include "pb/event_player_properties_changed.pb.h" +#include "pb/event_create_arrow.pb.h" +#include "pb/event_delete_arrow.pb.h" +#include "pb/event_set_card_counter.pb.h" +#include "pb/event_flip_card.pb.h" +#include "pb/event_attach_card.pb.h" +#include "pb/event_create_token.pb.h" +#include "pb/event_room_say.pb.h" +#include "pb/event_create_counter.pb.h" +#include "pb/event_del_counter.pb.h" +#include "pb/event_set_counter.pb.h" +#include "pb/event_dump_zone.pb.h" +#include "pb/event_stop_dump_zone.pb.h" +#include "pb/context_deck_select.pb.h" +#include "pb/context_concede.pb.h" +#include "pb/context_ready_start.pb.h" +#include "pb/context_mulligan.pb.h" #include Server_ProtocolHandler::Server_ProtocolHandler(Server *_server, QObject *parent) @@ -107,11 +140,57 @@ void Server_ProtocolHandler::playerRemovedFromGame(Server_Game *game) games.remove(game->getGameId()); } -ResponseCode Server_ProtocolHandler::processSessionCommandContainer(const CommandContainer &cont, BlaContainer *bla) +void Server_ProtocolHandler::sendProtocolItem(const Response &item) { - ResponseCode finalResponseCode = RespOk; + ServerMessage msg; + msg.mutable_response()->CopyFrom(item); + msg.set_message_type(ServerMessage::RESPONSE); + + transmitProtocolItem(msg); +} + +void Server_ProtocolHandler::sendProtocolItem(const SessionEvent &item) +{ + ServerMessage msg; + msg.mutable_session_event()->CopyFrom(item); + msg.set_message_type(ServerMessage::SESSION_EVENT); + + transmitProtocolItem(msg); +} + +void Server_ProtocolHandler::sendProtocolItem(const GameEventContainer &item) +{ + ServerMessage msg; + msg.mutable_game_event_container()->CopyFrom(item); + msg.set_message_type(ServerMessage::GAME_EVENT_CONTAINER); + + transmitProtocolItem(msg); +} + +void Server_ProtocolHandler::sendProtocolItem(const RoomEvent &item) +{ + ServerMessage msg; + msg.mutable_room_event()->CopyFrom(item); + msg.set_message_type(ServerMessage::ROOM_EVENT); + + transmitProtocolItem(msg); +} + +void Server_ProtocolHandler::sendProtocolItem(ServerMessage::MessageType type, const ::google::protobuf::Message &item) +{ + switch (type) { + case ServerMessage::RESPONSE: sendProtocolItem(static_cast(item)); break; + case ServerMessage::SESSION_EVENT: sendProtocolItem(static_cast(item)); break; + case ServerMessage::GAME_EVENT_CONTAINER: sendProtocolItem(static_cast(item)); break; + case ServerMessage::ROOM_EVENT: sendProtocolItem(static_cast(item)); break; + } +} + +Response::ResponseCode Server_ProtocolHandler::processSessionCommandContainer(const CommandContainer &cont, ResponseContainer &rc) +{ + Response::ResponseCode finalResponseCode = Response::RespOk; for (int i = cont.session_command_size() - 1; i >= 0; --i) { - ResponseCode resp = RespInvalidCommand; + Response::ResponseCode resp = Response::RespInvalidCommand; const SessionCommand &sc = cont.session_command(i); std::vector< const ::google::protobuf::FieldDescriptor * > fieldList; sc.GetReflection()->ListFields(sc, &fieldList); @@ -122,43 +201,43 @@ ResponseCode Server_ProtocolHandler::processSessionCommandContainer(const Comman break; } switch ((SessionCommand::SessionCommandType) num) { - case SessionCommand::PING: resp = cmdPing(sc.GetExtension(Command_Ping::ext)); break; - case SessionCommand::LOGIN: resp = cmdLogin(sc.GetExtension(Command_Login::ext), bla); break; - case SessionCommand::MESSAGE: resp = cmdMessage(sc.GetExtension(Command_Message::ext), bla); break; - case SessionCommand::ADD_TO_LIST: resp = cmdAddToList(sc.GetExtension(Command_AddToList::ext), bla); break; - case SessionCommand::REMOVE_FROM_LIST: resp = cmdRemoveFromList(sc.GetExtension(Command_RemoveFromList::ext), bla); break; - case SessionCommand::DECK_LIST: resp = cmdDeckList(sc.GetExtension(Command_DeckList::ext), bla); break; - case SessionCommand::DECK_NEW_DIR: resp = cmdDeckNewDir(sc.GetExtension(Command_DeckNewDir::ext), bla); break; - case SessionCommand::DECK_DEL_DIR: resp = cmdDeckDelDir(sc.GetExtension(Command_DeckDelDir::ext), bla); break; - case SessionCommand::DECK_DEL: resp = cmdDeckDel(sc.GetExtension(Command_DeckDel::ext), bla); break; - case SessionCommand::DECK_UPLOAD: resp = cmdDeckUpload(sc.GetExtension(Command_DeckUpload::ext), bla); break; - case SessionCommand::DECK_DOWNLOAD: resp = cmdDeckDownload(sc.GetExtension(Command_DeckDownload::ext), bla); break; - case SessionCommand::GET_GAMES_OF_USER: resp = cmdGetGamesOfUser(sc.GetExtension(Command_GetGamesOfUser::ext), bla); break; - case SessionCommand::GET_USER_INFO: resp = cmdGetUserInfo(sc.GetExtension(Command_GetUserInfo::ext), bla); break; - case SessionCommand::LIST_ROOMS: resp = cmdListRooms(sc.GetExtension(Command_ListRooms::ext), bla); break; - case SessionCommand::JOIN_ROOM: resp = cmdJoinRoom(sc.GetExtension(Command_JoinRoom::ext), bla); break; - case SessionCommand::LIST_USERS: resp = cmdListUsers(sc.GetExtension(Command_ListUsers::ext), bla); break; + case SessionCommand::PING: resp = cmdPing(sc.GetExtension(Command_Ping::ext), rc); break; + case SessionCommand::LOGIN: resp = cmdLogin(sc.GetExtension(Command_Login::ext), rc); break; + case SessionCommand::MESSAGE: resp = cmdMessage(sc.GetExtension(Command_Message::ext), rc); break; + case SessionCommand::ADD_TO_LIST: resp = cmdAddToList(sc.GetExtension(Command_AddToList::ext), rc); break; + case SessionCommand::REMOVE_FROM_LIST: resp = cmdRemoveFromList(sc.GetExtension(Command_RemoveFromList::ext), rc); break; + case SessionCommand::DECK_LIST: resp = cmdDeckList(sc.GetExtension(Command_DeckList::ext), rc); break; + case SessionCommand::DECK_NEW_DIR: resp = cmdDeckNewDir(sc.GetExtension(Command_DeckNewDir::ext), rc); break; + case SessionCommand::DECK_DEL_DIR: resp = cmdDeckDelDir(sc.GetExtension(Command_DeckDelDir::ext), rc); break; + case SessionCommand::DECK_DEL: resp = cmdDeckDel(sc.GetExtension(Command_DeckDel::ext), rc); break; + case SessionCommand::DECK_UPLOAD: resp = cmdDeckUpload(sc.GetExtension(Command_DeckUpload::ext), rc); break; + case SessionCommand::DECK_DOWNLOAD: resp = cmdDeckDownload(sc.GetExtension(Command_DeckDownload::ext), rc); break; + case SessionCommand::GET_GAMES_OF_USER: resp = cmdGetGamesOfUser(sc.GetExtension(Command_GetGamesOfUser::ext), rc); break; + case SessionCommand::GET_USER_INFO: resp = cmdGetUserInfo(sc.GetExtension(Command_GetUserInfo::ext), rc); break; + case SessionCommand::LIST_ROOMS: resp = cmdListRooms(sc.GetExtension(Command_ListRooms::ext), rc); break; + case SessionCommand::JOIN_ROOM: resp = cmdJoinRoom(sc.GetExtension(Command_JoinRoom::ext), rc); break; + case SessionCommand::LIST_USERS: resp = cmdListUsers(sc.GetExtension(Command_ListUsers::ext), rc); break; } - if ((resp != RespOk) && (resp != RespNothing)) + if ((resp != Response::RespOk) && (resp != Response::RespNothing)) finalResponseCode = resp; } return finalResponseCode; } -ResponseCode Server_ProtocolHandler::processRoomCommandContainer(const CommandContainer &cont, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::processRoomCommandContainer(const CommandContainer &cont, ResponseContainer &rc) { if (authState == PasswordWrong) - return RespLoginNeeded; + return Response::RespLoginNeeded; Server_Room *room = rooms.value(cont.room_id(), 0); if (!room) - return RespNotInRoom; + return Response::RespNotInRoom; QMutexLocker locker(&room->roomMutex); - ResponseCode finalResponseCode = RespOk; + Response::ResponseCode finalResponseCode = Response::RespOk; for (int i = cont.room_command_size() - 1; i >= 0; --i) { - ResponseCode resp = RespInvalidCommand; + Response::ResponseCode resp = Response::RespInvalidCommand; const RoomCommand &sc = cont.room_command(i); std::vector< const ::google::protobuf::FieldDescriptor * > fieldList; sc.GetReflection()->ListFields(sc, &fieldList); @@ -169,26 +248,26 @@ ResponseCode Server_ProtocolHandler::processRoomCommandContainer(const CommandCo break; } switch ((RoomCommand::RoomCommandType) num) { - case RoomCommand::LEAVE_ROOM: resp = cmdLeaveRoom(sc.GetExtension(Command_LeaveRoom::ext), room); break; - case RoomCommand::ROOM_SAY: resp = cmdRoomSay(sc.GetExtension(Command_RoomSay::ext), room); break; - case RoomCommand::CREATE_GAME: resp = cmdCreateGame(sc.GetExtension(Command_CreateGame::ext), room); break; - case RoomCommand::JOIN_GAME: resp = cmdJoinGame(sc.GetExtension(Command_JoinGame::ext), room); break; + case RoomCommand::LEAVE_ROOM: resp = cmdLeaveRoom(sc.GetExtension(Command_LeaveRoom::ext), room, rc); break; + case RoomCommand::ROOM_SAY: resp = cmdRoomSay(sc.GetExtension(Command_RoomSay::ext), room, rc); break; + case RoomCommand::CREATE_GAME: resp = cmdCreateGame(sc.GetExtension(Command_CreateGame::ext), room, rc); break; + case RoomCommand::JOIN_GAME: resp = cmdJoinGame(sc.GetExtension(Command_JoinGame::ext), room, rc); break; } - if ((resp != RespOk) && (resp != RespNothing)) + if ((resp != Response::RespOk) && (resp != Response::RespNothing)) finalResponseCode = resp; } return finalResponseCode; } -ResponseCode Server_ProtocolHandler::processGameCommandContainer(const CommandContainer &cont, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::processGameCommandContainer(const CommandContainer &cont, ResponseContainer &rc) { if (authState == PasswordWrong) - return RespLoginNeeded; + return Response::RespLoginNeeded; gameListMutex.lock(); if (!games.contains(cont.game_id())) { qDebug() << "invalid game"; - return RespNotInRoom; + return Response::RespNotInRoom; } QPair gamePair = games.value(cont.game_id()); Server_Game *game = gamePair.first; @@ -197,9 +276,10 @@ ResponseCode Server_ProtocolHandler::processGameCommandContainer(const CommandCo QMutexLocker locker(&game->gameMutex); gameListMutex.unlock(); - ResponseCode finalResponseCode = RespOk; + GameEventStorage ges; + Response::ResponseCode finalResponseCode = Response::RespOk; for (int i = cont.game_command_size() - 1; i >= 0; --i) { - ResponseCode resp = RespInvalidCommand; + Response::ResponseCode resp = Response::RespInvalidCommand; const GameCommand &sc = cont.game_command(i); std::vector< const ::google::protobuf::FieldDescriptor * > fieldList; sc.GetReflection()->ListFields(sc, &fieldList); @@ -210,130 +290,47 @@ ResponseCode Server_ProtocolHandler::processGameCommandContainer(const CommandCo break; } switch ((GameCommand::GameCommandType) num) { - case GameCommand::KICK_FROM_GAME: resp = cmdKickFromGame(sc.GetExtension(Command_KickFromGame::ext), game, player, bla); break; - case GameCommand::LEAVE_GAME: resp = cmdLeaveGame(sc.GetExtension(Command_LeaveGame::ext), game, player, bla); break; - case GameCommand::GAME_SAY: resp = cmdGameSay(sc.GetExtension(Command_GameSay::ext), game, player, bla); break; - case GameCommand::SHUFFLE: resp = cmdShuffle(sc.GetExtension(Command_Shuffle::ext), game, player, bla); break; - case GameCommand::MULLIGAN: resp = cmdMulligan(sc.GetExtension(Command_Mulligan::ext), game, player, bla); break; - case GameCommand::ROLL_DIE: resp = cmdRollDie(sc.GetExtension(Command_RollDie::ext), game, player, bla); break; - case GameCommand::DRAW_CARDS: resp = cmdDrawCards(sc.GetExtension(Command_DrawCards::ext), game, player, bla); break; - case GameCommand::UNDO_DRAW: resp = cmdUndoDraw(sc.GetExtension(Command_UndoDraw::ext), game, player, bla); break; - case GameCommand::FLIP_CARD: resp = cmdFlipCard(sc.GetExtension(Command_FlipCard::ext), game, player, bla); break; - case GameCommand::ATTACH_CARD: resp = cmdAttachCard(sc.GetExtension(Command_AttachCard::ext), game, player, bla); break; - case GameCommand::CREATE_TOKEN: resp = cmdCreateToken(sc.GetExtension(Command_CreateToken::ext), game, player, bla); break; - case GameCommand::CREATE_ARROW: resp = cmdCreateArrow(sc.GetExtension(Command_CreateArrow::ext), game, player, bla); break; - case GameCommand::DELETE_ARROW: resp = cmdDeleteArrow(sc.GetExtension(Command_DeleteArrow::ext), game, player, bla); break; - case GameCommand::SET_CARD_ATTR: resp = cmdSetCardAttr(sc.GetExtension(Command_SetCardAttr::ext), game, player, bla); break; - case GameCommand::SET_CARD_COUNTER: resp = cmdSetCardCounter(sc.GetExtension(Command_SetCardCounter::ext), game, player, bla); break; - case GameCommand::INC_CARD_COUNTER: resp = cmdIncCardCounter(sc.GetExtension(Command_IncCardCounter::ext), game, player, bla); break; - case GameCommand::READY_START: resp = cmdReadyStart(sc.GetExtension(Command_ReadyStart::ext), game, player, bla); break; - case GameCommand::CONCEDE: resp = cmdConcede(sc.GetExtension(Command_Concede::ext), game, player, bla); break; - case GameCommand::INC_COUNTER: resp = cmdIncCounter(sc.GetExtension(Command_IncCounter::ext), game, player, bla); break; - case GameCommand::CREATE_COUNTER: resp = cmdCreateCounter(sc.GetExtension(Command_CreateCounter::ext), game, player, bla); break; - case GameCommand::SET_COUNTER: resp = cmdSetCounter(sc.GetExtension(Command_SetCounter::ext), game, player, bla); break; - case GameCommand::DEL_COUNTER: resp = cmdDelCounter(sc.GetExtension(Command_DelCounter::ext), game, player, bla); break; - case GameCommand::NEXT_TURN: resp = cmdNextTurn(sc.GetExtension(Command_NextTurn::ext), game, player, bla); break; - case GameCommand::SET_ACTIVE_PHASE: resp = cmdSetActivePhase(sc.GetExtension(Command_SetActivePhase::ext), game, player, bla); break; - case GameCommand::DUMP_ZONE: resp = cmdDumpZone(sc.GetExtension(Command_DumpZone::ext), game, player, bla); break; - case GameCommand::STOP_DUMP_ZONE: resp = cmdStopDumpZone(sc.GetExtension(Command_StopDumpZone::ext), game, player, bla); break; - case GameCommand::REVEAL_CARDS: resp = cmdRevealCards(sc.GetExtension(Command_RevealCards::ext), game, player, bla); break; - case GameCommand::MOVE_CARD: resp = cmdMoveCard(sc.GetExtension(Command_MoveCard::ext), game, player, bla); break; - case GameCommand::SET_SIDEBOARD_PLAN: resp = cmdSetSideboardPlan(sc.GetExtension(Command_SetSideboardPlan::ext), game, player, bla); break; - case GameCommand::DECK_SELECT: resp = cmdDeckSelect(sc.GetExtension(Command_DeckSelect::ext), game, player, bla); break; + case GameCommand::KICK_FROM_GAME: resp = cmdKickFromGame(sc.GetExtension(Command_KickFromGame::ext), game, player, rc, ges); break; + case GameCommand::LEAVE_GAME: resp = cmdLeaveGame(sc.GetExtension(Command_LeaveGame::ext), game, player, rc, ges); break; + case GameCommand::GAME_SAY: resp = cmdGameSay(sc.GetExtension(Command_GameSay::ext), game, player, rc, ges); break; + case GameCommand::SHUFFLE: resp = cmdShuffle(sc.GetExtension(Command_Shuffle::ext), game, player, rc, ges); break; + case GameCommand::MULLIGAN: resp = cmdMulligan(sc.GetExtension(Command_Mulligan::ext), game, player, rc, ges); break; + case GameCommand::ROLL_DIE: resp = cmdRollDie(sc.GetExtension(Command_RollDie::ext), game, player, rc, ges); break; + case GameCommand::DRAW_CARDS: resp = cmdDrawCards(sc.GetExtension(Command_DrawCards::ext), game, player, rc, ges); break; + case GameCommand::UNDO_DRAW: resp = cmdUndoDraw(sc.GetExtension(Command_UndoDraw::ext), game, player, rc, ges); break; + case GameCommand::FLIP_CARD: resp = cmdFlipCard(sc.GetExtension(Command_FlipCard::ext), game, player, rc, ges); break; + case GameCommand::ATTACH_CARD: resp = cmdAttachCard(sc.GetExtension(Command_AttachCard::ext), game, player, rc, ges); break; + case GameCommand::CREATE_TOKEN: resp = cmdCreateToken(sc.GetExtension(Command_CreateToken::ext), game, player, rc, ges); break; + case GameCommand::CREATE_ARROW: resp = cmdCreateArrow(sc.GetExtension(Command_CreateArrow::ext), game, player, rc, ges); break; + case GameCommand::DELETE_ARROW: resp = cmdDeleteArrow(sc.GetExtension(Command_DeleteArrow::ext), game, player, rc, ges); break; + case GameCommand::SET_CARD_ATTR: resp = cmdSetCardAttr(sc.GetExtension(Command_SetCardAttr::ext), game, player, rc, ges); break; + case GameCommand::SET_CARD_COUNTER: resp = cmdSetCardCounter(sc.GetExtension(Command_SetCardCounter::ext), game, player, rc, ges); break; + case GameCommand::INC_CARD_COUNTER: resp = cmdIncCardCounter(sc.GetExtension(Command_IncCardCounter::ext), game, player, rc, ges); break; + case GameCommand::READY_START: resp = cmdReadyStart(sc.GetExtension(Command_ReadyStart::ext), game, player, rc, ges); break; + case GameCommand::CONCEDE: resp = cmdConcede(sc.GetExtension(Command_Concede::ext), game, player, rc, ges); break; + case GameCommand::INC_COUNTER: resp = cmdIncCounter(sc.GetExtension(Command_IncCounter::ext), game, player, rc, ges); break; + case GameCommand::CREATE_COUNTER: resp = cmdCreateCounter(sc.GetExtension(Command_CreateCounter::ext), game, player, rc, ges); break; + case GameCommand::SET_COUNTER: resp = cmdSetCounter(sc.GetExtension(Command_SetCounter::ext), game, player, rc, ges); break; + case GameCommand::DEL_COUNTER: resp = cmdDelCounter(sc.GetExtension(Command_DelCounter::ext), game, player, rc, ges); break; + case GameCommand::NEXT_TURN: resp = cmdNextTurn(sc.GetExtension(Command_NextTurn::ext), game, player, rc, ges); break; + case GameCommand::SET_ACTIVE_PHASE: resp = cmdSetActivePhase(sc.GetExtension(Command_SetActivePhase::ext), game, player, rc, ges); break; + case GameCommand::DUMP_ZONE: resp = cmdDumpZone(sc.GetExtension(Command_DumpZone::ext), game, player, rc, ges); break; + case GameCommand::STOP_DUMP_ZONE: resp = cmdStopDumpZone(sc.GetExtension(Command_StopDumpZone::ext), game, player, rc, ges); break; + case GameCommand::REVEAL_CARDS: resp = cmdRevealCards(sc.GetExtension(Command_RevealCards::ext), game, player, rc, ges); break; + case GameCommand::MOVE_CARD: resp = cmdMoveCard(sc.GetExtension(Command_MoveCard::ext), game, player, rc, ges); break; + case GameCommand::SET_SIDEBOARD_PLAN: resp = cmdSetSideboardPlan(sc.GetExtension(Command_SetSideboardPlan::ext), game, player, rc, ges); break; + case GameCommand::DECK_SELECT: resp = cmdDeckSelect(sc.GetExtension(Command_DeckSelect::ext), game, player, rc, ges); break; } - if ((resp != RespOk) && (resp != RespNothing)) + if ((resp != Response::RespOk) && (resp != Response::RespNothing)) finalResponseCode = resp; } - return finalResponseCode; -} - -ResponseCode Server_ProtocolHandler::processModeratorCommandContainer(const CommandContainer &cont, BlaContainer *bla) -{ - if (!userInfo) - return RespLoginNeeded; - if (!(userInfo->getUserLevel() & ServerInfo_User::IsModerator)) - return RespLoginNeeded; - - ResponseCode finalResponseCode = RespOk; - for (int i = cont.moderator_command_size() - 1; i >= 0; --i) { - ResponseCode resp = RespInvalidCommand; - const ModeratorCommand &sc = cont.moderator_command(i); - std::vector< const ::google::protobuf::FieldDescriptor * > fieldList; - sc.GetReflection()->ListFields(sc, &fieldList); - int num = 0; - for (unsigned int j = 0; j < fieldList.size(); ++j) - if (fieldList[j]->is_extension()) { - num = fieldList[j]->number(); - break; - } - switch ((ModeratorCommand::ModeratorCommandType) num) { - case ModeratorCommand::BAN_FROM_SERVER: resp = cmdBanFromServer(sc.GetExtension(Command_BanFromServer::ext), bla); break; - } - if ((resp != RespOk) && (resp != RespNothing)) - finalResponseCode = resp; - } - return finalResponseCode; -} - -ResponseCode Server_ProtocolHandler::processAdminCommandContainer(const CommandContainer &cont, BlaContainer *bla) -{ - if (!userInfo) - return RespLoginNeeded; - if (!(userInfo->getUserLevel() & ServerInfo_User::IsAdmin)) - return RespLoginNeeded; - - ResponseCode finalResponseCode = RespOk; - for (int i = cont.admin_command_size() - 1; i >= 0; --i) { - ResponseCode resp = RespInvalidCommand; - const AdminCommand &sc = cont.admin_command(i); - std::vector< const ::google::protobuf::FieldDescriptor * > fieldList; - sc.GetReflection()->ListFields(sc, &fieldList); - int num = 0; - for (unsigned int j = 0; j < fieldList.size(); ++j) - if (fieldList[j]->is_extension()) { - num = fieldList[j]->number(); - break; - } - switch ((AdminCommand::AdminCommandType) num) { - case AdminCommand::SHUTDOWN_SERVER: resp = cmdShutdownServer(sc.GetExtension(Command_ShutdownServer::ext), bla); break; - case AdminCommand::UPDATE_SERVER_MESSAGE: resp = cmdUpdateServerMessage(sc.GetExtension(Command_UpdateServerMessage::ext), bla); break; - } - if ((resp != RespOk) && (resp != RespNothing)) - finalResponseCode = resp; - } - return finalResponseCode; -} - -void Server_ProtocolHandler::processCommandContainer(const CommandContainer &cont) -{ - lastDataReceived = timeRunning; - - BlaContainer *bla = new BlaContainer; - ResponseCode finalResponseCode; - - if (cont.game_command_size()) - finalResponseCode = processGameCommandContainer(cont, bla); - else if (cont.room_command_size()) - finalResponseCode = processRoomCommandContainer(cont, bla); - else if (cont.session_command_size()) - finalResponseCode = processSessionCommandContainer(cont, bla); - else if (cont.moderator_command_size()) - finalResponseCode = processModeratorCommandContainer(cont, bla); - else if (cont.admin_command_size()) - finalResponseCode = processAdminCommandContainer(cont, bla); - else - finalResponseCode = RespInvalidCommand; - - ProtocolResponse *pr = bla->getResponse(); - if (!pr) - pr = new ProtocolResponse(cont.cmd_id(), finalResponseCode); - else - pr->setCmdId(cont.cmd_id()); - - gameListMutex.lock(); + /* + * XXX konvertieren zu GameEventStorage (oben deklariert als "ges") + * GameEventContainer *gQPublic = bla->getGameEventQueuePublic(); if (gQPublic) { - QPair gamePlayerPair = games.value(gQPublic->getGameId()); + gameListMutex.lock(); + QPair gamePlayerPair = games.value(gQPublic->game_id()); if (gamePlayerPair.first) { GameEventContainer *gQPrivate = bla->getGameEventQueuePrivate(); GameEventContainer *gQOmniscient = bla->getGameEventQueueOmniscient(); @@ -353,17 +350,104 @@ void Server_ProtocolHandler::processCommandContainer(const CommandContainer &con } else gamePlayerPair.first->sendGameEventContainer(gQPublic); } + gameListMutex.unlock(); } - gameListMutex.unlock(); + */ + return finalResponseCode; +} + +Response::ResponseCode Server_ProtocolHandler::processModeratorCommandContainer(const CommandContainer &cont, ResponseContainer &rc) +{ + if (!userInfo) + return Response::RespLoginNeeded; + if (!(userInfo->user_level() & ServerInfo_User::IsModerator)) + return Response::RespLoginNeeded; + + Response::ResponseCode finalResponseCode = Response::RespOk; + for (int i = cont.moderator_command_size() - 1; i >= 0; --i) { + Response::ResponseCode resp = Response::RespInvalidCommand; + const ModeratorCommand &sc = cont.moderator_command(i); + std::vector< const ::google::protobuf::FieldDescriptor * > fieldList; + sc.GetReflection()->ListFields(sc, &fieldList); + int num = 0; + for (unsigned int j = 0; j < fieldList.size(); ++j) + if (fieldList[j]->is_extension()) { + num = fieldList[j]->number(); + break; + } + switch ((ModeratorCommand::ModeratorCommandType) num) { + case ModeratorCommand::BAN_FROM_SERVER: resp = cmdBanFromServer(sc.GetExtension(Command_BanFromServer::ext), rc); break; + } + if ((resp != Response::RespOk) && (resp != Response::RespNothing)) + finalResponseCode = resp; + } + return finalResponseCode; +} + +Response::ResponseCode Server_ProtocolHandler::processAdminCommandContainer(const CommandContainer &cont, ResponseContainer &rc) +{ + if (!userInfo) + return Response::RespLoginNeeded; + if (!(userInfo->user_level() & ServerInfo_User::IsAdmin)) + return Response::RespLoginNeeded; + + Response::ResponseCode finalResponseCode = Response::RespOk; + for (int i = cont.admin_command_size() - 1; i >= 0; --i) { + Response::ResponseCode resp = Response::RespInvalidCommand; + const AdminCommand &sc = cont.admin_command(i); + std::vector< const ::google::protobuf::FieldDescriptor * > fieldList; + sc.GetReflection()->ListFields(sc, &fieldList); + int num = 0; + for (unsigned int j = 0; j < fieldList.size(); ++j) + if (fieldList[j]->is_extension()) { + num = fieldList[j]->number(); + break; + } + switch ((AdminCommand::AdminCommandType) num) { + case AdminCommand::SHUTDOWN_SERVER: resp = cmdShutdownServer(sc.GetExtension(Command_ShutdownServer::ext), rc); break; + case AdminCommand::UPDATE_SERVER_MESSAGE: resp = cmdUpdateServerMessage(sc.GetExtension(Command_UpdateServerMessage::ext), rc); break; + } + if ((resp != Response::RespOk) && (resp != Response::RespNothing)) + finalResponseCode = resp; + } + return finalResponseCode; +} + +void Server_ProtocolHandler::processCommandContainer(const CommandContainer &cont) +{ + lastDataReceived = timeRunning; - const QList &iQ = bla->getItemQueue(); - for (int i = 0; i < iQ.size(); ++i) - sendProtocolItem(iQ[i]); + ResponseContainer responseContainer; + Response::ResponseCode finalResponseCode; - sendProtocolItem(pr); + if (cont.game_command_size()) + finalResponseCode = processGameCommandContainer(cont, responseContainer); + else if (cont.room_command_size()) + finalResponseCode = processRoomCommandContainer(cont, responseContainer); + else if (cont.session_command_size()) + finalResponseCode = processSessionCommandContainer(cont, responseContainer); + else if (cont.moderator_command_size()) + finalResponseCode = processModeratorCommandContainer(cont, responseContainer); + else if (cont.admin_command_size()) + finalResponseCode = processAdminCommandContainer(cont, responseContainer); + else + finalResponseCode = Response::RespInvalidCommand; - while (!itemQueue.isEmpty()) - sendProtocolItem(itemQueue.takeFirst()); + const QList > &preResponseQueue = responseContainer.getPreResponseQueue(); + for (int i = 0; i < preResponseQueue.size(); ++i) + sendProtocolItem(preResponseQueue[i].first, *preResponseQueue[i].second); + + Response response; + response.set_cmd_id(cont.cmd_id()); + response.set_response_code(finalResponseCode); + ::google::protobuf::Message *responseExtension = responseContainer.getResponseExtension(); + if (responseExtension) + response.GetReflection()->MutableMessage(&response, responseExtension->GetDescriptor()->FindExtensionByName("ext"))->CopyFrom(*responseExtension); + sendProtocolItem(response); + + const QList > &postResponseQueue = responseContainer.getPostResponseQueue(); + for (int i = 0; i < postResponseQueue.size(); ++i) + sendProtocolItem(postResponseQueue[i].first, *postResponseQueue[i].second); } void Server_ProtocolHandler::pingClockTimeout() @@ -383,11 +467,6 @@ void Server_ProtocolHandler::pingClockTimeout() ++timeRunning; } -void Server_ProtocolHandler::enqueueProtocolItem(ProtocolItem *item) -{ - itemQueue.append(item); -} - QPair Server_ProtocolHandler::getGame(int gameId) const { if (games.contains(gameId)) @@ -395,40 +474,44 @@ QPair Server_ProtocolHandler::getGame(int gameId return QPair(0, 0); } -ResponseCode Server_ProtocolHandler::cmdPing(const Command_Ping & /*cmd*/) +Response::ResponseCode Server_ProtocolHandler::cmdPing(const Command_Ping & /*cmd*/, ResponseContainer & /*rc*/) { - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd, ResponseContainer &rc) { QString userName = QString::fromStdString(cmd.user_name()).simplified(); if (userName.isEmpty() || (userInfo != 0)) - return RespContextError; + return Response::RespContextError; authState = server->loginUser(this, userName, QString::fromStdString(cmd.password())); if (authState == PasswordWrong) - return RespWrongPassword; + return Response::RespWrongPassword; if (authState == WouldOverwriteOldSession) - return RespWouldOverwriteOldSession; - - ProtocolItem *serverMessage = new Event_ServerMessage(server->getLoginMessage()); - if (getCompressionSupport()) - serverMessage->setCompressed(true); - enqueueProtocolItem(serverMessage); - + return Response::RespWouldOverwriteOldSession; + + userName = QString::fromStdString(userInfo->name()); + Event_ServerMessage event; + event.set_message(server->getLoginMessage().toStdString()); + rc.enqueuePostResponseItem(ServerMessage::SESSION_EVENT, prepareSessionEvent(event)); + + Response_Login *re = new Response_Login; + // XXX stimmt so nicht, beim alten Kopierkonstruktor wurde hier "true" übergeben + re->mutable_user_info()->CopyFrom(*userInfo); + QList _buddyList, _ignoreList; if (authState == PasswordRight) { - buddyList = server->getBuddyList(userInfo->getName()); + buddyList = server->getBuddyList(userName); QMapIterator buddyIterator(buddyList); while (buddyIterator.hasNext()) - _buddyList.append(new ServerInfo_User(buddyIterator.next().value())); + re->add_buddy_list()->CopyFrom(*buddyIterator.next().value()); - ignoreList = server->getIgnoreList(userInfo->getName()); + ignoreList = server->getIgnoreList(userName); QMapIterator ignoreIterator(ignoreList); while (ignoreIterator.hasNext()) - _ignoreList.append(new ServerInfo_User(ignoreIterator.next().value())); + re->add_ignore_list()->CopyFrom(*ignoreIterator.next().value()); } server->serverMutex.lock(); @@ -445,13 +528,30 @@ ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd, BlaConta QMutexLocker gameLocker(&game->gameMutex); const QList &gamePlayers = game->getPlayers().values(); for (int j = 0; j < gamePlayers.size(); ++j) - if (gamePlayers[j]->getUserInfo()->getName() == userInfo->getName()) { + if (gamePlayers[j]->getUserInfo()->name() == userInfo->name()) { gamePlayers[j]->setProtocolHandler(this); game->postConnectionStatusUpdate(gamePlayers[j], true); games.insert(game->getGameId(), QPair(game, gamePlayers[j])); - enqueueProtocolItem(new Event_GameJoined(game->getGameId(), game->getDescription(), game->getHostId(), gamePlayers[j]->getPlayerId(), gamePlayers[j]->getSpectator(), game->getSpectatorsCanTalk(), game->getSpectatorsSeeEverything(), true)); - enqueueProtocolItem(GameEventContainer::makeNew(new Event_GameStateChanged(game->getGameStarted(), game->getActivePlayer(), game->getActivePhase(), game->getGameState(gamePlayers[j])), game->getGameId())); + Event_GameJoined event1; + event1.set_game_id(game->getGameId()); + event1.set_game_description(game->getDescription().toStdString()); + event1.set_host_id(game->getHostId()); + event1.set_player_id(gamePlayers[j]->getPlayerId()); + event1.set_spectator(gamePlayers[j]->getSpectator()); + event1.set_spectators_can_talk(game->getSpectatorsCanTalk()); + event1.set_spectators_see_everything(game->getSpectatorsSeeEverything()); + event1.set_resuming(true); + rc.enqueuePostResponseItem(ServerMessage::SESSION_EVENT, prepareSessionEvent(event1)); + + Event_GameStateChanged event2; + QListIterator gameStateIterator(game->getGameState(gamePlayers[j])); + while (gameStateIterator.hasNext()) + event2.add_player_list()->CopyFrom(gameStateIterator.next()); + event2.set_game_started(game->getGameStarted()); + event2.set_active_player_id(game->getActivePlayer()); + event2.set_active_phase(game->getActivePhase()); + rc.enqueuePostResponseItem(ServerMessage::GAME_EVENT_CONTAINER, game->prepareGameEvent(event2, -1)); break; } @@ -460,148 +560,148 @@ ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd, BlaConta } server->serverMutex.unlock(); - ProtocolResponse *resp = new Response_Login(-1, RespOk, new ServerInfo_User(userInfo, true), _buddyList, _ignoreList); - if (getCompressionSupport()) - resp->setCompressed(true); - bla->setResponse(resp); - return RespNothing; + rc.setResponseExtension(re); + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdMessage(const Command_Message &cmd, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdMessage(const Command_Message &cmd, ResponseContainer &rc) { if (authState == PasswordWrong) - return RespLoginNeeded; + return Response::RespLoginNeeded; QString receiver = QString::fromStdString(cmd.user_name()); Server_ProtocolHandler *userHandler = server->getUsers().value(receiver); - qDebug() << "cmdMessage: recv=" << receiver << (userHandler == 0 ? "not found" : "found"); if (!userHandler) - return RespNameNotFound; - if (userHandler->getIgnoreList().contains(userInfo->getName())) - return RespInIgnoreList; + return Response::RespNameNotFound; + if (userHandler->getIgnoreList().contains(getUserName())) + return Response::RespInIgnoreList; - QString message = QString::fromStdString(cmd.message()); - bla->enqueueItem(new Event_Message(userInfo->getName(), receiver, message)); - userHandler->sendProtocolItem(new Event_Message(userInfo->getName(), receiver, message)); - return RespOk; + Event_UserMessage event; + event.set_sender_name(userInfo->name()); + event.set_receiver_name(cmd.user_name()); + event.set_message(cmd.message()); + + SessionEvent *se = prepareSessionEvent(event); + userHandler->sendProtocolItem(*se); + rc.enqueuePreResponseItem(ServerMessage::SESSION_EVENT, se); + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(const Command_GetGamesOfUser &cmd, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(const Command_GetGamesOfUser &cmd, ResponseContainer &rc) { if (authState == PasswordWrong) - return RespLoginNeeded; + return Response::RespLoginNeeded; server->serverMutex.lock(); if (!server->getUsers().contains(QString::fromStdString(cmd.user_name()))) - return RespNameNotFound; + return Response::RespNameNotFound; + Response_GetGamesOfUser *re = new Response_GetGamesOfUser; QList roomList; QList gameList; QMapIterator roomIterator(server->getRooms()); while (roomIterator.hasNext()) { Server_Room *room = roomIterator.next().value(); room->roomMutex.lock(); - roomList.append(room->getInfo(false, true)); - gameList << room->getGamesOfUser(QString::fromStdString(cmd.user_name())); + re->add_room_list()->CopyFrom(room->getInfo(false, true)); + QListIterator gameIterator(room->getGamesOfUser(QString::fromStdString(cmd.user_name()))); + while (gameIterator.hasNext()) + re->add_game_list()->CopyFrom(gameIterator.next()); room->roomMutex.unlock(); } server->serverMutex.unlock(); - ProtocolResponse *resp = new Response_GetGamesOfUser(-1, RespOk, roomList, gameList); - if (getCompressionSupport()) - resp->setCompressed(true); - bla->setResponse(resp); - return RespNothing; + rc.setResponseExtension(re); + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdGetUserInfo(const Command_GetUserInfo &cmd, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdGetUserInfo(const Command_GetUserInfo &cmd, ResponseContainer &rc) { if (authState == PasswordWrong) - return RespLoginNeeded; + return Response::RespLoginNeeded; QString userName = QString::fromStdString(cmd.user_name()); - ServerInfo_User *result; + Response_GetUserInfo *re = new Response_GetUserInfo; if (userName.isEmpty()) - result = new ServerInfo_User(userInfo); + re->mutable_user_info()->CopyFrom(*userInfo); else { Server_ProtocolHandler *handler = server->getUsers().value(userName); if (!handler) - return RespNameNotFound; - result = new ServerInfo_User(handler->getUserInfo(), true, userInfo->getUserLevel() & ServerInfo_User::IsModerator); + return Response::RespNameNotFound; + re->mutable_user_info()->CopyFrom(handler->copyUserInfo(true, userInfo->user_level() & ServerInfo_User::IsModerator)); } - bla->setResponse(new Response_GetUserInfo(-1, RespOk, result)); - return RespNothing; + rc.setResponseExtension(re); + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdListRooms(const Command_ListRooms & /*cmd*/, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdListRooms(const Command_ListRooms & /*cmd*/, ResponseContainer &rc) { if (authState == PasswordWrong) - return RespLoginNeeded; + return Response::RespLoginNeeded; - QList eventRoomList; + Event_ListRooms event; QMapIterator roomIterator(server->getRooms()); while (roomIterator.hasNext()) - eventRoomList.append(roomIterator.next().value()->getInfo(false)); - bla->enqueueItem(new Event_ListRooms(eventRoomList)); + event.add_room_list()->CopyFrom(roomIterator.next().value()->getInfo(false)); + rc.enqueuePreResponseItem(ServerMessage::SESSION_EVENT, prepareSessionEvent(event)); acceptsRoomListChanges = true; - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoom &cmd, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc) { if (authState == PasswordWrong) - return RespLoginNeeded; + return Response::RespLoginNeeded; if (rooms.contains(cmd.room_id())) - return RespContextError; + return Response::RespContextError; Server_Room *r = server->getRooms().value(cmd.room_id(), 0); if (!r) - return RespNameNotFound; + return Response::RespNameNotFound; QMutexLocker serverLocker(&server->serverMutex); QMutexLocker roomLocker(&r->roomMutex); r->addClient(this); rooms.insert(r->getId(), r); - enqueueProtocolItem(new Event_RoomSay(r->getId(), QString(), r->getJoinMessage())); + Event_RoomSay joinMessageEvent; + joinMessageEvent.set_message(r->getJoinMessage().toStdString()); + rc.enqueuePostResponseItem(ServerMessage::ROOM_EVENT, r->prepareRoomEvent(joinMessageEvent)); - ServerInfo_Room *info = r->getInfo(true); - if (getCompressionSupport()) - info->setCompressed(true); - bla->setResponse(new Response_JoinRoom(-1, RespOk, info)); - return RespNothing; + Response_JoinRoom *re = new Response_JoinRoom; + re->mutable_room_info()->CopyFrom(r->getInfo(true)); + + rc.setResponseExtension(re); + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdListUsers(const Command_ListUsers & /*cmd*/, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdListUsers(const Command_ListUsers & /*cmd*/, ResponseContainer &rc) { if (authState == PasswordWrong) - return RespLoginNeeded; + return Response::RespLoginNeeded; - QList resultList; + Response_ListUsers *re = new Response_ListUsers; QMapIterator userIterator = server->getUsers(); while (userIterator.hasNext()) - resultList.append(new ServerInfo_User(userIterator.next().value()->getUserInfo(), false)); + re->add_user_list()->CopyFrom(userIterator.next().value()->copyUserInfo(false)); acceptsUserListChanges = true; - - ProtocolResponse *resp = new Response_ListUsers(-1, RespOk, resultList); - if (getCompressionSupport()) - resp->setCompressed(true); - bla->setResponse(resp); - return RespNothing; + + rc.setResponseExtension(re); + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdLeaveRoom(const Command_LeaveRoom & /*cmd*/, Server_Room *room) +Response::ResponseCode Server_ProtocolHandler::cmdLeaveRoom(const Command_LeaveRoom & /*cmd*/, Server_Room *room, ResponseContainer & /*rc*/) { rooms.remove(room->getId()); room->removeClient(this); - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdRoomSay(const Command_RoomSay &cmd, Server_Room *room) +Response::ResponseCode Server_ProtocolHandler::cmdRoomSay(const Command_RoomSay &cmd, Server_Room *room, ResponseContainer & /*rc*/) { QString msg = QString::fromStdString(cmd.message()); @@ -620,22 +720,22 @@ ResponseCode Server_ProtocolHandler::cmdRoomSay(const Command_RoomSay &cmd, Serv totalCount += messageCountOverTime[i]; if ((totalSize > server->getMaxMessageSizePerInterval()) || (totalCount > server->getMaxMessageCountPerInterval())) - return RespChatFlood; + return Response::RespChatFlood; } msg.replace(QChar('\n'), QChar(' ')); room->say(this, msg); - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room *room) +Response::ResponseCode Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd, Server_Room *room, ResponseContainer &rc) { if (authState == PasswordWrong) - return RespLoginNeeded; + return Response::RespLoginNeeded; if (server->getMaxGamesPerUser() > 0) - if (room->getGamesCreatedByUser(userInfo->getName()) >= server->getMaxGamesPerUser()) - return RespContextError; + if (room->getGamesCreatedByUser(getUserName()) >= server->getMaxGamesPerUser()) + return Response::RespContextError; QList gameTypes; for (int i = cmd.game_type_ids_size() - 1; i >= 0; --i) @@ -651,90 +751,134 @@ ResponseCode Server_ProtocolHandler::cmdCreateGame(const Command_CreateGame &cmd QMutexLocker gameListLocker(&gameListMutex); games.insert(game->getGameId(), QPair(game, creator)); - sendProtocolItem(new Event_GameJoined(game->getGameId(), game->getDescription(), creator->getPlayerId(), creator->getPlayerId(), false, game->getSpectatorsCanTalk(), game->getSpectatorsSeeEverything(), false)); - sendProtocolItem(GameEventContainer::makeNew(new Event_GameStateChanged(game->getGameStarted(), game->getActivePlayer(), game->getActivePhase(), game->getGameState(creator)), game->getGameId())); + Event_GameJoined event1; + event1.set_game_id(game->getGameId()); + event1.set_game_description(game->getDescription().toStdString()); + event1.set_host_id(creator->getPlayerId()); + event1.set_player_id(creator->getPlayerId()); + event1.set_spectator(false); + event1.set_spectators_can_talk(game->getSpectatorsCanTalk()); + event1.set_spectators_see_everything(game->getSpectatorsSeeEverything()); + event1.set_resuming(false); + rc.enqueuePreResponseItem(ServerMessage::SESSION_EVENT, prepareSessionEvent(event1)); + Event_GameStateChanged event2; + QListIterator gameStateIterator(game->getGameState(creator)); + while (gameStateIterator.hasNext()) + event2.add_player_list()->CopyFrom(gameStateIterator.next()); + event2.set_game_started(game->getGameStarted()); + event2.set_active_player_id(game->getActivePlayer()); + event2.set_active_phase(game->getActivePhase()); + rc.enqueuePreResponseItem(ServerMessage::GAME_EVENT_CONTAINER, game->prepareGameEvent(event2, -1)); + game->gameMutex.unlock(); - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdJoinGame(const Command_JoinGame &cmd, Server_Room *room) +Response::ResponseCode Server_ProtocolHandler::cmdJoinGame(const Command_JoinGame &cmd, Server_Room *room, ResponseContainer &rc) { if (authState == PasswordWrong) - return RespLoginNeeded; + return Response::RespLoginNeeded; QMutexLocker gameListLocker(&gameListMutex); if (games.contains(cmd.game_id())) - return RespContextError; + return Response::RespContextError; Server_Game *g = room->getGames().value(cmd.game_id()); if (!g) - return RespNameNotFound; + return Response::RespNameNotFound; QMutexLocker locker(&g->gameMutex); - ResponseCode result = g->checkJoin(userInfo, QString::fromStdString(cmd.password()), cmd.spectator(), cmd.override_restrictions()); - if (result == RespOk) { + Response::ResponseCode result = g->checkJoin(userInfo, QString::fromStdString(cmd.password()), cmd.spectator(), cmd.override_restrictions()); + if (result == Response::RespOk) { Server_Player *player = g->addPlayer(this, cmd.spectator()); games.insert(cmd.game_id(), QPair(g, player)); - enqueueProtocolItem(new Event_GameJoined(cmd.game_id(), g->getDescription(), g->getHostId(), player->getPlayerId(), cmd.spectator(), g->getSpectatorsCanTalk(), g->getSpectatorsSeeEverything(), false)); - enqueueProtocolItem(GameEventContainer::makeNew(new Event_GameStateChanged(g->getGameStarted(), g->getActivePlayer(), g->getActivePhase(), g->getGameState(player)), cmd.game_id())); + + Event_GameJoined event1; + event1.set_game_id(g->getGameId()); + event1.set_game_description(g->getDescription().toStdString()); + event1.set_host_id(g->getHostId()); + event1.set_player_id(player->getPlayerId()); + event1.set_spectator(cmd.spectator()); + event1.set_spectators_can_talk(g->getSpectatorsCanTalk()); + event1.set_spectators_see_everything(g->getSpectatorsSeeEverything()); + event1.set_resuming(false); + rc.enqueuePostResponseItem(ServerMessage::SESSION_EVENT, prepareSessionEvent(event1)); + + Event_GameStateChanged event2; + QListIterator gameStateIterator(g->getGameState(player)); + while (gameStateIterator.hasNext()) + event2.add_player_list()->CopyFrom(gameStateIterator.next()); + event2.set_game_started(g->getGameStarted()); + event2.set_active_player_id(g->getActivePlayer()); + event2.set_active_phase(g->getActivePhase()); + rc.enqueuePostResponseItem(ServerMessage::GAME_EVENT_CONTAINER, g->prepareGameEvent(event2, -1)); } return result; } -ResponseCode Server_ProtocolHandler::cmdLeaveGame(const Command_LeaveGame & /*cmd*/, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdLeaveGame(const Command_LeaveGame & /*cmd*/, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage & /*ges*/) { game->removePlayer(player); - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdKickFromGame(const Command_KickFromGame &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdKickFromGame(const Command_KickFromGame &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage & /*ges*/) { - if ((game->getHostId() != player->getPlayerId()) && !(userInfo->getUserLevel() & ServerInfo_User::IsModerator)) - return RespFunctionNotAllowed; + if ((game->getHostId() != player->getPlayerId()) && !(userInfo->user_level() & ServerInfo_User::IsModerator)) + return Response::RespFunctionNotAllowed; if (!game->kickPlayer(cmd.player_id())) - return RespNameNotFound; + return Response::RespNameNotFound; - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdDeckSelect(const Command_DeckSelect &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdDeckSelect(const Command_DeckSelect &cmd, Server_Game * /*game*/, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; DeckList *deck; if (cmd.has_deck_id()) { try { deck = getDeckFromDatabase(cmd.deck_id()); - } catch(ResponseCode r) { + } catch(Response::ResponseCode r) { return r; } } else deck = new DeckList(QString::fromStdString(cmd.deck())); - + player->setDeck(deck); - game->sendGameEvent(new Event_PlayerPropertiesChanged(player->getPlayerId(), player->getProperties()), new Context_DeckSelect(deck->getDeckHash())); - - bla->setResponse(new Response_DeckDownload(-1, RespOk, new DeckList(deck))); - return RespNothing; + Event_PlayerPropertiesChanged event; + event.mutable_player_properties()->CopyFrom(player->getProperties()); + ges.enqueueGameEventPublic(event, player->getPlayerId()); + + Context_DeckSelect *context = new Context_DeckSelect; + context->set_deck_hash(deck->getDeckHash().toStdString()); + ges.setGameEventContext(context); + + Response_DeckDownload *re = new Response_DeckDownload; + re->set_deck(deck->writeToString_Native().toStdString()); + + rc.setResponseExtension(re); + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdSetSideboardPlan(const Command_SetSideboardPlan &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdSetSideboardPlan(const Command_SetSideboardPlan &cmd, Server_Game * /*game*/, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage & /*ges*/) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (player->getReadyStart()) - return RespContextError; + return Response::RespContextError; DeckList *deck = player->getDeck(); if (!deck) - return RespContextError; + return Response::RespContextError; QList sideboardPlan; for (int i = 0; i < cmd.move_list_size(); ++i) { @@ -744,80 +888,94 @@ ResponseCode Server_ProtocolHandler::cmdSetSideboardPlan(const Command_SetSidebo deck->setCurrentSideboardPlan(sideboardPlan); for (int i = 0; i < sideboardPlan.size(); ++i) delete sideboardPlan[i]; - // TEMPORARY HACK - return RespOk; + // XXX TEMPORARY HACK + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdConcede(const Command_Concede & /*cmd*/, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdConcede(const Command_Concede & /*cmd*/, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; player->setConceded(true); game->removeArrowsToPlayer(player); player->clearZones(); - game->sendGameEvent(new Event_PlayerPropertiesChanged(player->getPlayerId(), player->getProperties()), new Context_Concede); + + Event_PlayerPropertiesChanged event; + event.mutable_player_properties()->CopyFrom(player->getProperties()); + ges.enqueueGameEventPublic(event, player->getPlayerId()); + ges.setGameEventContext(new Context_Concede()); + game->stopGameIfFinished(); if (game->getGameStarted() && (game->getActivePlayer() == player->getPlayerId())) game->nextTurn(); - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdReadyStart(const Command_ReadyStart &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdReadyStart(const Command_ReadyStart &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!player->getDeck() || game->getGameStarted()) - return RespContextError; + return Response::RespContextError; if (player->getReadyStart() == cmd.ready()) - return RespContextError; + return Response::RespContextError; player->setReadyStart(cmd.ready()); - game->sendGameEvent(new Event_PlayerPropertiesChanged(player->getPlayerId(), player->getProperties()), new Context_ReadyStart); - game->startGameIfReady(); - return RespOk; -} - -ResponseCode Server_ProtocolHandler::cmdGameSay(const Command_GameSay &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) -{ - if (player->getSpectator() && !game->getSpectatorsCanTalk() && !(userInfo->getUserLevel() & ServerInfo_User::IsModerator)) - return RespFunctionNotAllowed; - game->sendGameEvent(new Event_Say(player->getPlayerId(), QString::fromStdString(cmd.message()))); - return RespOk; + Event_PlayerPropertiesChanged event; + event.mutable_player_properties()->CopyFrom(player->getProperties()); + ges.enqueueGameEventPublic(event, player->getPlayerId()); + ges.setGameEventContext(new Context_ReadyStart()); + + game->startGameIfReady(); + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdShuffle(const Command_Shuffle & /*cmd*/, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdGameSay(const Command_GameSay &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) +{ + if (player->getSpectator() && !game->getSpectatorsCanTalk() && !(userInfo->user_level() & ServerInfo_User::IsModerator)) + return Response::RespFunctionNotAllowed; + + Event_GameSay event; + event.set_message(cmd.message()); + ges.enqueueGameEventPublic(event, player->getPlayerId()); + + return Response::RespOk; +} + +Response::ResponseCode Server_ProtocolHandler::cmdShuffle(const Command_Shuffle & /*cmd*/, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; player->getZones().value("deck")->shuffle(); - game->sendGameEvent(new Event_Shuffle(player->getPlayerId())); - return RespOk; + + ges.enqueueGameEventPublic(Event_Shuffle(), player->getPlayerId()); + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdMulligan(const Command_Mulligan & /*cmd*/, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdMulligan(const Command_Mulligan & /*cmd*/, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; Server_CardZone *hand = player->getZones().value("hand"); int number = (hand->cards.size() <= 1) ? player->getInitialCards() : hand->cards.size() - 1; @@ -826,128 +984,136 @@ ResponseCode Server_ProtocolHandler::cmdMulligan(const Command_Mulligan & /*cmd* while (!hand->cards.isEmpty()) { CardToMove *cardToMove = new CardToMove; cardToMove->set_card_id(hand->cards.first()->getId()); - player->moveCard(bla, hand, QList() << cardToMove, deck, 0, 0, false); + player->moveCard(ges, hand, QList() << cardToMove, deck, 0, 0, false); delete cardToMove; } - + deck->shuffle(); - bla->enqueueGameEventPrivate(new Event_Shuffle(player->getPlayerId()), game->getGameId()); - bla->enqueueGameEventOmniscient(new Event_Shuffle(player->getPlayerId()), game->getGameId()); - bla->enqueueGameEventPublic(new Event_Shuffle(player->getPlayerId()), game->getGameId()); + ges.enqueueGameEvent(Event_Shuffle(), player->getPlayerId()); - player->drawCards(bla, number); + player->drawCards(ges, number); if (number == player->getInitialCards()) number = -1; - bla->getGameEventQueuePrivate()->setContext(new Context_Mulligan(number)); - bla->getGameEventQueuePublic()->setContext(new Context_Mulligan(number)); - bla->getGameEventQueueOmniscient()->setContext(new Context_Mulligan(number)); - - return RespOk; -} - -ResponseCode Server_ProtocolHandler::cmdRollDie(const Command_RollDie &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) -{ - if (player->getSpectator()) - return RespFunctionNotAllowed; - if (player->getConceded()) - return RespContextError; - game->sendGameEvent(new Event_RollDie(player->getPlayerId(), cmd.sides(), rng->getNumber(1, cmd.sides()))); - return RespOk; + Context_Mulligan *context = new Context_Mulligan; + context->set_number(number); + ges.setGameEventContext(context); + + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdDrawCards(const Command_DrawCards &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdRollDie(const Command_RollDie &cmd, Server_Game * /*game*/, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; + if (player->getConceded()) + return Response::RespContextError; + + Event_RollDie event; + event.set_sides(cmd.sides()); + event.set_value(rng->getNumber(1, cmd.sides())); + ges.enqueueGameEvent(event, player->getPlayerId()); + + return Response::RespOk; +} + +Response::ResponseCode Server_ProtocolHandler::cmdDrawCards(const Command_DrawCards &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) +{ + if (player->getSpectator()) + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; - return player->drawCards(bla, cmd.number()); + return player->drawCards(ges, cmd.number()); } -ResponseCode Server_ProtocolHandler::cmdUndoDraw(const Command_UndoDraw & /*cmd*/, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdUndoDraw(const Command_UndoDraw & /*cmd*/, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; - return player->undoDraw(bla); + return player->undoDraw(ges); } -ResponseCode Server_ProtocolHandler::cmdMoveCard(const Command_MoveCard &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdMoveCard(const Command_MoveCard &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; QList cardsToMove; for (int i = 0; i < cmd.cards_to_move().card_size(); ++i) cardsToMove.append(&cmd.cards_to_move().card(i)); - return player->moveCard(bla, QString::fromStdString(cmd.start_zone()), cardsToMove, cmd.target_player_id(), QString::fromStdString(cmd.target_zone()), cmd.x(), cmd.y()); + return player->moveCard(ges, QString::fromStdString(cmd.start_zone()), cardsToMove, cmd.target_player_id(), QString::fromStdString(cmd.target_zone()), cmd.x(), cmd.y()); } -ResponseCode Server_ProtocolHandler::cmdFlipCard(const Command_FlipCard &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdFlipCard(const Command_FlipCard &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; Server_CardZone *zone = player->getZones().value(QString::fromStdString(cmd.zone())); if (!zone) - return RespNameNotFound; + return Response::RespNameNotFound; if (!zone->hasCoords()) - return RespContextError; + return Response::RespContextError; Server_Card *card = zone->getCard(cmd.card_id()); if (!card) - return RespNameNotFound; + return Response::RespNameNotFound; const bool faceDown = cmd.face_down(); if (faceDown == card->getFaceDown()) - return RespContextError; + return Response::RespContextError; card->setFaceDown(faceDown); - bla->enqueueGameEventPrivate(new Event_FlipCard(player->getPlayerId(), zone->getName(), card->getId(), card->getName(), faceDown), game->getGameId()); - bla->enqueueGameEventPublic(new Event_FlipCard(player->getPlayerId(), zone->getName(), card->getId(), card->getName(), faceDown), game->getGameId()); - return RespOk; + Event_FlipCard event; + event.set_zone_name(zone->getName().toStdString()); + event.set_card_id(card->getId()); + event.set_card_name(card->getName().toStdString()); + event.set_face_down(faceDown); + ges.enqueueGameEvent(event, player->getPlayerId()); + + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdAttachCard(const Command_AttachCard &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdAttachCard(const Command_AttachCard &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; Server_CardZone *startzone = player->getZones().value(QString::fromStdString(cmd.start_zone())); if (!startzone) - return RespNameNotFound; + return Response::RespNameNotFound; Server_Card *card = startzone->getCard(cmd.card_id()); if (!card) - return RespNameNotFound; + return Response::RespNameNotFound; Server_Player *targetPlayer = 0; Server_CardZone *targetzone = 0; @@ -956,24 +1122,24 @@ ResponseCode Server_ProtocolHandler::cmdAttachCard(const Command_AttachCard &cmd if (cmd.has_target_player_id()) { targetPlayer = game->getPlayer(cmd.target_player_id()); if (!targetPlayer) - return RespNameNotFound; + return Response::RespNameNotFound; } else if (!card->getParentCard()) - return RespContextError; + return Response::RespContextError; if (targetPlayer) targetzone = targetPlayer->getZones().value(QString::fromStdString(cmd.target_zone())); if (targetzone) { // This is currently enough to make sure cards don't get attached to a card that is not on the table. // Possibly a flag will have to be introduced for this sometime. if (!targetzone->hasCoords()) - return RespContextError; + return Response::RespContextError; if (cmd.has_target_card_id()) targetCard = targetzone->getCard(cmd.target_card_id()); if (targetCard) if (targetCard->getParentCard()) - return RespContextError; + return Response::RespContextError; } if (!startzone->hasCoords()) - return RespContextError; + return Response::RespContextError; // Get all arrows pointing to or originating from the card being attached and delete them. QMapIterator playerIterator(game->getPlayers()); @@ -988,8 +1154,9 @@ ResponseCode Server_ProtocolHandler::cmdAttachCard(const Command_AttachCard &cmd toDelete.append(a); } for (int i = 0; i < toDelete.size(); ++i) { - bla->enqueueGameEventPrivate(new Event_DeleteArrow(p->getPlayerId(), toDelete[i]->getId()), game->getGameId()); - bla->enqueueGameEventPublic(new Event_DeleteArrow(p->getPlayerId(), toDelete[i]->getId()), game->getGameId()); + Event_DeleteArrow event; + event.set_arrow_id(toDelete[i]->getId()); + ges.enqueueGameEvent(event, p->getPlayerId()); p->deleteArrow(toDelete[i]->getId()); } } @@ -999,39 +1166,46 @@ ResponseCode Server_ProtocolHandler::cmdAttachCard(const Command_AttachCard &cmd // Make a copy of the list because its contents change during the loop otherwise. QList attachedList = card->getAttachedCards(); for (int i = 0; i < attachedList.size(); ++i) - attachedList[i]->getZone()->getPlayer()->unattachCard(bla, attachedList[i]); + attachedList[i]->getZone()->getPlayer()->unattachCard(ges, attachedList[i]); if (targetzone->isColumnStacked(targetCard->getX(), targetCard->getY())) { CardToMove *cardToMove = new CardToMove; cardToMove->set_card_id(targetCard->getId()); - targetPlayer->moveCard(bla, targetzone, QList() << cardToMove, targetzone, targetzone->getFreeGridColumn(-2, targetCard->getY(), targetCard->getName()), targetCard->getY(), targetCard->getFaceDown()); + targetPlayer->moveCard(ges, targetzone, QList() << cardToMove, targetzone, targetzone->getFreeGridColumn(-2, targetCard->getY(), targetCard->getName()), targetCard->getY(), targetCard->getFaceDown()); delete cardToMove; } card->setParentCard(targetCard); card->setCoords(-1, card->getY()); - bla->enqueueGameEventPrivate(new Event_AttachCard(player->getPlayerId(), startzone->getName(), card->getId(), targetPlayer->getPlayerId(), targetzone->getName(), targetCard->getId()), game->getGameId()); - bla->enqueueGameEventPublic(new Event_AttachCard(player->getPlayerId(), startzone->getName(), card->getId(), targetPlayer->getPlayerId(), targetzone->getName(), targetCard->getId()), game->getGameId()); - startzone->fixFreeSpaces(bla); + + Event_AttachCard event; + event.set_start_zone(startzone->getName().toStdString()); + event.set_card_id(card->getId()); + event.set_target_player_id(targetPlayer->getPlayerId()); + event.set_target_zone(targetzone->getName().toStdString()); + event.set_target_card_id(targetCard->getId()); + ges.enqueueGameEvent(event, player->getPlayerId()); + + startzone->fixFreeSpaces(ges); } else - player->unattachCard(bla, card); + player->unattachCard(ges, card); - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdCreateToken(const Command_CreateToken &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdCreateToken(const Command_CreateToken &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; Server_CardZone *zone = player->getZones().value(QString::fromStdString(cmd.zone())); if (!zone) - return RespNameNotFound; + return Response::RespNameNotFound; QString cardName = QString::fromStdString(cmd.card_name()); int x = cmd.x(); @@ -1051,25 +1225,36 @@ ResponseCode Server_ProtocolHandler::cmdCreateToken(const Command_CreateToken &c card->setDestroyOnZoneChange(cmd.destroy_on_zone_change()); zone->insertCard(card, x, y); - game->sendGameEvent(new Event_CreateToken(player->getPlayerId(), zone->getName(), card->getId(), card->getName(), card->getColor(), card->getPT(), card->getAnnotation(), card->getDestroyOnZoneChange(), x, y)); - return RespOk; + Event_CreateToken event; + event.set_zone_name(zone->getName().toStdString()); + event.set_card_id(card->getId()); + event.set_card_name(card->getName().toStdString()); + event.set_color(card->getColor().toStdString()); + event.set_pt(card->getPT().toStdString()); + event.set_annotation(card->getAnnotation().toStdString()); + event.set_destroy_on_zone_change(card->getDestroyOnZoneChange()); + event.set_x(x); + event.set_y(y); + ges.enqueueGameEvent(event, player->getPlayerId()); + + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdCreateArrow(const Command_CreateArrow &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdCreateArrow(const Command_CreateArrow &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; Server_Player *startPlayer = game->getPlayer(cmd.start_player_id()); Server_Player *targetPlayer = game->getPlayer(cmd.target_player_id()); if (!startPlayer || !targetPlayer) - return RespNameNotFound; + return Response::RespNameNotFound; QString startZoneName = QString::fromStdString(cmd.start_zone()); Server_CardZone *startZone = startPlayer->getZones().value(startZoneName); QString targetZoneName = QString::fromStdString(cmd.target_zone()); @@ -1078,16 +1263,16 @@ ResponseCode Server_ProtocolHandler::cmdCreateArrow(const Command_CreateArrow &c if (!playerTarget) targetZone = targetPlayer->getZones().value(targetZoneName); if (!startZone || (!targetZone && !playerTarget)) - return RespNameNotFound; - if (startZone->getType() != PublicZone) - return RespContextError; + return Response::RespNameNotFound; + if (startZone->getType() != ServerInfo_Zone::PublicZone) + return Response::RespContextError; Server_Card *startCard = startZone->getCard(cmd.start_card_id()); if (!startCard) - return RespNameNotFound; + return Response::RespNameNotFound; Server_Card *targetCard = 0; if (!playerTarget) { - if (targetZone->getType() != PublicZone) - return RespContextError; + if (targetZone->getType() != ServerInfo_Zone::PublicZone) + return Response::RespContextError; targetCard = targetZone->getCard(cmd.target_card_id()); } @@ -1097,319 +1282,385 @@ ResponseCode Server_ProtocolHandler::cmdCreateArrow(const Command_CreateArrow &c else targetItem = targetCard; if (!targetItem) - return RespNameNotFound; + return Response::RespNameNotFound; QMapIterator arrowIterator(player->getArrows()); while (arrowIterator.hasNext()) { Server_Arrow *temp = arrowIterator.next().value(); if ((temp->getStartCard() == startCard) && (temp->getTargetItem() == targetItem)) - return RespContextError; + return Response::RespContextError; } Server_Arrow *arrow = new Server_Arrow(player->newArrowId(), startCard, targetItem, cmd.arrow_color()); player->addArrow(arrow); - game->sendGameEvent(new Event_CreateArrows(player->getPlayerId(), QList() << new ServerInfo_Arrow( - arrow->getId(), - startPlayer->getPlayerId(), - startZoneName, - startCard->getId(), - targetPlayer->getPlayerId(), - targetZoneName, - cmd.target_card_id(), - cmd.arrow_color() - ))); - return RespOk; + + Event_CreateArrow event; + ServerInfo_Arrow *arrowInfo = event.mutable_arrow_info(); + arrowInfo->set_id(arrow->getId()); + arrowInfo->set_start_player_id(startPlayer->getPlayerId()); + arrowInfo->set_start_zone(startZoneName.toStdString()); + arrowInfo->set_start_card_id(startCard->getId()); + arrowInfo->set_target_player_id(targetPlayer->getPlayerId()); + arrowInfo->set_target_zone(targetZoneName.toStdString()); + arrowInfo->set_target_card_id(cmd.target_card_id()); + arrowInfo->mutable_arrow_color()->CopyFrom(cmd.arrow_color()); + ges.enqueueGameEvent(event, player->getPlayerId()); + + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdDeleteArrow(const Command_DeleteArrow &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdDeleteArrow(const Command_DeleteArrow &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; if (!player->deleteArrow(cmd.arrow_id())) - return RespNameNotFound; + return Response::RespNameNotFound; - game->sendGameEvent(new Event_DeleteArrow(player->getPlayerId(), cmd.arrow_id())); - return RespOk; + Event_DeleteArrow event; + event.set_arrow_id(cmd.arrow_id()); + ges.enqueueGameEvent(event, player->getPlayerId()); + + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdSetCardAttr(const Command_SetCardAttr &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdSetCardAttr(const Command_SetCardAttr &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; - return player->setCardAttrHelper(bla, 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(), QString::fromStdString(cmd.attr_name()), QString::fromStdString(cmd.attr_value())); } -ResponseCode Server_ProtocolHandler::cmdSetCardCounter(const Command_SetCardCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdSetCardCounter(const Command_SetCardCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; Server_CardZone *zone = player->getZones().value(QString::fromStdString(cmd.zone())); if (!zone) - return RespNameNotFound; + return Response::RespNameNotFound; if (!zone->hasCoords()) - return RespContextError; + return Response::RespContextError; Server_Card *card = zone->getCard(cmd.card_id()); if (!card) - return RespNameNotFound; + return Response::RespNameNotFound; card->setCounter(cmd.counter_id(), cmd.counter_value()); - bla->enqueueGameEventPrivate(new Event_SetCardCounter(player->getPlayerId(), zone->getName(), card->getId(), cmd.counter_id(), cmd.counter_value()), game->getGameId()); - bla->enqueueGameEventPublic(new Event_SetCardCounter(player->getPlayerId(), zone->getName(), card->getId(), cmd.counter_id(), cmd.counter_value()), game->getGameId()); - return RespOk; + Event_SetCardCounter event; + event.set_zone_name(zone->getName().toStdString()); + event.set_card_id(card->getId()); + event.set_counter_id(cmd.counter_id()); + event.set_counter_value(cmd.counter_value()); + ges.enqueueGameEvent(event, player->getPlayerId()); + + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdIncCardCounter(const Command_IncCardCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdIncCardCounter(const Command_IncCardCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer &/*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; Server_CardZone *zone = player->getZones().value(QString::fromStdString(cmd.zone())); if (!zone) - return RespNameNotFound; + return Response::RespNameNotFound; if (!zone->hasCoords()) - return RespContextError; + return Response::RespContextError; Server_Card *card = zone->getCard(cmd.card_id()); if (!card) - return RespNameNotFound; + return Response::RespNameNotFound; int newValue = card->getCounter(cmd.counter_id()) + cmd.counter_delta(); card->setCounter(cmd.counter_id(), newValue); - bla->enqueueGameEventPrivate(new Event_SetCardCounter(player->getPlayerId(), zone->getName(), card->getId(), cmd.counter_id(), newValue), game->getGameId()); - bla->enqueueGameEventPublic(new Event_SetCardCounter(player->getPlayerId(), zone->getName(), card->getId(), cmd.counter_id(), newValue), game->getGameId()); - return RespOk; + Event_SetCardCounter event; + event.set_zone_name(zone->getName().toStdString()); + event.set_card_id(card->getId()); + event.set_counter_id(cmd.counter_id()); + event.set_counter_value(newValue); + ges.enqueueGameEvent(event, player->getPlayerId()); + + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdIncCounter(const Command_IncCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdIncCounter(const Command_IncCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; const QMap counters = player->getCounters(); Server_Counter *c = counters.value(cmd.counter_id(), 0); if (!c) - return RespNameNotFound; + return Response::RespNameNotFound; c->setCount(c->getCount() + cmd.delta()); - game->sendGameEvent(new Event_SetCounter(player->getPlayerId(), c->getId(), c->getCount())); - return RespOk; + + Event_SetCounter event; + event.set_counter_id(c->getId()); + event.set_value(c->getCount()); + ges.enqueueGameEvent(event, player->getPlayerId()); + + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdCreateCounter(const Command_CreateCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdCreateCounter(const Command_CreateCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; Server_Counter *c = new Server_Counter(player->newCounterId(), QString::fromStdString(cmd.counter_name()), cmd.counter_color(), cmd.radius(), cmd.value()); player->addCounter(c); - game->sendGameEvent(new Event_CreateCounters(player->getPlayerId(), QList() << new ServerInfo_Counter(c->getId(), c->getName(), c->getColor(), c->getRadius(), c->getCount()))); - return RespOk; + Event_CreateCounter event; + ServerInfo_Counter *counterInfo = event.mutable_counter_info(); + counterInfo->set_id(c->getId()); + counterInfo->set_name(c->getName().toStdString()); + counterInfo->mutable_counter_color()->CopyFrom(cmd.counter_color()); + counterInfo->set_radius(c->getRadius()); + counterInfo->set_count(c->getCount()); + ges.enqueueGameEvent(event, player->getPlayerId()); + + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdSetCounter(const Command_SetCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdSetCounter(const Command_SetCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; Server_Counter *c = player->getCounters().value(cmd.counter_id(), 0);; if (!c) - return RespNameNotFound; + return Response::RespNameNotFound; c->setCount(cmd.value()); - game->sendGameEvent(new Event_SetCounter(player->getPlayerId(), c->getId(), c->getCount())); - return RespOk; + + Event_SetCounter event; + event.set_counter_id(c->getId()); + event.set_value(c->getCount()); + ges.enqueueGameEvent(event, player->getPlayerId()); + + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdDelCounter(const Command_DelCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdDelCounter(const Command_DelCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; if (!player->deleteCounter(cmd.counter_id())) - return RespNameNotFound; - game->sendGameEvent(new Event_DelCounter(player->getPlayerId(), cmd.counter_id())); - return RespOk; + return Response::RespNameNotFound; + + Event_DelCounter event; + event.set_counter_id(cmd.counter_id()); + ges.enqueueGameEvent(event, player->getPlayerId()); + + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdNextTurn(const Command_NextTurn & /*cmd*/, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdNextTurn(const Command_NextTurn & /*cmd*/, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage & /*ges*/) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; game->nextTurn(); - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdSetActivePhase(const Command_SetActivePhase &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdSetActivePhase(const Command_SetActivePhase &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage & /*ges*/) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; if (game->getActivePlayer() != player->getPlayerId()) - return RespContextError; + return Response::RespContextError; game->setActivePhase(cmd.phase()); - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdDumpZone(const Command_DumpZone &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdDumpZone(const Command_DumpZone &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges) { if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; Server_Player *otherPlayer = game->getPlayer(cmd.player_id()); if (!otherPlayer) - return RespNameNotFound; + return Response::RespNameNotFound; Server_CardZone *zone = otherPlayer->getZones().value(QString::fromStdString(cmd.zone_name())); if (!zone) - return RespNameNotFound; - if (!((zone->getType() == PublicZone) || (player == otherPlayer))) - return RespContextError; + return Response::RespNameNotFound; + if (!((zone->getType() == ServerInfo_Zone::PublicZone) || (player == otherPlayer))) + return Response::RespContextError; int numberCards = cmd.number_cards(); + + Response_DumpZone *re = new Response_DumpZone; + ServerInfo_Zone *zoneInfo = re->mutable_zone_info(); + zoneInfo->set_name(zone->getName().toStdString()); + zoneInfo->set_type(zone->getType()); + zoneInfo->set_with_coords(zone->hasCoords()); + zoneInfo->set_card_count(numberCards < zone->cards.size() ? zone->cards.size() : numberCards); + QList respCardList; for (int i = 0; (i < zone->cards.size()) && (i < numberCards || numberCards == -1); ++i) { Server_Card *card = zone->cards[i]; QString displayedName = card->getFaceDown() ? QString() : card->getName(); - if (zone->getType() == HiddenZone) - respCardList.append(new ServerInfo_Card(i, displayedName)); + ServerInfo_Card *cardInfo = zoneInfo->add_card_list(); + cardInfo->set_name(displayedName.toStdString()); + if (zone->getType() == ServerInfo_Zone::HiddenZone) + cardInfo->set_id(i); else { + cardInfo->set_id(card->getId()); + cardInfo->set_x(card->getX()); + cardInfo->set_y(card->getY()); + cardInfo->set_face_down(card->getFaceDown()); + cardInfo->set_tapped(card->getTapped()); + cardInfo->set_attacking(card->getAttacking()); + cardInfo->set_color(card->getColor().toStdString()); + cardInfo->set_pt(card->getPT().toStdString()); + cardInfo->set_annotation(card->getAnnotation().toStdString()); + cardInfo->set_destroy_on_zone_change(card->getDestroyOnZoneChange()); + cardInfo->set_doesnt_untap(card->getDoesntUntap()); + QList cardCounterList; QMapIterator cardCounterIterator(card->getCounters()); while (cardCounterIterator.hasNext()) { cardCounterIterator.next(); - cardCounterList.append(new ServerInfo_CardCounter(cardCounterIterator.key(), cardCounterIterator.value())); + ServerInfo_CardCounter *counterInfo = cardInfo->add_counter_list(); + counterInfo->set_id(cardCounterIterator.key()); + counterInfo->set_value(cardCounterIterator.value()); } - int attachPlayerId = -1; - QString attachZone; - int attachCardId = -1; if (card->getParentCard()) { - attachPlayerId = card->getParentCard()->getZone()->getPlayer()->getPlayerId(); - attachZone = card->getParentCard()->getZone()->getName(); - attachCardId = card->getParentCard()->getId(); + cardInfo->set_attach_player_id(card->getParentCard()->getZone()->getPlayer()->getPlayerId()); + cardInfo->set_attach_zone(card->getParentCard()->getZone()->getName().toStdString()); + cardInfo->set_attach_card_id(card->getParentCard()->getId()); } - - respCardList.append(new ServerInfo_Card(card->getId(), displayedName, card->getX(), card->getY(), card->getFaceDown(), card->getTapped(), card->getAttacking(), card->getColor(), card->getPT(), card->getAnnotation(), card->getDestroyOnZoneChange(), card->getDoesntUntap(), cardCounterList, attachPlayerId, attachZone, attachCardId)); } } - if (zone->getType() == HiddenZone) { + if (zone->getType() == ServerInfo_Zone::HiddenZone) { zone->setCardsBeingLookedAt(numberCards); - game->sendGameEvent(new Event_DumpZone(player->getPlayerId(), otherPlayer->getPlayerId(), zone->getName(), numberCards)); + + Event_DumpZone event; + event.set_zone_owner_id(otherPlayer->getPlayerId()); + event.set_zone_name(zone->getName().toStdString()); + event.set_number_cards(numberCards); + ges.enqueueGameEvent(event, player->getPlayerId()); } - bla->setResponse(new Response_DumpZone(-1, RespOk, new ServerInfo_Zone(zone->getName(), zone->getType(), zone->hasCoords(), numberCards < zone->cards.size() ? zone->cards.size() : numberCards, respCardList))); - return RespNothing; + rc.setResponseExtension(re); + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdStopDumpZone(const Command_StopDumpZone &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdStopDumpZone(const Command_StopDumpZone &cmd, Server_Game *game, Server_Player *player, ResponseContainer & /*rc*/, GameEventStorage &ges) { if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; Server_Player *otherPlayer = game->getPlayer(cmd.player_id()); if (!otherPlayer) - return RespNameNotFound; + return Response::RespNameNotFound; Server_CardZone *zone = otherPlayer->getZones().value(QString::fromStdString(cmd.zone_name())); if (!zone) - return RespNameNotFound; + return Response::RespNameNotFound; - if (zone->getType() == HiddenZone) { + if (zone->getType() == ServerInfo_Zone::HiddenZone) { zone->setCardsBeingLookedAt(0); - game->sendGameEvent(new Event_StopDumpZone(player->getPlayerId(), cmd.player_id(), zone->getName())); + + Event_StopDumpZone event; + event.set_zone_owner_id(cmd.player_id()); + event.set_zone_name(zone->getName().toStdString()); + ges.enqueueGameEvent(event, player->getPlayerId()); } - return RespOk; + return Response::RespOk; } -ResponseCode Server_ProtocolHandler::cmdRevealCards(const Command_RevealCards &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla) +Response::ResponseCode Server_ProtocolHandler::cmdRevealCards(const Command_RevealCards &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges) { if (player->getSpectator()) - return RespFunctionNotAllowed; + return Response::RespFunctionNotAllowed; if (!game->getGameStarted()) - return RespGameNotStarted; + return Response::RespGameNotStarted; if (player->getConceded()) - return RespContextError; + return Response::RespContextError; Server_Player *otherPlayer = 0; if (cmd.has_player_id()) { otherPlayer = game->getPlayer(cmd.player_id()); if (!otherPlayer) - return RespNameNotFound; + return Response::RespNameNotFound; } Server_CardZone *zone = player->getZones().value(QString::fromStdString(cmd.zone_name())); if (!zone) - return RespNameNotFound; + return Response::RespNameNotFound; QList cardsToReveal; if (!cmd.has_card_id()) cardsToReveal = zone->cards; else if (cmd.card_id() == -2) { if (zone->cards.isEmpty()) - return RespContextError; + return Response::RespContextError; cardsToReveal.append(zone->cards.at(rng->getNumber(0, zone->cards.size() - 1))); } else { Server_Card *card = zone->getCard(cmd.card_id()); if (!card) - return RespNameNotFound; + return Response::RespNameNotFound; cardsToReveal.append(card); } @@ -1447,5 +1698,5 @@ ResponseCode Server_ProtocolHandler::cmdRevealCards(const Command_RevealCards &c bla->enqueueGameEventOmniscient(new Event_RevealCards(player->getPlayerId(), zone->getName(), cmd.card_id(), otherPlayer->getPlayerId(), respCardListOmniscient), game->getGameId()); } - return RespOk; + return Response::RespOk; } diff --git a/common/server_protocolhandler.h b/common/server_protocolhandler.h index d01b4c26..5fb1dff4 100644 --- a/common/server_protocolhandler.h +++ b/common/server_protocolhandler.h @@ -5,7 +5,8 @@ #include #include "server.h" #include "protocol.h" -#include "protocol_items.h" +#include "pb/response.pb.h" +#include "pb/server_message.pb.h" class Server_Player; class Server_Card; @@ -13,6 +14,14 @@ class ServerInfo_User; class Server_Room; class QTimer; +class Message; +class ServerMessage; + +class Response; +class SessionEvent; +class GameEventContainer; +class RoomEvent; + class CommandContainer; class Command_Ping; class Command_Login; @@ -90,72 +99,74 @@ protected: virtual bool getCompressionSupport() const = 0; int sessionId; private: - QList itemQueue; + QString thisUserName; QList messageSizeOverTime, messageCountOverTime; int timeRunning, lastDataReceived; QTimer *pingClock; + virtual void transmitProtocolItem(const ServerMessage &item) = 0; + virtual DeckList *getDeckFromDatabase(int deckId) = 0; - ResponseCode cmdPing(const Command_Ping &cmd); - ResponseCode cmdLogin(const Command_Login &cmd, BlaContainer *bla); - ResponseCode cmdMessage(const Command_Message &cmd, BlaContainer *bla); - virtual ResponseCode cmdAddToList(const Command_AddToList &cmd, BlaContainer *bla) = 0; - virtual ResponseCode cmdRemoveFromList(const Command_RemoveFromList &cmd, BlaContainer *bla) = 0; - virtual ResponseCode cmdDeckList(const Command_DeckList &cmd, BlaContainer *bla) = 0; - virtual ResponseCode cmdDeckNewDir(const Command_DeckNewDir &cmd, BlaContainer *bla) = 0; - virtual ResponseCode cmdDeckDelDir(const Command_DeckDelDir &cmd, BlaContainer *bla) = 0; - virtual ResponseCode cmdDeckDel(const Command_DeckDel &cmd, BlaContainer *bla) = 0; - virtual ResponseCode cmdDeckUpload(const Command_DeckUpload &cmd, BlaContainer *bla) = 0; - virtual ResponseCode cmdDeckDownload(const Command_DeckDownload &cmd, BlaContainer *bla) = 0; - ResponseCode cmdGetGamesOfUser(const Command_GetGamesOfUser &cmd, BlaContainer *bla); - ResponseCode cmdGetUserInfo(const Command_GetUserInfo &cmd, BlaContainer *bla); - ResponseCode cmdListRooms(const Command_ListRooms &cmd, BlaContainer *bla); - ResponseCode cmdJoinRoom(const Command_JoinRoom &cmd, BlaContainer *bla); - ResponseCode cmdListUsers(const Command_ListUsers &cmd, BlaContainer *bla); - ResponseCode cmdLeaveRoom(const Command_LeaveRoom &cmd, Server_Room *room); - ResponseCode cmdRoomSay(const Command_RoomSay &cmd, Server_Room *room); - ResponseCode cmdCreateGame(const Command_CreateGame &cmd, Server_Room *room); - ResponseCode cmdJoinGame(const Command_JoinGame &cmd, Server_Room *room); - ResponseCode cmdLeaveGame(const Command_LeaveGame &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdKickFromGame(const Command_KickFromGame &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdConcede(const Command_Concede &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdReadyStart(const Command_ReadyStart &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdDeckSelect(const Command_DeckSelect &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdSetSideboardPlan(const Command_SetSideboardPlan &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdGameSay(const Command_GameSay &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdShuffle(const Command_Shuffle &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdMulligan(const Command_Mulligan &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdRollDie(const Command_RollDie &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdDrawCards(const Command_DrawCards &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdUndoDraw(const Command_UndoDraw &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdMoveCard(const Command_MoveCard &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdFlipCard(const Command_FlipCard &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdAttachCard(const Command_AttachCard &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdCreateToken(const Command_CreateToken &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdCreateArrow(const Command_CreateArrow &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdDeleteArrow(const Command_DeleteArrow &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdSetCardAttr(const Command_SetCardAttr &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdSetCardCounter(const Command_SetCardCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdIncCardCounter(const Command_IncCardCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdIncCounter(const Command_IncCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdCreateCounter(const Command_CreateCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdSetCounter(const Command_SetCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdDelCounter(const Command_DelCounter &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdNextTurn(const Command_NextTurn &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdSetActivePhase(const Command_SetActivePhase &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdDumpZone(const Command_DumpZone &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdStopDumpZone(const Command_StopDumpZone &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - ResponseCode cmdRevealCards(const Command_RevealCards &cmd, Server_Game *game, Server_Player *player, BlaContainer *bla); - virtual ResponseCode cmdBanFromServer(const Command_BanFromServer &cmd, BlaContainer *bla) = 0; - virtual ResponseCode cmdShutdownServer(const Command_ShutdownServer &cmd, BlaContainer *bla) = 0; - virtual ResponseCode cmdUpdateServerMessage(const Command_UpdateServerMessage &cmd, BlaContainer *bla) = 0; + Response::ResponseCode cmdPing(const Command_Ping &cmd, ResponseContainer &rc); + Response::ResponseCode cmdLogin(const Command_Login &cmd, ResponseContainer &rc); + Response::ResponseCode cmdMessage(const Command_Message &cmd, ResponseContainer &rc); + virtual Response::ResponseCode cmdAddToList(const Command_AddToList &cmd, ResponseContainer &rc) = 0; + virtual Response::ResponseCode cmdRemoveFromList(const Command_RemoveFromList &cmd, ResponseContainer &rc) = 0; + virtual Response::ResponseCode cmdDeckList(const Command_DeckList &cmd, ResponseContainer &rc) = 0; + virtual Response::ResponseCode cmdDeckNewDir(const Command_DeckNewDir &cmd, ResponseContainer &rc) = 0; + virtual Response::ResponseCode cmdDeckDelDir(const Command_DeckDelDir &cmd, ResponseContainer &rc) = 0; + virtual Response::ResponseCode cmdDeckDel(const Command_DeckDel &cmd, ResponseContainer &rc) = 0; + virtual Response::ResponseCode cmdDeckUpload(const Command_DeckUpload &cmd, ResponseContainer &rc) = 0; + virtual Response::ResponseCode cmdDeckDownload(const Command_DeckDownload &cmd, ResponseContainer &rc) = 0; + Response::ResponseCode cmdGetGamesOfUser(const Command_GetGamesOfUser &cmd, ResponseContainer &rc); + Response::ResponseCode cmdGetUserInfo(const Command_GetUserInfo &cmd, ResponseContainer &rc); + Response::ResponseCode cmdListRooms(const Command_ListRooms &cmd, ResponseContainer &rc); + Response::ResponseCode cmdJoinRoom(const Command_JoinRoom &cmd, ResponseContainer &rc); + Response::ResponseCode cmdListUsers(const Command_ListUsers &cmd, ResponseContainer &rc); + Response::ResponseCode cmdLeaveRoom(const Command_LeaveRoom &cmd, Server_Room *room, ResponseContainer &rc); + Response::ResponseCode cmdRoomSay(const Command_RoomSay &cmd, Server_Room *room, ResponseContainer &rc); + Response::ResponseCode cmdCreateGame(const Command_CreateGame &cmd, Server_Room *room, ResponseContainer &rc); + Response::ResponseCode cmdJoinGame(const Command_JoinGame &cmd, Server_Room *room, ResponseContainer &rc); + Response::ResponseCode cmdLeaveGame(const Command_LeaveGame &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdKickFromGame(const Command_KickFromGame &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdConcede(const Command_Concede &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdReadyStart(const Command_ReadyStart &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdDeckSelect(const Command_DeckSelect &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdSetSideboardPlan(const Command_SetSideboardPlan &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdGameSay(const Command_GameSay &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdShuffle(const Command_Shuffle &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdMulligan(const Command_Mulligan &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdRollDie(const Command_RollDie &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdDrawCards(const Command_DrawCards &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdUndoDraw(const Command_UndoDraw &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdMoveCard(const Command_MoveCard &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdFlipCard(const Command_FlipCard &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdAttachCard(const Command_AttachCard &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdCreateToken(const Command_CreateToken &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdCreateArrow(const Command_CreateArrow &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdDeleteArrow(const Command_DeleteArrow &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdSetCardAttr(const Command_SetCardAttr &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdSetCardCounter(const Command_SetCardCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdIncCardCounter(const Command_IncCardCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdIncCounter(const Command_IncCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdCreateCounter(const Command_CreateCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdSetCounter(const Command_SetCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdDelCounter(const Command_DelCounter &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdNextTurn(const Command_NextTurn &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdSetActivePhase(const Command_SetActivePhase &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdDumpZone(const Command_DumpZone &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdStopDumpZone(const Command_StopDumpZone &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + Response::ResponseCode cmdRevealCards(const Command_RevealCards &cmd, Server_Game *game, Server_Player *player, ResponseContainer &rc, GameEventStorage &ges); + virtual Response::ResponseCode cmdBanFromServer(const Command_BanFromServer &cmd, ResponseContainer &rc) = 0; + virtual Response::ResponseCode cmdShutdownServer(const Command_ShutdownServer &cmd, ResponseContainer &rc) = 0; + virtual Response::ResponseCode cmdUpdateServerMessage(const Command_UpdateServerMessage &cmd, ResponseContainer &rc) = 0; - ResponseCode processSessionCommandContainer(const CommandContainer &cont, BlaContainer *bla); - ResponseCode processRoomCommandContainer(const CommandContainer &cont, BlaContainer *bla); - ResponseCode processGameCommandContainer(const CommandContainer &cont, BlaContainer *bla); - ResponseCode processModeratorCommandContainer(const CommandContainer &cont, BlaContainer *bla); - ResponseCode processAdminCommandContainer(const CommandContainer &cont, BlaContainer *bla); + Response::ResponseCode processSessionCommandContainer(const CommandContainer &cont, ResponseContainer &rc); + Response::ResponseCode processRoomCommandContainer(const CommandContainer &cont, ResponseContainer &rc); + Response::ResponseCode processGameCommandContainer(const CommandContainer &cont, ResponseContainer &rc); + Response::ResponseCode processModeratorCommandContainer(const CommandContainer &cont, ResponseContainer &rc); + Response::ResponseCode processAdminCommandContainer(const CommandContainer &cont, ResponseContainer &rc); private slots: void pingClockTimeout(); public: @@ -168,6 +179,8 @@ public: bool getAcceptsUserListChanges() const { return acceptsUserListChanges; } bool getAcceptsRoomListChanges() const { return acceptsRoomListChanges; } ServerInfo_User *getUserInfo() const { return userInfo; } + ServerInfo_User copyUserInfo(bool complete, bool moderatorInfo = false) const; + const QString &getUserName() const { return thisUserName; } virtual QString getAddress() const = 0; void setUserInfo(ServerInfo_User *_userInfo) { userInfo = _userInfo; } const QMap &getBuddyList() const { return buddyList; } @@ -177,8 +190,14 @@ public: int getLastCommandTime() const { return timeRunning - lastDataReceived; } void processCommandContainer(const CommandContainer &cont); - virtual void sendProtocolItem(ProtocolItem *item, bool deleteItem = true) = 0; - void enqueueProtocolItem(ProtocolItem *item); + + void sendProtocolItem(const Response &item); + void sendProtocolItem(const SessionEvent &item); + void sendProtocolItem(const GameEventContainer &item); + void sendProtocolItem(const RoomEvent &item); + void sendProtocolItem(ServerMessage::MessageType type, const ::google::protobuf::Message &item); + + SessionEvent *prepareSessionEvent(const ::google::protobuf::Message &sessionEvent); }; #endif diff --git a/common/server_room.h b/common/server_room.h index 663c9cee..0fb8b344 100644 --- a/common/server_room.h +++ b/common/server_room.h @@ -6,6 +6,7 @@ #include #include #include +#include "pb/serverinfo_room.pb.h" class Server_ProtocolHandler; class RoomEvent; @@ -38,9 +39,9 @@ public: QString getJoinMessage() const { return joinMessage; } const QMap &getGames() const { return games; } Server *getServer() const; - ServerInfo_Room *getInfo(bool complete, bool showGameTypes = false) const; + ServerInfo_Room getInfo(bool complete, bool showGameTypes = false) const; int getGamesCreatedByUser(const QString &name) const; - QList getGamesOfUser(const QString &name) const; + QList getGamesOfUser(const QString &name) const; void addClient(Server_ProtocolHandler *client); void removeClient(Server_ProtocolHandler *client); @@ -50,6 +51,7 @@ public: void removeGame(Server_Game *game); void sendRoomEvent(RoomEvent *event); + RoomEvent *prepareRoomEvent(const ::google::protobuf::Message &roomEvent); }; #endif diff --git a/servatrice/servatrice.pro b/servatrice/servatrice.pro index a62bed94..b702c3ed 100755 --- a/servatrice/servatrice.pro +++ b/servatrice/servatrice.pro @@ -24,7 +24,6 @@ HEADERS += src/main.h \ ../common/serializable_item.h \ ../common/decklist.h \ ../common/protocol.h \ - ../common/protocol_datastructures.h \ ../common/rng_abstract.h \ ../common/rng_sfmt.h \ ../common/server.h \ @@ -48,7 +47,6 @@ SOURCES += src/main.cpp \ ../common/serializable_item.cpp \ ../common/decklist.cpp \ ../common/protocol.cpp \ - ../common/protocol_datastructures.cpp \ ../common/rng_abstract.cpp \ ../common/rng_sfmt.cpp \ ../common/sfmt/SFMT.c \