2023-11-11 16:43:58 +02:00
|
|
|
/*
|
2023-12-27 19:07:49 +02:00
|
|
|
* GlobalLobbyClient.cpp, part of VCMI engine
|
2023-11-11 16:43:58 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2023-12-27 19:07:49 +02:00
|
|
|
#include "GlobalLobbyClient.h"
|
|
|
|
|
2023-12-28 21:27:21 +02:00
|
|
|
#include "GlobalLobbyLoginWindow.h"
|
2024-01-11 22:51:36 +02:00
|
|
|
#include "GlobalLobbyWindow.h"
|
2023-11-11 16:43:58 +02:00
|
|
|
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../gui/WindowHandler.h"
|
2023-11-12 13:27:22 +02:00
|
|
|
#include "../windows/InfoWindows.h"
|
2024-01-12 16:55:36 +02:00
|
|
|
#include "../CServerHandler.h"
|
2024-01-29 22:05:11 +02:00
|
|
|
#include "../mainmenu/CMainMenu.h"
|
2023-11-12 13:27:22 +02:00
|
|
|
|
2023-11-12 21:23:42 +02:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
2024-01-11 22:51:36 +02:00
|
|
|
#include "../../lib/MetaString.h"
|
2024-01-08 20:50:43 +02:00
|
|
|
#include "../../lib/TextOperations.h"
|
2023-11-12 16:35:53 +02:00
|
|
|
|
2024-01-12 16:55:36 +02:00
|
|
|
GlobalLobbyClient::GlobalLobbyClient() = default;
|
2024-01-12 01:10:41 +02:00
|
|
|
GlobalLobbyClient::~GlobalLobbyClient() = default;
|
2023-12-27 19:07:49 +02:00
|
|
|
|
2023-11-12 21:23:42 +02:00
|
|
|
static std::string getCurrentTimeFormatted(int timeOffsetSeconds = 0)
|
|
|
|
{
|
|
|
|
// FIXME: better/unified way to format date
|
|
|
|
auto timeNowChrono = std::chrono::system_clock::now();
|
|
|
|
timeNowChrono += std::chrono::seconds(timeOffsetSeconds);
|
|
|
|
|
2024-01-08 20:50:43 +02:00
|
|
|
return TextOperations::getFormattedTimeLocal(std::chrono::system_clock::to_time_t(timeNowChrono));
|
2023-11-12 21:23:42 +02:00
|
|
|
}
|
|
|
|
|
2024-01-12 01:10:41 +02:00
|
|
|
void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<uint8_t> & message)
|
2023-11-11 16:43:58 +02:00
|
|
|
{
|
2023-12-28 21:27:21 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
|
2023-12-29 00:31:53 +02:00
|
|
|
JsonNode json(message.data(), message.size());
|
2023-11-12 16:35:53 +02:00
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(json["type"].String() == "accountCreated")
|
2024-01-08 20:50:43 +02:00
|
|
|
return receiveAccountCreated(json);
|
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
if(json["type"].String() == "operationFailed")
|
|
|
|
return receiveOperationFailed(json);
|
2024-01-08 20:50:43 +02:00
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(json["type"].String() == "loginSuccess")
|
2024-01-08 20:50:43 +02:00
|
|
|
return receiveLoginSuccess(json);
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(json["type"].String() == "chatHistory")
|
2024-01-08 20:50:43 +02:00
|
|
|
return receiveChatHistory(json);
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(json["type"].String() == "chatMessage")
|
2024-01-08 20:50:43 +02:00
|
|
|
return receiveChatMessage(json);
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(json["type"].String() == "activeAccounts")
|
2024-01-08 20:50:43 +02:00
|
|
|
return receiveActiveAccounts(json);
|
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
if(json["type"].String() == "activeGameRooms")
|
|
|
|
return receiveActiveGameRooms(json);
|
|
|
|
|
|
|
|
if(json["type"].String() == "joinRoomSuccess")
|
|
|
|
return receiveJoinRoomSuccess(json);
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
throw std::runtime_error("Received unexpected message from lobby server: " + json["type"].String());
|
2024-01-08 20:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::receiveAccountCreated(const JsonNode & json)
|
|
|
|
{
|
|
|
|
auto loginWindowPtr = loginWindow.lock();
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
|
2024-01-08 20:50:43 +02:00
|
|
|
throw std::runtime_error("lobby connection finished without active login window!");
|
|
|
|
|
2023-12-28 21:27:21 +02:00
|
|
|
{
|
2024-01-08 20:50:43 +02:00
|
|
|
Settings configID = settings.write["lobby"]["accountID"];
|
|
|
|
configID->String() = json["accountID"].String();
|
2023-12-28 21:27:21 +02:00
|
|
|
|
2024-01-08 20:50:43 +02:00
|
|
|
Settings configName = settings.write["lobby"]["displayName"];
|
|
|
|
configName->String() = json["displayName"].String();
|
2023-12-28 21:27:21 +02:00
|
|
|
|
2024-01-08 20:50:43 +02:00
|
|
|
Settings configCookie = settings.write["lobby"]["accountCookie"];
|
|
|
|
configCookie->String() = json["accountCookie"].String();
|
2023-12-28 21:27:21 +02:00
|
|
|
}
|
|
|
|
|
2024-01-08 20:50:43 +02:00
|
|
|
sendClientLogin();
|
|
|
|
}
|
|
|
|
|
2024-02-02 00:12:30 +02:00
|
|
|
void GlobalLobbyClient::receiveOperationFailed(const JsonNode & json)
|
2024-01-08 20:50:43 +02:00
|
|
|
{
|
|
|
|
auto loginWindowPtr = loginWindow.lock();
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
|
2024-01-08 20:50:43 +02:00
|
|
|
throw std::runtime_error("lobby connection finished without active login window!");
|
|
|
|
|
|
|
|
loginWindowPtr->onConnectionFailed(json["reason"].String());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::receiveLoginSuccess(const JsonNode & json)
|
|
|
|
{
|
2023-11-12 16:35:53 +02:00
|
|
|
{
|
2024-01-08 20:50:43 +02:00
|
|
|
Settings configCookie = settings.write["lobby"]["accountCookie"];
|
|
|
|
configCookie->String() = json["accountCookie"].String();
|
|
|
|
|
|
|
|
Settings configName = settings.write["lobby"]["displayName"];
|
|
|
|
configName->String() = json["displayName"].String();
|
2023-11-12 21:23:42 +02:00
|
|
|
}
|
2023-11-12 16:35:53 +02:00
|
|
|
|
2024-01-08 20:50:43 +02:00
|
|
|
auto loginWindowPtr = loginWindow.lock();
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
|
2024-01-08 20:50:43 +02:00
|
|
|
throw std::runtime_error("lobby connection finished without active login window!");
|
|
|
|
|
|
|
|
loginWindowPtr->onConnectionSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::receiveChatHistory(const JsonNode & json)
|
|
|
|
{
|
2024-01-11 22:51:36 +02:00
|
|
|
for(const auto & entry : json["messages"].Vector())
|
2023-11-12 21:23:42 +02:00
|
|
|
{
|
2024-01-08 20:50:43 +02:00
|
|
|
std::string accountID = entry["accountID"].String();
|
|
|
|
std::string displayName = entry["displayName"].String();
|
|
|
|
std::string messageText = entry["messageText"].String();
|
|
|
|
int ageSeconds = entry["ageSeconds"].Integer();
|
|
|
|
std::string timeFormatted = getCurrentTimeFormatted(-ageSeconds);
|
|
|
|
|
2023-12-28 21:27:21 +02:00
|
|
|
auto lobbyWindowPtr = lobbyWindow.lock();
|
|
|
|
if(lobbyWindowPtr)
|
2024-01-08 20:50:43 +02:00
|
|
|
lobbyWindowPtr->onGameChatMessage(displayName, messageText, timeFormatted);
|
2023-11-12 21:23:42 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-12 16:35:53 +02:00
|
|
|
|
2024-01-08 20:50:43 +02:00
|
|
|
void GlobalLobbyClient::receiveChatMessage(const JsonNode & json)
|
|
|
|
{
|
|
|
|
std::string accountID = json["accountID"].String();
|
|
|
|
std::string displayName = json["displayName"].String();
|
|
|
|
std::string messageText = json["messageText"].String();
|
|
|
|
std::string timeFormatted = getCurrentTimeFormatted();
|
|
|
|
auto lobbyWindowPtr = lobbyWindow.lock();
|
|
|
|
if(lobbyWindowPtr)
|
|
|
|
lobbyWindowPtr->onGameChatMessage(displayName, messageText, timeFormatted);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::receiveActiveAccounts(const JsonNode & json)
|
|
|
|
{
|
2024-01-21 16:48:36 +02:00
|
|
|
activeAccounts.clear();
|
|
|
|
|
|
|
|
for (auto const & jsonEntry : json["accounts"].Vector())
|
|
|
|
{
|
|
|
|
GlobalLobbyAccount account;
|
|
|
|
|
|
|
|
account.accountID = jsonEntry["accountID"].String();
|
|
|
|
account.displayName = jsonEntry["displayName"].String();
|
|
|
|
account.status = jsonEntry["status"].String();
|
|
|
|
|
|
|
|
activeAccounts.push_back(account);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto lobbyWindowPtr = lobbyWindow.lock();
|
|
|
|
if(lobbyWindowPtr)
|
|
|
|
lobbyWindowPtr->onActiveAccounts(activeAccounts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::receiveActiveGameRooms(const JsonNode & json)
|
|
|
|
{
|
|
|
|
activeRooms.clear();
|
|
|
|
|
|
|
|
for (auto const & jsonEntry : json["gameRooms"].Vector())
|
|
|
|
{
|
|
|
|
GlobalLobbyRoom room;
|
|
|
|
|
|
|
|
room.gameRoomID = jsonEntry["gameRoomID"].String();
|
|
|
|
room.hostAccountID = jsonEntry["hostAccountID"].String();
|
|
|
|
room.hostAccountDisplayName = jsonEntry["hostAccountDisplayName"].String();
|
|
|
|
room.description = jsonEntry["description"].String();
|
|
|
|
// room.status = jsonEntry["status"].String();
|
|
|
|
room.playersCount = jsonEntry["playersCount"].Integer();
|
|
|
|
room.playersLimit = jsonEntry["playersLimit"].Integer();
|
|
|
|
|
|
|
|
activeRooms.push_back(room);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto lobbyWindowPtr = lobbyWindow.lock();
|
|
|
|
if(lobbyWindowPtr)
|
|
|
|
lobbyWindowPtr->onActiveRooms(activeRooms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::receiveJoinRoomSuccess(const JsonNode & json)
|
|
|
|
{
|
2024-01-29 22:05:11 +02:00
|
|
|
Settings configRoom = settings.write["lobby"]["roomID"];
|
|
|
|
configRoom->String() = json["gameRoomID"].String();
|
|
|
|
|
|
|
|
if (json["proxyMode"].Bool())
|
|
|
|
{
|
|
|
|
CSH->resetStateForLobby(EStartMode::NEW_GAME, ESelectionScreen::newGame, EServerMode::LOBBY_GUEST, {});
|
|
|
|
CSH->loadMode = ELoadMode::MULTI;
|
|
|
|
|
|
|
|
std::string hostname = settings["lobby"]["hostname"].String();
|
|
|
|
int16_t port = settings["lobby"]["port"].Integer();
|
|
|
|
CSH->connectToServer(hostname, port);
|
|
|
|
}
|
2024-01-08 20:50:43 +02:00
|
|
|
}
|
|
|
|
|
2024-01-12 16:55:36 +02:00
|
|
|
void GlobalLobbyClient::onConnectionEstablished(const std::shared_ptr<INetworkConnection> & connection)
|
2023-11-12 21:23:42 +02:00
|
|
|
{
|
2024-01-12 16:55:36 +02:00
|
|
|
networkConnection = connection;
|
|
|
|
|
2023-11-12 21:23:42 +02:00
|
|
|
JsonNode toSend;
|
2023-11-12 16:35:53 +02:00
|
|
|
|
2024-01-08 20:50:43 +02:00
|
|
|
std::string accountID = settings["lobby"]["accountID"].String();
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(accountID.empty())
|
2024-01-08 20:50:43 +02:00
|
|
|
sendClientRegister();
|
|
|
|
else
|
|
|
|
sendClientLogin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::sendClientRegister()
|
|
|
|
{
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "clientRegister";
|
|
|
|
toSend["displayName"] = settings["lobby"]["displayName"];
|
|
|
|
sendMessage(toSend);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::sendClientLogin()
|
|
|
|
{
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "clientLogin";
|
|
|
|
toSend["accountID"] = settings["lobby"]["accountID"];
|
|
|
|
toSend["accountCookie"] = settings["lobby"]["accountCookie"];
|
2023-11-12 21:23:42 +02:00
|
|
|
sendMessage(toSend);
|
2023-11-11 16:43:58 +02:00
|
|
|
}
|
|
|
|
|
2023-12-27 17:07:44 +02:00
|
|
|
void GlobalLobbyClient::onConnectionFailed(const std::string & errorMessage)
|
2023-11-12 13:27:22 +02:00
|
|
|
{
|
2023-12-28 21:27:21 +02:00
|
|
|
auto loginWindowPtr = loginWindow.lock();
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
|
2023-12-28 21:27:21 +02:00
|
|
|
throw std::runtime_error("lobby connection failed without active login window!");
|
|
|
|
|
|
|
|
logGlobal->warn("Connection to game lobby failed! Reason: %s", errorMessage);
|
|
|
|
loginWindowPtr->onConnectionFailed(errorMessage);
|
2023-11-12 13:27:22 +02:00
|
|
|
}
|
|
|
|
|
2024-01-12 16:55:36 +02:00
|
|
|
void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection> & connection)
|
2023-11-12 13:27:22 +02:00
|
|
|
{
|
2024-01-12 16:55:36 +02:00
|
|
|
assert(connection == networkConnection);
|
|
|
|
networkConnection.reset();
|
|
|
|
|
2023-11-12 13:27:22 +02:00
|
|
|
GH.windows().popWindows(1);
|
|
|
|
CInfoWindow::showInfoDialog("Connection to game lobby was lost!", {});
|
|
|
|
}
|
|
|
|
|
2023-12-27 17:07:44 +02:00
|
|
|
void GlobalLobbyClient::sendMessage(const JsonNode & data)
|
2023-11-12 15:32:54 +02:00
|
|
|
{
|
|
|
|
std::string payloadString = data.toJson(true);
|
|
|
|
|
|
|
|
// FIXME: find better approach
|
2024-01-11 22:51:36 +02:00
|
|
|
uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
|
2023-11-12 15:32:54 +02:00
|
|
|
uint8_t * payloadEnd = payloadBegin + payloadString.size();
|
|
|
|
|
|
|
|
std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
|
|
|
|
|
2024-01-12 16:55:36 +02:00
|
|
|
networkConnection->sendPacket(payloadBuffer);
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
void GlobalLobbyClient::sendOpenPublicRoom()
|
|
|
|
{
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "openGameRoom";
|
|
|
|
toSend["hostAccountID"] = settings["lobby"]["accountID"];
|
|
|
|
toSend["roomType"].String() = "public";
|
|
|
|
sendMessage(toSend);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::sendOpenPrivateRoom()
|
|
|
|
{
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "openGameRoom";
|
|
|
|
toSend["hostAccountID"] = settings["lobby"]["accountID"];
|
|
|
|
toSend["roomType"].String() = "private";
|
|
|
|
sendMessage(toSend);
|
|
|
|
}
|
|
|
|
|
2023-12-28 21:27:21 +02:00
|
|
|
void GlobalLobbyClient::connect()
|
|
|
|
{
|
2024-01-08 20:50:43 +02:00
|
|
|
std::string hostname = settings["lobby"]["hostname"].String();
|
|
|
|
int16_t port = settings["lobby"]["port"].Integer();
|
2024-01-12 16:55:36 +02:00
|
|
|
CSH->networkHandler->connectToRemote(*this, hostname, port);
|
2023-12-28 21:27:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalLobbyClient::isConnected()
|
2023-11-18 16:34:18 +02:00
|
|
|
{
|
2024-01-12 16:55:36 +02:00
|
|
|
return networkConnection != nullptr;
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|
|
|
|
|
2023-12-28 21:27:21 +02:00
|
|
|
std::shared_ptr<GlobalLobbyLoginWindow> GlobalLobbyClient::createLoginWindow()
|
2023-11-18 16:34:18 +02:00
|
|
|
{
|
2023-12-28 21:27:21 +02:00
|
|
|
auto loginWindowPtr = loginWindow.lock();
|
2024-01-11 22:51:36 +02:00
|
|
|
if(loginWindowPtr)
|
2023-12-28 21:27:21 +02:00
|
|
|
return loginWindowPtr;
|
|
|
|
|
|
|
|
auto loginWindowNew = std::make_shared<GlobalLobbyLoginWindow>();
|
|
|
|
loginWindow = loginWindowNew;
|
|
|
|
|
|
|
|
return loginWindowNew;
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|
|
|
|
|
2023-12-28 21:27:21 +02:00
|
|
|
std::shared_ptr<GlobalLobbyWindow> GlobalLobbyClient::createLobbyWindow()
|
2023-11-18 16:34:18 +02:00
|
|
|
{
|
2023-12-28 21:27:21 +02:00
|
|
|
auto lobbyWindowPtr = lobbyWindow.lock();
|
2024-01-11 22:51:36 +02:00
|
|
|
if(lobbyWindowPtr)
|
2023-12-28 21:27:21 +02:00
|
|
|
return lobbyWindowPtr;
|
|
|
|
|
|
|
|
lobbyWindowPtr = std::make_shared<GlobalLobbyWindow>();
|
|
|
|
lobbyWindow = lobbyWindowPtr;
|
|
|
|
lobbyWindowLock = lobbyWindowPtr;
|
|
|
|
return lobbyWindowPtr;
|
2023-11-12 15:32:54 +02:00
|
|
|
}
|
2024-01-21 16:48:36 +02:00
|
|
|
|
|
|
|
const std::vector<GlobalLobbyAccount> & GlobalLobbyClient::getActiveAccounts() const
|
|
|
|
{
|
|
|
|
return activeAccounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<GlobalLobbyRoom> & GlobalLobbyClient::getActiveRooms() const
|
|
|
|
{
|
|
|
|
return activeRooms;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::activateInterface()
|
|
|
|
{
|
|
|
|
if (!GH.windows().findWindows<GlobalLobbyWindow>().empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!GH.windows().findWindows<GlobalLobbyLoginWindow>().empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isConnected())
|
|
|
|
GH.windows().pushWindow(createLobbyWindow());
|
|
|
|
else
|
|
|
|
GH.windows().pushWindow(createLoginWindow());
|
|
|
|
}
|
2024-01-29 22:05:11 +02:00
|
|
|
|
|
|
|
void GlobalLobbyClient::sendProxyConnectionLogin(const NetworkConnectionPtr & netConnection)
|
|
|
|
{
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "clientProxyLogin";
|
|
|
|
toSend["accountID"] = settings["lobby"]["accountID"];
|
|
|
|
toSend["accountCookie"] = settings["lobby"]["accountCookie"];
|
|
|
|
toSend["gameRoomID"] = settings["lobby"]["roomID"];
|
|
|
|
|
|
|
|
std::string payloadString = toSend.toJson(true);
|
|
|
|
|
|
|
|
// FIXME: find better approach
|
|
|
|
uint8_t * payloadBegin = reinterpret_cast<uint8_t *>(payloadString.data());
|
|
|
|
uint8_t * payloadEnd = payloadBegin + payloadString.size();
|
|
|
|
|
|
|
|
std::vector<uint8_t> payloadBuffer(payloadBegin, payloadEnd);
|
|
|
|
|
|
|
|
netConnection->sendPacket(payloadBuffer);
|
|
|
|
}
|