1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/client/CServerHandler.cpp

965 lines
24 KiB
C++
Raw Normal View History

/*
* CServerHandler.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 "CServerHandler.h"
#include "Client.h"
#include "CGameInfo.h"
#include "ServerRunner.h"
#include "GameChatHandler.h"
#include "CPlayerInterface.h"
#include "gui/CGuiHandler.h"
#include "gui/WindowHandler.h"
#include "globalLobby/GlobalLobbyClient.h"
#include "lobby/CSelectionBase.h"
#include "lobby/CLobbyScreen.h"
2024-09-05 21:31:17 +02:00
#include "lobby/CBonusSelection.h"
#include "windows/InfoWindows.h"
2024-09-12 23:35:21 +02:00
#include "windows/GUIClasses.h"
2024-09-05 21:31:17 +02:00
#include "media/CMusicHandler.h"
#include "media/IVideoPlayer.h"
#include "mainmenu/CMainMenu.h"
#include "mainmenu/CPrologEpilogVideo.h"
2023-09-22 20:39:20 +02:00
#include "mainmenu/CHighScoreScreen.h"
#include "../lib/CConfigHandler.h"
#include "../lib/texts/CGeneralTextHandler.h"
#include "ConditionalWait.h"
#include "../lib/CThreadHelper.h"
#include "../lib/StartInfo.h"
2023-08-25 18:20:26 +02:00
#include "../lib/TurnTimerInfo.h"
#include "../lib/VCMIDirs.h"
2023-06-25 21:28:24 +02:00
#include "../lib/campaign/CampaignState.h"
#include "../lib/gameState/CGameState.h"
2024-08-02 19:37:46 +02:00
#include "../lib/gameState/HighScore.h"
#include "../lib/CPlayerState.h"
#include "../lib/mapping/CMapInfo.h"
#include "../lib/mapObjects/CGTownInstance.h"
#include "../lib/mapObjects/MiscObjects.h"
#include "../lib/modding/ModIncompatibility.h"
#include "../lib/rmg/CMapGenOptions.h"
#include "../lib/serializer/Connection.h"
2023-03-15 21:34:29 +02:00
#include "../lib/filesystem/Filesystem.h"
#include "../lib/serializer/CMemorySerializer.h"
#include "../lib/UnlockGuard.h"
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include "LobbyClientNetPackVisitors.h"
#include <vcmi/events/EventBus.h>
CServerHandler::~CServerHandler()
{
if (serverRunner)
serverRunner->shutdown();
2024-01-12 01:10:41 +02:00
networkHandler->stop();
2024-02-02 00:29:15 +02:00
try
{
if (serverRunner)
serverRunner->wait();
serverRunner.reset();
{
auto unlockInterface = vstd::makeUnlockGuard(GH.interfaceMutex);
threadNetwork.join();
}
2024-02-02 00:29:15 +02:00
}
catch (const std::runtime_error & e)
{
logGlobal->error("Failed to shut down network thread! Reason: %s", e.what());
assert(0);
2024-02-02 00:29:15 +02:00
}
}
void CServerHandler::endNetwork()
{
if (client)
client->endNetwork();
networkHandler->stop();
{
auto unlockInterface = vstd::makeUnlockGuard(GH.interfaceMutex);
threadNetwork.join();
}
}
CServerHandler::CServerHandler()
2024-02-03 22:59:56 +02:00
: networkHandler(INetworkHandler::createHandler())
2024-01-12 16:55:36 +02:00
, lobbyClient(std::make_unique<GlobalLobbyClient>())
, gameChat(std::make_unique<GameChatHandler>())
, threadNetwork(&CServerHandler::threadRunNetwork, this)
2024-01-26 17:40:31 +02:00
, state(EClientState::NONE)
2024-02-04 19:56:04 +02:00
, serverPort(0)
, campaignStateToSend(nullptr)
2024-01-26 17:40:31 +02:00
, screenType(ESelectionScreen::unknown)
, serverMode(EServerMode::NONE)
2024-01-26 17:40:31 +02:00
, loadMode(ELoadMode::NONE)
, client(nullptr)
{
2022-11-08 02:44:34 +02:00
uuid = boost::uuids::to_string(boost::uuids::random_generator()());
}
void CServerHandler::threadRunNetwork()
{
logGlobal->info("Starting network thread");
setThreadName("runNetwork");
try {
networkHandler->run();
}
catch (const TerminationRequestedException &)
{
logGlobal->info("Terminating network thread");
return;
}
logGlobal->info("Ending network thread");
}
void CServerHandler::resetStateForLobby(EStartMode mode, ESelectionScreen screen, EServerMode newServerMode, const std::vector<std::string> & playerNames)
{
hostClientId = -1;
2024-02-04 19:56:04 +02:00
setState(EClientState::NONE);
serverMode = newServerMode;
mapToStart = nullptr;
2022-12-07 23:36:20 +02:00
th = std::make_unique<CStopWatch>();
2024-02-11 16:26:27 +02:00
logicConnection.reset();
si = std::make_shared<StartInfo>();
localPlayerNames.clear();
si->difficulty = 1;
si->mode = mode;
screenType = screen;
localPlayerNames.clear();
if(!playerNames.empty()) //if have custom set of player names - use it
localPlayerNames = playerNames;
else
2024-10-07 18:37:07 +02:00
{
std::string playerName = settings["general"]["playerName"].String();
if(playerName == "Player")
playerName = CGI->generaltexth->translate("core.genrltxt.434");
localPlayerNames.push_back(playerName);
}
gameChat->resetMatchState();
2024-03-20 18:40:37 +02:00
lobbyClient->resetMatchState();
}
GameChatHandler & CServerHandler::getGameChat()
{
return *gameChat;
}
GlobalLobbyClient & CServerHandler::getGlobalLobby()
{
return *lobbyClient;
}
2024-02-03 22:59:56 +02:00
INetworkHandler & CServerHandler::getNetworkHandler()
{
return *networkHandler;
}
void CServerHandler::startLocalServerAndConnect(bool connectToLobby)
{
logNetwork->trace("\tLocal server startup has been requested");
#ifdef VCMI_MOBILE
// mobile apps can't spawn separate processes - only thread mode is available
serverRunner.reset(new ServerThreadRunner());
#else
if (settings["server"]["useProcess"].Bool())
serverRunner.reset(new ServerProcessRunner());
else
serverRunner.reset(new ServerThreadRunner());
#endif
2024-03-01 11:57:48 +02:00
auto si = std::make_shared<StartInfo>();
auto lastDifficulty = settings["general"]["lastDifficulty"];
2024-03-04 22:23:17 +02:00
si->difficulty = lastDifficulty.Integer();
2024-03-01 11:57:48 +02:00
logNetwork->trace("\tStarting local server");
uint16_t srvport = serverRunner->start(getLocalPort(), connectToLobby, si);
logNetwork->trace("\tConnecting to local server");
connectToServer(getLocalHostname(), srvport);
logNetwork->trace("\tWaiting for connection");
}
void CServerHandler::connectToServer(const std::string & addr, const ui16 port)
{
logNetwork->info("Establishing connection to %s:%d...", addr, port);
2024-02-04 19:56:04 +02:00
setState(EClientState::CONNECTING);
serverHostname = addr;
serverPort = port;
if (!isServerLocal())
2022-10-30 17:59:43 +02:00
{
2024-01-20 00:26:25 +02:00
Settings remoteAddress = settings.write["server"]["remoteHostname"];
remoteAddress->String() = addr;
2024-01-20 00:26:25 +02:00
Settings remotePort = settings.write["server"]["remotePort"];
remotePort->Integer() = port;
2022-10-30 17:59:43 +02:00
}
2024-01-12 16:55:36 +02:00
networkHandler->connectToRemote(*this, addr, port);
}
void CServerHandler::onConnectionFailed(const std::string & errorMessage)
{
2024-02-04 19:56:04 +02:00
assert(getState() == EClientState::CONNECTING);
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
if (isServerLocal())
{
// retry - local server might be still starting up
logNetwork->debug("\nCannot establish connection. %s. Retrying...", errorMessage);
2024-01-12 01:10:41 +02:00
networkHandler->createTimer(*this, std::chrono::milliseconds(100));
}
else
{
// remote server refused connection - show error message
2024-02-04 19:56:04 +02:00
setState(EClientState::NONE);
CInfoWindow::showInfoDialog(CGI->generaltexth->translate("vcmi.mainMenu.serverConnectionFailed"), {});
}
}
void CServerHandler::onTimer()
{
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
2024-02-04 19:56:04 +02:00
if(getState() == EClientState::CONNECTION_CANCELLED)
{
logNetwork->info("Connection aborted by player!");
serverRunner->wait();
serverRunner.reset();
if (GH.windows().topWindow<CSimpleJoinScreen>() != nullptr)
GH.windows().popWindows(1);
return;
}
assert(isServerLocal());
2024-01-12 16:55:36 +02:00
networkHandler->connectToRemote(*this, getLocalHostname(), getLocalPort());
}
void CServerHandler::onConnectionEstablished(const NetworkConnectionPtr & netConnection)
{
2024-02-04 19:56:04 +02:00
assert(getState() == EClientState::CONNECTING);
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
2024-01-12 16:55:36 +02:00
networkConnection = netConnection;
logNetwork->info("Connection established");
if (serverMode == EServerMode::LOBBY_GUEST)
{
// say hello to lobby to switch connection to proxy mode
getGlobalLobby().sendProxyConnectionLogin(netConnection);
}
2024-02-11 16:26:27 +02:00
logicConnection = std::make_shared<CConnection>(netConnection);
logicConnection->uuid = uuid;
logicConnection->enterLobbyConnectionMode();
sendClientConnecting();
}
2023-12-25 22:26:59 +02:00
void CServerHandler::applyPackOnLobbyScreen(CPackForLobby & pack)
{
ApplyOnLobbyScreenNetPackVisitor visitor(*this, dynamic_cast<CLobbyScreen *>(SEL));
pack.visit(visitor);
2023-12-25 22:26:59 +02:00
GH.windows().totalRedraw();
}
std::set<PlayerColor> CServerHandler::getHumanColors()
{
2024-02-11 16:26:27 +02:00
return clientHumanColors(logicConnection->connectionID);
}
PlayerColor CServerHandler::myFirstColor() const
{
2024-02-11 16:26:27 +02:00
return clientFirstColor(logicConnection->connectionID);
}
bool CServerHandler::isMyColor(PlayerColor color) const
{
2024-02-11 16:26:27 +02:00
return isClientColor(logicConnection->connectionID, color);
}
ui8 CServerHandler::myFirstId() const
{
2024-02-11 16:26:27 +02:00
return clientFirstId(logicConnection->connectionID);
}
2024-02-04 19:56:04 +02:00
EClientState CServerHandler::getState() const
{
return state;
}
void CServerHandler::setState(EClientState newState)
{
if (newState == EClientState::CONNECTION_CANCELLED && serverRunner != nullptr)
serverRunner->shutdown();
2024-02-04 19:56:04 +02:00
state = newState;
}
bool CServerHandler::isServerLocal() const
{
return serverRunner != nullptr;
}
bool CServerHandler::isHost() const
{
2024-02-11 16:26:27 +02:00
return logicConnection && hostClientId == logicConnection->connectionID;
}
bool CServerHandler::isGuest() const
{
2024-02-11 16:26:27 +02:00
return !logicConnection || hostClientId != logicConnection->connectionID;
}
const std::string & CServerHandler::getLocalHostname() const
{
return settings["server"]["localHostname"].String();
}
ui16 CServerHandler::getLocalPort() const
{
return settings["server"]["localPort"].Integer();
}
const std::string & CServerHandler::getRemoteHostname() const
2022-11-08 02:44:34 +02:00
{
return settings["server"]["remoteHostname"].String();
2022-11-08 02:44:34 +02:00
}
ui16 CServerHandler::getRemotePort() const
2022-11-08 02:44:34 +02:00
{
return settings["server"]["remotePort"].Integer();
}
const std::string & CServerHandler::getCurrentHostname() const
{
return serverHostname;
}
ui16 CServerHandler::getCurrentPort() const
{
return serverPort;
2022-11-08 02:44:34 +02:00
}
void CServerHandler::sendClientConnecting() const
{
LobbyClientConnected lcc;
lcc.uuid = uuid;
lcc.names = localPlayerNames;
lcc.mode = si->mode;
sendLobbyPack(lcc);
}
void CServerHandler::sendClientDisconnecting()
{
// FIXME: This is workaround needed to make sure client not trying to sent anything to non existed server
2024-02-04 19:56:04 +02:00
if(getState() == EClientState::DISCONNECTING)
{
assert(0);
return;
}
2024-02-04 19:56:04 +02:00
setState(EClientState::DISCONNECTING);
mapToStart = nullptr;
LobbyClientDisconnected lcd;
2024-02-11 16:26:27 +02:00
lcd.clientId = logicConnection->connectionID;
logNetwork->info("Connection has been requested to be closed.");
if(isServerLocal())
{
lcd.shutdownServer = true;
logNetwork->info("Sent closing signal to the server");
}
else
{
logNetwork->info("Sent leaving signal to the server");
}
sendLobbyPack(lcd);
2024-02-04 19:56:04 +02:00
networkConnection->close();
networkConnection.reset();
2024-02-11 16:26:27 +02:00
logicConnection.reset();
waitForServerShutdown();
}
void CServerHandler::setCampaignState(std::shared_ptr<CampaignState> newCampaign)
{
2024-02-04 19:56:04 +02:00
setState(EClientState::LOBBY_CAMPAIGN);
LobbySetCampaign lsc;
lsc.ourCampaign = newCampaign;
sendLobbyPack(lsc);
}
void CServerHandler::setCampaignMap(CampaignScenarioID mapId) const
{
2024-02-04 19:56:04 +02:00
if(getState() == EClientState::GAMEPLAY) // FIXME: UI shouldn't sent commands in first place
return;
LobbySetCampaignMap lscm;
lscm.mapId = mapId;
sendLobbyPack(lscm);
}
void CServerHandler::setCampaignBonus(int bonusId) const
{
2024-02-04 19:56:04 +02:00
if(getState() == EClientState::GAMEPLAY) // FIXME: UI shouldn't sent commands in first place
return;
LobbySetCampaignBonus lscb;
lscb.bonusId = bonusId;
sendLobbyPack(lscb);
}
void CServerHandler::setMapInfo(std::shared_ptr<CMapInfo> to, std::shared_ptr<CMapGenOptions> mapGenOpts) const
{
LobbySetMap lsm;
lsm.mapInfo = to;
lsm.mapGenOpts = mapGenOpts;
sendLobbyPack(lsm);
}
void CServerHandler::setPlayer(PlayerColor color) const
{
LobbySetPlayer lsp;
lsp.clickedColor = color;
sendLobbyPack(lsp);
}
2023-10-16 21:35:29 +02:00
void CServerHandler::setPlayerName(PlayerColor color, const std::string & name) const
2023-10-13 23:04:35 +02:00
{
LobbySetPlayerName lspn;
lspn.color = color;
lspn.name = name;
sendLobbyPack(lspn);
}
2024-07-29 22:54:42 +02:00
void CServerHandler::setPlayerHandicap(PlayerColor color, Handicap handicap) const
2024-07-16 00:40:39 +02:00
{
LobbySetPlayerHandicap lsph;
lsph.color = color;
lsph.handicap = handicap;
sendLobbyPack(lsph);
}
2023-08-17 17:29:31 +02:00
void CServerHandler::setPlayerOption(ui8 what, int32_t value, PlayerColor player) const
{
LobbyChangePlayerOption lcpo;
lcpo.what = what;
lcpo.value = value;
lcpo.color = player;
sendLobbyPack(lcpo);
}
void CServerHandler::setDifficulty(int to) const
{
LobbySetDifficulty lsd;
lsd.difficulty = to;
sendLobbyPack(lsd);
}
void CServerHandler::setSimturnsInfo(const SimturnsInfo & info) const
{
LobbySetSimturns pack;
pack.simturnsInfo = info;
sendLobbyPack(pack);
}
2023-08-25 18:20:26 +02:00
void CServerHandler::setTurnTimerInfo(const TurnTimerInfo & info) const
{
LobbySetTurnTime lstt;
2023-08-25 18:20:26 +02:00
lstt.turnTimerInfo = info;
sendLobbyPack(lstt);
}
2023-12-28 21:48:19 +02:00
void CServerHandler::setExtraOptionsInfo(const ExtraOptionsInfo & info) const
2023-12-27 15:39:35 +02:00
{
2023-12-28 21:48:19 +02:00
LobbySetExtraOptions lseo;
lseo.extraOptionsInfo = info;
sendLobbyPack(lseo);
2023-12-27 15:39:35 +02:00
}
void CServerHandler::sendMessage(const std::string & txt) const
{
std::istringstream readed;
readed.str(txt);
std::string command;
readed >> command;
if(command == "!passhost")
{
std::string id;
readed >> id;
if(id.length())
{
LobbyChangeHost lch;
lch.newHostConnectionId = boost::lexical_cast<int>(id);
sendLobbyPack(lch);
}
}
else if(command == "!forcep")
{
std::string connectedId;
std::string playerColorId;
readed >> connectedId;
readed >> playerColorId;
2022-11-13 04:35:16 +02:00
if(connectedId.length() && playerColorId.length())
{
ui8 connected = boost::lexical_cast<int>(connectedId);
auto color = PlayerColor(boost::lexical_cast<int>(playerColorId));
if(color.isValidPlayer() && playerNames.find(connected) != playerNames.end())
{
LobbyForceSetPlayer lfsp;
lfsp.targetConnectedPlayer = connected;
lfsp.targetPlayerColor = color;
sendLobbyPack(lfsp);
}
}
}
else
{
gameChat->sendMessageLobby(playerNames.find(myFirstId())->second.name, txt);
}
}
void CServerHandler::sendGuiAction(ui8 action) const
{
LobbyGuiAction lga;
lga.action = static_cast<LobbyGuiAction::EAction>(action);
sendLobbyPack(lga);
}
void CServerHandler::sendRestartGame() const
{
if(si->campState && !si->campState->getLoadingBackground().empty())
GH.windows().createAndPushWindow<CLoadingScreen>(si->campState->getLoadingBackground());
else
GH.windows().createAndPushWindow<CLoadingScreen>();
LobbyRestartGame endGame;
sendLobbyPack(endGame);
}
bool CServerHandler::validateGameStart(bool allowOnlyAI) const
{
try
{
verifyStateBeforeStart(allowOnlyAI ? true : settings["session"]["onlyai"].Bool());
}
catch(ModIncompatibility & e)
{
logGlobal->warn("Incompatibility exception during start scenario: %s", e.what());
2023-09-23 00:32:48 +02:00
std::string errorMsg;
if(!e.whatMissing().empty())
{
errorMsg += VLC->generaltexth->translate("vcmi.server.errors.modsToEnable") + '\n';
errorMsg += e.whatMissing();
}
if(!e.whatExcessive().empty())
{
errorMsg += VLC->generaltexth->translate("vcmi.server.errors.modsToDisable") + '\n';
errorMsg += e.whatExcessive();
}
showServerError(errorMsg);
return false;
}
catch(std::exception & e)
{
logGlobal->error("Exception during startScenario: %s", e.what());
showServerError( std::string("Unable to start map! Reason: ") + e.what());
return false;
}
return true;
}
void CServerHandler::sendStartGame(bool allowOnlyAI) const
{
verifyStateBeforeStart(allowOnlyAI ? true : settings["session"]["onlyai"].Bool());
2023-12-03 18:39:25 +02:00
if(!settings["session"]["headless"].Bool())
{
if(si->campState && !si->campState->getLoadingBackground().empty())
GH.windows().createAndPushWindow<CLoadingScreen>(si->campState->getLoadingBackground());
else
GH.windows().createAndPushWindow<CLoadingScreen>();
}
2023-09-21 22:28:29 +02:00
LobbyPrepareStartGame lpsg;
sendLobbyPack(lpsg);
LobbyStartGame lsg;
sendLobbyPack(lsg);
}
void CServerHandler::startMapAfterConnection(std::shared_ptr<CMapInfo> to)
{
mapToStart = to;
}
void CServerHandler::startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState)
{
if(CMM)
CMM->disable();
2024-01-10 19:43:34 +02:00
switch(si->mode)
{
case EStartMode::NEW_GAME:
2022-09-28 21:15:05 +02:00
client->newGame(gameState);
break;
case EStartMode::CAMPAIGN:
2024-06-23 15:40:46 +02:00
if(si->campState->conqueredScenarios().empty())
2024-07-26 03:33:44 +02:00
si->campState->highscoreParameters.clear();
2022-09-28 21:15:05 +02:00
client->newGame(gameState);
break;
case EStartMode::LOAD_GAME:
2022-09-29 19:33:44 +02:00
client->loadGame(gameState);
break;
default:
throw std::runtime_error("Invalid mode");
}
// After everything initialized we can accept CPackToClient netpacks
2024-02-11 16:26:27 +02:00
logicConnection->enterGameplayConnectionMode(client->gameState());
2024-02-04 19:56:04 +02:00
setState(EClientState::GAMEPLAY);
}
2024-08-14 21:51:08 +02:00
void CServerHandler::showHighScoresAndEndGameplay(PlayerColor player, bool victory, const StatisticDataSet & statistic)
{
2024-08-02 19:37:46 +02:00
HighScoreParameter param = HighScore::prepareHighScores(client->gameState(), player, victory);
if(victory && client->gameState()->getStartInfo()->campState)
{
startCampaignScenario(param, client->gameState()->getStartInfo()->campState, statistic);
}
else
{
2024-04-07 20:21:48 +02:00
HighScoreCalculation scenarioHighScores;
scenarioHighScores.parameters.push_back(param);
scenarioHighScores.isCampaign = false;
endGameplay();
CMM->menu->switchToTab("main");
GH.windows().createAndPushWindow<CHighScoreInputScreen>(victory, scenarioHighScores, statistic);
}
}
void CServerHandler::endGameplay()
{
// Game is ending
// Tell the network thread to reach a stable state
sendClientDisconnecting();
logNetwork->info("Closed connection.");
client->endGame();
2024-01-10 19:43:34 +02:00
client.reset();
if(CMM)
{
GH.curInt = CMM.get();
CMM->enable();
2024-09-18 22:10:25 +02:00
CMM->playMusic();
}
else
{
2024-09-18 22:10:25 +02:00
auto mainMenu = CMainMenu::create();
GH.curInt = mainMenu.get();
mainMenu->playMusic();
}
}
void CServerHandler::restartGameplay()
{
client->endGame();
client.reset();
2024-02-11 16:26:27 +02:00
logicConnection->enterLobbyConnectionMode();
}
2024-08-14 21:51:08 +02:00
void CServerHandler::startCampaignScenario(HighScoreParameter param, std::shared_ptr<CampaignState> cs, const StatisticDataSet & statistic)
{
std::shared_ptr<CampaignState> ourCampaign = cs;
if (!cs)
ourCampaign = si->campState;
2023-09-27 22:53:13 +02:00
param.campaignName = cs->getNameTranslated();
2024-07-26 03:33:44 +02:00
cs->highscoreParameters.push_back(param);
auto campaignScoreCalculator = std::make_shared<HighScoreCalculation>();
campaignScoreCalculator->isCampaign = true;
campaignScoreCalculator->parameters = cs->highscoreParameters;
endGameplay();
auto & epilogue = ourCampaign->scenario(*ourCampaign->lastScenario()).epilog;
auto finisher = [ourCampaign, campaignScoreCalculator, statistic]()
{
if(ourCampaign->campaignSet != "" && ourCampaign->isCampaignFinished())
{
Settings entry = persistentStorage.write["completedCampaigns"][ourCampaign->getFilename()];
entry->Bool() = true;
}
GH.windows().pushWindow(CMM);
GH.windows().pushWindow(CMM->menu);
2023-09-21 23:41:00 +02:00
if(!ourCampaign->isCampaignFinished())
CMM->openCampaignLobby(ourCampaign);
else
{
CMM->openCampaignScreen(ourCampaign->campaignSet);
2024-09-12 23:06:33 +02:00
if(!ourCampaign->getOutroVideo().empty() && CCS->videoh->open(ourCampaign->getOutroVideo(), 1))
2024-09-05 21:31:17 +02:00
{
CCS->musich->stopMusic();
2024-09-12 23:35:21 +02:00
GH.windows().createAndPushWindow<VideoWindow>(ourCampaign->getOutroVideo(), ourCampaign->getVideoRim().empty() ? ImagePath::builtin("INTRORIM") : ourCampaign->getVideoRim(), false, 1, [campaignScoreCalculator, statistic](bool skipped){
2024-09-05 21:31:17 +02:00
GH.windows().createAndPushWindow<CHighScoreInputScreen>(true, *campaignScoreCalculator, statistic);
});
}
else
GH.windows().createAndPushWindow<CHighScoreInputScreen>(true, *campaignScoreCalculator, statistic);
}
};
if(epilogue.hasPrologEpilog)
{
GH.windows().createAndPushWindow<CPrologEpilogVideo>(epilogue, finisher);
}
else
{
finisher();
}
}
void CServerHandler::showServerError(const std::string & txt) const
{
2023-09-21 04:31:08 +02:00
if(auto w = GH.windows().topWindow<CLoadingScreen>())
GH.windows().popWindow(w);
CInfoWindow::showInfoDialog(txt, {});
}
int CServerHandler::howManyPlayerInterfaces()
{
int playerInts = 0;
for(auto pint : client->playerint)
{
if(dynamic_cast<CPlayerInterface *>(pint.second.get()))
playerInts++;
}
return playerInts;
}
ELoadMode CServerHandler::getLoadMode()
{
2024-02-04 19:56:04 +02:00
if(loadMode != ELoadMode::TUTORIAL && getState() == EClientState::GAMEPLAY)
{
if(si->campState)
return ELoadMode::CAMPAIGN;
for(auto pn : playerNames)
{
2024-02-11 16:26:27 +02:00
if(pn.second.connection != logicConnection->connectionID)
return ELoadMode::MULTI;
}
if(howManyPlayerInterfaces() > 1) //this condition will work for hotseat mode OR multiplayer with allowed more than 1 color per player to control
return ELoadMode::MULTI;
return ELoadMode::SINGLE;
}
return loadMode;
}
void CServerHandler::debugStartTest(std::string filename, bool save)
{
logGlobal->info("Starting debug test with file: %s", filename);
auto mapInfo = std::make_shared<CMapInfo>();
if(save)
{
resetStateForLobby(EStartMode::LOAD_GAME, ESelectionScreen::loadGame, EServerMode::LOCAL, {});
mapInfo->saveInit(ResourcePath(filename, EResType::SAVEGAME));
}
else
{
resetStateForLobby(EStartMode::NEW_GAME, ESelectionScreen::newGame, EServerMode::LOCAL, {});
mapInfo->mapInit(filename);
}
if(settings["session"]["donotstartserver"].Bool())
connectToServer(getLocalHostname(), getLocalPort());
else
startLocalServerAndConnect(false);
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
2021-05-16 14:39:38 +02:00
2023-05-16 17:34:23 +02:00
while(!settings["session"]["headless"].Bool() && !GH.windows().topWindow<CLobbyScreen>())
boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
2023-05-16 17:34:23 +02:00
while(!mi || mapInfo->fileURI != mi->fileURI)
{
setMapInfo(mapInfo);
boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
}
// "Click" on color to remove us from it
setPlayer(myFirstColor());
while(myFirstColor() != PlayerColor::CANNOT_DETERMINE)
boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
while(true)
{
try
{
sendStartGame();
break;
}
catch(...)
{
}
boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
}
}
class ServerHandlerCPackVisitor : public VCMI_LIB_WRAP_NAMESPACE(ICPackVisitor)
{
private:
CServerHandler & handler;
public:
ServerHandlerCPackVisitor(CServerHandler & handler)
:handler(handler)
{
}
bool callTyped() override { return false; }
void visitForLobby(CPackForLobby & lobbyPack) override
{
handler.visitForLobby(lobbyPack);
}
void visitForClient(CPackForClient & clientPack) override
{
handler.visitForClient(clientPack);
}
};
2024-02-02 01:27:19 +02:00
void CServerHandler::onPacketReceived(const std::shared_ptr<INetworkConnection> &, const std::vector<std::byte> & message)
{
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
2024-02-04 19:56:04 +02:00
if(getState() == EClientState::DISCONNECTING)
return;
auto pack = logicConnection->retrievePack(message);
ServerHandlerCPackVisitor visitor(*this);
pack->visit(visitor);
}
2024-02-02 01:27:19 +02:00
void CServerHandler::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
{
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
if (connection != networkConnection)
{
// ServerHandler already closed this connection on its own
// This is the final call from network thread that informs serverHandler that connection has died
// ignore it since serverHandler have already shut down this connection (and possibly started a new one)
return;
}
waitForServerShutdown();
2024-02-04 19:56:04 +02:00
if(getState() == EClientState::DISCONNECTING)
{
// Note: this branch can be reached on app shutdown, when main thread holds mutex till destruction
logNetwork->info("Successfully closed connection to server!");
return;
}
logNetwork->error("Lost connection to server! Connection has been closed");
if(client)
{
endGameplay();
2024-02-04 19:56:04 +02:00
CMM->menu->switchToTab("main");
showServerError(CGI->generaltexth->translate("vcmi.server.errors.disconnected"));
}
else
{
LobbyClientDisconnected lcd;
2024-02-11 16:26:27 +02:00
lcd.clientId = logicConnection->connectionID;
applyPackOnLobbyScreen(lcd);
}
2024-02-04 19:56:04 +02:00
networkConnection.reset();
}
void CServerHandler::waitForServerShutdown()
{
if (!serverRunner)
return; // may not exist for guest in MP
serverRunner->wait();
int exitCode = serverRunner->exitCode();
serverRunner.reset();
if (exitCode == 0)
{
logNetwork->info("Server closed correctly");
}
else
{
2024-02-04 19:56:04 +02:00
if (getState() == EClientState::CONNECTING)
{
2024-02-04 19:56:04 +02:00
showServerError(CGI->generaltexth->translate("vcmi.server.errors.existingProcess"));
setState(EClientState::CONNECTION_CANCELLED); // stop attempts to reconnect
}
logNetwork->error("Error: server failed to close correctly or crashed!");
logNetwork->error("Check log file for more info");
}
serverRunner.reset();
}
void CServerHandler::visitForLobby(CPackForLobby & lobbyPack)
{
ApplyOnLobbyHandlerNetPackVisitor visitor(*this);
lobbyPack.visit(visitor);
if(visitor.getResult())
{
if(!settings["session"]["headless"].Bool())
2023-12-25 22:26:59 +02:00
applyPackOnLobbyScreen(lobbyPack);
}
}
void CServerHandler::visitForClient(CPackForClient & clientPack)
{
client->handlePack(clientPack);
}
void CServerHandler::sendLobbyPack(const CPackForLobby & pack) const
{
2024-02-04 19:56:04 +02:00
if(getState() != EClientState::STARTING)
logicConnection->sendPack(pack);
}
2024-02-10 19:02:25 +02:00
bool CServerHandler::inLobbyRoom() const
{
return serverMode == EServerMode::LOBBY_HOST || serverMode == EServerMode::LOBBY_GUEST;
2024-02-10 19:02:25 +02:00
}
bool CServerHandler::inGame() const
{
2024-02-11 16:26:27 +02:00
return logicConnection != nullptr;
2024-02-10 19:02:25 +02:00
}