mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-11 00:24:47 -07:00
rooms work mostly
This commit is contained in:
parent
b73001e9fd
commit
80277ff573
25 changed files with 726 additions and 576 deletions
|
|
@ -37,6 +37,7 @@ void ProtocolItem::initializeHash()
|
|||
|
||||
registerSerializableItem("resp", ProtocolResponse::newItem);
|
||||
ProtocolResponse::initializeHash();
|
||||
registerSerializableItem("respjoin_room", Response_JoinRoom::newItem);
|
||||
registerSerializableItem("resplist_users", Response_ListUsers::newItem);
|
||||
registerSerializableItem("respget_user_info", Response_GetUserInfo::newItem);
|
||||
registerSerializableItem("respdeck_list", Response_DeckList::newItem);
|
||||
|
|
@ -220,6 +221,14 @@ void ProtocolResponse::initializeHash()
|
|||
responseHash.insert("spectators_not_allowed", RespSpectatorsNotAllowed);
|
||||
}
|
||||
|
||||
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")
|
||||
{
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ enum ItemId {
|
|||
ItemId_Response_DeckDownload = ItemId_Other + 303,
|
||||
ItemId_Response_DeckUpload = ItemId_Other + 304,
|
||||
ItemId_Response_DumpZone = ItemId_Other + 305,
|
||||
ItemId_Response_JoinRoom = ItemId_Other + 306,
|
||||
ItemId_Invalid = ItemId_Other + 1000
|
||||
};
|
||||
|
||||
|
|
@ -204,6 +205,15 @@ public:
|
|||
ResponseCode getResponseCode() const { return responseHash.value(static_cast<SerializableItem_String *>(itemMap.value("response_code"))->getData(), RespOk); }
|
||||
};
|
||||
|
||||
class Response_JoinRoom : public ProtocolResponse {
|
||||
Q_OBJECT
|
||||
public:
|
||||
Response_JoinRoom(int _cmdId = -1, ResponseCode _responseCode = RespOk, ServerInfo_Room *_roomInfo = 0);
|
||||
int getItemId() const { return ItemId_Response_JoinRoom; }
|
||||
static SerializableItem *newItem() { return new Response_JoinRoom; }
|
||||
ServerInfo_Room *getRoomInfo() const { return static_cast<ServerInfo_Room *>(itemMap.value("room")); }
|
||||
};
|
||||
|
||||
class Response_ListUsers : public ProtocolResponse {
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
|
|
|||
|
|
@ -3,17 +3,6 @@
|
|||
#include <QXmlStreamReader>
|
||||
#include <QXmlStreamWriter>
|
||||
|
||||
ServerInfo_Room::ServerInfo_Room(int _roomId, const QString &_name, const QString &_description, int _gameCount, int _playerCount, bool _autoJoin)
|
||||
: 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));
|
||||
}
|
||||
|
||||
ServerInfo_User::ServerInfo_User(const QString &_name, int _userLevel, const QString &_country, const QByteArray &_avatarBmp)
|
||||
: SerializableItem_Map("user")
|
||||
{
|
||||
|
|
@ -23,13 +12,13 @@ ServerInfo_User::ServerInfo_User(const QString &_name, int _userLevel, const QSt
|
|||
insertItem(new SerializableItem_ByteArray("avatar_bmp", _avatarBmp));
|
||||
}
|
||||
|
||||
ServerInfo_User::ServerInfo_User(const ServerInfo_User *other)
|
||||
ServerInfo_User::ServerInfo_User(const ServerInfo_User *other, bool complete)
|
||||
: SerializableItem_Map("user")
|
||||
{
|
||||
insertItem(new SerializableItem_String("name", other->getName()));
|
||||
insertItem(new SerializableItem_Int("userlevel", other->getUserLevel()));
|
||||
insertItem(new SerializableItem_String("country", other->getCountry()));
|
||||
insertItem(new SerializableItem_ByteArray("avatar_bmp", other->getAvatarBmp()));
|
||||
insertItem(new SerializableItem_ByteArray("avatar_bmp", complete ? other->getAvatarBmp() : QByteArray()));
|
||||
}
|
||||
|
||||
ServerInfo_Game::ServerInfo_Game(int _gameId, const QString &_description, bool _hasPassword, int _playerCount, int _maxPlayers, ServerInfo_User *_creatorInfo, bool _spectatorsAllowed, bool _spectatorsNeedPassword, int _spectatorCount)
|
||||
|
|
@ -48,6 +37,40 @@ ServerInfo_Game::ServerInfo_Game(int _gameId, const QString &_description, bool
|
|||
insertItem(new SerializableItem_Int("spectator_count", _spectatorCount));
|
||||
}
|
||||
|
||||
ServerInfo_Room::ServerInfo_Room(int _roomId, const QString &_name, const QString &_description, int _gameCount, int _playerCount, bool _autoJoin, const QList<ServerInfo_Game *> &_gameList, const QList<ServerInfo_User *> &_userList)
|
||||
: 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]);
|
||||
}
|
||||
|
||||
void ServerInfo_Room::extractData()
|
||||
{
|
||||
for (int i = 0; i < itemList.size(); ++i) {
|
||||
ServerInfo_User *user = dynamic_cast<ServerInfo_User *>(itemList[i]);
|
||||
if (user) {
|
||||
userList.append(user);
|
||||
continue;
|
||||
}
|
||||
ServerInfo_Game *game = dynamic_cast<ServerInfo_Game *>(itemList[i]);
|
||||
if (game) {
|
||||
gameList.append(game);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ServerInfo_CardCounter::ServerInfo_CardCounter(int _id, int _value)
|
||||
: SerializableItem_Map("card_counter")
|
||||
{
|
||||
|
|
|
|||
|
|
@ -20,18 +20,6 @@ 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 ServerInfo_Room : public SerializableItem_Map {
|
||||
public:
|
||||
ServerInfo_Room(int _id = -1, const QString &_name = QString(), const QString &_description = QString(), int _gameCount = -1, int _playerCount = -1, bool _autoJoin = false);
|
||||
static SerializableItem *newItem() { return new ServerInfo_Room; }
|
||||
int getRoomId() const { return static_cast<SerializableItem_Int *>(itemMap.value("room_id"))->getData(); }
|
||||
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 getGameCount() const { return static_cast<SerializableItem_Int *>(itemMap.value("game_count"))->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 ServerInfo_User : public SerializableItem_Map {
|
||||
public:
|
||||
enum UserLevelFlags {
|
||||
|
|
@ -42,7 +30,7 @@ public:
|
|||
IsAdmin = 0x08
|
||||
};
|
||||
ServerInfo_User(const QString &_name = QString(), int _userLevel = IsNothing, const QString &_country = QString(), const QByteArray &_avatarBmp = QByteArray());
|
||||
ServerInfo_User(const ServerInfo_User *other);
|
||||
ServerInfo_User(const ServerInfo_User *other, bool complete = true);
|
||||
static SerializableItem *newItem() { return new ServerInfo_User; }
|
||||
QString getName() const { return static_cast<SerializableItem_String *>(itemMap.value("name"))->getData(); }
|
||||
int getUserLevel() const { return static_cast<SerializableItem_Int *>(itemMap.value("userlevel"))->getData(); }
|
||||
|
|
@ -66,6 +54,25 @@ public:
|
|||
int getSpectatorCount() const { return static_cast<SerializableItem_Int *>(itemMap.value("spectator_count"))->getData(); }
|
||||
};
|
||||
|
||||
class ServerInfo_Room : public SerializableItem_Map {
|
||||
private:
|
||||
QList<ServerInfo_Game *> gameList;
|
||||
QList<ServerInfo_User *> userList;
|
||||
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<ServerInfo_Game *> &_gameList = QList<ServerInfo_Game *>(), const QList<ServerInfo_User *> &_userList = QList<ServerInfo_User *>());
|
||||
static SerializableItem *newItem() { return new ServerInfo_Room; }
|
||||
int getRoomId() const { return static_cast<SerializableItem_Int *>(itemMap.value("room_id"))->getData(); }
|
||||
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 getGameCount() const { return static_cast<SerializableItem_Int *>(itemMap.value("game_count"))->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(); }
|
||||
const QList<ServerInfo_Game *> &getGameList() const { return gameList; }
|
||||
const QList<ServerInfo_User *> &getUserList() const { return userList; }
|
||||
};
|
||||
|
||||
class ServerInfo_CardCounter : public SerializableItem_Map {
|
||||
public:
|
||||
ServerInfo_CardCounter(int _id = -1, int _value = 0);
|
||||
|
|
|
|||
|
|
@ -279,10 +279,8 @@ ResponseCode Server_ProtocolHandler::cmdListRooms(Command_ListRooms * /*cmd*/, C
|
|||
|
||||
QList<ServerInfo_Room *> eventRoomList;
|
||||
QMapIterator<int, Server_Room *> roomIterator(server->getRooms());
|
||||
while (roomIterator.hasNext()) {
|
||||
Server_Room *r = roomIterator.next().value();
|
||||
eventRoomList.append(new ServerInfo_Room(r->getId(), r->getName(), r->getDescription(), r->getGames().size(), r->size(), r->getAutoJoin()));
|
||||
}
|
||||
while (roomIterator.hasNext())
|
||||
eventRoomList.append(roomIterator.next().value()->getInfo(false));
|
||||
cont->enqueueItem(new Event_ListRooms(eventRoomList));
|
||||
|
||||
acceptsRoomListChanges = true;
|
||||
|
|
@ -303,7 +301,9 @@ ResponseCode Server_ProtocolHandler::cmdJoinRoom(Command_JoinRoom *cmd, CommandC
|
|||
|
||||
r->addClient(this);
|
||||
rooms.insert(r->getId(), r);
|
||||
return RespOk;
|
||||
|
||||
cont->setResponse(new Response_JoinRoom(cont->getCmdId(), RespOk, r->getInfo(true)));
|
||||
return RespNothing;
|
||||
}
|
||||
|
||||
ResponseCode Server_ProtocolHandler::cmdLeaveRoom(Command_LeaveRoom * /*cmd*/, CommandContainer *cont, Server_Room *room)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#include "server_room.h"
|
||||
#include "server_protocolhandler.h"
|
||||
#include "server_game.h"
|
||||
#include <QDebug>
|
||||
|
||||
Server_Room::Server_Room(int _id, const QString &_name, const QString &_description, bool _autoJoin, const QString &_joinMessage, Server *parent)
|
||||
: QObject(parent), id(_id), name(_name), description(_description), autoJoin(_autoJoin), joinMessage(_joinMessage)
|
||||
|
|
@ -12,7 +13,24 @@ Server *Server_Room::getServer() const
|
|||
return static_cast<Server *>(parent());
|
||||
}
|
||||
|
||||
QList<ServerInfo_User *> Server_Room::addClient(Server_ProtocolHandler *client)
|
||||
ServerInfo_Room *Server_Room::getInfo(bool complete) const
|
||||
{
|
||||
QList<ServerInfo_Game *> gameList;
|
||||
QList<ServerInfo_User *> userList;
|
||||
qDebug() << "getInfo: complete=" << complete;
|
||||
if (complete) {
|
||||
QMapIterator<int, Server_Game *> gameIterator(games);
|
||||
while (gameIterator.hasNext())
|
||||
gameList.append(gameIterator.next().value()->getInfo());
|
||||
|
||||
for (int i = 0; i < size(); ++i)
|
||||
userList.append(new ServerInfo_User(at(i)->getUserInfo(), false));
|
||||
}
|
||||
|
||||
return new ServerInfo_Room(id, name, description, games.size(), size(), autoJoin, gameList, userList);
|
||||
}
|
||||
|
||||
void Server_Room::addClient(Server_ProtocolHandler *client)
|
||||
{
|
||||
sendRoomEvent(new Event_JoinRoom(id, new ServerInfo_User(client->getUserInfo())));
|
||||
append(client);
|
||||
|
|
@ -22,7 +40,6 @@ QList<ServerInfo_User *> Server_Room::addClient(Server_ProtocolHandler *client)
|
|||
eventUserList.append(new ServerInfo_User(at(i)->getUserInfo()));
|
||||
|
||||
emit roomInfoChanged();
|
||||
return eventUserList;
|
||||
}
|
||||
|
||||
void Server_Room::removeClient(Server_ProtocolHandler *client)
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
class Server_ProtocolHandler;
|
||||
class RoomEvent;
|
||||
class ServerInfo_User;
|
||||
class ServerInfo_Room;
|
||||
class Server_Game;
|
||||
class Server;
|
||||
|
||||
|
|
@ -35,8 +36,9 @@ public:
|
|||
bool getAutoJoin() const { return autoJoin; }
|
||||
const QMap<int, Server_Game *> &getGames() const { return games; }
|
||||
Server *getServer() const;
|
||||
ServerInfo_Room *getInfo(bool complete) const;
|
||||
|
||||
QList<ServerInfo_User *> addClient(Server_ProtocolHandler *client);
|
||||
void addClient(Server_ProtocolHandler *client);
|
||||
void removeClient(Server_ProtocolHandler *client);
|
||||
void say(Server_ProtocolHandler *client, const QString &s);
|
||||
void broadcastGameListUpdate(Server_Game *game);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue