mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2025-12-31 15:07:29 -08:00
added responses + events
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
#include <QXmlStreamReader>
|
||||
#include <QXmlStreamWriter>
|
||||
#include "protocol.h"
|
||||
#include "protocol_items.h"
|
||||
#include "decklist.h"
|
||||
|
||||
ProtocolItem::ProtocolItem(const QString &_itemType, const QString &_itemSubType)
|
||||
: SerializableItem_Map(_itemType, _itemSubType), receiverMayDelete(true)
|
||||
@@ -10,90 +8,6 @@ ProtocolItem::ProtocolItem(const QString &_itemType, const QString &_itemSubType
|
||||
}
|
||||
|
||||
void ProtocolItem::initializeHash()
|
||||
{
|
||||
initializeHashAuto();
|
||||
|
||||
registerSerializableItem("move_card_to_zone", MoveCardToZone::newItem);
|
||||
registerSerializableItem("room", ServerInfo_Room::newItem);
|
||||
registerSerializableItem("user", ServerInfo_User::newItem);
|
||||
registerSerializableItem("game", ServerInfo_Game::newItem);
|
||||
registerSerializableItem("game_type", ServerInfo_GameType::newItem);
|
||||
registerSerializableItem("card_counter", ServerInfo_CardCounter::newItem);
|
||||
registerSerializableItem("card", ServerInfo_Card::newItem);
|
||||
registerSerializableItem("zone", ServerInfo_Zone::newItem);
|
||||
registerSerializableItem("counter", ServerInfo_Counter::newItem);
|
||||
registerSerializableItem("arrow", ServerInfo_Arrow::newItem);
|
||||
registerSerializableItem("player_properties", ServerInfo_PlayerProperties::newItem);
|
||||
registerSerializableItem("player", ServerInfo_Player::newItem);
|
||||
registerSerializableItem("player_ping", ServerInfo_PlayerPing::newItem);
|
||||
registerSerializableItem("file", DeckList_File::newItem);
|
||||
registerSerializableItem("directory", DeckList_Directory::newItem);
|
||||
registerSerializableItem("game_type_id", GameTypeId::newItem);
|
||||
|
||||
registerSerializableItem("containergame_event", GameEventContainer::newItem);
|
||||
|
||||
registerSerializableItem("resp", ProtocolResponse::newItem);
|
||||
ProtocolResponse::initializeHash();
|
||||
registerSerializableItem("respjoin_room", Response_JoinRoom::newItem);
|
||||
registerSerializableItem("resplist_users", Response_ListUsers::newItem);
|
||||
registerSerializableItem("respget_games_of_user", Response_GetGamesOfUser::newItem);
|
||||
registerSerializableItem("respget_user_info", Response_GetUserInfo::newItem);
|
||||
registerSerializableItem("respdeck_list", Response_DeckList::newItem);
|
||||
registerSerializableItem("respdeck_download", Response_DeckDownload::newItem);
|
||||
registerSerializableItem("respdeck_upload", Response_DeckUpload::newItem);
|
||||
registerSerializableItem("respdump_zone", Response_DumpZone::newItem);
|
||||
registerSerializableItem("resplogin", Response_Login::newItem);
|
||||
|
||||
registerSerializableItem("room_eventlist_games", Event_ListGames::newItem);
|
||||
registerSerializableItem("room_eventjoin_room", Event_JoinRoom::newItem);
|
||||
registerSerializableItem("generic_eventadd_to_list", Event_AddToList::newItem);
|
||||
registerSerializableItem("generic_eventuser_joined", Event_UserJoined::newItem);
|
||||
registerSerializableItem("generic_eventlist_rooms", Event_ListRooms::newItem);
|
||||
registerSerializableItem("game_eventjoin", Event_Join::newItem);
|
||||
registerSerializableItem("game_eventgame_state_changed", Event_GameStateChanged::newItem);
|
||||
registerSerializableItem("game_eventplayer_properties_changed", Event_PlayerPropertiesChanged::newItem);
|
||||
registerSerializableItem("game_eventcreate_arrows", Event_CreateArrows::newItem);
|
||||
registerSerializableItem("game_eventcreate_counters", Event_CreateCounters::newItem);
|
||||
registerSerializableItem("game_eventdraw_cards", Event_DrawCards::newItem);
|
||||
registerSerializableItem("game_eventreveal_cards", Event_RevealCards::newItem);
|
||||
registerSerializableItem("game_eventping", Event_Ping::newItem);
|
||||
}
|
||||
|
||||
TopLevelProtocolItem::TopLevelProtocolItem()
|
||||
: SerializableItem(QString()), currentItem(0)
|
||||
{
|
||||
}
|
||||
|
||||
bool TopLevelProtocolItem::readCurrentItem(QXmlStreamReader *xml)
|
||||
{
|
||||
if (currentItem) {
|
||||
if (currentItem->read(xml)) {
|
||||
emit protocolItemReceived(currentItem);
|
||||
currentItem = 0;
|
||||
}
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TopLevelProtocolItem::readElement(QXmlStreamReader *xml)
|
||||
{
|
||||
if (!readCurrentItem(xml) && (xml->isStartElement())) {
|
||||
QString childName = xml->name().toString();
|
||||
QString childSubType = xml->attributes().value("type").toString();
|
||||
|
||||
currentItem = dynamic_cast<ProtocolItem *>(getNewItem(childName + childSubType));
|
||||
if (!currentItem)
|
||||
currentItem = new ProtocolItem_Invalid;
|
||||
if (xml->attributes().value("comp").toString().toInt() == 1)
|
||||
currentItem->setCompressed(true);
|
||||
|
||||
readCurrentItem(xml);
|
||||
}
|
||||
return SerializableItem::readElement(xml);
|
||||
}
|
||||
|
||||
void TopLevelProtocolItem::writeElement(QXmlStreamWriter * /*xml*/)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -136,96 +50,7 @@ void BlaContainer::enqueueGameEventPrivate(GameEvent *event, int gameId, int pla
|
||||
gameEventQueuePrivate->setContext(context);
|
||||
}
|
||||
|
||||
QHash<QString, ResponseCode> ProtocolResponse::responseHash;
|
||||
|
||||
ProtocolResponse::ProtocolResponse(int _cmdId, ResponseCode _responseCode, const QString &_itemName)
|
||||
: ProtocolItem("resp", _itemName)
|
||||
{
|
||||
insertItem(new SerializableItem_Int("cmd_id", _cmdId));
|
||||
insertItem(new SerializableItem_String("response_code", responseHash.key(_responseCode)));
|
||||
}
|
||||
|
||||
void ProtocolResponse::initializeHash()
|
||||
{
|
||||
responseHash.insert(QString(), RespNothing);
|
||||
responseHash.insert("ok", RespOk);
|
||||
responseHash.insert("not_in_room", RespNotInRoom);
|
||||
responseHash.insert("internal_error", RespInternalError);
|
||||
responseHash.insert("invalid_command", RespInvalidCommand);
|
||||
responseHash.insert("name_not_found", RespNameNotFound);
|
||||
responseHash.insert("login_needed", RespLoginNeeded);
|
||||
responseHash.insert("function_not_allowed", RespFunctionNotAllowed);
|
||||
responseHash.insert("game_not_started", RespGameNotStarted);
|
||||
responseHash.insert("game_full", RespGameFull);
|
||||
responseHash.insert("context_error", RespContextError);
|
||||
responseHash.insert("wrong_password", RespWrongPassword);
|
||||
responseHash.insert("spectators_not_allowed", RespSpectatorsNotAllowed);
|
||||
responseHash.insert("only_buddies", RespOnlyBuddies);
|
||||
responseHash.insert("user_level_too_low", RespUserLevelTooLow);
|
||||
responseHash.insert("in_ignore_list", RespInIgnoreList);
|
||||
responseHash.insert("would_overwrite_old_session", RespWouldOverwriteOldSession);
|
||||
responseHash.insert("chat_flood", RespChatFlood);
|
||||
}
|
||||
|
||||
Response_JoinRoom::Response_JoinRoom(int _cmdId, ResponseCode _responseCode, ServerInfo_Room *_roomInfo)
|
||||
: ProtocolResponse(_cmdId, _responseCode, "join_room")
|
||||
{
|
||||
if (!_roomInfo)
|
||||
_roomInfo = new ServerInfo_Room;
|
||||
insertItem(_roomInfo);
|
||||
}
|
||||
|
||||
Response_ListUsers::Response_ListUsers(int _cmdId, ResponseCode _responseCode, const QList<ServerInfo_User *> &_userList)
|
||||
: ProtocolResponse(_cmdId, _responseCode, "list_users")
|
||||
{
|
||||
for (int i = 0; i < _userList.size(); ++i)
|
||||
itemList.append(_userList[i]);
|
||||
}
|
||||
|
||||
Response_DeckList::Response_DeckList(int _cmdId, ResponseCode _responseCode, DeckList_Directory *_root)
|
||||
: ProtocolResponse(_cmdId, _responseCode, "deck_list")
|
||||
{
|
||||
if (!_root)
|
||||
_root = new DeckList_Directory;
|
||||
insertItem(_root);
|
||||
}
|
||||
|
||||
Response_GetGamesOfUser::Response_GetGamesOfUser(int _cmdId, ResponseCode _responseCode, const QList<ServerInfo_Room *> &_roomList, const QList<ServerInfo_Game *> &_gameList)
|
||||
: ProtocolResponse(_cmdId, _responseCode, "get_games_of_user")
|
||||
{
|
||||
roomList = _roomList;
|
||||
for (int i = 0; i < _roomList.size(); ++i)
|
||||
itemList.append(_roomList[i]);
|
||||
|
||||
gameList = _gameList;
|
||||
for (int i = 0; i < _gameList.size(); ++i)
|
||||
itemList.append(_gameList[i]);
|
||||
}
|
||||
|
||||
void Response_GetGamesOfUser::extractData()
|
||||
{
|
||||
for (int i = 0; i < itemList.size(); ++i) {
|
||||
ServerInfo_Room *room = dynamic_cast<ServerInfo_Room *>(itemList[i]);
|
||||
if (room) {
|
||||
roomList.append(room);
|
||||
continue;
|
||||
}
|
||||
ServerInfo_Game *game = dynamic_cast<ServerInfo_Game *>(itemList[i]);
|
||||
if (game) {
|
||||
gameList.append(game);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Response_GetUserInfo::Response_GetUserInfo(int _cmdId, ResponseCode _responseCode, ServerInfo_User *_user)
|
||||
: ProtocolResponse(_cmdId, _responseCode, "get_user_info")
|
||||
{
|
||||
if (!_user)
|
||||
_user = new ServerInfo_User;
|
||||
insertItem(_user);
|
||||
}
|
||||
|
||||
/*
|
||||
Response_DeckDownload::Response_DeckDownload(int _cmdId, ResponseCode _responseCode, DeckList *_deck)
|
||||
: ProtocolResponse(_cmdId, _responseCode, "deck_download")
|
||||
{
|
||||
@@ -246,90 +71,8 @@ Response_DeckUpload::Response_DeckUpload(int _cmdId, ResponseCode _responseCode,
|
||||
_file = new DeckList_File;
|
||||
insertItem(_file);
|
||||
}
|
||||
|
||||
Response_DumpZone::Response_DumpZone(int _cmdId, ResponseCode _responseCode, ServerInfo_Zone *_zone)
|
||||
: ProtocolResponse(_cmdId, _responseCode, "dump_zone")
|
||||
{
|
||||
if (!_zone)
|
||||
_zone = new ServerInfo_Zone;
|
||||
insertItem(_zone);
|
||||
}
|
||||
|
||||
Response_Login::Response_Login(int _cmdId, ResponseCode _responseCode, ServerInfo_User *_userInfo, const QList<ServerInfo_User *> &_buddyList, const QList<ServerInfo_User *> &_ignoreList)
|
||||
: ProtocolResponse(_cmdId, _responseCode, "login")
|
||||
{
|
||||
if (!_userInfo)
|
||||
_userInfo = new ServerInfo_User;
|
||||
insertItem(_userInfo);
|
||||
|
||||
insertItem(new ServerInfo_UserList("buddy_list", _buddyList));
|
||||
insertItem(new ServerInfo_UserList("ignore_list", _ignoreList));
|
||||
}
|
||||
|
||||
GameEvent::GameEvent(const QString &_eventName, int _playerId)
|
||||
: ProtocolItem("game_event", _eventName)
|
||||
{
|
||||
insertItem(new SerializableItem_Int("player_id", _playerId));
|
||||
}
|
||||
|
||||
GameEventContext::GameEventContext(const QString &_contextName)
|
||||
: ProtocolItem("game_event_context", _contextName)
|
||||
{
|
||||
}
|
||||
|
||||
RoomEvent::RoomEvent(const QString &_eventName, int _roomId)
|
||||
: ProtocolItem("room_event", _eventName)
|
||||
{
|
||||
insertItem(new SerializableItem_Int("room_id", _roomId));
|
||||
}
|
||||
|
||||
Event_ListRooms::Event_ListRooms(const QList<ServerInfo_Room *> &_roomList)
|
||||
: GenericEvent("list_rooms")
|
||||
{
|
||||
for (int i = 0; i < _roomList.size(); ++i)
|
||||
itemList.append(_roomList[i]);
|
||||
}
|
||||
|
||||
Event_JoinRoom::Event_JoinRoom(int _roomId, ServerInfo_User *_info)
|
||||
: RoomEvent("join_room", _roomId)
|
||||
{
|
||||
if (!_info)
|
||||
_info = new ServerInfo_User;
|
||||
insertItem(_info);
|
||||
}
|
||||
|
||||
Event_ListGames::Event_ListGames(int _roomId, const QList<ServerInfo_Game *> &_gameList)
|
||||
: RoomEvent("list_games", _roomId)
|
||||
{
|
||||
for (int i = 0; i < _gameList.size(); ++i)
|
||||
itemList.append(_gameList[i]);
|
||||
}
|
||||
|
||||
Event_AddToList::Event_AddToList(const QString &_list, ServerInfo_User *_userInfo)
|
||||
: GenericEvent("add_to_list")
|
||||
{
|
||||
if (!_userInfo)
|
||||
_userInfo = new ServerInfo_User;
|
||||
insertItem(_userInfo);
|
||||
insertItem(new SerializableItem_String("list", _list));
|
||||
}
|
||||
|
||||
Event_UserJoined::Event_UserJoined(ServerInfo_User *_userInfo)
|
||||
: GenericEvent("user_joined")
|
||||
{
|
||||
if (!_userInfo)
|
||||
_userInfo = new ServerInfo_User;
|
||||
insertItem(_userInfo);
|
||||
}
|
||||
|
||||
Event_Join::Event_Join(ServerInfo_PlayerProperties *player)
|
||||
: GameEvent("join", -1)
|
||||
{
|
||||
if (!player)
|
||||
player = new ServerInfo_PlayerProperties;
|
||||
insertItem(player);
|
||||
}
|
||||
|
||||
*/
|
||||
/*
|
||||
GameEventContainer::GameEventContainer(const QList<GameEvent *> &_eventList, int _gameId, GameEventContext *_context)
|
||||
: ProtocolItem("container", "game_event")
|
||||
{
|
||||
@@ -380,61 +123,4 @@ GameEventContainer *GameEventContainer::makeNew(GameEvent *event, int _gameId)
|
||||
{
|
||||
return new GameEventContainer(QList<GameEvent *>() << event, _gameId);
|
||||
}
|
||||
|
||||
Event_GameStateChanged::Event_GameStateChanged(bool _gameStarted, int _activePlayer, int _activePhase, const QList<ServerInfo_Player *> &_playerList)
|
||||
: GameEvent("game_state_changed", -1)
|
||||
{
|
||||
insertItem(new SerializableItem_Bool("game_started", _gameStarted));
|
||||
insertItem(new SerializableItem_Int("active_player", _activePlayer));
|
||||
insertItem(new SerializableItem_Int("active_phase", _activePhase));
|
||||
for (int i = 0; i < _playerList.size(); ++i)
|
||||
itemList.append(_playerList[i]);
|
||||
}
|
||||
|
||||
Event_PlayerPropertiesChanged::Event_PlayerPropertiesChanged(int _playerId, ServerInfo_PlayerProperties *_properties)
|
||||
: GameEvent("player_properties_changed", _playerId)
|
||||
{
|
||||
if (!_properties)
|
||||
_properties = new ServerInfo_PlayerProperties;
|
||||
insertItem(_properties);
|
||||
}
|
||||
|
||||
Event_Ping::Event_Ping(int _secondsElapsed, const QList<ServerInfo_PlayerPing *> &_pingList)
|
||||
: GameEvent("ping", -1)
|
||||
{
|
||||
insertItem(new SerializableItem_Int("seconds_elapsed", _secondsElapsed));
|
||||
for (int i = 0; i < _pingList.size(); ++i)
|
||||
itemList.append(_pingList[i]);
|
||||
}
|
||||
|
||||
Event_CreateArrows::Event_CreateArrows(int _playerId, const QList<ServerInfo_Arrow *> &_arrowList)
|
||||
: GameEvent("create_arrows", _playerId)
|
||||
{
|
||||
for (int i = 0; i < _arrowList.size(); ++i)
|
||||
itemList.append(_arrowList[i]);
|
||||
}
|
||||
|
||||
Event_CreateCounters::Event_CreateCounters(int _playerId, const QList<ServerInfo_Counter *> &_counterList)
|
||||
: GameEvent("create_counters", _playerId)
|
||||
{
|
||||
for (int i = 0; i < _counterList.size(); ++i)
|
||||
itemList.append(_counterList[i]);
|
||||
}
|
||||
|
||||
Event_DrawCards::Event_DrawCards(int _playerId, int _numberCards, const QList<ServerInfo_Card *> &_cardList)
|
||||
: GameEvent("draw_cards", _playerId)
|
||||
{
|
||||
insertItem(new SerializableItem_Int("number_cards", _numberCards));
|
||||
for (int i = 0; i < _cardList.size(); ++i)
|
||||
itemList.append(_cardList[i]);
|
||||
}
|
||||
|
||||
Event_RevealCards::Event_RevealCards(int _playerId, const QString &_zoneName, int _cardId, int _otherPlayerId, const QList<ServerInfo_Card *> &_cardList)
|
||||
: GameEvent("reveal_cards", _playerId)
|
||||
{
|
||||
insertItem(new SerializableItem_String("zone_name", _zoneName));
|
||||
insertItem(new SerializableItem_Int("card_id", _cardId));
|
||||
insertItem(new SerializableItem_Int("other_player_id", _otherPlayerId));
|
||||
for (int i = 0; i < _cardList.size(); ++i)
|
||||
itemList.append(_cardList[i]);
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user