浏览代码

Store and show correct player room limit in UI

Ivan Savenko 1 年之前
父节点
当前提交
3023db6f0f

+ 3 - 2
client/NetPacksLobbyClient.cpp

@@ -58,11 +58,12 @@ void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyClientConnected(LobbyClientCon
 				// announce opened game room
 				// TODO: find better approach?
 				int roomType = settings["lobby"]["roomType"].Integer();
+				int roomPlayerLimit = settings["lobby"]["roomPlayerLimit"].Integer();
 
 				if (roomType != 0)
-					handler.getGlobalLobby().sendOpenPrivateRoom();
+					handler.getGlobalLobby().sendOpenRoom("private", roomPlayerLimit);
 				else
-					handler.getGlobalLobby().sendOpenPublicRoom();
+					handler.getGlobalLobby().sendOpenRoom("public", roomPlayerLimit);
 			}
 
 			while (!GH.windows().findWindows<GlobalLobbyWindow>().empty())

+ 4 - 12
client/globalLobby/GlobalLobbyClient.cpp

@@ -184,7 +184,7 @@ void GlobalLobbyClient::receiveActiveGameRooms(const JsonNode & json)
 		room.hostAccountDisplayName = jsonEntry["hostAccountDisplayName"].String();
 		room.description = jsonEntry["description"].String();
 		room.playersCount = jsonEntry["playersCount"].Integer();
-		room.playersLimit = jsonEntry["playersLimit"].Integer();
+		room.playerLimit = jsonEntry["playerLimit"].Integer();
 
 		activeRooms.push_back(room);
 	}
@@ -284,21 +284,13 @@ void GlobalLobbyClient::sendMessage(const JsonNode & data)
 	networkConnection->sendPacket(data.toBytes());
 }
 
-void GlobalLobbyClient::sendOpenPublicRoom()
+void GlobalLobbyClient::sendOpenRoom(const std::string & mode, int playerLimit)
 {
 	JsonNode toSend;
 	toSend["type"].String() = "activateGameRoom";
 	toSend["hostAccountID"] = settings["lobby"]["accountID"];
-	toSend["roomType"].String() = "public";
-	sendMessage(toSend);
-}
-
-void GlobalLobbyClient::sendOpenPrivateRoom()
-{
-	JsonNode toSend;
-	toSend["type"].String() = "activateGameRoom";
-	toSend["hostAccountID"] = settings["lobby"]["accountID"];
-	toSend["roomType"].String() = "private";
+	toSend["roomType"].String() = mode;
+	toSend["playerLimit"].Integer() = playerLimit;
 	sendMessage(toSend);
 }
 

+ 1 - 2
client/globalLobby/GlobalLobbyClient.h

@@ -60,8 +60,7 @@ public:
 	void sendMessage(const JsonNode & data);
 	void sendClientRegister(const std::string & accountName);
 	void sendClientLogin();
-	void sendOpenPublicRoom();
-	void sendOpenPrivateRoom();
+	void sendOpenRoom(const std::string & mode, int playerLimit);
 
 	void sendProxyConnectionLogin(const NetworkConnectionPtr & netConnection);
 

+ 1 - 1
client/globalLobby/GlobalLobbyDefines.h

@@ -23,5 +23,5 @@ struct GlobalLobbyRoom
 	std::string hostAccountDisplayName;
 	std::string description;
 	int playersCount;
-	int playersLimit;
+	int playerLimit;
 };

+ 1 - 1
client/globalLobby/GlobalLobbyWidget.cpp

@@ -142,7 +142,7 @@ GlobalLobbyRoomCard::GlobalLobbyRoomCard(GlobalLobbyWindow * window, const Globa
 
 	auto roomSizeText = MetaString::createFromRawString("%d/%d");
 	roomSizeText.replaceNumber(roomDescription.playersCount);
-	roomSizeText.replaceNumber(roomDescription.playersLimit);
+	roomSizeText.replaceNumber(roomDescription.playerLimit);
 
 	pos.w = 230;
 	pos.h = 40;

+ 8 - 1
config/schemas/lobbyProtocol/activateGameRoom.json

@@ -22,6 +22,13 @@
 			"type" : "string",
 			"enum" : [ "public", "private" ],
 			"description" : "Room type to use for activation"
-		}
+		},
+		"playerLimit" :
+		{
+			"type" : "number",
+			"minimum" : 1,
+			"maximum" : 8,
+			"description" : "Maximum number of players that can enter this room"
+		},
 	}
 }

+ 4 - 2
config/schemas/lobbyProtocol/activeGameRooms.json

@@ -20,7 +20,7 @@
 			{
 				"type" : "object",
 				"additionalProperties" : false,
-				"required" : [ "gameRoomID", "hostAccountID", "hostAccountDisplayName", "description", "playersCount", "playersLimit" ],
+				"required" : [ "gameRoomID", "hostAccountID", "hostAccountDisplayName", "description", "playersCount", "playerLimit" ],
 				"properties" : {
 					"gameRoomID" :
 					{
@@ -47,9 +47,11 @@
 						"type" : "number",
 						"description" : "Current number of players in this room, including host"
 					},
-					"playersLimit" :
+					"playerLimit" :
 					{
 						"type" : "number",
+						"minimum" : 1,
+						"maximum" : 8,
 						"description" : "Maximum number of players that can join this room, including host"
 					}
 				}

+ 13 - 1
lobby/LobbyDatabase.cpp

@@ -162,6 +162,12 @@ void LobbyDatabase::prepareStatements()
 		WHERE roomID  = ?
 	)";
 
+	static const std::string updateRoomPlayerLimitText = R"(
+		UPDATE gameRooms
+		SET playerLimit = ?
+		WHERE roomID  = ?
+	)";
+
 	// SELECT FROM
 
 	static const std::string getRecentMessageHistoryText = R"(
@@ -278,6 +284,7 @@ void LobbyDatabase::prepareStatements()
 	setGameRoomStatusStatement = database->prepare(setGameRoomStatusText);
 	updateAccountLoginTimeStatement = database->prepare(updateAccountLoginTimeText);
 	updateRoomDescriptionStatement = database->prepare(updateRoomDescriptionText);
+	updateRoomPlayerLimitStatement = database->prepare(updateRoomPlayerLimitText);
 
 	getRecentMessageHistoryStatement = database->prepare(getRecentMessageHistoryText);
 	getIdleGameRoomStatement = database->prepare(getIdleGameRoomText);
@@ -400,6 +407,11 @@ void LobbyDatabase::updateAccountLoginTime(const std::string & accountID)
 	updateAccountLoginTimeStatement->executeOnce(accountID);
 }
 
+void LobbyDatabase::updateRoomPlayerLimit(const std::string & gameRoomID, int playerLimit)
+{
+	updateRoomPlayerLimitStatement->executeOnce(playerLimit, gameRoomID);
+}
+
 void LobbyDatabase::updateRoomDescription(const std::string & gameRoomID, const std::string & description)
 {
 	updateRoomDescriptionStatement->executeOnce(description, gameRoomID);
@@ -499,7 +511,7 @@ std::vector<LobbyGameRoom> LobbyDatabase::getActiveGameRooms()
 	while(getActiveGameRoomsStatement->execute())
 	{
 		LobbyGameRoom entry;
-		getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.description, entry.roomStatus, entry.playersLimit);
+		getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.description, entry.roomStatus, entry.playerLimit);
 		result.push_back(entry);
 	}
 	getActiveGameRoomsStatement->reset();

+ 2 - 0
lobby/LobbyDatabase.h

@@ -35,6 +35,7 @@ class LobbyDatabase
 	SQLiteStatementPtr setGameRoomStatusStatement;
 	SQLiteStatementPtr updateAccountLoginTimeStatement;
 	SQLiteStatementPtr updateRoomDescriptionStatement;
+	SQLiteStatementPtr updateRoomPlayerLimitStatement;
 
 	SQLiteStatementPtr getRecentMessageHistoryStatement;
 	SQLiteStatementPtr getIdleGameRoomStatement;
@@ -76,6 +77,7 @@ public:
 	void insertChatMessage(const std::string & sender, const std::string & roomType, const std::string & roomID, const std::string & messageText);
 
 	void updateAccountLoginTime(const std::string & accountID);
+	void updateRoomPlayerLimit(const std::string & gameRoomID, int playerLimit);
 	void updateRoomDescription(const std::string & gameRoomID, const std::string & description);
 
 	std::vector<LobbyGameRoom> getActiveGameRooms();

+ 1 - 1
lobby/LobbyDefines.h

@@ -23,7 +23,7 @@ struct LobbyGameRoom
 	std::string description;
 	std::string roomStatus;
 	uint32_t playersCount;
-	uint32_t playersLimit;
+	uint32_t playerLimit;
 };
 
 struct LobbyChatMessage

+ 3 - 1
lobby/LobbyServer.cpp

@@ -164,7 +164,7 @@ JsonNode LobbyServer::prepareActiveGameRooms()
 		jsonEntry["hostAccountDisplayName"].String() = gameRoom.hostAccountDisplayName;
 		jsonEntry["description"].String() = gameRoom.description;
 		jsonEntry["playersCount"].Integer() = gameRoom.playersCount;
-		jsonEntry["playersLimit"].Integer() = gameRoom.playersLimit;
+		jsonEntry["playerLimit"].Integer() = gameRoom.playerLimit;
 		reply["gameRooms"].Vector().push_back(jsonEntry);
 	}
 
@@ -520,6 +520,7 @@ void LobbyServer::receiveActivateGameRoom(const NetworkConnectionPtr & connectio
 {
 	std::string hostAccountID = json["hostAccountID"].String();
 	std::string accountID = activeAccounts[connection];
+	int playerLimit = json["playerLimit"].Integer();
 
 	if(database->isPlayerInGameRoom(accountID))
 		return sendOperationFailed(connection, "Player already in the room!");
@@ -537,6 +538,7 @@ void LobbyServer::receiveActivateGameRoom(const NetworkConnectionPtr & connectio
 	if(roomType == "private")
 		database->setGameRoomStatus(gameRoomID, LobbyRoomState::PRIVATE);
 
+	database->updateRoomPlayerLimit(gameRoomID, playerLimit);
 	database->insertPlayerIntoGameRoom(accountID, gameRoomID);
 	broadcastActiveGameRooms();
 	sendJoinRoomSuccess(connection, gameRoomID, false);