2023-11-11 16:43:58 +02:00
|
|
|
/*
|
|
|
|
* LobbyServer.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "LobbyServer.h"
|
|
|
|
|
2023-12-29 13:09:56 +02:00
|
|
|
#include "LobbyDatabase.h"
|
2023-11-11 16:43:58 +02:00
|
|
|
|
2024-03-27 13:08:41 +02:00
|
|
|
#include "../lib/Languages.h"
|
|
|
|
#include "../lib/TextOperations.h"
|
2024-03-04 14:12:15 +02:00
|
|
|
#include "../lib/json/JsonFormatException.h"
|
2024-02-14 13:21:38 +02:00
|
|
|
#include "../lib/json/JsonNode.h"
|
2024-03-04 14:12:15 +02:00
|
|
|
#include "../lib/json/JsonUtils.h"
|
2023-11-12 16:35:53 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
#include <boost/uuid/uuid_generators.hpp>
|
2024-01-11 22:51:36 +02:00
|
|
|
#include <boost/uuid/uuid_io.hpp>
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-01-26 17:40:31 +02:00
|
|
|
bool LobbyServer::isAccountNameValid(const std::string & accountName) const
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
2024-03-27 13:08:41 +02:00
|
|
|
// Arbitrary limit on account name length.
|
|
|
|
// Can be extended if there are no issues with UI space
|
2024-01-11 22:51:36 +02:00
|
|
|
if(accountName.size() < 4)
|
2023-12-30 00:41:16 +02:00
|
|
|
return false;
|
|
|
|
|
2024-02-12 18:57:20 +02:00
|
|
|
if(accountName.size() > 20)
|
2023-12-30 00:41:16 +02:00
|
|
|
return false;
|
|
|
|
|
2024-03-27 13:08:41 +02:00
|
|
|
// For now permit only latin alphabet and numbers
|
|
|
|
// Can be extended, but makes sure that such symbols will be present in all H3 fonts
|
2024-01-11 22:51:36 +02:00
|
|
|
for(const auto & c : accountName)
|
|
|
|
if(!std::isalnum(c))
|
2023-12-30 00:41:16 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LobbyServer::sanitizeChatMessage(const std::string & inputString) const
|
|
|
|
{
|
2024-03-27 13:08:41 +02:00
|
|
|
static const std::string blacklist = "{}";
|
|
|
|
std::string sanitized;
|
|
|
|
|
|
|
|
for(const auto & ch : inputString)
|
|
|
|
{
|
|
|
|
// Remove all control characters
|
|
|
|
if (ch >= '\0' && ch < ' ')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Remove blacklisted characters such as brackets that are used for text formatting
|
|
|
|
if (blacklist.find(ch) != std::string::npos)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sanitized += ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return boost::trim_copy(sanitized);
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
2024-01-26 17:40:31 +02:00
|
|
|
NetworkConnectionPtr LobbyServer::findAccount(const std::string & accountID) const
|
2023-11-12 16:35:53 +02:00
|
|
|
{
|
2024-01-11 22:51:36 +02:00
|
|
|
for(const auto & account : activeAccounts)
|
2024-02-02 00:29:15 +02:00
|
|
|
if(account.second == accountID)
|
2023-12-30 00:41:16 +02:00
|
|
|
return account.first;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-01-26 17:40:31 +02:00
|
|
|
NetworkConnectionPtr LobbyServer::findGameRoom(const std::string & gameRoomID) const
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
2024-01-11 22:51:36 +02:00
|
|
|
for(const auto & account : activeGameRooms)
|
2024-02-02 00:29:15 +02:00
|
|
|
if(account.second == gameRoomID)
|
2023-12-30 00:41:16 +02:00
|
|
|
return account.first;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::sendMessage(const NetworkConnectionPtr & target, const JsonNode & json)
|
|
|
|
{
|
2024-03-12 20:53:39 +02:00
|
|
|
logGlobal->info("Sending message of type %s", json["type"].String());
|
|
|
|
|
2024-03-08 14:23:08 +02:00
|
|
|
assert(JsonUtils::validate(json, "vcmi:lobbyProtocol/" + json["type"].String(), json["type"].String() + " pack"));
|
2024-02-13 13:18:10 +02:00
|
|
|
target->sendPacket(json.toBytes());
|
2023-11-12 16:35:53 +02:00
|
|
|
}
|
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
void LobbyServer::sendAccountCreated(const NetworkConnectionPtr & target, const std::string & accountID, const std::string & accountCookie)
|
2023-12-26 20:54:32 +02:00
|
|
|
{
|
2023-12-30 00:41:16 +02:00
|
|
|
JsonNode reply;
|
|
|
|
reply["type"].String() = "accountCreated";
|
|
|
|
reply["accountID"].String() = accountID;
|
|
|
|
reply["accountCookie"].String() = accountCookie;
|
|
|
|
sendMessage(target, reply);
|
2023-12-26 20:54:32 +02:00
|
|
|
}
|
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
void LobbyServer::sendInviteReceived(const NetworkConnectionPtr & target, const std::string & accountID, const std::string & gameRoomID)
|
|
|
|
{
|
|
|
|
JsonNode reply;
|
|
|
|
reply["type"].String() = "inviteReceived";
|
|
|
|
reply["accountID"].String() = accountID;
|
|
|
|
reply["gameRoomID"].String() = gameRoomID;
|
|
|
|
sendMessage(target, reply);
|
|
|
|
}
|
2023-11-11 16:43:58 +02:00
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
void LobbyServer::sendOperationFailed(const NetworkConnectionPtr & target, const std::string & reason)
|
2023-11-12 21:23:42 +02:00
|
|
|
{
|
2023-12-30 00:41:16 +02:00
|
|
|
JsonNode reply;
|
2024-02-02 00:12:30 +02:00
|
|
|
reply["type"].String() = "operationFailed";
|
2023-12-30 00:41:16 +02:00
|
|
|
reply["reason"].String() = reason;
|
|
|
|
sendMessage(target, reply);
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 17:48:56 +02:00
|
|
|
void LobbyServer::sendClientLoginSuccess(const NetworkConnectionPtr & target, const std::string & accountCookie, const std::string & displayName)
|
2023-11-11 16:43:58 +02:00
|
|
|
{
|
2023-12-30 00:41:16 +02:00
|
|
|
JsonNode reply;
|
2024-03-11 17:48:56 +02:00
|
|
|
reply["type"].String() = "clientLoginSuccess";
|
|
|
|
reply["accountCookie"].String() = accountCookie;
|
|
|
|
reply["displayName"].String() = displayName;
|
|
|
|
sendMessage(target, reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::sendServerLoginSuccess(const NetworkConnectionPtr & target, const std::string & accountCookie)
|
|
|
|
{
|
|
|
|
JsonNode reply;
|
|
|
|
reply["type"].String() = "serverLoginSuccess";
|
2023-12-30 00:41:16 +02:00
|
|
|
reply["accountCookie"].String() = accountCookie;
|
|
|
|
sendMessage(target, reply);
|
|
|
|
}
|
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
void LobbyServer::sendFullChatHistory(const NetworkConnectionPtr & target, const std::string & channelType, const std::string & channelName, const std::string & channelNameForClient)
|
|
|
|
{
|
|
|
|
sendChatHistory(target, channelType, channelNameForClient, database->getFullMessageHistory(channelType, channelName));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::sendRecentChatHistory(const NetworkConnectionPtr & target, const std::string & channelType, const std::string & channelName)
|
|
|
|
{
|
|
|
|
sendChatHistory(target, channelType, channelName, database->getRecentMessageHistory(channelType, channelName));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::sendChatHistory(const NetworkConnectionPtr & target, const std::string & channelType, const std::string & channelName, const std::vector<LobbyChatMessage> & history)
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
|
|
|
JsonNode reply;
|
|
|
|
reply["type"].String() = "chatHistory";
|
2024-03-13 23:02:53 +02:00
|
|
|
reply["channelType"].String() = channelType;
|
|
|
|
reply["channelName"].String() = channelName;
|
2024-03-08 14:23:08 +02:00
|
|
|
reply["messages"].Vector(); // force creation of empty vector
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
for(const auto & message : boost::adaptors::reverse(history))
|
|
|
|
{
|
|
|
|
JsonNode jsonEntry;
|
2023-11-11 16:43:58 +02:00
|
|
|
|
2024-01-08 20:50:43 +02:00
|
|
|
jsonEntry["accountID"].String() = message.accountID;
|
|
|
|
jsonEntry["displayName"].String() = message.displayName;
|
2023-12-30 00:41:16 +02:00
|
|
|
jsonEntry["messageText"].String() = message.messageText;
|
|
|
|
jsonEntry["ageSeconds"].Integer() = message.age.count();
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
reply["messages"].Vector().push_back(jsonEntry);
|
|
|
|
}
|
2023-12-27 19:07:49 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
sendMessage(target, reply);
|
2023-12-27 14:24:49 +02:00
|
|
|
}
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
void LobbyServer::broadcastActiveAccounts()
|
2023-12-27 14:24:49 +02:00
|
|
|
{
|
2023-12-30 00:41:16 +02:00
|
|
|
auto activeAccountsStats = database->getActiveAccounts();
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
JsonNode reply;
|
|
|
|
reply["type"].String() = "activeAccounts";
|
2024-03-08 14:23:08 +02:00
|
|
|
reply["accounts"].Vector(); // force creation of empty vector
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
for(const auto & account : activeAccountsStats)
|
|
|
|
{
|
|
|
|
JsonNode jsonEntry;
|
|
|
|
jsonEntry["accountID"].String() = account.accountID;
|
|
|
|
jsonEntry["displayName"].String() = account.displayName;
|
2024-01-21 16:48:36 +02:00
|
|
|
jsonEntry["status"].String() = "In Lobby"; // TODO: in room status, in match status, offline status(?)
|
2023-12-30 00:41:16 +02:00
|
|
|
reply["accounts"].Vector().push_back(jsonEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(const auto & connection : activeAccounts)
|
|
|
|
sendMessage(connection.first, reply);
|
|
|
|
}
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2024-03-20 18:40:37 +02:00
|
|
|
static JsonNode loadLobbyAccountToJson(const LobbyAccount & account)
|
|
|
|
{
|
|
|
|
JsonNode jsonEntry;
|
|
|
|
jsonEntry["accountID"].String() = account.accountID;
|
|
|
|
jsonEntry["displayName"].String() = account.displayName;
|
|
|
|
return jsonEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JsonNode loadLobbyGameRoomToJson(const LobbyGameRoom & gameRoom)
|
|
|
|
{
|
|
|
|
static constexpr std::array LOBBY_ROOM_STATE_NAMES = {
|
|
|
|
"idle",
|
|
|
|
"public",
|
|
|
|
"private",
|
|
|
|
"busy",
|
|
|
|
"cancelled",
|
|
|
|
"closed"
|
|
|
|
};
|
|
|
|
|
|
|
|
JsonNode jsonEntry;
|
|
|
|
jsonEntry["gameRoomID"].String() = gameRoom.roomID;
|
|
|
|
jsonEntry["hostAccountID"].String() = gameRoom.hostAccountID;
|
|
|
|
jsonEntry["hostAccountDisplayName"].String() = gameRoom.hostAccountDisplayName;
|
|
|
|
jsonEntry["description"].String() = gameRoom.description;
|
2024-04-15 12:34:16 +02:00
|
|
|
jsonEntry["version"].String() = gameRoom.version;
|
2024-03-20 18:40:37 +02:00
|
|
|
jsonEntry["status"].String() = LOBBY_ROOM_STATE_NAMES[vstd::to_underlying(gameRoom.roomState)];
|
|
|
|
jsonEntry["playerLimit"].Integer() = gameRoom.playerLimit;
|
2024-03-20 20:12:43 +02:00
|
|
|
jsonEntry["ageSeconds"].Integer() = gameRoom.age.count();
|
2024-05-12 21:26:08 +02:00
|
|
|
if (!gameRoom.modsJson.empty()) // not present in match history
|
|
|
|
jsonEntry["mods"] = JsonNode(reinterpret_cast<const std::byte *>(gameRoom.modsJson.data()), gameRoom.modsJson.size());
|
2024-03-20 18:40:37 +02:00
|
|
|
|
2024-03-27 13:08:41 +02:00
|
|
|
for(const auto & account : gameRoom.participants)
|
2024-03-20 18:40:37 +02:00
|
|
|
jsonEntry["participants"].Vector().push_back(loadLobbyAccountToJson(account));
|
|
|
|
|
2024-05-13 13:43:28 +02:00
|
|
|
for(const auto & account : gameRoom.invited)
|
|
|
|
jsonEntry["invited"].Vector().push_back(loadLobbyAccountToJson(account));
|
|
|
|
|
2024-03-20 18:40:37 +02:00
|
|
|
return jsonEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::sendMatchesHistory(const NetworkConnectionPtr & target)
|
|
|
|
{
|
|
|
|
std::string accountID = activeAccounts.at(target);
|
|
|
|
|
|
|
|
auto matchesHistory = database->getAccountGameHistory(accountID);
|
|
|
|
JsonNode reply;
|
|
|
|
reply["type"].String() = "matchesHistory";
|
|
|
|
reply["matchesHistory"].Vector(); // force creation of empty vector
|
|
|
|
|
|
|
|
for(const auto & gameRoom : matchesHistory)
|
|
|
|
reply["matchesHistory"].Vector().push_back(loadLobbyGameRoomToJson(gameRoom));
|
|
|
|
|
|
|
|
sendMessage(target, reply);
|
|
|
|
}
|
2024-03-12 19:52:42 +02:00
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
JsonNode LobbyServer::prepareActiveGameRooms()
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
|
|
|
auto activeGameRoomStats = database->getActiveGameRooms();
|
|
|
|
JsonNode reply;
|
|
|
|
reply["type"].String() = "activeGameRooms";
|
2024-03-08 14:23:08 +02:00
|
|
|
reply["gameRooms"].Vector(); // force creation of empty vector
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
for(const auto & gameRoom : activeGameRoomStats)
|
2024-03-20 18:40:37 +02:00
|
|
|
reply["gameRooms"].Vector().push_back(loadLobbyGameRoomToJson(gameRoom));
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::broadcastActiveGameRooms()
|
|
|
|
{
|
|
|
|
auto reply = prepareActiveGameRooms();
|
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
for(const auto & connection : activeAccounts)
|
|
|
|
sendMessage(connection.first, reply);
|
|
|
|
}
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
void LobbyServer::sendAccountJoinsRoom(const NetworkConnectionPtr & target, const std::string & accountID)
|
|
|
|
{
|
|
|
|
JsonNode reply;
|
|
|
|
reply["type"].String() = "accountJoinsRoom";
|
|
|
|
reply["accountID"].String() = accountID;
|
|
|
|
sendMessage(target, reply);
|
|
|
|
}
|
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
void LobbyServer::sendJoinRoomSuccess(const NetworkConnectionPtr & target, const std::string & gameRoomID, bool proxyMode)
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
|
|
|
JsonNode reply;
|
|
|
|
reply["type"].String() = "joinRoomSuccess";
|
|
|
|
reply["gameRoomID"].String() = gameRoomID;
|
2024-01-29 22:05:11 +02:00
|
|
|
reply["proxyMode"].Bool() = proxyMode;
|
2023-12-30 00:41:16 +02:00
|
|
|
sendMessage(target, reply);
|
|
|
|
}
|
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
void LobbyServer::sendChatMessage(const NetworkConnectionPtr & target, const std::string & channelType, const std::string & channelName, const std::string & accountID, const std::string & displayName, const std::string & messageText)
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
2023-12-27 14:24:49 +02:00
|
|
|
JsonNode reply;
|
|
|
|
reply["type"].String() = "chatMessage";
|
|
|
|
reply["messageText"].String() = messageText;
|
2024-01-08 20:50:43 +02:00
|
|
|
reply["accountID"].String() = accountID;
|
|
|
|
reply["displayName"].String() = displayName;
|
2024-03-13 23:02:53 +02:00
|
|
|
reply["channelType"].String() = channelType;
|
|
|
|
reply["channelName"].String() = channelName;
|
2023-12-27 14:24:49 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
sendMessage(target, reply);
|
2023-12-27 14:24:49 +02:00
|
|
|
}
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
void LobbyServer::onNewConnection(const NetworkConnectionPtr & connection)
|
|
|
|
{
|
2024-05-14 21:40:20 +02:00
|
|
|
connection->setAsyncWritesEnabled(true);
|
2023-12-30 00:41:16 +02:00
|
|
|
// no-op - waiting for incoming data
|
|
|
|
}
|
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
void LobbyServer::onDisconnected(const NetworkConnectionPtr & connection, const std::string & errorMessage)
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
2024-02-02 02:36:57 +02:00
|
|
|
if(activeAccounts.count(connection))
|
2024-02-02 15:32:06 +02:00
|
|
|
{
|
2024-03-30 10:24:13 +02:00
|
|
|
logGlobal->info("Account %s disconnecting. Accounts online: %d", activeAccounts.at(connection), activeAccounts.size() - 1);
|
2024-02-02 00:29:15 +02:00
|
|
|
database->setAccountOnline(activeAccounts.at(connection), false);
|
2024-02-02 15:32:06 +02:00
|
|
|
activeAccounts.erase(connection);
|
|
|
|
}
|
2024-01-21 16:48:36 +02:00
|
|
|
|
2024-02-02 02:36:57 +02:00
|
|
|
if(activeGameRooms.count(connection))
|
2024-02-02 15:32:06 +02:00
|
|
|
{
|
2024-03-20 18:40:37 +02:00
|
|
|
std::string gameRoomID = activeGameRooms.at(connection);
|
2024-03-30 10:24:13 +02:00
|
|
|
logGlobal->info("Game room %s disconnecting. Rooms online: %d", gameRoomID, activeGameRooms.size() - 1);
|
2024-03-20 18:40:37 +02:00
|
|
|
|
2024-03-25 23:59:48 +02:00
|
|
|
if (database->getGameRoomStatus(gameRoomID) == LobbyRoomState::BUSY)
|
|
|
|
{
|
|
|
|
database->setGameRoomStatus(gameRoomID, LobbyRoomState::CLOSED);
|
|
|
|
for(const auto & accountConnection : activeAccounts)
|
|
|
|
if (database->isPlayerInGameRoom(accountConnection.second, gameRoomID))
|
|
|
|
sendMatchesHistory(accountConnection.first);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
database->setGameRoomStatus(gameRoomID, LobbyRoomState::CANCELLED);
|
|
|
|
|
2024-02-02 15:32:06 +02:00
|
|
|
activeGameRooms.erase(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(activeProxies.count(connection))
|
|
|
|
{
|
2024-02-03 22:59:56 +02:00
|
|
|
const auto & otherConnection = activeProxies.at(connection);
|
2024-02-02 15:32:06 +02:00
|
|
|
|
|
|
|
if (otherConnection)
|
|
|
|
otherConnection->close();
|
2024-01-21 16:48:36 +02:00
|
|
|
|
2024-02-02 15:32:06 +02:00
|
|
|
activeProxies.erase(connection);
|
|
|
|
activeProxies.erase(otherConnection);
|
|
|
|
}
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
broadcastActiveAccounts();
|
|
|
|
broadcastActiveGameRooms();
|
|
|
|
}
|
|
|
|
|
2024-03-04 14:12:15 +02:00
|
|
|
JsonNode LobbyServer::parseAndValidateMessage(const std::vector<std::byte> & message) const
|
|
|
|
{
|
|
|
|
JsonParsingSettings parserSettings;
|
|
|
|
parserSettings.mode = JsonParsingSettings::JsonFormatMode::JSON;
|
|
|
|
parserSettings.maxDepth = 2;
|
|
|
|
parserSettings.strict = true;
|
|
|
|
|
|
|
|
JsonNode json;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
JsonNode jsonTemp(message.data(), message.size());
|
|
|
|
json = std::move(jsonTemp);
|
|
|
|
}
|
|
|
|
catch (const JsonFormatException & e)
|
|
|
|
{
|
|
|
|
logGlobal->info(std::string("Json parsing error encountered: ") + e.what());
|
|
|
|
return JsonNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string messageType = json["type"].String();
|
|
|
|
|
|
|
|
if (messageType.empty())
|
|
|
|
{
|
|
|
|
logGlobal->info("Json parsing error encountered: Message type not set!");
|
|
|
|
return JsonNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string schemaName = "vcmi:lobbyProtocol/" + messageType;
|
|
|
|
|
2024-03-08 14:23:08 +02:00
|
|
|
if (!JsonUtils::validate(json, schemaName, messageType + " pack"))
|
2024-03-04 14:12:15 +02:00
|
|
|
{
|
|
|
|
logGlobal->info("Json validation error encountered!");
|
2024-03-04 22:13:06 +02:00
|
|
|
assert(0);
|
2024-03-04 14:12:15 +02:00
|
|
|
return JsonNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
void LobbyServer::onPacketReceived(const NetworkConnectionPtr & connection, const std::vector<std::byte> & message)
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
|
|
|
// proxy connection - no processing, only redirect
|
2024-01-11 22:51:36 +02:00
|
|
|
if(activeProxies.count(connection))
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
2024-02-02 15:32:06 +02:00
|
|
|
auto lockedPtr = activeProxies.at(connection);
|
2024-01-11 22:51:36 +02:00
|
|
|
if(lockedPtr)
|
2024-01-29 22:05:11 +02:00
|
|
|
return lockedPtr->sendPacket(message);
|
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
logGlobal->info("Received unexpected message for inactive proxy!");
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
2023-12-27 19:07:49 +02:00
|
|
|
|
2024-03-04 14:12:15 +02:00
|
|
|
JsonNode json = parseAndValidateMessage(message);
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
std::string messageType = json["type"].String();
|
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
// communication messages from vcmiclient
|
2024-01-11 22:51:36 +02:00
|
|
|
if(activeAccounts.count(connection))
|
2023-12-28 21:27:21 +02:00
|
|
|
{
|
2024-02-02 00:29:15 +02:00
|
|
|
std::string accountName = activeAccounts.at(connection);
|
2024-02-02 00:21:52 +02:00
|
|
|
logGlobal->info("%s: Received message of type %s", accountName, messageType);
|
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
if(messageType == "sendChatMessage")
|
2023-12-30 00:41:16 +02:00
|
|
|
return receiveSendChatMessage(connection, json);
|
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
if(messageType == "requestChatHistory")
|
|
|
|
return receiveRequestChatHistory(connection, json);
|
|
|
|
|
2024-03-04 14:11:45 +02:00
|
|
|
if(messageType == "activateGameRoom")
|
|
|
|
return receiveActivateGameRoom(connection, json);
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
if(messageType == "joinGameRoom")
|
2023-12-30 00:41:16 +02:00
|
|
|
return receiveJoinGameRoom(connection, json);
|
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
if(messageType == "sendInvite")
|
2023-12-30 00:41:16 +02:00
|
|
|
return receiveSendInvite(connection, json);
|
|
|
|
|
2024-02-02 00:21:52 +02:00
|
|
|
logGlobal->warn("%s: Unknown message type: %s", accountName, messageType);
|
2024-02-02 00:12:30 +02:00
|
|
|
return;
|
2023-12-28 21:27:21 +02:00
|
|
|
}
|
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
// communication messages from vcmiserver
|
2024-01-11 22:51:36 +02:00
|
|
|
if(activeGameRooms.count(connection))
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
2024-02-02 00:29:15 +02:00
|
|
|
std::string roomName = activeGameRooms.at(connection);
|
2024-02-02 00:21:52 +02:00
|
|
|
logGlobal->info("%s: Received message of type %s", roomName, messageType);
|
|
|
|
|
2024-03-11 19:47:35 +02:00
|
|
|
if(messageType == "changeRoomDescription")
|
|
|
|
return receiveChangeRoomDescription(connection, json);
|
|
|
|
|
2024-03-12 20:53:39 +02:00
|
|
|
if(messageType == "gameStarted")
|
|
|
|
return receiveGameStarted(connection, json);
|
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
if(messageType == "leaveGameRoom")
|
2023-12-30 00:41:16 +02:00
|
|
|
return receiveLeaveGameRoom(connection, json);
|
|
|
|
|
2024-02-02 00:21:52 +02:00
|
|
|
logGlobal->warn("%s: Unknown message type: %s", roomName, messageType);
|
2024-02-02 00:12:30 +02:00
|
|
|
return;
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
2024-02-02 00:21:52 +02:00
|
|
|
logGlobal->info("(unauthorised): Received message of type %s", messageType);
|
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
// unauthorized connections - permit only login or register attempts
|
2024-02-02 00:12:30 +02:00
|
|
|
if(messageType == "clientLogin")
|
2023-12-30 00:41:16 +02:00
|
|
|
return receiveClientLogin(connection, json);
|
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
if(messageType == "clientRegister")
|
2023-12-30 00:41:16 +02:00
|
|
|
return receiveClientRegister(connection, json);
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
if(messageType == "serverLogin")
|
2023-12-30 00:41:16 +02:00
|
|
|
return receiveServerLogin(connection, json);
|
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
if(messageType == "clientProxyLogin")
|
2023-12-30 00:41:16 +02:00
|
|
|
return receiveClientProxyLogin(connection, json);
|
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
if(messageType == "serverProxyLogin")
|
2023-12-30 00:41:16 +02:00
|
|
|
return receiveServerProxyLogin(connection, json);
|
|
|
|
|
2024-02-02 15:32:06 +02:00
|
|
|
connection->close();
|
2024-02-02 00:21:52 +02:00
|
|
|
logGlobal->info("(unauthorised): Unknown message type %s", messageType);
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
void LobbyServer::receiveRequestChatHistory(const NetworkConnectionPtr & connection, const JsonNode & json)
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
2024-02-02 00:29:15 +02:00
|
|
|
std::string accountID = activeAccounts[connection];
|
2024-03-13 23:02:53 +02:00
|
|
|
std::string channelType = json["channelType"].String();
|
|
|
|
std::string channelName = json["channelName"].String();
|
|
|
|
|
|
|
|
if (channelType == "global")
|
|
|
|
{
|
|
|
|
// can only be sent on connection, initiated by server
|
|
|
|
sendOperationFailed(connection, "Operation not supported!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channelType == "match")
|
|
|
|
{
|
|
|
|
if (!database->isPlayerInGameRoom(accountID, channelName))
|
|
|
|
return sendOperationFailed(connection, "Can not access room you are not part of!");
|
|
|
|
|
|
|
|
sendFullChatHistory(connection, channelType, channelName, channelName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channelType == "player")
|
|
|
|
{
|
|
|
|
if (!database->isAccountIDExists(channelName))
|
|
|
|
return sendOperationFailed(connection, "Such player does not exists!");
|
|
|
|
|
|
|
|
// room ID for private messages is actually <player 1 ID>_<player 2 ID>, with player ID's sorted alphabetically (to generate unique room ID)
|
|
|
|
std::string roomID = std::min(accountID, channelName) + "_" + std::max(accountID, channelName);
|
|
|
|
sendFullChatHistory(connection, channelType, roomID, channelName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::receiveSendChatMessage(const NetworkConnectionPtr & connection, const JsonNode & json)
|
|
|
|
{
|
|
|
|
std::string senderAccountID = activeAccounts[connection];
|
2023-12-30 00:41:16 +02:00
|
|
|
std::string messageText = json["messageText"].String();
|
2024-03-13 23:02:53 +02:00
|
|
|
std::string channelType = json["channelType"].String();
|
|
|
|
std::string channelName = json["channelName"].String();
|
|
|
|
std::string displayName = database->getAccountDisplayName(senderAccountID);
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-03-29 15:03:42 +02:00
|
|
|
if(!TextOperations::isValidUnicodeString(messageText))
|
2024-03-27 13:08:41 +02:00
|
|
|
return sendOperationFailed(connection, "String contains invalid characters!");
|
|
|
|
|
|
|
|
std::string messageTextClean = sanitizeChatMessage(messageText);
|
2024-01-11 22:51:36 +02:00
|
|
|
if(messageTextClean.empty())
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "No printable characters in sent message!");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
if (channelType == "global")
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Languages::getLanguageOptions(channelName);
|
|
|
|
}
|
2024-03-27 13:08:41 +02:00
|
|
|
catch (const std::out_of_range &)
|
2024-03-13 23:02:53 +02:00
|
|
|
{
|
|
|
|
return sendOperationFailed(connection, "Unknown language!");
|
|
|
|
}
|
|
|
|
database->insertChatMessage(senderAccountID, channelType, channelName, messageText);
|
|
|
|
|
|
|
|
for(const auto & otherConnection : activeAccounts)
|
|
|
|
sendChatMessage(otherConnection.first, channelType, channelName, senderAccountID, displayName, messageText);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channelType == "match")
|
|
|
|
{
|
|
|
|
if (!database->isPlayerInGameRoom(senderAccountID, channelName))
|
|
|
|
return sendOperationFailed(connection, "Can not access room you are not part of!");
|
|
|
|
|
|
|
|
database->insertChatMessage(senderAccountID, channelType, channelName, messageText);
|
|
|
|
|
2024-03-27 13:08:41 +02:00
|
|
|
LobbyRoomState roomStatus = database->getGameRoomStatus(channelName);
|
|
|
|
|
|
|
|
// Broadcast chat message only if it being sent to already closed match
|
|
|
|
// Othervice it will be handled by match server
|
|
|
|
if (roomStatus == LobbyRoomState::CLOSED)
|
2024-03-13 23:02:53 +02:00
|
|
|
{
|
2024-03-27 13:08:41 +02:00
|
|
|
for(const auto & otherConnection : activeAccounts)
|
|
|
|
{
|
|
|
|
if (database->isPlayerInGameRoom(otherConnection.second, channelName))
|
|
|
|
sendChatMessage(otherConnection.first, channelType, channelName, senderAccountID, displayName, messageText);
|
|
|
|
}
|
2024-03-13 23:02:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channelType == "player")
|
|
|
|
{
|
|
|
|
const std::string & receiverAccountID = channelName;
|
|
|
|
std::string roomID = std::min(senderAccountID, receiverAccountID) + "_" + std::max(senderAccountID, receiverAccountID);
|
|
|
|
|
|
|
|
if (!database->isAccountIDExists(receiverAccountID))
|
|
|
|
return sendOperationFailed(connection, "Such player does not exists!");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
database->insertChatMessage(senderAccountID, channelType, roomID, messageText);
|
|
|
|
|
2024-03-18 19:52:53 +02:00
|
|
|
sendChatMessage(connection, channelType, receiverAccountID, senderAccountID, displayName, messageText);
|
|
|
|
if (senderAccountID != receiverAccountID)
|
2024-03-13 23:02:53 +02:00
|
|
|
{
|
2024-03-18 19:52:53 +02:00
|
|
|
for(const auto & otherConnection : activeAccounts)
|
|
|
|
if (otherConnection.second == receiverAccountID)
|
|
|
|
sendChatMessage(otherConnection.first, channelType, senderAccountID, senderAccountID, displayName, messageText);
|
2024-03-13 23:02:53 +02:00
|
|
|
}
|
|
|
|
}
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::receiveClientRegister(const NetworkConnectionPtr & connection, const JsonNode & json)
|
|
|
|
{
|
|
|
|
std::string displayName = json["displayName"].String();
|
|
|
|
std::string language = json["language"].String();
|
|
|
|
|
2024-03-08 14:23:08 +02:00
|
|
|
if(!isAccountNameValid(displayName))
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "Illegal account name");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(database->isAccountNameExists(displayName))
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "Account name already in use");
|
2024-01-08 20:50:43 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
std::string accountCookie = boost::uuids::to_string(boost::uuids::random_generator()());
|
2024-01-08 20:50:43 +02:00
|
|
|
std::string accountID = boost::uuids::to_string(boost::uuids::random_generator()());
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
database->insertAccount(accountID, displayName);
|
|
|
|
database->insertAccessCookie(accountID, accountCookie);
|
|
|
|
|
|
|
|
sendAccountCreated(connection, accountID, accountCookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::receiveClientLogin(const NetworkConnectionPtr & connection, const JsonNode & json)
|
|
|
|
{
|
|
|
|
std::string accountID = json["accountID"].String();
|
|
|
|
std::string accountCookie = json["accountCookie"].String();
|
|
|
|
std::string language = json["language"].String();
|
|
|
|
std::string version = json["version"].String();
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(!database->isAccountIDExists(accountID))
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "Account not found");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-02-02 15:32:06 +02:00
|
|
|
auto clientCookieStatus = database->getAccountCookieStatus(accountID, accountCookie);
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(clientCookieStatus == LobbyCookieStatus::INVALID)
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "Authentification failure");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
database->updateAccountLoginTime(accountID);
|
2024-01-21 16:48:36 +02:00
|
|
|
database->setAccountOnline(accountID, true);
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
std::string displayName = database->getAccountDisplayName(accountID);
|
|
|
|
|
2024-02-02 00:29:15 +02:00
|
|
|
activeAccounts[connection] = accountID;
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-03-29 15:04:03 +02:00
|
|
|
logGlobal->info("%s: Logged in as %s", accountID, displayName);
|
2024-03-11 17:48:56 +02:00
|
|
|
sendClientLoginSuccess(connection, accountCookie, displayName);
|
2024-03-13 23:02:53 +02:00
|
|
|
sendRecentChatHistory(connection, "global", "english");
|
|
|
|
if (language != "english")
|
|
|
|
sendRecentChatHistory(connection, "global", language);
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-02-02 15:32:06 +02:00
|
|
|
// send active game rooms list to new account
|
|
|
|
// and update acount list to everybody else including new account
|
2023-12-30 00:41:16 +02:00
|
|
|
broadcastActiveAccounts();
|
2024-01-29 22:05:11 +02:00
|
|
|
sendMessage(connection, prepareActiveGameRooms());
|
2024-03-20 18:40:37 +02:00
|
|
|
sendMatchesHistory(connection);
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::receiveServerLogin(const NetworkConnectionPtr & connection, const JsonNode & json)
|
|
|
|
{
|
|
|
|
std::string gameRoomID = json["gameRoomID"].String();
|
|
|
|
std::string accountID = json["accountID"].String();
|
|
|
|
std::string accountCookie = json["accountCookie"].String();
|
|
|
|
std::string version = json["version"].String();
|
|
|
|
|
2024-02-02 15:32:06 +02:00
|
|
|
auto clientCookieStatus = database->getAccountCookieStatus(accountID, accountCookie);
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(clientCookieStatus == LobbyCookieStatus::INVALID)
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
2024-02-02 00:12:30 +02:00
|
|
|
sendOperationFailed(connection, "Invalid credentials");
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
|
|
|
else
|
2023-12-27 14:24:49 +02:00
|
|
|
{
|
2024-04-15 12:34:16 +02:00
|
|
|
std::string modListString = json["mods"].isNull() ? "[]" : json["mods"].toCompactString();
|
|
|
|
database->insertGameRoom(gameRoomID, accountID, version, modListString);
|
2024-02-02 00:29:15 +02:00
|
|
|
activeGameRooms[connection] = gameRoomID;
|
2024-03-11 17:48:56 +02:00
|
|
|
sendServerLoginSuccess(connection, accountCookie);
|
2024-02-02 00:12:30 +02:00
|
|
|
broadcastActiveGameRooms();
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
void LobbyServer::receiveClientProxyLogin(const NetworkConnectionPtr & connection, const JsonNode & json)
|
|
|
|
{
|
|
|
|
std::string gameRoomID = json["gameRoomID"].String();
|
|
|
|
std::string accountID = json["accountID"].String();
|
|
|
|
std::string accountCookie = json["accountCookie"].String();
|
|
|
|
|
2024-02-02 15:32:06 +02:00
|
|
|
auto clientCookieStatus = database->getAccountCookieStatus(accountID, accountCookie);
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(clientCookieStatus != LobbyCookieStatus::INVALID)
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
2024-01-11 22:51:36 +02:00
|
|
|
for(auto & proxyEntry : awaitingProxies)
|
2023-12-28 21:27:21 +02:00
|
|
|
{
|
2024-01-11 22:51:36 +02:00
|
|
|
if(proxyEntry.accountID != accountID)
|
2023-12-30 00:41:16 +02:00
|
|
|
continue;
|
2024-01-11 22:51:36 +02:00
|
|
|
if(proxyEntry.roomID != gameRoomID)
|
2023-12-30 00:41:16 +02:00
|
|
|
continue;
|
2023-11-12 21:23:42 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
proxyEntry.accountConnection = connection;
|
2023-12-27 14:24:49 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
auto gameRoomConnection = proxyEntry.roomConnection.lock();
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(gameRoomConnection)
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
|
|
|
activeProxies[gameRoomConnection] = connection;
|
|
|
|
activeProxies[connection] = gameRoomConnection;
|
|
|
|
}
|
|
|
|
return;
|
2023-12-28 21:27:21 +02:00
|
|
|
}
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
2023-12-28 21:27:21 +02:00
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
sendOperationFailed(connection, "Invalid credentials");
|
2024-02-02 15:32:06 +02:00
|
|
|
connection->close();
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::receiveServerProxyLogin(const NetworkConnectionPtr & connection, const JsonNode & json)
|
|
|
|
{
|
|
|
|
std::string gameRoomID = json["gameRoomID"].String();
|
|
|
|
std::string guestAccountID = json["guestAccountID"].String();
|
2024-01-29 22:05:11 +02:00
|
|
|
std::string accountCookie = json["accountCookie"].String();
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
// FIXME: find host account ID and validate his cookie
|
|
|
|
//auto clientCookieStatus = database->getAccountCookieStatus(hostAccountID, accountCookie, accountCookieLifetime);
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
//if(clientCookieStatus != LobbyCookieStatus::INVALID)
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
|
|
|
NetworkConnectionPtr targetAccount = findAccount(guestAccountID);
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(targetAccount == nullptr)
|
2024-02-02 00:12:30 +02:00
|
|
|
{
|
|
|
|
sendOperationFailed(connection, "Invalid credentials");
|
2023-12-30 00:41:16 +02:00
|
|
|
return; // unknown / disconnected account
|
2024-02-02 00:12:30 +02:00
|
|
|
}
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
sendJoinRoomSuccess(targetAccount, gameRoomID, true);
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
AwaitingProxyState proxy;
|
|
|
|
proxy.accountID = guestAccountID;
|
|
|
|
proxy.roomID = gameRoomID;
|
|
|
|
proxy.roomConnection = connection;
|
|
|
|
awaitingProxies.push_back(proxy);
|
|
|
|
return;
|
2023-12-28 21:27:21 +02:00
|
|
|
}
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-02-02 15:32:06 +02:00
|
|
|
//connection->close();
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
2024-03-04 14:11:45 +02:00
|
|
|
void LobbyServer::receiveActivateGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json)
|
2023-12-27 19:07:49 +02:00
|
|
|
{
|
2023-12-30 00:41:16 +02:00
|
|
|
std::string hostAccountID = json["hostAccountID"].String();
|
2024-02-02 00:29:15 +02:00
|
|
|
std::string accountID = activeAccounts[connection];
|
2024-03-11 20:03:33 +02:00
|
|
|
int playerLimit = json["playerLimit"].Integer();
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
if(database->isPlayerInGameRoom(accountID))
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "Player already in the room!");
|
2023-12-27 19:07:49 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
std::string gameRoomID = database->getIdleGameRoom(hostAccountID);
|
2024-01-11 22:51:36 +02:00
|
|
|
if(gameRoomID.empty())
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "Failed to find idle server to join!");
|
2023-12-27 19:07:49 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
std::string roomType = json["roomType"].String();
|
2024-02-02 00:12:30 +02:00
|
|
|
if(roomType != "public" && roomType != "private")
|
|
|
|
return sendOperationFailed(connection, "Invalid room type!");
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(roomType == "public")
|
2023-12-30 00:41:16 +02:00
|
|
|
database->setGameRoomStatus(gameRoomID, LobbyRoomState::PUBLIC);
|
2024-01-11 22:51:36 +02:00
|
|
|
if(roomType == "private")
|
2023-12-30 00:41:16 +02:00
|
|
|
database->setGameRoomStatus(gameRoomID, LobbyRoomState::PRIVATE);
|
|
|
|
|
2024-03-11 20:03:33 +02:00
|
|
|
database->updateRoomPlayerLimit(gameRoomID, playerLimit);
|
2024-01-27 23:41:36 +02:00
|
|
|
database->insertPlayerIntoGameRoom(accountID, gameRoomID);
|
2023-12-30 00:41:16 +02:00
|
|
|
broadcastActiveGameRooms();
|
2024-01-29 22:05:11 +02:00
|
|
|
sendJoinRoomSuccess(connection, gameRoomID, false);
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::receiveJoinGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json)
|
|
|
|
{
|
|
|
|
std::string gameRoomID = json["gameRoomID"].String();
|
2024-02-02 00:29:15 +02:00
|
|
|
std::string accountID = activeAccounts[connection];
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
if(database->isPlayerInGameRoom(accountID))
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "Player already in the room!");
|
2023-12-27 19:07:49 +02:00
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
NetworkConnectionPtr targetRoom = findGameRoom(gameRoomID);
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(targetRoom == nullptr)
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "Failed to find game room to join!");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
auto roomStatus = database->getGameRoomStatus(gameRoomID);
|
|
|
|
|
2024-01-28 00:04:54 +02:00
|
|
|
if(roomStatus != LobbyRoomState::PRIVATE && roomStatus != LobbyRoomState::PUBLIC)
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "Room does not accepts new players!");
|
2024-01-28 00:04:54 +02:00
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(roomStatus == LobbyRoomState::PRIVATE)
|
2023-12-30 00:41:16 +02:00
|
|
|
{
|
2024-01-11 22:51:36 +02:00
|
|
|
if(database->getAccountInviteStatus(accountID, gameRoomID) != LobbyInviteStatus::INVITED)
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "You are not permitted to join private room without invite!");
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(database->getGameRoomFreeSlots(gameRoomID) == 0)
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "Room is already full!");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-01-27 23:41:36 +02:00
|
|
|
database->insertPlayerIntoGameRoom(accountID, gameRoomID);
|
2023-12-30 00:41:16 +02:00
|
|
|
sendAccountJoinsRoom(targetRoom, accountID);
|
|
|
|
//No reply to client - will be sent once match server establishes proxy connection with lobby
|
|
|
|
|
|
|
|
broadcastActiveGameRooms();
|
|
|
|
}
|
|
|
|
|
2024-03-11 19:47:35 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-03-12 20:53:39 +02:00
|
|
|
void LobbyServer::receiveGameStarted(const NetworkConnectionPtr & connection, const JsonNode & json)
|
|
|
|
{
|
|
|
|
std::string gameRoomID = activeGameRooms[connection];
|
|
|
|
|
|
|
|
database->setGameRoomStatus(gameRoomID, LobbyRoomState::BUSY);
|
|
|
|
broadcastActiveGameRooms();
|
|
|
|
}
|
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
void LobbyServer::receiveLeaveGameRoom(const NetworkConnectionPtr & connection, const JsonNode & json)
|
|
|
|
{
|
2024-02-10 19:02:25 +02:00
|
|
|
std::string accountID = json["accountID"].String();
|
|
|
|
std::string gameRoomID = activeGameRooms[connection];
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-02-10 19:02:25 +02:00
|
|
|
if(!database->isPlayerInGameRoom(accountID, gameRoomID))
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "You are not in the room!");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-02-10 19:02:25 +02:00
|
|
|
database->deletePlayerFromGameRoom(accountID, gameRoomID);
|
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
broadcastActiveGameRooms();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::receiveSendInvite(const NetworkConnectionPtr & connection, const JsonNode & json)
|
|
|
|
{
|
2024-02-02 00:29:15 +02:00
|
|
|
std::string senderName = activeAccounts[connection];
|
2023-12-30 00:41:16 +02:00
|
|
|
std::string accountID = json["accountID"].String();
|
|
|
|
std::string gameRoomID = database->getAccountGameRoom(senderName);
|
|
|
|
|
2024-03-25 18:04:44 +02:00
|
|
|
auto targetAccountConnection = findAccount(accountID);
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-03-25 18:04:44 +02:00
|
|
|
if(!targetAccountConnection)
|
|
|
|
return sendOperationFailed(connection, "Player is offline or does not exists!");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
if(!database->isPlayerInGameRoom(senderName))
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "You are not in the room!");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
if(database->isPlayerInGameRoom(accountID))
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "This player is already in a room!");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(database->getAccountInviteStatus(accountID, gameRoomID) != LobbyInviteStatus::NOT_INVITED)
|
2024-02-02 00:12:30 +02:00
|
|
|
return sendOperationFailed(connection, "This player is already invited!");
|
2023-12-30 00:41:16 +02:00
|
|
|
|
|
|
|
database->insertGameRoomInvite(accountID, gameRoomID);
|
2024-03-25 18:04:44 +02:00
|
|
|
sendInviteReceived(targetAccountConnection, senderName, gameRoomID);
|
2024-05-13 14:12:55 +02:00
|
|
|
broadcastActiveGameRooms();
|
2023-12-30 00:41:16 +02:00
|
|
|
}
|
|
|
|
|
2023-12-29 13:09:56 +02:00
|
|
|
LobbyServer::~LobbyServer() = default;
|
|
|
|
|
2023-12-30 00:41:16 +02:00
|
|
|
LobbyServer::LobbyServer(const boost::filesystem::path & databasePath)
|
2024-01-26 17:40:31 +02:00
|
|
|
: database(std::make_unique<LobbyDatabase>(databasePath))
|
2024-01-12 01:10:41 +02:00
|
|
|
, networkHandler(INetworkHandler::createHandler())
|
|
|
|
, networkServer(networkHandler->createServerTCP(*this))
|
2023-11-11 16:43:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
void LobbyServer::start(uint16_t port)
|
|
|
|
{
|
|
|
|
networkServer->start(port);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LobbyServer::run()
|
|
|
|
{
|
2024-01-12 01:10:41 +02:00
|
|
|
networkHandler->run();
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|