don't use Server_Room::getInfo when updating

This commit is contained in:
Max-Wilhelm Bruker 2013-02-15 19:31:37 +01:00
parent 7a30b827c8
commit ce642e3000
4 changed files with 69 additions and 23 deletions

View file

@ -76,7 +76,9 @@ void RoomSelector::processListRoomsEvent(const Event_ListRooms &event)
twi->setData(0, Qt::DisplayRole, QString::fromStdString(room.name())); twi->setData(0, Qt::DisplayRole, QString::fromStdString(room.name()));
if (room.has_description()) if (room.has_description())
twi->setData(1, Qt::DisplayRole, QString::fromStdString(room.description())); twi->setData(1, Qt::DisplayRole, QString::fromStdString(room.description()));
if (room.has_player_count())
twi->setData(2, Qt::DisplayRole, room.player_count()); twi->setData(2, Qt::DisplayRole, room.player_count());
if (room.has_game_count())
twi->setData(3, Qt::DisplayRole, room.game_count()); twi->setData(3, Qt::DisplayRole, room.game_count());
return; return;
} }
@ -91,6 +93,7 @@ void RoomSelector::processListRoomsEvent(const Event_ListRooms &event)
twi->setData(3, Qt::DisplayRole, room.game_count()); twi->setData(3, Qt::DisplayRole, room.game_count());
twi->setTextAlignment(2, Qt::AlignRight); twi->setTextAlignment(2, Qt::AlignRight);
twi->setTextAlignment(3, Qt::AlignRight); twi->setTextAlignment(3, Qt::AlignRight);
roomList->addTopLevelItem(twi); roomList->addTopLevelItem(twi);
if (room.has_auto_join()) if (room.has_auto_join())
if (room.auto_join()) if (room.auto_join())

View file

@ -39,15 +39,13 @@ Server *Server_Room::getServer() const
return static_cast<Server *>(parent()); return static_cast<Server *>(parent());
} }
const ServerInfo_Room &Server_Room::getInfo(ServerInfo_Room &result, bool complete, bool showGameTypes, bool updating, bool includeExternalData) const const ServerInfo_Room &Server_Room::getInfo(ServerInfo_Room &result, bool complete, bool showGameTypes, bool includeExternalData) const
{ {
result.set_room_id(id); result.set_room_id(id);
if (!updating) {
result.set_name(name.toStdString()); result.set_name(name.toStdString());
result.set_description(description.toStdString()); result.set_description(description.toStdString());
result.set_auto_join(autoJoin); result.set_auto_join(autoJoin);
}
gamesLock.lockForRead(); gamesLock.lockForRead();
result.set_game_count(games.size() + externalGames.size()); result.set_game_count(games.size() + externalGames.size());
@ -101,26 +99,44 @@ void Server_Room::addClient(Server_ProtocolHandler *client)
event.mutable_user_info()->CopyFrom(client->copyUserInfo(false)); event.mutable_user_info()->CopyFrom(client->copyUserInfo(false));
sendRoomEvent(prepareRoomEvent(event)); sendRoomEvent(prepareRoomEvent(event));
ServerInfo_Room roomInfo;
roomInfo.set_room_id(id);
usersLock.lockForWrite(); usersLock.lockForWrite();
users.insert(QString::fromStdString(client->getUserInfo()->name()), client); users.insert(QString::fromStdString(client->getUserInfo()->name()), client);
roomInfo.set_player_count(users.size() + externalUsers.size());
usersLock.unlock(); usersLock.unlock();
ServerInfo_Room roomInfo; // XXX This can be removed during the next client update.
emit roomInfoChanged(getInfo(roomInfo, false, false, true)); gamesLock.lockForRead();
roomInfo.set_game_count(games.size() + externalGames.size());
gamesLock.unlock();
// -----------
emit roomInfoChanged(roomInfo);
} }
void Server_Room::removeClient(Server_ProtocolHandler *client) void Server_Room::removeClient(Server_ProtocolHandler *client)
{ {
usersLock.lockForWrite(); usersLock.lockForWrite();
users.remove(QString::fromStdString(client->getUserInfo()->name())); users.remove(QString::fromStdString(client->getUserInfo()->name()));
ServerInfo_Room roomInfo;
roomInfo.set_room_id(id);
roomInfo.set_player_count(users.size() + externalUsers.size());
usersLock.unlock(); usersLock.unlock();
Event_LeaveRoom event; Event_LeaveRoom event;
event.set_name(client->getUserInfo()->name()); event.set_name(client->getUserInfo()->name());
sendRoomEvent(prepareRoomEvent(event)); sendRoomEvent(prepareRoomEvent(event));
ServerInfo_Room roomInfo; // XXX This can be removed during the next client update.
emit roomInfoChanged(getInfo(roomInfo, false, false, true)); gamesLock.lockForRead();
roomInfo.set_game_count(games.size() + externalGames.size());
gamesLock.unlock();
// -----------
emit roomInfoChanged(roomInfo);
} }
void Server_Room::addExternalUser(const ServerInfo_User &userInfo) void Server_Room::addExternalUser(const ServerInfo_User &userInfo)
@ -131,43 +147,52 @@ void Server_Room::addExternalUser(const ServerInfo_User &userInfo)
event.mutable_user_info()->CopyFrom(userInfoContainer.copyUserInfo(false)); event.mutable_user_info()->CopyFrom(userInfoContainer.copyUserInfo(false));
sendRoomEvent(prepareRoomEvent(event), false); sendRoomEvent(prepareRoomEvent(event), false);
ServerInfo_Room roomInfo;
roomInfo.set_room_id(id);
usersLock.lockForWrite(); usersLock.lockForWrite();
externalUsers.insert(QString::fromStdString(userInfo.name()), userInfoContainer); externalUsers.insert(QString::fromStdString(userInfo.name()), userInfoContainer);
roomInfo.set_player_count(users.size() + externalUsers.size());
usersLock.unlock(); usersLock.unlock();
ServerInfo_Room roomInfo; emit roomInfoChanged(roomInfo);
emit roomInfoChanged(getInfo(roomInfo, false, false, true));
} }
void Server_Room::removeExternalUser(const QString &name) void Server_Room::removeExternalUser(const QString &name)
{ {
// This function is always called from the Server thread with server->roomsMutex locked. // This function is always called from the Server thread with server->roomsMutex locked.
ServerInfo_Room roomInfo;
roomInfo.set_room_id(id);
usersLock.lockForWrite(); usersLock.lockForWrite();
if (externalUsers.contains(name)) if (externalUsers.contains(name))
externalUsers.remove(name); externalUsers.remove(name);
roomInfo.set_player_count(users.size() + externalUsers.size());
usersLock.unlock(); usersLock.unlock();
Event_LeaveRoom event; Event_LeaveRoom event;
event.set_name(name.toStdString()); event.set_name(name.toStdString());
sendRoomEvent(prepareRoomEvent(event), false); sendRoomEvent(prepareRoomEvent(event), false);
ServerInfo_Room roomInfo; emit roomInfoChanged(roomInfo);
emit roomInfoChanged(getInfo(roomInfo, false, false, true));
} }
void Server_Room::updateExternalGameList(const ServerInfo_Game &gameInfo) void Server_Room::updateExternalGameList(const ServerInfo_Game &gameInfo)
{ {
// This function is always called from the Server thread with server->roomsMutex locked. // This function is always called from the Server thread with server->roomsMutex locked.
ServerInfo_Room roomInfo;
roomInfo.set_room_id(id);
gamesLock.lockForWrite(); gamesLock.lockForWrite();
if (!gameInfo.has_player_count() && externalGames.contains(gameInfo.game_id())) if (!gameInfo.has_player_count() && externalGames.contains(gameInfo.game_id()))
externalGames.remove(gameInfo.game_id()); externalGames.remove(gameInfo.game_id());
else else
externalGames.insert(gameInfo.game_id(), gameInfo); externalGames.insert(gameInfo.game_id(), gameInfo);
roomInfo.set_game_count(games.size() + externalGames.size());
gamesLock.unlock(); gamesLock.unlock();
broadcastGameListUpdate(gameInfo, false); broadcastGameListUpdate(gameInfo, false);
ServerInfo_Room roomInfo; emit roomInfoChanged(roomInfo);
emit roomInfoChanged(getInfo(roomInfo, false, false, true));
} }
Response::ResponseCode Server_Room::processJoinGameCommand(const Command_JoinGame &cmd, ResponseContainer &rc, Server_AbstractUserInterface *userInterface) Response::ResponseCode Server_Room::processJoinGameCommand(const Command_JoinGame &cmd, ResponseContainer &rc, Server_AbstractUserInterface *userInterface)
@ -233,6 +258,9 @@ void Server_Room::broadcastGameListUpdate(const ServerInfo_Game &gameInfo, bool
void Server_Room::addGame(Server_Game *game) void Server_Room::addGame(Server_Game *game)
{ {
ServerInfo_Room roomInfo;
roomInfo.set_room_id(id);
gamesLock.lockForWrite(); gamesLock.lockForWrite();
connect(game, SIGNAL(gameInfoChanged(ServerInfo_Game)), this, SLOT(broadcastGameListUpdate(ServerInfo_Game))); connect(game, SIGNAL(gameInfoChanged(ServerInfo_Game)), this, SLOT(broadcastGameListUpdate(ServerInfo_Game)));
@ -240,12 +268,18 @@ void Server_Room::addGame(Server_Game *game)
games.insert(game->getGameId(), game); games.insert(game->getGameId(), game);
ServerInfo_Game gameInfo; ServerInfo_Game gameInfo;
game->getInfo(gameInfo); game->getInfo(gameInfo);
roomInfo.set_game_count(games.size() + externalGames.size());
game->gameMutex.unlock(); game->gameMutex.unlock();
gamesLock.unlock(); gamesLock.unlock();
// XXX This can be removed during the next client update.
usersLock.lockForRead();
roomInfo.set_player_count(users.size() + externalUsers.size());
usersLock.unlock();
// -----------
emit gameListChanged(gameInfo); emit gameListChanged(gameInfo);
ServerInfo_Room roomInfo; emit roomInfoChanged(roomInfo);
emit roomInfoChanged(getInfo(roomInfo, false, false, true));
} }
void Server_Room::removeGame(Server_Game *game) void Server_Room::removeGame(Server_Game *game)
@ -262,7 +296,16 @@ void Server_Room::removeGame(Server_Game *game)
games.remove(game->getGameId()); games.remove(game->getGameId());
ServerInfo_Room roomInfo; ServerInfo_Room roomInfo;
emit roomInfoChanged(getInfo(roomInfo, false, false, true)); roomInfo.set_room_id(id);
roomInfo.set_game_count(games.size() + externalGames.size());
// XXX This can be removed during the next client update.
usersLock.lockForRead();
roomInfo.set_player_count(users.size() + externalUsers.size());
usersLock.unlock();
// -----------
emit roomInfoChanged(roomInfo);
} }
int Server_Room::getGamesCreatedByUser(const QString &userName) const int Server_Room::getGamesCreatedByUser(const QString &userName) const

View file

@ -55,7 +55,7 @@ public:
const QMap<int, Server_Game *> &getGames() const { return games; } const QMap<int, Server_Game *> &getGames() const { return games; }
const QMap<int, ServerInfo_Game> &getExternalGames() const { return externalGames; } const QMap<int, ServerInfo_Game> &getExternalGames() const { return externalGames; }
Server *getServer() const; Server *getServer() const;
const ServerInfo_Room &getInfo(ServerInfo_Room &result, bool complete, bool showGameTypes = false, bool updating = false, bool includeExternalData = true) const; const ServerInfo_Room &getInfo(ServerInfo_Room &result, bool complete, bool showGameTypes = false, bool includeExternalData = true) 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;

View file

@ -129,7 +129,7 @@ void IslInterface::initServer()
Server_Room *room = roomIterator.next().value(); Server_Room *room = roomIterator.next().value();
room->usersLock.lockForRead(); room->usersLock.lockForRead();
room->gamesLock.lockForRead(); room->gamesLock.lockForRead();
room->getInfo(*event.add_room_list(), true, true, false, false); room->getInfo(*event.add_room_list(), true, true, false);
} }
IslMessage message; IslMessage message;