Browse Source

Implemented room description display (map/template name)

Ivan Savenko 1 năm trước cách đây
mục cha
commit
715e094f5c

+ 1 - 1
client/globalLobby/GlobalLobbyWidget.cpp

@@ -149,7 +149,7 @@ GlobalLobbyRoomCard::GlobalLobbyRoomCard(GlobalLobbyWindow * window, const Globa
 
 	backgroundOverlay = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 64, 64, 64));
 	labelName = std::make_shared<CLabel>(5, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, roomDescription.hostAccountDisplayName);
-	labelStatus = std::make_shared<CLabel>(5, 20, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, roomDescription.description);
+	labelDescription = std::make_shared<CLabel>(5, 20, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, roomDescription.description);
 	labelRoomSize = std::make_shared<CLabel>(160, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::YELLOW, roomSizeText.toString());
 	iconRoomSize = std::make_shared<CPicture>(ImagePath::builtin("lobby/iconPlayer"), Point(145, 5));
 

+ 1 - 1
client/globalLobby/GlobalLobbyWidget.h

@@ -52,7 +52,7 @@ public:
 	std::shared_ptr<TransparentFilledRectangle> backgroundOverlay;
 	std::shared_ptr<CLabel> labelName;
 	std::shared_ptr<CLabel> labelRoomSize;
-	std::shared_ptr<CLabel> labelStatus;
+	std::shared_ptr<CLabel> labelDescription;
 	std::shared_ptr<CButton> buttonJoin;
 	std::shared_ptr<CPicture> iconRoomSize;
 };

+ 15 - 2
lobby/LobbyDatabase.cpp

@@ -29,6 +29,7 @@ void LobbyDatabase::createTables()
 			id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
 			roomID TEXT,
 			hostAccountID TEXT,
+			description TEXT NOT NULL DEFAULT '',
 			status INTEGER NOT NULL DEFAULT 0,
 			playerLimit INTEGER NOT NULL,
 			creationTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
@@ -155,6 +156,12 @@ void LobbyDatabase::prepareStatements()
 		WHERE accountID = ?
 	)";
 
+	static const std::string updateRoomDescriptionText = R"(
+		UPDATE gameRooms
+		SET description = ?
+		WHERE roomID  = ?
+	)";
+
 	// SELECT FROM
 
 	static const std::string getRecentMessageHistoryText = R"(
@@ -194,7 +201,7 @@ void LobbyDatabase::prepareStatements()
 	)";
 
 	static const std::string getActiveGameRoomsText = R"(
-		SELECT roomID, hostAccountID, displayName, status, playerLimit
+		SELECT roomID, hostAccountID, displayName, description, status, playerLimit
 		FROM gameRooms
 		LEFT JOIN accounts ON hostAccountID = accountID
 		WHERE status = 1
@@ -270,6 +277,7 @@ void LobbyDatabase::prepareStatements()
 	setAccountOnlineStatement = database->prepare(setAccountOnlineText);
 	setGameRoomStatusStatement = database->prepare(setGameRoomStatusText);
 	updateAccountLoginTimeStatement = database->prepare(updateAccountLoginTimeText);
+	updateRoomDescriptionStatement = database->prepare(updateRoomDescriptionText);
 
 	getRecentMessageHistoryStatement = database->prepare(getRecentMessageHistoryText);
 	getIdleGameRoomStatement = database->prepare(getIdleGameRoomText);
@@ -392,6 +400,11 @@ void LobbyDatabase::updateAccountLoginTime(const std::string & accountID)
 	updateAccountLoginTimeStatement->executeOnce(accountID);
 }
 
+void LobbyDatabase::updateRoomDescription(const std::string & gameRoomID, const std::string & description)
+{
+	updateRoomDescriptionStatement->executeOnce(description, gameRoomID);
+}
+
 std::string LobbyDatabase::getAccountDisplayName(const std::string & accountID)
 {
 	std::string result;
@@ -486,7 +499,7 @@ std::vector<LobbyGameRoom> LobbyDatabase::getActiveGameRooms()
 	while(getActiveGameRoomsStatement->execute())
 	{
 		LobbyGameRoom entry;
-		getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.roomStatus, entry.playersLimit);
+		getActiveGameRoomsStatement->getColumns(entry.roomID, entry.hostAccountID, entry.hostAccountDisplayName, entry.description, entry.roomStatus, entry.playersLimit);
 		result.push_back(entry);
 	}
 	getActiveGameRoomsStatement->reset();

+ 2 - 0
lobby/LobbyDatabase.h

@@ -34,6 +34,7 @@ class LobbyDatabase
 	SQLiteStatementPtr setAccountOnlineStatement;
 	SQLiteStatementPtr setGameRoomStatusStatement;
 	SQLiteStatementPtr updateAccountLoginTimeStatement;
+	SQLiteStatementPtr updateRoomDescriptionStatement;
 
 	SQLiteStatementPtr getRecentMessageHistoryStatement;
 	SQLiteStatementPtr getIdleGameRoomStatement;
@@ -75,6 +76,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 updateRoomDescription(const std::string & gameRoomID, const std::string & description);
 
 	std::vector<LobbyGameRoom> getActiveGameRooms();
 	std::vector<LobbyAccount> getActiveAccounts();

+ 1 - 0
lobby/LobbyDefines.h

@@ -20,6 +20,7 @@ struct LobbyGameRoom
 	std::string roomID;
 	std::string hostAccountID;
 	std::string hostAccountDisplayName;
+	std::string description;
 	std::string roomStatus;
 	uint32_t playersCount;
 	uint32_t playersLimit;

+ 13 - 1
lobby/LobbyServer.cpp

@@ -162,7 +162,7 @@ JsonNode LobbyServer::prepareActiveGameRooms()
 		jsonEntry["gameRoomID"].String() = gameRoom.roomID;
 		jsonEntry["hostAccountID"].String() = gameRoom.hostAccountID;
 		jsonEntry["hostAccountDisplayName"].String() = gameRoom.hostAccountDisplayName;
-		jsonEntry["description"].String() = "TODO: ROOM DESCRIPTION";
+		jsonEntry["description"].String() = gameRoom.description;
 		jsonEntry["playersCount"].Integer() = gameRoom.playersCount;
 		jsonEntry["playersLimit"].Integer() = gameRoom.playersLimit;
 		reply["gameRooms"].Vector().push_back(jsonEntry);
@@ -329,6 +329,9 @@ void LobbyServer::onPacketReceived(const NetworkConnectionPtr & connection, cons
 		std::string roomName = activeGameRooms.at(connection);
 		logGlobal->info("%s: Received message of type %s", roomName, messageType);
 
+		if(messageType == "changeRoomDescription")
+			return receiveChangeRoomDescription(connection, json);
+
 		if(messageType == "leaveGameRoom")
 			return receiveLeaveGameRoom(connection, json);
 
@@ -573,6 +576,15 @@ void LobbyServer::receiveJoinGameRoom(const NetworkConnectionPtr & connection, c
 	broadcastActiveGameRooms();
 }
 
+void LobbyServer::receiveChangeRoomDescription(const NetworkConnectionPtr & connection, const JsonNode & json)
+{
+	std::string gameRoomID = activeGameRooms[connection];
+	std::string description = json["description"].String();
+
+	database->updateRoomDescription(gameRoomID, description);
+	broadcastActiveGameRooms();
+}
+
 void LobbyServer::receiveLeaveGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json)
 {
 	std::string accountID = json["accountID"].String();

+ 1 - 0
lobby/LobbyServer.h

@@ -85,6 +85,7 @@ class LobbyServer final : public INetworkServerListener
 	void receiveActivateGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json);
 	void receiveJoinGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json);
 	void receiveLeaveGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json);
+	void receiveChangeRoomDescription(const NetworkConnectionPtr & connection, const JsonNode & json);
 	void receiveSendInvite(const NetworkConnectionPtr & connection, const JsonNode & json);
 	void receiveDeclineInvite(const NetworkConnectionPtr & connection, const JsonNode & json);
 

+ 18 - 0
server/CVCMIServer.cpp

@@ -595,6 +595,24 @@ void CVCMIServer::updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo,
 		else
 			si->mapGenOptions.reset();
 	}
+
+	if (lobbyProcessor)
+	{
+		std::string roomDescription;
+
+		if (si->mapGenOptions)
+		{
+			if (si->mapGenOptions->getMapTemplate())
+				roomDescription = si->mapGenOptions->getMapTemplate()->getName();
+			// else - no template selected.
+			// TODO: handle this somehow?
+		}
+		else
+			roomDescription = mi->getNameTranslated();
+
+		lobbyProcessor->sendChangeRoomDescription(roomDescription);
+	}
+
 	si->mapname = mi->fileURI;
 }
 

+ 18 - 6
server/GlobalLobbyProcessor.cpp

@@ -47,8 +47,7 @@ void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnecti
 					message["type"].String() = "leaveGameRoom";
 					message["accountID"].String() = proxy.first;
 
-					assert(JsonUtils::validate(message, "vcmi:lobbyProtocol/" + message["type"].String(), message["type"].String() + " pack"));
-					controlConnection->sendPacket(message.toBytes());
+					sendMessage(controlConnection, message);
 					break;
 				}
 			}
@@ -127,8 +126,7 @@ void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetwor
 		toSend["accountCookie"] = settings["lobby"]["accountCookie"];
 		toSend["version"].String() = VCMI_VERSION_STRING;
 
-		assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
-		connection->sendPacket(toSend.toBytes());
+		sendMessage(connection, toSend);
 	}
 	else
 	{
@@ -144,10 +142,24 @@ void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetwor
 		toSend["guestAccountID"].String() = guestAccountID;
 		toSend["accountCookie"] = settings["lobby"]["accountCookie"];
 
-		assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
-		connection->sendPacket(toSend.toBytes());
+		sendMessage(connection, toSend);
 
 		proxyConnections[guestAccountID] = connection;
 		owner.onNewConnection(connection);
 	}
 }
+
+void GlobalLobbyProcessor::sendChangeRoomDescription(const std::string & description)
+{
+	JsonNode toSend;
+	toSend["type"].String() = "changeRoomDescription";
+	toSend["description"].String() = description;
+
+	sendMessage(controlConnection, toSend);
+}
+
+void GlobalLobbyProcessor::sendMessage(const NetworkConnectionPtr & targetConnection, const JsonNode & toSend)
+{
+	assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
+	targetConnection->sendPacket(toSend.toBytes());
+}

+ 3 - 0
server/GlobalLobbyProcessor.h

@@ -34,6 +34,9 @@ class GlobalLobbyProcessor : public INetworkClientListener
 	void receiveAccountJoinsRoom(const JsonNode & json);
 
 	void establishNewConnection();
+	void sendMessage(const NetworkConnectionPtr & targetConnection, const JsonNode & payload);
 public:
+	void sendChangeRoomDescription(const std::string & description);
+
 	explicit GlobalLobbyProcessor(CVCMIServer & owner);
 };