mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2025-12-22 23:26:14 -08:00
restructured protocol code
This commit is contained in:
@@ -4,9 +4,7 @@
|
||||
#include <QString>
|
||||
#include <QColor>
|
||||
#include <QDateTime>
|
||||
|
||||
class QXmlStreamReader;
|
||||
class QXmlStreamWriter;
|
||||
#include "serializable_item.h"
|
||||
|
||||
enum ResponseCode { RespNothing, RespOk, RespInvalidCommand, RespInvalidData, RespNameNotFound, RespLoginNeeded, RespContextError, RespWrongPassword, RespSpectatorsNotAllowed };
|
||||
|
||||
@@ -20,210 +18,124 @@ enum ResponseCode { RespNothing, RespOk, RespInvalidCommand, RespInvalidData, Re
|
||||
// list index, whereas cards in any other zone are referenced by their ids.
|
||||
enum ZoneType { PrivateZone, PublicZone, HiddenZone };
|
||||
|
||||
class ColorConverter {
|
||||
class ServerInfo_ChatChannel : public SerializableItem_Map {
|
||||
public:
|
||||
static int colorToInt(const QColor &color)
|
||||
{
|
||||
return color.red() * 65536 + color.green() * 256 + color.blue();
|
||||
}
|
||||
static QColor colorFromInt(int colorValue)
|
||||
{
|
||||
return QColor(colorValue / 65536, (colorValue % 65536) / 256, colorValue % 256);
|
||||
}
|
||||
ServerInfo_ChatChannel(const QString &_name = QString(), const QString &_description = QString(), int _playerCount = -1, bool _autoJoin = false);
|
||||
static SerializableItem *newItem() { return new ServerInfo_ChatChannel; }
|
||||
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
|
||||
QString getDescription() const { return static_cast<SerializableItem_String *>(itemMap.value("description"))->getData(); }
|
||||
int getPlayerCount() const { return static_cast<SerializableItem_Int *>(itemMap.value("player_count"))->getData(); }
|
||||
bool getAutoJoin() const { return static_cast<SerializableItem_Bool *>(itemMap.value("auto_join"))->getData(); }
|
||||
};
|
||||
|
||||
class SerializableItem {
|
||||
protected:
|
||||
SerializableItem *currentItem;
|
||||
class ServerInfo_ChatUser : public SerializableItem_Map {
|
||||
public:
|
||||
SerializableItem() : currentItem(0) { }
|
||||
virtual bool readElement(QXmlStreamReader *xml) = 0;
|
||||
virtual void writeElement(QXmlStreamWriter *xml) = 0;
|
||||
ServerInfo_ChatUser(const QString &_name = QString());
|
||||
static SerializableItem *newItem() { return new ServerInfo_ChatUser; }
|
||||
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
|
||||
};
|
||||
|
||||
class ServerChatChannelInfo {
|
||||
class ServerInfo_Game : public SerializableItem_Map {
|
||||
public:
|
||||
ServerInfo_Game(int _gameId = -1, const QString &_description = QString(), bool _hasPassword = false, int _playerCount = -1, int _maxPlayers = -1, const QString &_creatorName = QString(), bool _spectatorsAllowed = false, int _spectatorCount = -1);
|
||||
static SerializableItem *newItem() { return new ServerInfo_Game; }
|
||||
int getGameId() const { return static_cast<SerializableItem_Int *>(itemMap.value("game_id"))->getData(); }
|
||||
QString getDescription() const { return static_cast<SerializableItem_String *>(itemMap.value("description"))->getData(); }
|
||||
bool getHasPassword() const { return static_cast<SerializableItem_Bool *>(itemMap.value("has_password"))->getData(); }
|
||||
int getPlayerCount() const { return static_cast<SerializableItem_Int *>(itemMap.value("player_count"))->getData(); }
|
||||
int getMaxPlayers() const { return static_cast<SerializableItem_Int *>(itemMap.value("max_players"))->getData(); }
|
||||
QString getCreatorName() const { return static_cast<SerializableItem_String *>(itemMap.value("creator_name"))->getData(); }
|
||||
bool getSpectatorsAllowed() const { return static_cast<SerializableItem_Bool *>(itemMap.value("spectators_allowed"))->getData(); }
|
||||
int getSpectatorCount() const { return static_cast<SerializableItem_Int *>(itemMap.value("spectator_count"))->getData(); }
|
||||
};
|
||||
|
||||
class ServerInfo_Card : public SerializableItem_Map {
|
||||
public:
|
||||
ServerInfo_Card(int _id = -1, const QString &_name = QString(), int _x = -1, int _y = -1, int _counters = -1, bool _tapped = false, bool _attacking = false, const QString &_annotation = QString());
|
||||
static SerializableItem *newItem() { return new ServerInfo_Card; }
|
||||
int getId() const { return static_cast<SerializableItem_Int *>(itemMap.value("id"))->getData(); }
|
||||
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
|
||||
int getX() const { return static_cast<SerializableItem_Int *>(itemMap.value("x"))->getData(); }
|
||||
int getY() const { return static_cast<SerializableItem_Int *>(itemMap.value("y"))->getData(); }
|
||||
int getCounters() const { return static_cast<SerializableItem_Int *>(itemMap.value("counters"))->getData(); }
|
||||
bool getTapped() const { return static_cast<SerializableItem_Bool *>(itemMap.value("tapped"))->getData(); }
|
||||
bool getAttacking() const { return static_cast<SerializableItem_Bool *>(itemMap.value("attacking"))->getData(); }
|
||||
QString getAnnotation() const { return static_cast<SerializableItem_String *>(itemMap.value("annotation"))->getData(); }
|
||||
};
|
||||
|
||||
class ServerInfo_Zone : public SerializableItem_Map {
|
||||
private:
|
||||
QString name;
|
||||
QString description;
|
||||
int playerCount;
|
||||
bool autoJoin;
|
||||
ZoneType typeFromString(const QString &type) const;
|
||||
QString typeToString(ZoneType type) const;
|
||||
public:
|
||||
ServerChatChannelInfo(const QString &_name, const QString &_description, int _playerCount, bool _autoJoin)
|
||||
: name(_name), description(_description), playerCount(_playerCount), autoJoin(_autoJoin) { }
|
||||
QString getName() const { return name; }
|
||||
QString getDescription() const { return description; }
|
||||
int getPlayerCount() const { return playerCount; }
|
||||
bool getAutoJoin() const { return autoJoin; }
|
||||
ServerInfo_Zone(const QString &_name = QString(), ZoneType _type = PrivateZone, bool _hasCoords = false, int _cardCount = -1, const QList<ServerInfo_Card *> &_cardList = QList<ServerInfo_Card *>());
|
||||
static SerializableItem *newItem() { return new ServerInfo_Zone; }
|
||||
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
|
||||
ZoneType getType() const { return typeFromString(static_cast<SerializableItem_String *>(itemMap.value("type"))->getData()); }
|
||||
bool getHasCoords() const { return static_cast<SerializableItem_Bool *>(itemMap.value("has_coords"))->getData(); }
|
||||
int getCardCount() const { return static_cast<SerializableItem_Int *>(itemMap.value("card_count"))->getData(); }
|
||||
QList<ServerInfo_Card *> getCardList() const;
|
||||
};
|
||||
|
||||
class ServerChatUserInfo {
|
||||
private:
|
||||
QString name;
|
||||
class ServerInfo_Counter : public SerializableItem_Map {
|
||||
public:
|
||||
ServerChatUserInfo(const QString &_name)
|
||||
: name(_name) { }
|
||||
QString getName() const { return name; }
|
||||
ServerInfo_Counter(int _id = -1, const QString &_name = QString(), const QColor &_color = QColor(), int _radius = -1, int _count = -1);
|
||||
static SerializableItem *newItem() { return new ServerInfo_Counter; }
|
||||
int getId() const { return static_cast<SerializableItem_Int *>(itemMap.value("id"))->getData(); }
|
||||
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
|
||||
QColor getColor() const { return static_cast<SerializableItem_Color *>(itemMap.value("color"))->getData(); }
|
||||
int getRadius() const { return static_cast<SerializableItem_Int *>(itemMap.value("radius"))->getData(); }
|
||||
int getCount() const { return static_cast<SerializableItem_Int *>(itemMap.value("count"))->getData(); }
|
||||
};
|
||||
|
||||
class ServerGameInfo {
|
||||
private:
|
||||
int gameId;
|
||||
QString description;
|
||||
bool hasPassword;
|
||||
int playerCount;
|
||||
int maxPlayers;
|
||||
QString creatorName;
|
||||
bool spectatorsAllowed;
|
||||
int spectatorCount;
|
||||
class ServerInfo_Arrow : public SerializableItem_Map {
|
||||
public:
|
||||
ServerGameInfo(int _gameId, const QString &_description, bool _hasPassword, int _playerCount, int _maxPlayers, const QString &_creatorName, bool _spectatorsAllowed, int _spectatorCount)
|
||||
: gameId(_gameId), description(_description), hasPassword(_hasPassword), playerCount(_playerCount), maxPlayers(_maxPlayers), creatorName(_creatorName), spectatorsAllowed(_spectatorsAllowed), spectatorCount(_spectatorCount) { }
|
||||
int getGameId() const { return gameId; }
|
||||
QString getDescription() const { return description; }
|
||||
bool getHasPassword() const { return hasPassword; }
|
||||
int getPlayerCount() const { return playerCount; }
|
||||
int getMaxPlayers() const { return maxPlayers; }
|
||||
QString getCreatorName() const { return creatorName; }
|
||||
bool getSpectatorsAllowed() const { return spectatorsAllowed; }
|
||||
int getSpectatorCount() const { return spectatorCount; }
|
||||
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 QColor &_color = QColor());
|
||||
static SerializableItem *newItem() { return new ServerInfo_Arrow; }
|
||||
int getId() const { return static_cast<SerializableItem_Int *>(itemMap.value("id"))->getData(); }
|
||||
int getStartPlayerId() const { return static_cast<SerializableItem_Int *>(itemMap.value("start_player_id"))->getData(); }
|
||||
QString getStartZone() const { return static_cast<SerializableItem_String *>(itemMap.value("start_zone"))->getData(); }
|
||||
int getStartCardId() const { return static_cast<SerializableItem_Int *>(itemMap.value("start_card_id"))->getData(); }
|
||||
int getTargetPlayerId() const { return static_cast<SerializableItem_Int *>(itemMap.value("target_player_id"))->getData(); }
|
||||
QString getTargetZone() const { return static_cast<SerializableItem_String *>(itemMap.value("target_zone"))->getData(); }
|
||||
int getTargetCardId() const { return static_cast<SerializableItem_Int *>(itemMap.value("target_card_id"))->getData(); }
|
||||
QColor getColor() const { return static_cast<SerializableItem_Color *>(itemMap.value("color"))->getData(); }
|
||||
};
|
||||
|
||||
class ServerInfo_Card : public SerializableItem {
|
||||
class ServerInfo_Player : public SerializableItem_Map {
|
||||
private:
|
||||
int id;
|
||||
QString name;
|
||||
int x, y;
|
||||
int counters;
|
||||
bool tapped;
|
||||
bool attacking;
|
||||
QString annotation;
|
||||
public:
|
||||
ServerInfo_Card(int _id = -1, const QString &_name = QString(), int _x = -1, int _y = -1, int _counters = -1, bool _tapped = false, bool _attacking = false, const QString &_annotation = QString())
|
||||
: id(_id), name(_name), x(_x), y(_y), counters(_counters), tapped(_tapped), attacking(_attacking), annotation(_annotation) { }
|
||||
int getId() const { return id; }
|
||||
QString getName() const { return name; }
|
||||
int getX() const { return x; }
|
||||
int getY() const { return y; }
|
||||
int getCounters() const { return counters; }
|
||||
bool getTapped() const { return tapped; }
|
||||
bool getAttacking() const { return attacking; }
|
||||
QString getAnnotation() const { return annotation; }
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
};
|
||||
|
||||
class ServerInfo_Zone : public SerializableItem {
|
||||
private:
|
||||
QString name;
|
||||
ZoneType type;
|
||||
bool hasCoords;
|
||||
int cardCount;
|
||||
QList<ServerInfo_Card *> cardList;
|
||||
public:
|
||||
ServerInfo_Zone(const QString &_name = QString(), ZoneType _type = PrivateZone, bool _hasCoords = false, int _cardCount = -1, const QList<ServerInfo_Card *> &_cardList = QList<ServerInfo_Card *>())
|
||||
: name(_name), type(_type), hasCoords(_hasCoords), cardCount(_cardCount), cardList(_cardList) { }
|
||||
~ServerInfo_Zone();
|
||||
QString getName() const { return name; }
|
||||
ZoneType getType() const { return type; }
|
||||
bool getHasCoords() const { return hasCoords; }
|
||||
int getCardCount() const { return cardCount; }
|
||||
const QList<ServerInfo_Card *> &getCardList() const { return cardList; }
|
||||
void addCard(ServerInfo_Card *card) { cardList.append(card); ++cardCount; }
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
};
|
||||
|
||||
class ServerInfo_Counter : public SerializableItem {
|
||||
private:
|
||||
int id;
|
||||
QString name;
|
||||
QColor color;
|
||||
int radius;
|
||||
int count;
|
||||
public:
|
||||
ServerInfo_Counter(int _id = -1, const QString &_name = QString(), const QColor &_color = QColor(), int _radius = -1, int _count = -1)
|
||||
: id(_id), name(_name), color(_color), radius(_radius), count(_count) { }
|
||||
int getId() const { return id; }
|
||||
QString getName() const { return name; }
|
||||
QColor getColor() const { return color; }
|
||||
int getRadius() const { return radius; }
|
||||
int getCount() const { return count; }
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
};
|
||||
|
||||
class ServerInfo_Arrow : public SerializableItem {
|
||||
private:
|
||||
int id;
|
||||
int startPlayerId;
|
||||
QString startZone;
|
||||
int startCardId;
|
||||
int targetPlayerId;
|
||||
QString targetZone;
|
||||
int targetCardId;
|
||||
QColor color;
|
||||
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 QColor &_color = QColor())
|
||||
: id(_id), startPlayerId(_startPlayerId), startZone(_startZone), startCardId(_startCardId), targetPlayerId(_targetPlayerId), targetZone(_targetZone), targetCardId(_targetCardId), color(_color) { }
|
||||
int getId() const { return id; }
|
||||
int getStartPlayerId() const { return startPlayerId; }
|
||||
QString getStartZone() const { return startZone; }
|
||||
int getStartCardId() const { return startCardId; }
|
||||
int getTargetPlayerId() const { return targetPlayerId; }
|
||||
QString getTargetZone() const { return targetZone; }
|
||||
int getTargetCardId() const { return targetCardId; }
|
||||
QColor getColor() const { return color; }
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
};
|
||||
|
||||
class ServerInfo_Player : public SerializableItem {
|
||||
private:
|
||||
int playerId;
|
||||
QString name;
|
||||
QList<ServerInfo_Zone *> zoneList;
|
||||
QList<ServerInfo_Counter *> counterList;
|
||||
QList<ServerInfo_Arrow *> arrowList;
|
||||
protected:
|
||||
void extractData();
|
||||
public:
|
||||
ServerInfo_Player(int _playerId = -1, const QString &_name = QString(), const QList<ServerInfo_Zone *> &_zoneList = QList<ServerInfo_Zone *>(), const QList<ServerInfo_Counter *> &_counterList = QList<ServerInfo_Counter *>(), const QList<ServerInfo_Arrow *> &_arrowList = QList<ServerInfo_Arrow *>())
|
||||
: playerId(_playerId), name(_name), zoneList(_zoneList), counterList(_counterList), arrowList(_arrowList) { }
|
||||
~ServerInfo_Player();
|
||||
int getPlayerId() const { return playerId; }
|
||||
QString getName() const { return name; }
|
||||
ServerInfo_Player(int _playerId = -1, const QString &_name = QString(), const QList<ServerInfo_Zone *> &_zoneList = QList<ServerInfo_Zone *>(), const QList<ServerInfo_Counter *> &_counterList = QList<ServerInfo_Counter *>(), const QList<ServerInfo_Arrow *> &_arrowList = QList<ServerInfo_Arrow *>());
|
||||
static SerializableItem *newItem() { return new ServerInfo_Player; }
|
||||
int getPlayerId() const { return static_cast<SerializableItem_Int *>(itemMap.value("player_id"))->getData(); }
|
||||
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
|
||||
const QList<ServerInfo_Zone *> &getZoneList() const { return zoneList; }
|
||||
const QList<ServerInfo_Counter *> &getCounterList() const { return counterList; }
|
||||
const QList<ServerInfo_Arrow *> &getArrowList() const { return arrowList; }
|
||||
void addZone(ServerInfo_Zone *zone) { zoneList.append(zone); }
|
||||
void addCounter(ServerInfo_Counter *counter) { counterList.append(counter); }
|
||||
void addArrow(ServerInfo_Arrow *arrow) { arrowList.append(arrow); }
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
};
|
||||
|
||||
class DeckList_TreeItem : public SerializableItem {
|
||||
protected:
|
||||
QString name;
|
||||
int id;
|
||||
class DeckList_TreeItem : public SerializableItem_Map {
|
||||
public:
|
||||
DeckList_TreeItem(const QString &_name, int _id) : name(_name), id(_id) { }
|
||||
QString getName() const { return name; }
|
||||
int getId() const { return id; }
|
||||
DeckList_TreeItem(const QString &_itemType, const QString &_name, int _id);
|
||||
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
|
||||
int getId() const { return static_cast<SerializableItem_Int *>(itemMap.value("id"))->getData(); }
|
||||
};
|
||||
class DeckList_File : public DeckList_TreeItem {
|
||||
private:
|
||||
QDateTime uploadTime;
|
||||
public:
|
||||
DeckList_File(const QString &_name, int _id, QDateTime _uploadTime) : DeckList_TreeItem(_name, _id), uploadTime(_uploadTime) { }
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
QDateTime getUploadTime() const { return uploadTime; }
|
||||
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<SerializableItem_DateTime *>(itemMap.value("upload_time"))->getData(); }
|
||||
};
|
||||
class DeckList_Directory : public DeckList_TreeItem, public QList<DeckList_TreeItem *> {
|
||||
class DeckList_Directory : public DeckList_TreeItem {
|
||||
public:
|
||||
DeckList_Directory(const QString &_name = QString(), int _id = 0) : DeckList_TreeItem(_name, _id) { }
|
||||
~DeckList_Directory();
|
||||
bool readElement(QXmlStreamReader *xml);
|
||||
void writeElement(QXmlStreamWriter *xml);
|
||||
DeckList_Directory(const QString &_name = QString(), int _id = 0);
|
||||
static SerializableItem *newItem() { return new DeckList_Directory; }
|
||||
QList<DeckList_TreeItem *> getTreeItems() const { return typecastItemList<DeckList_TreeItem *>(); }
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user