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"
|
|
|
|
|
2024-03-23 21:12:10 +02:00
|
|
|
#include "GlobalLobbyInviteWindow.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
|
|
|
|
2024-03-25 18:04:44 +02:00
|
|
|
#include "../CGameInfo.h"
|
|
|
|
#include "../CMusicHandler.h"
|
|
|
|
#include "../CServerHandler.h"
|
2023-11-11 16:43:58 +02:00
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../gui/WindowHandler.h"
|
2024-01-29 22:05:11 +02:00
|
|
|
#include "../mainmenu/CMainMenu.h"
|
2024-03-25 18:04:44 +02:00
|
|
|
#include "../windows/InfoWindows.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-03-04 22:13:06 +02:00
|
|
|
#include "../../lib/json/JsonUtils.h"
|
2024-01-08 20:50:43 +02:00
|
|
|
#include "../../lib/TextOperations.h"
|
2024-03-08 14:23:08 +02:00
|
|
|
#include "../../lib/CGeneralTextHandler.h"
|
2023-11-12 16:35:53 +02:00
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
GlobalLobbyClient::GlobalLobbyClient()
|
|
|
|
{
|
|
|
|
activeChannels.emplace_back("english");
|
|
|
|
if (CGI->generaltexth->getPreferredLanguage() != "english")
|
|
|
|
activeChannels.emplace_back(CGI->generaltexth->getPreferredLanguage());
|
|
|
|
}
|
|
|
|
|
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-03-20 20:12:43 +02:00
|
|
|
static std::string getCurrentDateTimeFormatted(int timeOffsetSeconds = 0)
|
|
|
|
{
|
|
|
|
// FIXME: better/unified way to format date
|
|
|
|
auto timeNowChrono = std::chrono::system_clock::now();
|
|
|
|
timeNowChrono += std::chrono::seconds(timeOffsetSeconds);
|
|
|
|
|
|
|
|
return TextOperations::getFormattedDateTimeLocal(std::chrono::system_clock::to_time_t(timeNowChrono));
|
|
|
|
}
|
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<std::byte> & 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-03-11 17:48:56 +02:00
|
|
|
if(json["type"].String() == "clientLoginSuccess")
|
|
|
|
return receiveClientLoginSuccess(json);
|
2024-01-08 20:50:43 +02:00
|
|
|
|
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-02-10 19:02:25 +02:00
|
|
|
if(json["type"].String() == "inviteReceived")
|
|
|
|
return receiveInviteReceived(json);
|
|
|
|
|
2024-03-20 18:40:37 +02:00
|
|
|
if(json["type"].String() == "matchesHistory")
|
|
|
|
return receiveMatchesHistory(json);
|
|
|
|
|
2024-02-04 21:23:21 +02:00
|
|
|
logGlobal->error("Received unexpected message from lobby server: %s", 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-02-04 21:23:21 +02:00
|
|
|
if(loginWindowPtr)
|
|
|
|
loginWindowPtr->onConnectionFailed(json["reason"].String());
|
2024-01-08 20:50:43 +02:00
|
|
|
|
2024-02-04 21:23:21 +02:00
|
|
|
// TODO: handle errors in lobby menu
|
2024-01-08 20:50:43 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 17:48:56 +02:00
|
|
|
void GlobalLobbyClient::receiveClientLoginSuccess(const JsonNode & json)
|
2024-01-08 20:50:43 +02:00
|
|
|
{
|
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!");
|
|
|
|
|
2024-03-08 14:23:08 +02:00
|
|
|
loginWindowPtr->onLoginSuccess();
|
2024-01-08 20:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::receiveChatHistory(const JsonNode & json)
|
|
|
|
{
|
2024-03-13 23:02:53 +02:00
|
|
|
std::string channelType = json["channelType"].String();
|
|
|
|
std::string channelName = json["channelName"].String();
|
|
|
|
std::string channelKey = channelType + '_' + channelName;
|
|
|
|
|
|
|
|
// create empty entry, potentially replacing any pre-existing data
|
|
|
|
chatHistory[channelKey] = {};
|
|
|
|
|
|
|
|
auto lobbyWindowPtr = lobbyWindow.lock();
|
|
|
|
|
2024-01-11 22:51:36 +02:00
|
|
|
for(const auto & entry : json["messages"].Vector())
|
2023-11-12 21:23:42 +02:00
|
|
|
{
|
2024-03-13 23:02:53 +02:00
|
|
|
GlobalLobbyChannelMessage message;
|
|
|
|
|
|
|
|
message.accountID = entry["accountID"].String();
|
|
|
|
message.displayName = entry["displayName"].String();
|
|
|
|
message.messageText = entry["messageText"].String();
|
2024-01-08 20:50:43 +02:00
|
|
|
int ageSeconds = entry["ageSeconds"].Integer();
|
2024-03-13 23:02:53 +02:00
|
|
|
message.timeFormatted = getCurrentTimeFormatted(-ageSeconds);
|
|
|
|
|
|
|
|
chatHistory[channelKey].push_back(message);
|
2024-01-08 20:50:43 +02:00
|
|
|
|
2023-12-28 21:27:21 +02:00
|
|
|
if(lobbyWindowPtr)
|
2024-03-13 23:02:53 +02:00
|
|
|
lobbyWindowPtr->onGameChatMessage(message.displayName, message.messageText, message.timeFormatted, channelType, channelName);
|
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)
|
|
|
|
{
|
2024-03-13 23:02:53 +02:00
|
|
|
GlobalLobbyChannelMessage message;
|
|
|
|
|
|
|
|
message.accountID = json["accountID"].String();
|
|
|
|
message.displayName = json["displayName"].String();
|
|
|
|
message.messageText = json["messageText"].String();
|
|
|
|
message.timeFormatted = getCurrentTimeFormatted();
|
|
|
|
|
|
|
|
std::string channelType = json["channelType"].String();
|
|
|
|
std::string channelName = json["channelName"].String();
|
|
|
|
std::string channelKey = channelType + '_' + channelName;
|
|
|
|
|
|
|
|
chatHistory[channelKey].push_back(message);
|
|
|
|
|
2024-01-08 20:50:43 +02:00
|
|
|
auto lobbyWindowPtr = lobbyWindow.lock();
|
|
|
|
if(lobbyWindowPtr)
|
2024-03-13 23:02:53 +02:00
|
|
|
lobbyWindowPtr->onGameChatMessage(message.displayName, message.messageText, message.timeFormatted, channelType, channelName);
|
2024-03-25 18:04:44 +02:00
|
|
|
|
|
|
|
CCS->soundh->playSound(AudioPath::builtin("CHAT"));
|
2024-01-08 20:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::receiveActiveAccounts(const JsonNode & json)
|
|
|
|
{
|
2024-01-21 16:48:36 +02:00
|
|
|
activeAccounts.clear();
|
|
|
|
|
2024-03-23 21:12:10 +02:00
|
|
|
for(const auto & jsonEntry : json["accounts"].Vector())
|
2024-01-21 16:48:36 +02:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2024-03-23 21:12:10 +02:00
|
|
|
for(const auto & jsonEntry : json["gameRooms"].Vector())
|
2024-01-21 16:48:36 +02:00
|
|
|
{
|
|
|
|
GlobalLobbyRoom room;
|
|
|
|
|
|
|
|
room.gameRoomID = jsonEntry["gameRoomID"].String();
|
|
|
|
room.hostAccountID = jsonEntry["hostAccountID"].String();
|
|
|
|
room.hostAccountDisplayName = jsonEntry["hostAccountDisplayName"].String();
|
|
|
|
room.description = jsonEntry["description"].String();
|
2024-03-12 19:52:42 +02:00
|
|
|
room.statusID = jsonEntry["status"].String();
|
2024-03-20 20:12:43 +02:00
|
|
|
int ageSeconds = jsonEntry["ageSeconds"].Integer();
|
|
|
|
room.startDateFormatted = getCurrentDateTimeFormatted(-ageSeconds);
|
|
|
|
|
2024-03-23 21:12:10 +02:00
|
|
|
for(const auto & jsonParticipant : jsonEntry["participants"].Vector())
|
2024-03-20 18:40:37 +02:00
|
|
|
{
|
|
|
|
GlobalLobbyAccount account;
|
|
|
|
account.accountID = jsonParticipant["accountID"].String();
|
|
|
|
account.displayName = jsonParticipant["displayName"].String();
|
|
|
|
room.participants.push_back(account);
|
|
|
|
}
|
2024-03-11 20:03:33 +02:00
|
|
|
room.playerLimit = jsonEntry["playerLimit"].Integer();
|
2024-01-21 16:48:36 +02:00
|
|
|
|
|
|
|
activeRooms.push_back(room);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto lobbyWindowPtr = lobbyWindow.lock();
|
|
|
|
if(lobbyWindowPtr)
|
|
|
|
lobbyWindowPtr->onActiveRooms(activeRooms);
|
|
|
|
}
|
|
|
|
|
2024-03-20 18:40:37 +02:00
|
|
|
void GlobalLobbyClient::receiveMatchesHistory(const JsonNode & json)
|
|
|
|
{
|
|
|
|
matchesHistory.clear();
|
|
|
|
|
2024-03-23 21:12:10 +02:00
|
|
|
for(const auto & jsonEntry : json["matchesHistory"].Vector())
|
2024-03-20 18:40:37 +02:00
|
|
|
{
|
|
|
|
GlobalLobbyRoom room;
|
|
|
|
|
|
|
|
room.gameRoomID = jsonEntry["gameRoomID"].String();
|
|
|
|
room.hostAccountID = jsonEntry["hostAccountID"].String();
|
|
|
|
room.hostAccountDisplayName = jsonEntry["hostAccountDisplayName"].String();
|
|
|
|
room.description = jsonEntry["description"].String();
|
|
|
|
room.statusID = jsonEntry["status"].String();
|
2024-03-20 20:12:43 +02:00
|
|
|
int ageSeconds = jsonEntry["ageSeconds"].Integer();
|
|
|
|
room.startDateFormatted = getCurrentDateTimeFormatted(-ageSeconds);
|
|
|
|
|
2024-03-23 21:12:10 +02:00
|
|
|
for(const auto & jsonParticipant : jsonEntry["participants"].Vector())
|
2024-03-20 18:40:37 +02:00
|
|
|
{
|
|
|
|
GlobalLobbyAccount account;
|
|
|
|
account.accountID = jsonParticipant["accountID"].String();
|
|
|
|
account.displayName = jsonParticipant["displayName"].String();
|
|
|
|
room.participants.push_back(account);
|
|
|
|
}
|
|
|
|
room.playerLimit = jsonEntry["playerLimit"].Integer();
|
|
|
|
|
|
|
|
matchesHistory.push_back(room);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto lobbyWindowPtr = lobbyWindow.lock();
|
|
|
|
if(lobbyWindowPtr)
|
2024-03-23 21:12:10 +02:00
|
|
|
lobbyWindowPtr->onMatchesHistory(matchesHistory);
|
2024-03-20 18:40:37 +02:00
|
|
|
}
|
|
|
|
|
2024-02-10 19:02:25 +02:00
|
|
|
void GlobalLobbyClient::receiveInviteReceived(const JsonNode & json)
|
|
|
|
{
|
2024-03-23 21:12:10 +02:00
|
|
|
auto lobbyWindowPtr = lobbyWindow.lock();
|
2024-03-25 18:04:44 +02:00
|
|
|
std::string gameRoomID = json["gameRoomID"].String();
|
|
|
|
std::string accountID = json["accountID"].String();
|
2024-03-23 21:12:10 +02:00
|
|
|
if(lobbyWindowPtr)
|
2024-03-25 18:04:44 +02:00
|
|
|
{
|
|
|
|
std::string message = MetaString::createFromTextID("vcmi.lobby.invite.notification").toString();
|
|
|
|
std::string time = getCurrentTimeFormatted();
|
|
|
|
|
|
|
|
lobbyWindowPtr->onGameChatMessage("System", message, time, "player", accountID);
|
|
|
|
lobbyWindowPtr->onInviteReceived(gameRoomID, accountID);
|
|
|
|
}
|
2024-03-23 21:12:10 +02:00
|
|
|
|
2024-03-25 18:04:44 +02:00
|
|
|
CCS->soundh->playSound(AudioPath::builtin("CHAT"));
|
2024-02-10 19:02:25 +02:00
|
|
|
}
|
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
void GlobalLobbyClient::receiveJoinRoomSuccess(const JsonNode & json)
|
|
|
|
{
|
2024-03-18 19:52:53 +02:00
|
|
|
currentGameRoomUUID = json["gameRoomID"].String();
|
2024-01-29 22:05:11 +02:00
|
|
|
|
|
|
|
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-02-03 22:24:32 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2024-01-12 16:55:36 +02:00
|
|
|
networkConnection = connection;
|
|
|
|
|
2024-02-09 23:02:41 +02:00
|
|
|
auto loginWindowPtr = loginWindow.lock();
|
2023-11-12 16:35:53 +02:00
|
|
|
|
2024-02-09 23:02:41 +02:00
|
|
|
if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
|
|
|
|
throw std::runtime_error("lobby connection established without active login window!");
|
2024-01-08 20:50:43 +02:00
|
|
|
|
2024-02-09 23:02:41 +02:00
|
|
|
loginWindowPtr->onConnectionSuccess();
|
2024-01-08 20:50:43 +02:00
|
|
|
}
|
|
|
|
|
2024-02-09 23:02:41 +02:00
|
|
|
void GlobalLobbyClient::sendClientRegister(const std::string & accountName)
|
2024-01-08 20:50:43 +02:00
|
|
|
{
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "clientRegister";
|
2024-02-09 23:02:41 +02:00
|
|
|
toSend["displayName"].String() = accountName;
|
2024-03-08 14:23:08 +02:00
|
|
|
toSend["language"].String() = CGI->generaltexth->getPreferredLanguage();
|
|
|
|
toSend["version"].String() = VCMI_VERSION_STRING;
|
2024-01-08 20:50:43 +02:00
|
|
|
sendMessage(toSend);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalLobbyClient::sendClientLogin()
|
|
|
|
{
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "clientLogin";
|
|
|
|
toSend["accountID"] = settings["lobby"]["accountID"];
|
|
|
|
toSend["accountCookie"] = settings["lobby"]["accountCookie"];
|
2024-03-08 14:23:08 +02:00
|
|
|
toSend["language"].String() = CGI->generaltexth->getPreferredLanguage();
|
|
|
|
toSend["version"].String() = VCMI_VERSION_STRING;
|
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
|
|
|
{
|
2024-02-03 22:24:32 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
|
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-02-02 01:27:19 +02:00
|
|
|
void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
|
2023-11-12 13:27:22 +02:00
|
|
|
{
|
2024-02-03 22:24:32 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
|
2024-01-12 16:55:36 +02:00
|
|
|
assert(connection == networkConnection);
|
|
|
|
networkConnection.reset();
|
|
|
|
|
2024-02-04 21:23:21 +02:00
|
|
|
while (!GH.windows().findWindows<GlobalLobbyWindow>().empty())
|
|
|
|
{
|
|
|
|
// if global lobby is open, pop all dialogs on top of it as well as lobby itself
|
|
|
|
GH.windows().popWindows(1);
|
|
|
|
}
|
|
|
|
|
2023-11-12 13:27:22 +02:00
|
|
|
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
|
|
|
{
|
2024-03-08 14:23:08 +02:00
|
|
|
assert(JsonUtils::validate(data, "vcmi:lobbyProtocol/" + data["type"].String(), data["type"].String() + " pack"));
|
2024-02-12 01:22:16 +02:00
|
|
|
networkConnection->sendPacket(data.toBytes());
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 20:03:33 +02:00
|
|
|
void GlobalLobbyClient::sendOpenRoom(const std::string & mode, int playerLimit)
|
2024-01-21 16:48:36 +02:00
|
|
|
{
|
|
|
|
JsonNode toSend;
|
2024-03-04 14:11:45 +02:00
|
|
|
toSend["type"].String() = "activateGameRoom";
|
2024-01-21 16:48:36 +02:00
|
|
|
toSend["hostAccountID"] = settings["lobby"]["accountID"];
|
2024-03-11 20:03:33 +02:00
|
|
|
toSend["roomType"].String() = mode;
|
|
|
|
toSend["playerLimit"].Integer() = playerLimit;
|
2024-01-21 16:48:36 +02:00
|
|
|
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-02-03 22:59:56 +02:00
|
|
|
CSH->getNetworkHandler().connectToRemote(*this, hostname, port);
|
2023-12-28 21:27:21 +02:00
|
|
|
}
|
|
|
|
|
2024-02-10 19:02:25 +02:00
|
|
|
bool GlobalLobbyClient::isConnected() const
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-13 23:02:53 +02:00
|
|
|
const std::vector<std::string> & GlobalLobbyClient::getActiveChannels() const
|
|
|
|
{
|
|
|
|
return activeChannels;
|
|
|
|
}
|
|
|
|
|
2024-03-20 18:40:37 +02:00
|
|
|
const std::vector<GlobalLobbyRoom> & GlobalLobbyClient::getMatchesHistory() const
|
2024-03-13 23:02:53 +02:00
|
|
|
{
|
|
|
|
return matchesHistory;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<GlobalLobbyChannelMessage> & GlobalLobbyClient::getChannelHistory(const std::string & channelType, const std::string & channelName) const
|
|
|
|
{
|
|
|
|
static const std::vector<GlobalLobbyChannelMessage> emptyVector;
|
|
|
|
|
|
|
|
std::string keyToTest = channelType + '_' + channelName;
|
|
|
|
|
|
|
|
if (chatHistory.count(keyToTest) == 0)
|
|
|
|
{
|
|
|
|
if (channelType != "global")
|
|
|
|
{
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "requestChatHistory";
|
|
|
|
toSend["channelType"].String() = channelType;
|
|
|
|
toSend["channelName"].String() = channelName;
|
|
|
|
CSH->getGlobalLobby().sendMessage(toSend);
|
|
|
|
}
|
|
|
|
return emptyVector;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return chatHistory.at(keyToTest);
|
|
|
|
}
|
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
void GlobalLobbyClient::activateInterface()
|
|
|
|
{
|
2024-03-11 20:15:39 +02:00
|
|
|
if (GH.windows().topWindow<GlobalLobbyWindow>() != nullptr)
|
|
|
|
{
|
|
|
|
GH.windows().popWindows(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
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
|
|
|
|
2024-03-23 21:12:10 +02:00
|
|
|
void GlobalLobbyClient::activateRoomInviteInterface()
|
|
|
|
{
|
|
|
|
GH.windows().createAndPushWindow<GlobalLobbyInviteWindow>();
|
|
|
|
}
|
|
|
|
|
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"];
|
2024-03-18 19:52:53 +02:00
|
|
|
toSend["gameRoomID"] = settings["session"]["lobby"]["roomID"];
|
2024-01-29 22:05:11 +02:00
|
|
|
|
2024-03-08 14:23:08 +02:00
|
|
|
assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
|
2024-02-12 01:22:16 +02:00
|
|
|
netConnection->sendPacket(toSend.toBytes());
|
2024-01-29 22:05:11 +02:00
|
|
|
}
|
2024-03-18 19:52:53 +02:00
|
|
|
|
|
|
|
void GlobalLobbyClient::resetMatchState()
|
|
|
|
{
|
|
|
|
currentGameRoomUUID.clear();
|
|
|
|
}
|
2024-03-20 18:40:37 +02:00
|
|
|
|
|
|
|
void GlobalLobbyClient::sendMatchChatMessage(const std::string & messageText)
|
|
|
|
{
|
|
|
|
if (!isConnected())
|
|
|
|
return; // we are not playing with lobby
|
|
|
|
|
|
|
|
if (currentGameRoomUUID.empty())
|
|
|
|
return; // we are not playing through lobby
|
|
|
|
|
|
|
|
JsonNode toSend;
|
|
|
|
toSend["type"].String() = "sendChatMessage";
|
|
|
|
toSend["channelType"].String() = "match";
|
|
|
|
toSend["channelName"].String() = currentGameRoomUUID;
|
|
|
|
toSend["messageText"].String() = messageText;
|
|
|
|
|
|
|
|
CSH->getGlobalLobby().sendMessage(toSend);
|
|
|
|
}
|