2018-01-05 19:21:07 +02:00
|
|
|
/*
|
|
|
|
* NetPacksLobbyClient.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"
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
#include "LobbyClientNetPackVisitors.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "lobby/CSelectionBase.h"
|
|
|
|
#include "lobby/CLobbyScreen.h"
|
|
|
|
|
|
|
|
#include "lobby/OptionsTab.h"
|
|
|
|
#include "lobby/RandomMapTab.h"
|
2023-12-20 23:52:39 +02:00
|
|
|
#include "lobby/TurnOptionsTab.h"
|
2023-12-28 20:41:01 +02:00
|
|
|
#include "lobby/ExtraOptionsTab.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "lobby/SelectionTab.h"
|
|
|
|
#include "lobby/CBonusSelection.h"
|
2024-01-10 15:31:11 +02:00
|
|
|
#include "globalLobby/GlobalLobbyWindow.h"
|
2024-01-21 16:48:36 +02:00
|
|
|
#include "globalLobby/GlobalLobbyServerSetup.h"
|
|
|
|
#include "globalLobby/GlobalLobbyClient.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
#include "CServerHandler.h"
|
2024-03-18 19:52:53 +02:00
|
|
|
#include "GameChatHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "CGameInfo.h"
|
2024-02-03 19:57:23 +02:00
|
|
|
#include "Client.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "gui/CGuiHandler.h"
|
2023-05-16 14:10:26 +02:00
|
|
|
#include "gui/WindowHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "widgets/Buttons.h"
|
|
|
|
#include "widgets/TextControls.h"
|
2024-09-05 21:31:17 +02:00
|
|
|
#include "media/CMusicHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
#include "../lib/CConfigHandler.h"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../lib/texts/CGeneralTextHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/serializer/Connection.h"
|
2024-08-31 17:16:42 +02:00
|
|
|
#include "../lib/campaign/CampaignState.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyClientConnected(LobbyClientConnected & pack)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-02-12 09:23:39 +02:00
|
|
|
result = false;
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
// Check if it's LobbyClientConnected for our client
|
2024-02-11 16:26:27 +02:00
|
|
|
if(pack.uuid == handler.logicConnection->uuid)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-05-29 22:08:32 +02:00
|
|
|
handler.logicConnection->setSerializationVersion(pack.version);
|
2024-02-11 16:26:27 +02:00
|
|
|
handler.logicConnection->connectionID = pack.clientId;
|
2023-08-11 18:04:14 +02:00
|
|
|
if(handler.mapToStart)
|
2024-01-10 15:31:11 +02:00
|
|
|
{
|
2023-08-11 18:04:14 +02:00
|
|
|
handler.setMapInfo(handler.mapToStart);
|
2024-01-10 15:31:11 +02:00
|
|
|
}
|
2023-08-11 18:04:14 +02:00
|
|
|
else if(!settings["session"]["headless"].Bool())
|
2023-12-25 22:56:55 +02:00
|
|
|
{
|
|
|
|
if (GH.windows().topWindow<CSimpleJoinScreen>())
|
|
|
|
GH.windows().popWindows(1);
|
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
if (!GH.windows().findWindows<GlobalLobbyServerSetup>().empty())
|
|
|
|
{
|
2024-01-29 22:05:11 +02:00
|
|
|
assert(handler.serverMode == EServerMode::LOBBY_HOST);
|
2024-01-21 16:48:36 +02:00
|
|
|
// announce opened game room
|
|
|
|
// TODO: find better approach?
|
|
|
|
int roomType = settings["lobby"]["roomType"].Integer();
|
2024-03-11 20:03:33 +02:00
|
|
|
int roomPlayerLimit = settings["lobby"]["roomPlayerLimit"].Integer();
|
2024-01-21 16:48:36 +02:00
|
|
|
|
|
|
|
if (roomType != 0)
|
2024-03-11 20:03:33 +02:00
|
|
|
handler.getGlobalLobby().sendOpenRoom("private", roomPlayerLimit);
|
2024-01-21 16:48:36 +02:00
|
|
|
else
|
2024-03-11 20:03:33 +02:00
|
|
|
handler.getGlobalLobby().sendOpenRoom("public", roomPlayerLimit);
|
2024-01-21 16:48:36 +02:00
|
|
|
}
|
|
|
|
|
2024-01-10 15:31:11 +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);
|
|
|
|
}
|
|
|
|
|
2024-01-26 17:40:31 +02:00
|
|
|
GH.windows().createAndPushWindow<CLobbyScreen>(handler.screenType);
|
2023-12-25 22:56:55 +02:00
|
|
|
}
|
2024-02-04 19:56:04 +02:00
|
|
|
handler.setState(EClientState::LOBBY);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-02-11 16:26:27 +02:00
|
|
|
if(pack.clientId != handler.logicConnection->connectionID)
|
2023-02-12 09:23:39 +02:00
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
return;
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void ApplyOnLobbyScreenNetPackVisitor::visitLobbyClientDisconnected(LobbyClientDisconnected & pack)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-08-21 03:06:58 +02:00
|
|
|
if(auto w = GH.windows().topWindow<CLoadingScreen>())
|
|
|
|
GH.windows().popWindow(w);
|
|
|
|
|
2023-05-16 15:07:03 +02:00
|
|
|
if(GH.windows().count() > 0)
|
2023-05-16 15:20:35 +02:00
|
|
|
GH.windows().popWindows(1);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void ApplyOnLobbyScreenNetPackVisitor::visitLobbyChatMessage(LobbyChatMessage & pack)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-05-02 21:03:23 +02:00
|
|
|
handler.getGameChat().onNewLobbyMessageReceived(pack.playerName, pack.message.toString());
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void ApplyOnLobbyScreenNetPackVisitor::visitLobbyGuiAction(LobbyGuiAction & pack)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-02-12 09:23:39 +02:00
|
|
|
if(!lobby || !handler.isGuest())
|
2018-01-05 19:21:07 +02:00
|
|
|
return;
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
switch(pack.action)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-02-12 09:23:39 +02:00
|
|
|
case LobbyGuiAction::NO_TAB:
|
2018-01-05 19:21:07 +02:00
|
|
|
lobby->toggleTab(lobby->curTab);
|
|
|
|
break;
|
2023-02-12 09:23:39 +02:00
|
|
|
case LobbyGuiAction::OPEN_OPTIONS:
|
2018-01-05 19:21:07 +02:00
|
|
|
lobby->toggleTab(lobby->tabOpt);
|
|
|
|
break;
|
2023-02-12 09:23:39 +02:00
|
|
|
case LobbyGuiAction::OPEN_SCENARIO_LIST:
|
2018-01-05 19:21:07 +02:00
|
|
|
lobby->toggleTab(lobby->tabSel);
|
|
|
|
break;
|
2023-02-12 09:23:39 +02:00
|
|
|
case LobbyGuiAction::OPEN_RANDOM_MAP_OPTIONS:
|
2018-01-05 19:21:07 +02:00
|
|
|
lobby->toggleTab(lobby->tabRand);
|
|
|
|
break;
|
2023-12-20 23:52:39 +02:00
|
|
|
case LobbyGuiAction::OPEN_TURN_OPTIONS:
|
|
|
|
lobby->toggleTab(lobby->tabTurnOptions);
|
|
|
|
break;
|
2023-12-28 20:41:01 +02:00
|
|
|
case LobbyGuiAction::OPEN_EXTRA_OPTIONS:
|
|
|
|
lobby->toggleTab(lobby->tabExtraOptions);
|
|
|
|
break;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 19:08:45 +02:00
|
|
|
void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyRestartGame(LobbyRestartGame & pack)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-02-04 19:56:04 +02:00
|
|
|
assert(handler.getState() == EClientState::GAMEPLAY);
|
|
|
|
|
|
|
|
handler.restartGameplay();
|
|
|
|
handler.sendStartGame();
|
2022-09-29 19:08:05 +02:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:57:23 +02:00
|
|
|
void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyPrepareStartGame(LobbyPrepareStartGame & pack)
|
|
|
|
{
|
|
|
|
handler.client = std::make_unique<CClient>();
|
2024-02-11 16:26:27 +02:00
|
|
|
handler.logicConnection->enterLobbyConnectionMode();
|
|
|
|
handler.logicConnection->setCallback(handler.client.get());
|
2024-02-03 19:57:23 +02:00
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
|
2022-09-29 19:08:05 +02:00
|
|
|
{
|
2024-02-11 16:26:27 +02:00
|
|
|
if(pack.clientId != -1 && pack.clientId != handler.logicConnection->connectionID)
|
2023-02-12 09:23:39 +02:00
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
return;
|
|
|
|
}
|
2022-10-01 16:28:45 +02:00
|
|
|
|
2024-02-04 19:56:04 +02:00
|
|
|
handler.setState(EClientState::STARTING);
|
2024-02-11 16:26:27 +02:00
|
|
|
if(handler.si->mode != EStartMode::LOAD_GAME || pack.clientId == handler.logicConnection->connectionID)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-02-12 09:23:39 +02:00
|
|
|
auto modeBackup = handler.si->mode;
|
|
|
|
handler.si = pack.initializedStartInfo;
|
|
|
|
handler.si->mode = modeBackup;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2023-08-21 03:06:58 +02:00
|
|
|
handler.startGameplay(pack.initializedGameState);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-08-21 17:09:56 +02:00
|
|
|
void ApplyOnLobbyScreenNetPackVisitor::visitLobbyStartGame(LobbyStartGame & pack)
|
|
|
|
{
|
|
|
|
if(auto w = GH.windows().topWindow<CLoadingScreen>())
|
|
|
|
{
|
|
|
|
w->finish();
|
2023-08-22 18:10:20 +02:00
|
|
|
w->tick(0);
|
2023-08-21 17:09:56 +02:00
|
|
|
w->redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 03:06:58 +02:00
|
|
|
void ApplyOnLobbyScreenNetPackVisitor::visitLobbyLoadProgress(LobbyLoadProgress & pack)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-08-21 03:06:58 +02:00
|
|
|
if(auto w = GH.windows().topWindow<CLoadingScreen>())
|
2023-08-21 16:56:01 +02:00
|
|
|
{
|
2023-08-21 03:06:58 +02:00
|
|
|
w->set(pack.progress);
|
2023-08-22 18:10:20 +02:00
|
|
|
w->tick(0);
|
2023-08-21 16:56:01 +02:00
|
|
|
w->redraw();
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyUpdateState(LobbyUpdateState & pack)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-02-12 09:23:39 +02:00
|
|
|
pack.hostChanged = pack.state.hostClientId != handler.hostClientId;
|
|
|
|
static_cast<LobbyState &>(handler) = pack.state;
|
2023-08-11 18:04:14 +02:00
|
|
|
if(handler.mapToStart && handler.mi)
|
|
|
|
{
|
|
|
|
handler.startMapAfterConnection(nullptr);
|
|
|
|
handler.sendStartGame();
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void ApplyOnLobbyScreenNetPackVisitor::visitLobbyUpdateState(LobbyUpdateState & pack)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2022-09-30 17:32:48 +02:00
|
|
|
if(!lobby) //stub: ignore message for game mode
|
|
|
|
return;
|
|
|
|
|
2024-02-04 19:56:04 +02:00
|
|
|
if(!lobby->bonusSel && handler.si->campState && handler.getState() == EClientState::LOBBY_CAMPAIGN)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-09-05 21:31:17 +02:00
|
|
|
auto bonusSel = std::make_shared<CBonusSelection>();
|
|
|
|
lobby->bonusSel = bonusSel;
|
2024-08-31 17:57:27 +02:00
|
|
|
if(!handler.si->campState->conqueredScenarios().size() && !handler.si->campState->getIntroVideo().empty())
|
2024-09-05 21:31:17 +02:00
|
|
|
{
|
|
|
|
CCS->musich->stopMusic();
|
|
|
|
GH.windows().createAndPushWindow<CampaignRimVideo>(handler.si->campState->getIntroVideo(), handler.si->campState->getVideoRim().empty() ? ImagePath::builtin("INTRORIM") : handler.si->campState->getVideoRim(), [bonusSel](){
|
|
|
|
if(!CSH->si->campState->getMusic().empty())
|
|
|
|
CCS->musich->playMusic(CSH->si->campState->getMusic(), true, false);
|
|
|
|
GH.windows().pushWindow(bonusSel);
|
|
|
|
});
|
|
|
|
}
|
2024-08-31 17:16:42 +02:00
|
|
|
else
|
2024-09-05 21:31:17 +02:00
|
|
|
GH.windows().pushWindow(bonusSel);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(lobby->bonusSel)
|
|
|
|
lobby->bonusSel->updateAfterStateChange();
|
|
|
|
else
|
|
|
|
lobby->updateAfterStateChange();
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
if(pack.hostChanged)
|
|
|
|
lobby->toggleMode(handler.isHost());
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2022-09-23 13:02:19 +02:00
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void ApplyOnLobbyScreenNetPackVisitor::visitLobbyShowMessage(LobbyShowMessage & pack)
|
2022-09-23 13:02:19 +02:00
|
|
|
{
|
2022-09-30 17:32:48 +02:00
|
|
|
if(!lobby) //stub: ignore message for game mode
|
|
|
|
return;
|
|
|
|
|
2022-09-23 13:02:19 +02:00
|
|
|
lobby->buttonStart->block(false);
|
2024-05-02 21:03:23 +02:00
|
|
|
handler.showServerError(pack.message.toString());
|
2022-09-23 13:02:19 +02:00
|
|
|
}
|