浏览代码

Show number of players in open room

Ivan Savenko 1 年之前
父节点
当前提交
6d2ca070ea
共有 3 个文件被更改,包括 20 次插入2 次删除
  1. 17 2
      lobby/LobbyDatabase.cpp
  2. 1 0
      lobby/LobbyDatabase.h
  3. 2 0
      lobby/LobbyServer.cpp

+ 17 - 2
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<LobbyGameRoom> 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;
 }
 

+ 1 - 0
lobby/LobbyDatabase.h

@@ -41,6 +41,7 @@ class LobbyDatabase
 	SQLiteStatementPtr getActiveAccountsStatement;
 	SQLiteStatementPtr getAccountGameRoomStatement;
 	SQLiteStatementPtr getAccountDisplayNameStatement;
+	SQLiteStatementPtr countAccountsInRoomStatement;
 
 	SQLiteStatementPtr isAccountCookieValidStatement;
 	SQLiteStatementPtr isGameRoomCookieValidStatement;

+ 2 - 0
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