2024-01-12 13:30:53 +02:00
|
|
|
/*
|
|
|
|
* GlobalLobbyProcessor.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 "GlobalLobbyProcessor.h"
|
|
|
|
|
|
|
|
#include "CVCMIServer.h"
|
|
|
|
#include "../lib/CConfigHandler.h"
|
2024-03-04 22:13:06 +02:00
|
|
|
#include "../lib/json/JsonUtils.h"
|
2024-01-12 13:30:53 +02:00
|
|
|
|
|
|
|
GlobalLobbyProcessor::GlobalLobbyProcessor(CVCMIServer & owner)
|
|
|
|
: owner(owner)
|
2024-01-12 17:21:59 +02:00
|
|
|
{
|
|
|
|
logGlobal->info("Connecting to lobby server");
|
|
|
|
establishNewConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyProcessor::establishNewConnection()
|
2024-01-12 13:30:53 +02:00
|
|
|
{
|
|
|
|
std::string hostname = settings["lobby"]["hostname"].String();
|
2024-03-29 05:37:47 +02:00
|
|
|
uint16_t port = settings["lobby"]["port"].Integer();
|
2024-02-03 17:04:14 +02:00
|
|
|
owner.getNetworkHandler().connectToRemote(*this, hostname, port);
|
2024-01-12 13:30:53 +02:00
|
|
|
}
|
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
|
2024-01-12 13:30:53 +02:00
|
|
|
{
|
2024-02-02 15:32:06 +02:00
|
|
|
if (connection == controlConnection)
|
|
|
|
{
|
2024-02-03 19:08:45 +02:00
|
|
|
owner.setState(EServerState::SHUTDOWN);
|
|
|
|
return;
|
2024-02-02 15:32:06 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-10 19:02:25 +02:00
|
|
|
if (owner.getState() == EServerState::LOBBY)
|
|
|
|
{
|
|
|
|
for (auto const & proxy : proxyConnections)
|
|
|
|
{
|
|
|
|
if (proxy.second == connection)
|
|
|
|
{
|
|
|
|
JsonNode message;
|
|
|
|
message["type"].String() = "leaveGameRoom";
|
|
|
|
message["accountID"].String() = proxy.first;
|
2024-03-04 22:13:06 +02:00
|
|
|
|
2024-03-11 19:47:35 +02:00
|
|
|
sendMessage(controlConnection, message);
|
2024-02-10 19:02:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 15:32:06 +02:00
|
|
|
// player disconnected
|
|
|
|
owner.onDisconnected(connection, errorMessage);
|
|
|
|
}
|
2024-01-12 13:30:53 +02:00
|
|
|
}
|
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
void GlobalLobbyProcessor::onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message)
|
2024-01-12 13:30:53 +02:00
|
|
|
{
|
2024-01-29 22:05:11 +02:00
|
|
|
if (connection == controlConnection)
|
|
|
|
{
|
|
|
|
JsonNode json(message.data(), message.size());
|
2024-01-12 13:30:53 +02:00
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
if(json["type"].String() == "operationFailed")
|
|
|
|
return receiveOperationFailed(json);
|
2024-01-12 13:30:53 +02:00
|
|
|
|
2024-03-11 17:48:56 +02:00
|
|
|
if(json["type"].String() == "serverLoginSuccess")
|
|
|
|
return receiveServerLoginSuccess(json);
|
2024-01-12 13:30:53 +02:00
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
if(json["type"].String() == "accountJoinsRoom")
|
|
|
|
return receiveAccountJoinsRoom(json);
|
2024-01-12 16:55:36 +02:00
|
|
|
|
2024-02-02 15:32:06 +02:00
|
|
|
logGlobal->error("Received unexpected message from lobby server of type '%s' ", json["type"].String());
|
2024-01-29 22:05:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// received game message via proxy connection
|
|
|
|
owner.onPacketReceived(connection, message);
|
|
|
|
}
|
2024-01-12 13:30:53 +02:00
|
|
|
}
|
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
void GlobalLobbyProcessor::receiveOperationFailed(const JsonNode & json)
|
2024-01-12 13:30:53 +02:00
|
|
|
{
|
2024-01-29 22:05:11 +02:00
|
|
|
logGlobal->info("Lobby: Failed to login into a lobby server!");
|
|
|
|
|
2024-02-03 19:08:45 +02:00
|
|
|
owner.setState(EServerState::SHUTDOWN);
|
2024-01-12 13:30:53 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 17:48:56 +02:00
|
|
|
void GlobalLobbyProcessor::receiveServerLoginSuccess(const JsonNode & json)
|
2024-01-12 13:30:53 +02:00
|
|
|
{
|
|
|
|
// no-op, wait just for any new commands from lobby
|
2024-01-29 22:05:11 +02:00
|
|
|
logGlobal->info("Lobby: Succesfully connected to lobby server");
|
2024-01-21 16:48:36 +02:00
|
|
|
owner.startAcceptingIncomingConnections();
|
2024-01-12 13:30:53 +02:00
|
|
|
}
|
|
|
|
|
2024-01-12 16:55:36 +02:00
|
|
|
void GlobalLobbyProcessor::receiveAccountJoinsRoom(const JsonNode & json)
|
|
|
|
{
|
2024-01-12 17:21:59 +02:00
|
|
|
std::string accountID = json["accountID"].String();
|
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
logGlobal->info("Lobby: Account %s will join our room!", accountID);
|
2024-01-12 17:21:59 +02:00
|
|
|
assert(proxyConnections.count(accountID) == 0);
|
|
|
|
|
|
|
|
proxyConnections[accountID] = nullptr;
|
|
|
|
establishNewConnection();
|
2024-01-12 16:55:36 +02:00
|
|
|
}
|
|
|
|
|
2024-01-12 13:30:53 +02:00
|
|
|
void GlobalLobbyProcessor::onConnectionFailed(const std::string & errorMessage)
|
|
|
|
{
|
2024-02-03 19:08:45 +02:00
|
|
|
owner.setState(EServerState::SHUTDOWN);
|
2024-01-12 13:30:53 +02:00
|
|
|
}
|
|
|
|
|
2024-01-12 16:55:36 +02:00
|
|
|
void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
|
2024-01-12 13:30:53 +02:00
|
|
|
{
|
2024-01-12 17:21:59 +02:00
|
|
|
if (controlConnection == nullptr)
|
|
|
|
{
|
|
|
|
controlConnection = connection;
|
|
|
|
logGlobal->info("Connection to lobby server established");
|
|
|
|
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "serverLogin";
|
2024-01-21 16:48:36 +02:00
|
|
|
toSend["gameRoomID"].String() = owner.uuid;
|
2024-01-12 17:21:59 +02:00
|
|
|
toSend["accountID"] = settings["lobby"]["accountID"];
|
|
|
|
toSend["accountCookie"] = settings["lobby"]["accountCookie"];
|
2024-03-08 14:23:08 +02:00
|
|
|
toSend["version"].String() = VCMI_VERSION_STRING;
|
2024-03-04 22:13:06 +02:00
|
|
|
|
2024-03-11 19:47:35 +02:00
|
|
|
sendMessage(connection, toSend);
|
2024-01-12 17:21:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Proxy connection for a player
|
2024-01-29 22:05:11 +02:00
|
|
|
std::string guestAccountID;
|
2024-01-12 17:21:59 +02:00
|
|
|
for (auto const & proxies : proxyConnections)
|
|
|
|
if (proxies.second == nullptr)
|
2024-01-29 22:05:11 +02:00
|
|
|
guestAccountID = proxies.first;
|
2024-01-12 17:21:59 +02:00
|
|
|
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "serverProxyLogin";
|
2024-01-21 16:48:36 +02:00
|
|
|
toSend["gameRoomID"].String() = owner.uuid;
|
2024-01-29 22:05:11 +02:00
|
|
|
toSend["guestAccountID"].String() = guestAccountID;
|
2024-01-12 17:21:59 +02:00
|
|
|
toSend["accountCookie"] = settings["lobby"]["accountCookie"];
|
2024-03-04 22:13:06 +02:00
|
|
|
|
2024-03-11 19:47:35 +02:00
|
|
|
sendMessage(connection, toSend);
|
2024-01-12 17:21:59 +02:00
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
proxyConnections[guestAccountID] = connection;
|
2024-01-12 17:21:59 +02:00
|
|
|
owner.onNewConnection(connection);
|
|
|
|
}
|
2024-01-12 13:30:53 +02:00
|
|
|
}
|
2024-03-11 19:47:35 +02:00
|
|
|
|
2024-03-12 20:53:39 +02:00
|
|
|
void GlobalLobbyProcessor::sendGameStarted()
|
|
|
|
{
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "gameStarted";
|
|
|
|
sendMessage(controlConnection, toSend);
|
|
|
|
}
|
|
|
|
|
2024-03-11 19:47:35 +02:00
|
|
|
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());
|
|
|
|
}
|