Commit abd54257 authored by Max-Wilhelm Bruker's avatar Max-Wilhelm Bruker
Browse files

finished getGamesOfUser function

parent d5de76ec
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
class DeckList; class DeckList;
enum ResponseCode { RespNothing, RespOk, RespInternalError, RespInvalidCommand, RespInvalidData, RespNameNotFound, RespLoginNeeded, RespFunctionNotAllowed, RespGameNotStarted, RespGameFull, RespContextError, RespWrongPassword, RespSpectatorsNotAllowed, RespOnlyBuddies, RespUserLevelTooLow, RespInIgnoreList, RespWouldOverwriteOldSession, RespChatFlood }; 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, // PrivateZone: Contents of the zone are always visible to the owner,
// but not to anyone else. // but not to anyone else.
...@@ -71,6 +71,7 @@ class ServerInfo_Game : public SerializableItem_Map { ...@@ -71,6 +71,7 @@ class ServerInfo_Game : public SerializableItem_Map {
public: public:
ServerInfo_Game(int _roomId = -1, int _gameId = -1, const QString &_description = QString(), bool _hasPassword = false, int _playerCount = -1, int _maxPlayers = -1, const QList<GameTypeId *> &_gameTypes = QList<GameTypeId *>(), ServerInfo_User *creatorInfo = 0, bool _onlyBuddies = false, bool _onlyRegistered = false, bool _spectatorsAllowed = false, bool _spectatorsNeedPassword = false, int _spectatorCount = -1); ServerInfo_Game(int _roomId = -1, int _gameId = -1, const QString &_description = QString(), bool _hasPassword = false, int _playerCount = -1, int _maxPlayers = -1, const QList<GameTypeId *> &_gameTypes = QList<GameTypeId *>(), 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; } static SerializableItem *newItem() { return new ServerInfo_Game; }
int getRoomId() const { return static_cast<SerializableItem_Int *>(itemMap.value("room_id"))->getData(); }
int getGameId() const { return static_cast<SerializableItem_Int *>(itemMap.value("game_id"))->getData(); } 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(); } 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(); } bool getHasPassword() const { return static_cast<SerializableItem_Bool *>(itemMap.value("has_password"))->getData(); }
......
...@@ -80,7 +80,7 @@ ResponseCode Server_ProtocolHandler::processCommandHelper(Command *command, Comm ...@@ -80,7 +80,7 @@ ResponseCode Server_ProtocolHandler::processCommandHelper(Command *command, Comm
Server_Room *room = rooms.value(roomCommand->getRoomId(), 0); Server_Room *room = rooms.value(roomCommand->getRoomId(), 0);
if (!room) if (!room)
return RespNameNotFound; return RespNotInRoom;
QMutexLocker locker(&room->roomMutex); QMutexLocker locker(&room->roomMutex);
...@@ -101,7 +101,7 @@ ResponseCode Server_ProtocolHandler::processCommandHelper(Command *command, Comm ...@@ -101,7 +101,7 @@ ResponseCode Server_ProtocolHandler::processCommandHelper(Command *command, Comm
gameListMutex.lock(); gameListMutex.lock();
if (!games.contains(gameCommand->getGameId())) { if (!games.contains(gameCommand->getGameId())) {
qDebug() << "invalid game"; qDebug() << "invalid game";
return RespNameNotFound; return RespNotInRoom;
} }
QPair<Server_Game *, Server_Player *> gamePair = games.value(gameCommand->getGameId()); QPair<Server_Game *, Server_Player *> gamePair = games.value(gameCommand->getGameId());
Server_Game *game = gamePair.first; Server_Game *game = gamePair.first;
...@@ -340,13 +340,19 @@ ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(Command_GetGamesOfUser *c ...@@ -340,13 +340,19 @@ ResponseCode Server_ProtocolHandler::cmdGetGamesOfUser(Command_GetGamesOfUser *c
if (!server->getUsers().contains(cmd->getUserName())) if (!server->getUsers().contains(cmd->getUserName()))
return RespNameNotFound; return RespNameNotFound;
QList<ServerInfo_Room *> roomList;
QList<ServerInfo_Game *> gameList; QList<ServerInfo_Game *> gameList;
QMapIterator<int, Server_Room *> roomIterator(server->getRooms()); QMapIterator<int, Server_Room *> roomIterator(server->getRooms());
while (roomIterator.hasNext()) while (roomIterator.hasNext()) {
gameList.append(roomIterator.next().value()->getGamesOfUser(cmd->getUserName())); Server_Room *room = roomIterator.next().value();
room->roomMutex.lock();
roomList.append(room->getInfo(false, true));
gameList.append(room->getGamesOfUser(cmd->getUserName()));
room->roomMutex.unlock();
}
server->serverMutex.unlock(); server->serverMutex.unlock();
ProtocolResponse *resp = new Response_GetGamesOfUser(cont->getCmdId(), RespOk, gameList); ProtocolResponse *resp = new Response_GetGamesOfUser(cont->getCmdId(), RespOk, roomList, gameList);
if (getCompressionSupport()) if (getCompressionSupport())
resp->setCompressed(true); resp->setCompressed(true);
cont->setResponse(resp); cont->setResponse(resp);
......
...@@ -26,7 +26,7 @@ Server *Server_Room::getServer() const ...@@ -26,7 +26,7 @@ Server *Server_Room::getServer() const
return static_cast<Server *>(parent()); return static_cast<Server *>(parent());
} }
ServerInfo_Room *Server_Room::getInfo(bool complete) const ServerInfo_Room *Server_Room::getInfo(bool complete, bool showGameTypes) const
{ {
QMutexLocker locker(&roomMutex); QMutexLocker locker(&roomMutex);
...@@ -40,10 +40,10 @@ ServerInfo_Room *Server_Room::getInfo(bool complete) const ...@@ -40,10 +40,10 @@ ServerInfo_Room *Server_Room::getInfo(bool complete) const
for (int i = 0; i < size(); ++i) for (int i = 0; i < size(); ++i)
userList.append(new ServerInfo_User(at(i)->getUserInfo(), false)); userList.append(new ServerInfo_User(at(i)->getUserInfo(), false));
}
if (complete || showGameTypes)
for (int i = 0; i < gameTypes.size(); ++i) for (int i = 0; i < gameTypes.size(); ++i)
gameTypeList.append(new ServerInfo_GameType(i, gameTypes[i])); gameTypeList.append(new ServerInfo_GameType(i, gameTypes[i]));
}
return new ServerInfo_Room(id, name, description, games.size(), size(), autoJoin, gameList, userList, gameTypeList); return new ServerInfo_Room(id, name, description, games.size(), size(), autoJoin, gameList, userList, gameTypeList);
} }
...@@ -133,8 +133,6 @@ int Server_Room::getGamesCreatedByUser(const QString &userName) const ...@@ -133,8 +133,6 @@ int Server_Room::getGamesCreatedByUser(const QString &userName) const
QList<ServerInfo_Game *> Server_Room::getGamesOfUser(const QString &userName) const QList<ServerInfo_Game *> Server_Room::getGamesOfUser(const QString &userName) const
{ {
QMutexLocker locker(&roomMutex);
QList<ServerInfo_Game *> result; QList<ServerInfo_Game *> result;
QMapIterator<int, Server_Game *> gamesIterator(games); QMapIterator<int, Server_Game *> gamesIterator(games);
while (gamesIterator.hasNext()) { while (gamesIterator.hasNext()) {
......
...@@ -38,7 +38,7 @@ public: ...@@ -38,7 +38,7 @@ public:
QString getJoinMessage() const { return joinMessage; } QString getJoinMessage() const { return joinMessage; }
const QMap<int, Server_Game *> &getGames() const { return games; } const QMap<int, Server_Game *> &getGames() const { return games; }
Server *getServer() const; Server *getServer() const;
ServerInfo_Room *getInfo(bool complete) const; ServerInfo_Room *getInfo(bool complete, bool showGameTypes = false) const;
int getGamesCreatedByUser(const QString &name) const; int getGamesCreatedByUser(const QString &name) const;
QList<ServerInfo_Game *> getGamesOfUser(const QString &name) const; QList<ServerInfo_Game *> getGamesOfUser(const QString &name) const;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment