From 6d2ca070ea2961d76d468148a8071c7758fc7cf4 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Sat, 27 Jan 2024 23:41:36 +0200 Subject: [PATCH] Show number of players in open room --- lobby/LobbyDatabase.cpp | 19 +++++++++++++++++-- lobby/LobbyDatabase.h | 1 + lobby/LobbyServer.cpp | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lobby/LobbyDatabase.cpp b/lobby/LobbyDatabase.cpp index c4a40aa13..44a747a68 100644 --- a/lobby/LobbyDatabase.cpp +++ b/lobby/LobbyDatabase.cpp @@ -170,12 +170,18 @@ void LobbyDatabase::prepareStatements() )"; static const std::string getActiveGameRoomsText = R"( - SELECT roomID, hostAccountID, displayName, status, 0, playerLimit + SELECT roomID, hostAccountID, displayName, status, playerLimit FROM gameRooms LEFT JOIN accounts ON hostAccountID = accountID WHERE status = 1 )"; + static const std::string countAccountsInRoomText = R"( + SELECT COUNT(accountID) + FROM gameRoomPlayers + WHERE roomID = ? + )"; + static const std::string getAccountDisplayNameText = R"( SELECT displayName FROM accounts @@ -239,6 +245,7 @@ void LobbyDatabase::prepareStatements() getActiveAccountsStatement = database->prepare(getActiveAccountsText); getActiveGameRoomsStatement = database->prepare(getActiveGameRoomsText); getAccountDisplayNameStatement = database->prepare(getAccountDisplayNameText); + countAccountsInRoomStatement = database->prepare(countAccountsInRoomText); isAccountCookieValidStatement = database->prepare(isAccountCookieValidText); isPlayerInGameRoomStatement = database->prepare(isPlayerInGameRoomText); @@ -429,10 +436,18 @@ std::vector LobbyDatabase::getActiveGameRooms() while(getActiveGameRoomsStatement->execute()) { LobbyGameRoom entry; - getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.roomStatus, entry.playersCount, entry.playersLimit); + getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.roomStatus, entry.playersLimit); result.push_back(entry); } getActiveGameRoomsStatement->reset(); + + for (auto & room : result) + { + countAccountsInRoomStatement->setBinds(room.roomID); + if(countAccountsInRoomStatement->execute()) + countAccountsInRoomStatement->getColumns(room.playersCount); + countAccountsInRoomStatement->reset(); + } return result; } diff --git a/lobby/LobbyDatabase.h b/lobby/LobbyDatabase.h index 898256020..ef940e3b5 100644 --- a/lobby/LobbyDatabase.h +++ b/lobby/LobbyDatabase.h @@ -41,6 +41,7 @@ class LobbyDatabase SQLiteStatementPtr getActiveAccountsStatement; SQLiteStatementPtr getAccountGameRoomStatement; SQLiteStatementPtr getAccountDisplayNameStatement; + SQLiteStatementPtr countAccountsInRoomStatement; SQLiteStatementPtr isAccountCookieValidStatement; SQLiteStatementPtr isGameRoomCookieValidStatement; diff --git a/lobby/LobbyServer.cpp b/lobby/LobbyServer.cpp index a8270a069..0b745ed6b 100644 --- a/lobby/LobbyServer.cpp +++ b/lobby/LobbyServer.cpp @@ -465,6 +465,7 @@ void LobbyServer::receiveOpenGameRoom(const NetworkConnectionPtr & connection, c // TODO: additional flags / initial settings, e.g. allowCheats // TODO: connection mode: direct or proxy. For now direct is assumed. Proxy might be needed later, for hosted servers + database->insertPlayerIntoGameRoom(accountID, gameRoomID); broadcastActiveGameRooms(); sendJoinRoomSuccess(connection, gameRoomID); } @@ -493,6 +494,7 @@ void LobbyServer::receiveJoinGameRoom(const NetworkConnectionPtr & connection, c if(database->getGameRoomFreeSlots(gameRoomID) == 0) return; + database->insertPlayerIntoGameRoom(accountID, gameRoomID); sendAccountJoinsRoom(targetRoom, accountID); //No reply to client - will be sent once match server establishes proxy connection with lobby