2018-01-05 19:21:07 +02:00
|
|
|
/*
|
|
|
|
* 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 "CPlayerInterface.h"
|
|
|
|
#include "gui/CGuiHandler.h"
|
2023-05-16 14:10:26 +02:00
|
|
|
#include "gui/WindowHandler.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-12-28 21:27:21 +02:00
|
|
|
#include "globalLobby/GlobalLobbyClient.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "lobby/CSelectionBase.h"
|
|
|
|
#include "lobby/CLobbyScreen.h"
|
2022-09-22 13:39:43 +02:00
|
|
|
#include "windows/InfoWindows.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
#include "mainmenu/CMainMenu.h"
|
2023-06-26 20:51:10 +02:00
|
|
|
#include "mainmenu/CPrologEpilogVideo.h"
|
2023-09-22 20:39:20 +02:00
|
|
|
#include "mainmenu/CHighScoreScreen.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2021-03-02 05:56:57 +02:00
|
|
|
#ifdef VCMI_ANDROID
|
2018-05-25 21:08:14 +02:00
|
|
|
#include "../lib/CAndroidVMHelper.h"
|
2022-07-26 15:16:37 +02:00
|
|
|
#elif defined(VCMI_IOS)
|
2022-08-16 11:54:44 +02:00
|
|
|
#include "ios/utils.h"
|
|
|
|
#include <dispatch/dispatch.h>
|
2018-01-05 19:21:07 +02:00
|
|
|
#endif
|
2022-12-05 21:36:02 +02:00
|
|
|
|
|
|
|
#ifdef SINGLE_PROCESS_APP
|
|
|
|
#include "../server/CVCMIServer.h"
|
|
|
|
#endif
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/CConfigHandler.h"
|
|
|
|
#include "../lib/CGeneralTextHandler.h"
|
|
|
|
#include "../lib/CThreadHelper.h"
|
|
|
|
#include "../lib/StartInfo.h"
|
2023-08-25 18:20:26 +02:00
|
|
|
#include "../lib/TurnTimerInfo.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/VCMIDirs.h"
|
2023-06-25 21:28:24 +02:00
|
|
|
#include "../lib/campaign/CampaignState.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/mapping/CMapInfo.h"
|
|
|
|
#include "../lib/mapObjects/MiscObjects.h"
|
2023-09-15 12:59:02 +02:00
|
|
|
#include "../lib/modding/ModIncompatibility.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/rmg/CMapGenOptions.h"
|
2023-11-18 16:34:18 +02:00
|
|
|
#include "../lib/serializer/Connection.h"
|
2023-03-15 21:34:29 +02:00
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
2023-11-11 00:39:08 +02:00
|
|
|
#include "../lib/registerTypes/RegisterTypesLobbyPacks.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/serializer/CMemorySerializer.h"
|
2023-11-27 19:57:48 +02:00
|
|
|
#include "../lib/UnlockGuard.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
#include <boost/uuid/uuid.hpp>
|
|
|
|
#include <boost/uuid/uuid_io.hpp>
|
|
|
|
#include <boost/uuid/uuid_generators.hpp>
|
2018-08-13 23:48:00 +02:00
|
|
|
#include "../lib/serializer/Cast.h"
|
2023-02-12 09:23:39 +02:00
|
|
|
#include "LobbyClientNetPackVisitors.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
#include <vcmi/events/EventBus.h>
|
|
|
|
|
2022-09-10 15:03:54 +02:00
|
|
|
#ifdef VCMI_WINDOWS
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
template<typename T> class CApplyOnLobby;
|
|
|
|
|
2023-02-26 11:18:24 +02:00
|
|
|
#if defined(VCMI_ANDROID) && !defined(SINGLE_PROCESS_APP)
|
2018-05-25 21:08:14 +02:00
|
|
|
extern std::atomic_bool androidTestServerReadyFlag;
|
|
|
|
#endif
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
class CBaseForLobbyApply
|
|
|
|
{
|
|
|
|
public:
|
2023-12-25 22:26:59 +02:00
|
|
|
virtual bool applyOnLobbyHandler(CServerHandler * handler, CPackForLobby & pack) const = 0;
|
|
|
|
virtual void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler, CPackForLobby & pack) const = 0;
|
2018-01-05 19:21:07 +02:00
|
|
|
virtual ~CBaseForLobbyApply(){};
|
|
|
|
template<typename U> static CBaseForLobbyApply * getApplier(const U * t = nullptr)
|
|
|
|
{
|
|
|
|
return new CApplyOnLobby<U>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> class CApplyOnLobby : public CBaseForLobbyApply
|
|
|
|
{
|
|
|
|
public:
|
2023-12-25 22:26:59 +02:00
|
|
|
bool applyOnLobbyHandler(CServerHandler * handler, CPackForLobby & pack) const override
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-01-20 00:26:25 +02:00
|
|
|
auto & ref = static_cast<T&>(pack);
|
2023-02-12 09:23:39 +02:00
|
|
|
ApplyOnLobbyHandlerNetPackVisitor visitor(*handler);
|
|
|
|
|
2024-01-20 00:26:25 +02:00
|
|
|
logNetwork->trace("\tImmediately apply on lobby: %s", typeid(ref).name());
|
|
|
|
ref.visit(visitor);
|
2023-02-12 09:23:39 +02:00
|
|
|
|
|
|
|
return visitor.getResult();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-12-25 22:26:59 +02:00
|
|
|
void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler, CPackForLobby & pack) const override
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-01-20 00:26:25 +02:00
|
|
|
auto & ref = static_cast<T &>(pack);
|
2023-02-12 09:23:39 +02:00
|
|
|
ApplyOnLobbyScreenNetPackVisitor visitor(*handler, lobby);
|
|
|
|
|
2024-01-20 00:26:25 +02:00
|
|
|
logNetwork->trace("\tApply on lobby from queue: %s", typeid(ref).name());
|
|
|
|
ref.visit(visitor);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<> class CApplyOnLobby<CPack>: public CBaseForLobbyApply
|
|
|
|
{
|
|
|
|
public:
|
2023-12-25 22:26:59 +02:00
|
|
|
bool applyOnLobbyHandler(CServerHandler * handler, CPackForLobby & pack) const override
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
logGlobal->error("Cannot apply plain CPack!");
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-25 22:26:59 +02:00
|
|
|
void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler, CPackForLobby & pack) const override
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
logGlobal->error("Cannot apply plain CPack!");
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-25 21:23:27 +02:00
|
|
|
CServerHandler::~CServerHandler()
|
|
|
|
{
|
2024-01-12 01:10:41 +02:00
|
|
|
networkHandler->stop();
|
2024-02-02 00:29:15 +02:00
|
|
|
try
|
|
|
|
{
|
2024-02-03 19:08:45 +02:00
|
|
|
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());
|
2024-02-03 19:08:45 +02:00
|
|
|
assert(0);
|
2024-02-02 00:29:15 +02:00
|
|
|
}
|
2023-12-25 21:23:27 +02:00
|
|
|
}
|
2023-11-18 16:34:18 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
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>())
|
2024-02-03 22:59:56 +02:00
|
|
|
, applier(std::make_unique<CApplier<CBaseForLobbyApply>>())
|
2024-02-03 19:08:45 +02:00
|
|
|
, 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)
|
2023-11-18 16:34:18 +02:00
|
|
|
, campaignStateToSend(nullptr)
|
2024-01-26 17:40:31 +02:00
|
|
|
, screenType(ESelectionScreen::unknown)
|
2024-01-29 22:05:11 +02:00
|
|
|
, serverMode(EServerMode::NONE)
|
2024-01-26 17:40:31 +02:00
|
|
|
, loadMode(ELoadMode::NONE)
|
|
|
|
, client(nullptr)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2022-11-08 02:44:34 +02:00
|
|
|
uuid = boost::uuids::to_string(boost::uuids::random_generator()());
|
2018-01-05 19:21:07 +02:00
|
|
|
registerTypesLobbyPacks(*applier);
|
2023-12-25 21:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::threadRunNetwork()
|
|
|
|
{
|
|
|
|
logGlobal->info("Starting network thread");
|
|
|
|
setThreadName("runNetwork");
|
2024-01-12 01:10:41 +02:00
|
|
|
networkHandler->run();
|
2023-12-25 21:23:27 +02:00
|
|
|
logGlobal->info("Ending network thread");
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
void CServerHandler::resetStateForLobby(EStartMode mode, ESelectionScreen screen, EServerMode newServerMode, const std::vector<std::string> & names)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
hostClientId = -1;
|
2024-02-04 19:56:04 +02:00
|
|
|
setState(EClientState::NONE);
|
2024-01-29 22:05:11 +02:00
|
|
|
serverMode = newServerMode;
|
2023-08-11 18:04:14 +02:00
|
|
|
mapToStart = nullptr;
|
2022-12-07 23:36:20 +02:00
|
|
|
th = std::make_unique<CStopWatch>();
|
2018-01-05 19:21:07 +02:00
|
|
|
c.reset();
|
2018-04-30 17:09:48 +02:00
|
|
|
si = std::make_shared<StartInfo>();
|
2018-01-05 19:21:07 +02:00
|
|
|
playerNames.clear();
|
|
|
|
si->difficulty = 1;
|
|
|
|
si->mode = mode;
|
2024-01-21 16:48:36 +02:00
|
|
|
screenType = screen;
|
2018-01-05 19:21:07 +02:00
|
|
|
myNames.clear();
|
2024-01-21 16:48:36 +02:00
|
|
|
if(!names.empty()) //if have custom set of player names - use it
|
|
|
|
myNames = names;
|
2018-01-05 19:21:07 +02:00
|
|
|
else
|
|
|
|
myNames.push_back(settings["general"]["playerName"].String());
|
|
|
|
}
|
|
|
|
|
2023-12-28 21:27:21 +02:00
|
|
|
GlobalLobbyClient & CServerHandler::getGlobalLobby()
|
|
|
|
{
|
|
|
|
return *lobbyClient;
|
|
|
|
}
|
|
|
|
|
2024-02-03 22:59:56 +02:00
|
|
|
INetworkHandler & CServerHandler::getNetworkHandler()
|
|
|
|
{
|
|
|
|
return *networkHandler;
|
|
|
|
}
|
|
|
|
|
2024-01-10 15:31:11 +02:00
|
|
|
void CServerHandler::startLocalServerAndConnect(bool connectToLobby)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-02-03 19:08:45 +02:00
|
|
|
if(threadRunLocalServer.joinable())
|
|
|
|
threadRunLocalServer.join();
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
th->update();
|
2023-12-26 21:13:46 +02:00
|
|
|
|
2023-02-26 11:18:24 +02:00
|
|
|
#if defined(SINGLE_PROCESS_APP)
|
2022-08-15 15:31:58 +02:00
|
|
|
boost::condition_variable cond;
|
2024-01-10 15:31:11 +02:00
|
|
|
std::vector<std::string> args{"--port=" + std::to_string(getLocalPort())};
|
|
|
|
if(connectToLobby)
|
|
|
|
args.push_back("--lobby");
|
|
|
|
|
2024-02-03 19:08:45 +02:00
|
|
|
threadRunLocalServer = boost::thread([&cond, args] {
|
2022-08-15 15:31:58 +02:00
|
|
|
setThreadName("CVCMIServer");
|
2022-11-08 16:05:47 +02:00
|
|
|
CVCMIServer::create(&cond, args);
|
2022-08-15 15:31:58 +02:00
|
|
|
});
|
2023-02-26 11:18:24 +02:00
|
|
|
#elif defined(VCMI_ANDROID)
|
|
|
|
{
|
|
|
|
CAndroidVMHelper envHelper;
|
|
|
|
envHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "startServer", true);
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
#else
|
2024-02-03 19:08:45 +02:00
|
|
|
threadRunLocalServer = boost::thread(&CServerHandler::threadRunServer, this, connectToLobby); //runs server executable;
|
2018-01-05 19:21:07 +02:00
|
|
|
#endif
|
|
|
|
logNetwork->trace("Setting up thread calling server: %d ms", th->getDiff());
|
|
|
|
|
|
|
|
th->update();
|
|
|
|
|
2023-02-26 11:18:24 +02:00
|
|
|
#ifdef SINGLE_PROCESS_APP
|
2022-08-15 15:31:58 +02:00
|
|
|
{
|
2022-09-16 15:43:21 +02:00
|
|
|
#ifdef VCMI_IOS
|
2022-08-16 11:54:44 +02:00
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
iOS_utils::showLoadingIndicator();
|
|
|
|
});
|
2022-09-16 15:43:21 +02:00
|
|
|
#endif
|
2022-08-16 11:54:44 +02:00
|
|
|
|
2022-08-15 15:31:58 +02:00
|
|
|
boost::mutex m;
|
|
|
|
boost::unique_lock<boost::mutex> lock{m};
|
|
|
|
logNetwork->info("waiting for server");
|
|
|
|
cond.wait(lock);
|
|
|
|
logNetwork->info("server is ready");
|
2022-08-16 11:54:44 +02:00
|
|
|
|
2022-09-16 15:43:21 +02:00
|
|
|
#ifdef VCMI_IOS
|
2022-08-16 11:54:44 +02:00
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
|
|
|
iOS_utils::hideLoadingIndicator();
|
|
|
|
});
|
2022-09-16 15:43:21 +02:00
|
|
|
#endif
|
2022-08-15 15:31:58 +02:00
|
|
|
}
|
2023-02-26 11:18:24 +02:00
|
|
|
#elif defined(VCMI_ANDROID)
|
|
|
|
logNetwork->info("waiting for server");
|
|
|
|
while(!androidTestServerReadyFlag.load())
|
|
|
|
{
|
|
|
|
logNetwork->info("still waiting...");
|
2023-12-26 18:30:57 +02:00
|
|
|
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
|
2023-02-26 11:18:24 +02:00
|
|
|
}
|
|
|
|
logNetwork->info("waiting for server finished...");
|
|
|
|
androidTestServerReadyFlag = false;
|
2018-01-05 19:21:07 +02:00
|
|
|
#endif
|
|
|
|
logNetwork->trace("Waiting for server: %d ms", th->getDiff());
|
|
|
|
|
|
|
|
th->update(); //put breakpoint here to attach to server before it does something stupid
|
|
|
|
|
2023-12-26 19:27:47 +02:00
|
|
|
connectToServer(getLocalHostname(), getLocalPort());
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
logNetwork->trace("\tConnecting to the server: %d ms", th->getDiff());
|
|
|
|
}
|
|
|
|
|
2023-12-26 19:27:47 +02:00
|
|
|
void CServerHandler::connectToServer(const std::string & addr, const ui16 port)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-12-26 19:27:47 +02:00
|
|
|
logNetwork->info("Establishing connection to %s:%d...", addr, port);
|
2024-02-04 19:56:04 +02:00
|
|
|
setState(EClientState::CONNECTING);
|
2023-12-26 19:27:47 +02:00
|
|
|
serverHostname = addr;
|
|
|
|
serverPort = port;
|
2022-10-01 16:41:12 +02:00
|
|
|
|
2023-12-26 19:27:47 +02:00
|
|
|
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;
|
2023-12-26 19:27:47 +02:00
|
|
|
|
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
|
|
|
}
|
2023-11-18 16:34:18 +02:00
|
|
|
|
2024-01-12 16:55:36 +02:00
|
|
|
networkHandler->connectToRemote(*this, addr, port);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
void CServerHandler::onConnectionFailed(const std::string & errorMessage)
|
2023-12-26 18:30:57 +02:00
|
|
|
{
|
2024-02-04 19:56:04 +02:00
|
|
|
assert(getState() == EClientState::CONNECTING);
|
2024-02-03 22:24:32 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
|
2023-12-26 18:30:57 +02:00
|
|
|
if (isServerLocal())
|
|
|
|
{
|
|
|
|
// retry - local server might be still starting up
|
2023-12-26 19:27:47 +02:00
|
|
|
logNetwork->debug("\nCannot establish connection. %s. Retrying...", errorMessage);
|
2024-01-12 01:10:41 +02:00
|
|
|
networkHandler->createTimer(*this, std::chrono::milliseconds(100));
|
2023-12-26 18:30:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// remote server refused connection - show error message
|
2024-02-04 19:56:04 +02:00
|
|
|
setState(EClientState::NONE);
|
2023-12-26 18:30:57 +02:00
|
|
|
CInfoWindow::showInfoDialog(CGI->generaltexth->translate("vcmi.mainMenu.serverConnectionFailed"), {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::onTimer()
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-02-03 22:24:32 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
|
2024-02-04 19:56:04 +02:00
|
|
|
if(getState() == EClientState::CONNECTION_CANCELLED)
|
2023-11-18 16:34:18 +02:00
|
|
|
{
|
|
|
|
logNetwork->info("Connection aborted by player!");
|
2018-01-05 19:21:07 +02:00
|
|
|
return;
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|
|
|
|
|
2023-12-26 19:27:47 +02:00
|
|
|
assert(isServerLocal());
|
2024-01-12 16:55:36 +02:00
|
|
|
networkHandler->connectToRemote(*this, getLocalHostname(), getLocalPort());
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|
|
|
|
|
2024-01-29 22:05:11 +02:00
|
|
|
void CServerHandler::onConnectionEstablished(const NetworkConnectionPtr & netConnection)
|
2023-11-18 16:34:18 +02:00
|
|
|
{
|
2024-02-04 19:56:04 +02:00
|
|
|
assert(getState() == EClientState::CONNECTING);
|
2024-02-03 22:24:32 +02:00
|
|
|
|
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
|
2024-01-12 16:55:36 +02:00
|
|
|
networkConnection = netConnection;
|
|
|
|
|
2023-12-25 21:23:27 +02:00
|
|
|
logNetwork->info("Connection established");
|
2024-01-29 22:05:11 +02:00
|
|
|
|
|
|
|
if (serverMode == EServerMode::LOBBY_GUEST)
|
|
|
|
{
|
|
|
|
// say hello to lobby to switch connection to proxy mode
|
|
|
|
getGlobalLobby().sendProxyConnectionLogin(netConnection);
|
|
|
|
}
|
|
|
|
|
2023-12-25 21:23:27 +02:00
|
|
|
c = std::make_shared<CConnection>(netConnection);
|
2023-12-26 19:50:44 +02:00
|
|
|
c->uuid = uuid;
|
2023-11-18 16:34:18 +02:00
|
|
|
c->enterLobbyConnectionMode();
|
|
|
|
sendClientConnecting();
|
|
|
|
}
|
|
|
|
|
2023-12-25 22:26:59 +02:00
|
|
|
void CServerHandler::applyPackOnLobbyScreen(CPackForLobby & pack)
|
2023-11-18 16:34:18 +02:00
|
|
|
{
|
2024-01-20 00:26:25 +02:00
|
|
|
const CBaseForLobbyApply * apply = applier->getApplier(CTypeList::getInstance().getTypeID(&pack)); //find the applier
|
2023-12-25 22:26:59 +02:00
|
|
|
apply->applyOnLobbyScreen(dynamic_cast<CLobbyScreen *>(SEL), this, pack);
|
|
|
|
GH.windows().totalRedraw();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::set<PlayerColor> CServerHandler::getHumanColors()
|
|
|
|
{
|
|
|
|
return clientHumanColors(c->connectionID);
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerColor CServerHandler::myFirstColor() const
|
|
|
|
{
|
|
|
|
return clientFirstColor(c->connectionID);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CServerHandler::isMyColor(PlayerColor color) const
|
|
|
|
{
|
|
|
|
return isClientColor(c->connectionID, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui8 CServerHandler::myFirstId() const
|
|
|
|
{
|
|
|
|
return clientFirstId(c->connectionID);
|
|
|
|
}
|
|
|
|
|
2024-02-04 19:56:04 +02:00
|
|
|
EClientState CServerHandler::getState() const
|
|
|
|
{
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::setState(EClientState newState)
|
|
|
|
{
|
|
|
|
state = newState;
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
bool CServerHandler::isServerLocal() const
|
|
|
|
{
|
2024-02-03 19:08:45 +02:00
|
|
|
return threadRunLocalServer.joinable();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CServerHandler::isHost() const
|
|
|
|
{
|
|
|
|
return c && hostClientId == c->connectionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CServerHandler::isGuest() const
|
|
|
|
{
|
|
|
|
return !c || hostClientId != c->connectionID;
|
|
|
|
}
|
|
|
|
|
2023-12-26 19:27:47 +02:00
|
|
|
const std::string & CServerHandler::getLocalHostname() const
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-12-26 19:27:47 +02:00
|
|
|
return settings["server"]["localHostname"].String();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-12-26 19:27:47 +02:00
|
|
|
ui16 CServerHandler::getLocalPort() const
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-12-26 19:27:47 +02:00
|
|
|
return settings["server"]["localPort"].Integer();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-12-26 19:27:47 +02:00
|
|
|
const std::string & CServerHandler::getRemoteHostname() const
|
2022-11-08 02:44:34 +02:00
|
|
|
{
|
2023-12-26 19:27:47 +02:00
|
|
|
return settings["server"]["remoteHostname"].String();
|
2022-11-08 02:44:34 +02:00
|
|
|
}
|
|
|
|
|
2023-12-26 19:27:47 +02:00
|
|
|
ui16 CServerHandler::getRemotePort() const
|
2022-11-08 02:44:34 +02:00
|
|
|
{
|
2023-12-26 19:27:47 +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
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CServerHandler::sendClientConnecting() const
|
|
|
|
{
|
|
|
|
LobbyClientConnected lcc;
|
|
|
|
lcc.uuid = uuid;
|
|
|
|
lcc.names = myNames;
|
|
|
|
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)
|
2024-02-03 19:08:45 +02:00
|
|
|
{
|
|
|
|
assert(0);
|
2018-01-05 19:21:07 +02:00
|
|
|
return;
|
2024-02-03 19:08:45 +02:00
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2024-02-04 19:56:04 +02:00
|
|
|
setState(EClientState::DISCONNECTING);
|
2023-08-11 18:04:14 +02:00
|
|
|
mapToStart = nullptr;
|
2018-01-05 19:21:07 +02:00
|
|
|
LobbyClientDisconnected lcd;
|
|
|
|
lcd.clientId = c->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();
|
2024-02-03 22:24:32 +02:00
|
|
|
networkConnection.reset();
|
2024-02-03 19:08:45 +02:00
|
|
|
c.reset();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-06-25 20:16:03 +02:00
|
|
|
void CServerHandler::setCampaignState(std::shared_ptr<CampaignState> newCampaign)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-02-04 19:56:04 +02:00
|
|
|
setState(EClientState::LOBBY_CAMPAIGN);
|
2018-01-05 19:21:07 +02:00
|
|
|
LobbySetCampaign lsc;
|
|
|
|
lsc.ourCampaign = newCampaign;
|
|
|
|
sendLobbyPack(lsc);
|
|
|
|
}
|
|
|
|
|
2023-06-25 20:16:03 +02:00
|
|
|
void CServerHandler::setCampaignMap(CampaignScenarioID mapId) const
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-02-04 19:56:04 +02:00
|
|
|
if(getState() == EClientState::GAMEPLAY) // FIXME: UI shouldn't sent commands in first place
|
2018-01-05 19:21:07 +02:00
|
|
|
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
|
2018-01-05 19:21:07 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-08-17 17:29:31 +02:00
|
|
|
void CServerHandler::setPlayerOption(ui8 what, int32_t value, PlayerColor player) const
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
LobbyChangePlayerOption lcpo;
|
|
|
|
lcpo.what = what;
|
2023-08-14 00:08:48 +02:00
|
|
|
lcpo.value = value;
|
2018-01-05 19:21:07 +02:00
|
|
|
lcpo.color = player;
|
|
|
|
sendLobbyPack(lcpo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::setDifficulty(int to) const
|
|
|
|
{
|
|
|
|
LobbySetDifficulty lsd;
|
|
|
|
lsd.difficulty = to;
|
|
|
|
sendLobbyPack(lsd);
|
|
|
|
}
|
|
|
|
|
2023-09-22 18:57:43 +02:00
|
|
|
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
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
LobbySetTurnTime lstt;
|
2023-08-25 18:20:26 +02:00
|
|
|
lstt.turnTimerInfo = info;
|
2018-01-05 19:21:07 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +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")
|
|
|
|
{
|
2024-01-10 00:38:54 +02:00
|
|
|
std::string connectedId;
|
|
|
|
std::string playerColorId;
|
2018-01-05 19:21:07 +02:00
|
|
|
readed >> connectedId;
|
|
|
|
readed >> playerColorId;
|
2022-11-13 04:35:16 +02:00
|
|
|
if(connectedId.length() && playerColorId.length())
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
LobbyChatMessage lcm;
|
|
|
|
lcm.message = txt;
|
|
|
|
lcm.playerName = playerNames.find(myFirstId())->second.name;
|
|
|
|
sendLobbyPack(lcm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::sendGuiAction(ui8 action) const
|
|
|
|
{
|
|
|
|
LobbyGuiAction lga;
|
|
|
|
lga.action = static_cast<LobbyGuiAction::EAction>(action);
|
|
|
|
sendLobbyPack(lga);
|
|
|
|
}
|
|
|
|
|
2022-09-29 19:08:05 +02:00
|
|
|
void CServerHandler::sendRestartGame() const
|
|
|
|
{
|
2023-09-26 15:45:46 +02:00
|
|
|
GH.windows().createAndPushWindow<CLoadingScreen>();
|
|
|
|
|
2024-02-03 19:08:45 +02:00
|
|
|
LobbyRestartGame endGame;
|
2022-09-29 19:08:05 +02:00
|
|
|
sendLobbyPack(endGame);
|
|
|
|
}
|
|
|
|
|
2023-09-11 18:39:32 +02:00
|
|
|
bool CServerHandler::validateGameStart(bool allowOnlyAI) const
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-08-06 16:46:49 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
verifyStateBeforeStart(allowOnlyAI ? true : settings["session"]["onlyai"].Bool());
|
|
|
|
}
|
2023-09-15 12:59:02 +02:00
|
|
|
catch(ModIncompatibility & e)
|
2023-08-06 16:46:49 +02:00
|
|
|
{
|
2023-09-11 18:39:32 +02:00
|
|
|
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();
|
|
|
|
}
|
2023-09-11 18:39:32 +02:00
|
|
|
showServerError(errorMsg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(std::exception & e)
|
|
|
|
{
|
|
|
|
logGlobal->error("Exception during startScenario: %s", e.what());
|
2023-08-06 16:46:49 +02:00
|
|
|
showServerError( std::string("Unable to start map! Reason: ") + e.what());
|
2023-09-11 18:39:32 +02:00
|
|
|
return false;
|
2023-08-06 16:46:49 +02:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:39:32 +02:00
|
|
|
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())
|
|
|
|
GH.windows().createAndPushWindow<CLoadingScreen>();
|
2023-09-21 22:28:29 +02:00
|
|
|
|
2024-02-03 19:57:23 +02:00
|
|
|
LobbyPrepareStartGame lpsg;
|
|
|
|
sendLobbyPack(lpsg);
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
LobbyStartGame lsg;
|
2021-01-20 20:29:50 +02:00
|
|
|
if(client)
|
|
|
|
{
|
|
|
|
lsg.initializedStartInfo = std::make_shared<StartInfo>(* const_cast<StartInfo *>(client->getStartInfo(true)));
|
2024-01-21 16:48:36 +02:00
|
|
|
lsg.initializedStartInfo->mode = EStartMode::NEW_GAME;
|
2021-01-20 20:29:50 +02:00
|
|
|
lsg.initializedStartInfo->seedToBeUsed = lsg.initializedStartInfo->seedPostInit = 0;
|
|
|
|
* si = * lsg.initializedStartInfo;
|
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
sendLobbyPack(lsg);
|
|
|
|
}
|
|
|
|
|
2023-08-11 18:04:14 +02:00
|
|
|
void CServerHandler::startMapAfterConnection(std::shared_ptr<CMapInfo> to)
|
|
|
|
{
|
|
|
|
mapToStart = to;
|
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void CServerHandler::startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2018-04-07 13:42:11 +02:00
|
|
|
if(CMM)
|
|
|
|
CMM->disable();
|
2024-01-10 19:43:34 +02:00
|
|
|
|
2023-09-23 20:41:30 +02:00
|
|
|
highScoreCalc = nullptr;
|
2023-09-23 14:51:39 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
switch(si->mode)
|
|
|
|
{
|
2024-01-21 16:48:36 +02:00
|
|
|
case EStartMode::NEW_GAME:
|
2022-09-28 21:15:05 +02:00
|
|
|
client->newGame(gameState);
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
2024-01-21 16:48:36 +02:00
|
|
|
case EStartMode::CAMPAIGN:
|
2022-09-28 21:15:05 +02:00
|
|
|
client->newGame(gameState);
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
2024-01-21 16:48:36 +02:00
|
|
|
case EStartMode::LOAD_GAME:
|
2022-09-29 19:33:44 +02:00
|
|
|
client->loadGame(gameState);
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("Invalid mode");
|
|
|
|
}
|
|
|
|
// After everything initialized we can accept CPackToClient netpacks
|
|
|
|
c->enterGameplayConnectionMode(client->gameState());
|
2024-02-04 19:56:04 +02:00
|
|
|
setState(EClientState::GAMEPLAY);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:08:45 +02:00
|
|
|
void CServerHandler::endGameplay()
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-02-03 19:08:45 +02:00
|
|
|
// Game is ending
|
|
|
|
// Tell the network thread to reach a stable state
|
|
|
|
CSH->sendClientDisconnecting();
|
|
|
|
logNetwork->info("Closed connection.");
|
2023-11-27 19:57:48 +02:00
|
|
|
|
|
|
|
client->endGame();
|
2024-01-10 19:43:34 +02:00
|
|
|
client.reset();
|
2023-11-27 19:57:48 +02:00
|
|
|
|
2024-02-03 19:08:45 +02:00
|
|
|
if(CMM)
|
2018-04-07 13:42:11 +02:00
|
|
|
{
|
2024-02-03 19:08:45 +02:00
|
|
|
GH.curInt = CMM.get();
|
|
|
|
CMM->enable();
|
2018-04-07 13:42:11 +02:00
|
|
|
}
|
2024-02-03 19:08:45 +02:00
|
|
|
else
|
2023-10-05 23:34:29 +02:00
|
|
|
{
|
2024-02-03 19:08:45 +02:00
|
|
|
GH.curInt = CMainMenu::create().get();
|
2023-10-05 23:34:29 +02:00
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2024-02-03 19:08:45 +02:00
|
|
|
void CServerHandler::restartGameplay()
|
|
|
|
{
|
|
|
|
client->endGame();
|
|
|
|
client.reset();
|
|
|
|
|
|
|
|
c->enterLobbyConnectionMode();
|
|
|
|
}
|
|
|
|
|
2023-09-23 00:21:36 +02:00
|
|
|
void CServerHandler::startCampaignScenario(HighScoreParameter param, std::shared_ptr<CampaignState> cs)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-06-26 20:51:10 +02:00
|
|
|
std::shared_ptr<CampaignState> ourCampaign = cs;
|
|
|
|
|
|
|
|
if (!cs)
|
|
|
|
ourCampaign = si->campState;
|
|
|
|
|
2023-09-23 20:41:30 +02:00
|
|
|
if(highScoreCalc == nullptr)
|
2023-09-23 14:51:39 +02:00
|
|
|
{
|
2023-09-23 20:41:30 +02:00
|
|
|
highScoreCalc = std::make_shared<HighScoreCalculation>();
|
|
|
|
highScoreCalc->isCampaign = true;
|
|
|
|
highScoreCalc->parameters.clear();
|
2023-09-23 00:21:36 +02:00
|
|
|
}
|
2023-09-27 22:53:13 +02:00
|
|
|
param.campaignName = cs->getNameTranslated();
|
2023-09-23 20:41:30 +02:00
|
|
|
highScoreCalc->parameters.push_back(param);
|
2023-06-26 20:51:10 +02:00
|
|
|
|
2023-09-23 00:21:36 +02:00
|
|
|
GH.dispatchMainThread([ourCampaign, this]()
|
2023-06-26 20:51:10 +02:00
|
|
|
{
|
|
|
|
CSH->endGameplay();
|
|
|
|
|
|
|
|
auto & epilogue = ourCampaign->scenario(*ourCampaign->lastScenario()).epilog;
|
|
|
|
auto finisher = [=]()
|
|
|
|
{
|
2024-01-07 15:36:07 +02:00
|
|
|
if(ourCampaign->campaignSet != "" && ourCampaign->isCampaignFinished())
|
2023-09-20 22:28:45 +02:00
|
|
|
{
|
2023-09-21 21:27:06 +02:00
|
|
|
Settings entry = persistentStorage.write["completedCampaigns"][ourCampaign->getFilename()];
|
2023-09-20 22:28:45 +02:00
|
|
|
entry->Bool() = true;
|
|
|
|
}
|
2023-09-20 22:18:53 +02:00
|
|
|
|
2023-09-21 23:41:00 +02:00
|
|
|
GH.windows().pushWindow(CMM);
|
|
|
|
GH.windows().pushWindow(CMM->menu);
|
|
|
|
|
2023-06-26 20:51:10 +02:00
|
|
|
if(!ourCampaign->isCampaignFinished())
|
|
|
|
CMM->openCampaignLobby(ourCampaign);
|
2023-09-20 03:13:54 +02:00
|
|
|
else
|
2023-09-22 20:39:20 +02:00
|
|
|
{
|
2023-09-20 03:13:54 +02:00
|
|
|
CMM->openCampaignScreen(ourCampaign->campaignSet);
|
2023-09-23 20:41:30 +02:00
|
|
|
GH.windows().createAndPushWindow<CHighScoreInputScreen>(true, *highScoreCalc);
|
2023-09-22 20:39:20 +02:00
|
|
|
}
|
2023-06-26 20:51:10 +02:00
|
|
|
};
|
2024-02-03 19:08:45 +02:00
|
|
|
|
|
|
|
threadRunLocalServer.join();
|
2023-06-26 20:51:10 +02:00
|
|
|
if(epilogue.hasPrologEpilog)
|
|
|
|
{
|
|
|
|
GH.windows().createAndPushWindow<CPrologEpilogVideo>(epilogue, finisher);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
finisher();
|
|
|
|
}
|
|
|
|
});
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:39:32 +02:00
|
|
|
void CServerHandler::showServerError(const std::string & txt) const
|
2022-09-23 13:02:19 +02:00
|
|
|
{
|
2023-09-21 04:31:08 +02:00
|
|
|
if(auto w = GH.windows().topWindow<CLoadingScreen>())
|
|
|
|
GH.windows().popWindow(w);
|
|
|
|
|
2022-09-23 13:02:19 +02:00
|
|
|
CInfoWindow::showInfoDialog(txt, {});
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
int CServerHandler::howManyPlayerInterfaces()
|
|
|
|
{
|
|
|
|
int playerInts = 0;
|
|
|
|
for(auto pint : client->playerint)
|
|
|
|
{
|
|
|
|
if(dynamic_cast<CPlayerInterface *>(pint.second.get()))
|
|
|
|
playerInts++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return playerInts;
|
|
|
|
}
|
|
|
|
|
2024-01-21 16:48:36 +02:00
|
|
|
ELoadMode CServerHandler::getLoadMode()
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-02-04 19:56:04 +02:00
|
|
|
if(loadMode != ELoadMode::TUTORIAL && getState() == EClientState::GAMEPLAY)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(si->campState)
|
|
|
|
return ELoadMode::CAMPAIGN;
|
|
|
|
for(auto pn : playerNames)
|
|
|
|
{
|
|
|
|
if(pn.second.connection != c->connectionID)
|
|
|
|
return ELoadMode::MULTI;
|
|
|
|
}
|
2019-05-04 15:56:17 +02:00
|
|
|
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;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2024-01-29 22:05:11 +02:00
|
|
|
resetStateForLobby(EStartMode::LOAD_GAME, ESelectionScreen::loadGame, EServerMode::LOCAL, {});
|
2023-08-23 14:07:50 +02:00
|
|
|
mapInfo->saveInit(ResourcePath(filename, EResType::SAVEGAME));
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-29 22:05:11 +02:00
|
|
|
resetStateForLobby(EStartMode::NEW_GAME, ESelectionScreen::newGame, EServerMode::LOCAL, {});
|
2018-01-05 19:21:07 +02:00
|
|
|
mapInfo->mapInit(filename);
|
|
|
|
}
|
|
|
|
if(settings["session"]["donotstartserver"].Bool())
|
2023-12-26 19:27:47 +02:00
|
|
|
connectToServer(getLocalHostname(), getLocalPort());
|
2018-01-05 19:21:07 +02:00
|
|
|
else
|
2024-01-10 15:31:11 +02:00
|
|
|
startLocalServerAndConnect(false);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-08-20 22:45:41 +02:00
|
|
|
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>())
|
2023-08-20 22:45:41 +02:00
|
|
|
boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
|
2023-05-16 17:34:23 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
while(!mi || mapInfo->fileURI != CSH->mi->fileURI)
|
|
|
|
{
|
|
|
|
setMapInfo(mapInfo);
|
2023-08-20 22:45:41 +02:00
|
|
|
boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
// "Click" on color to remove us from it
|
|
|
|
setPlayer(myFirstColor());
|
|
|
|
while(myFirstColor() != PlayerColor::CANNOT_DETERMINE)
|
2023-08-20 22:45:41 +02:00
|
|
|
boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sendStartGame();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2023-08-20 22:45:41 +02:00
|
|
|
boost::this_thread::sleep_for(boost::chrono::milliseconds(50));
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
class ServerHandlerCPackVisitor : public VCMI_LIB_WRAP_NAMESPACE(ICPackVisitor)
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
CServerHandler & handler;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ServerHandlerCPackVisitor(CServerHandler & handler)
|
|
|
|
:handler(handler)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-02-10 21:22:08 +02:00
|
|
|
bool callTyped() override { return false; }
|
2023-02-12 09:23:39 +02:00
|
|
|
|
2024-02-10 21:22:08 +02:00
|
|
|
void visitForLobby(CPackForLobby & lobbyPack) override
|
2023-02-12 09:23:39 +02:00
|
|
|
{
|
|
|
|
handler.visitForLobby(lobbyPack);
|
|
|
|
}
|
|
|
|
|
2024-02-10 21:22:08 +02:00
|
|
|
void visitForClient(CPackForClient & clientPack) override
|
2023-02-12 09:23:39 +02:00
|
|
|
{
|
|
|
|
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)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-02-03 22:24:32 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
|
2024-02-04 19:56:04 +02:00
|
|
|
if(getState() == EClientState::DISCONNECTING)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2024-02-03 19:08:45 +02:00
|
|
|
assert(0); //Should not be possible - socket must be closed at this point
|
|
|
|
return;
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|
2024-02-03 19:08:45 +02:00
|
|
|
|
|
|
|
CPack * pack = c->retrievePack(message);
|
|
|
|
ServerHandlerCPackVisitor visitor(*this);
|
|
|
|
pack->visit(visitor);
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2024-02-02 01:27:19 +02:00
|
|
|
void CServerHandler::onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage)
|
2023-11-18 16:34:18 +02:00
|
|
|
{
|
2024-02-04 19:56:04 +02:00
|
|
|
if(getState() == EClientState::DISCONNECTING)
|
2023-11-18 16:34:18 +02:00
|
|
|
{
|
2024-02-03 22:24:32 +02:00
|
|
|
assert(networkConnection == nullptr);
|
|
|
|
// Note: this branch can be reached on app shutdown, when main thread holds mutex till destruction
|
|
|
|
logNetwork->info("Successfully closed connection to server!");
|
|
|
|
return;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2023-11-18 16:34:18 +02:00
|
|
|
|
2024-02-03 22:24:32 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2023-11-18 16:34:18 +02:00
|
|
|
|
2024-02-03 22:24:32 +02:00
|
|
|
logNetwork->error("Lost connection to server! Connection has been closed");
|
|
|
|
|
|
|
|
if(client)
|
|
|
|
{
|
2024-02-04 19:56:04 +02:00
|
|
|
CSH->endGameplay();
|
|
|
|
GH.defActionsDef = 63;
|
|
|
|
CMM->menu->switchToTab("main");
|
|
|
|
CSH->showServerError(CGI->generaltexth->translate("vcmi.server.errors.disconnected"));
|
2024-02-03 22:24:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LobbyClientDisconnected lcd;
|
|
|
|
lcd.clientId = c->connectionID;
|
|
|
|
applyPackOnLobbyScreen(lcd);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2024-02-04 19:56:04 +02:00
|
|
|
|
|
|
|
networkConnection.reset();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-02-12 09:23:39 +02:00
|
|
|
void CServerHandler::visitForLobby(CPackForLobby & lobbyPack)
|
|
|
|
{
|
2023-12-25 22:26:59 +02:00
|
|
|
if(applier->getApplier(CTypeList::getInstance().getTypeID(&lobbyPack))->applyOnLobbyHandler(this, lobbyPack))
|
2023-02-12 09:23:39 +02:00
|
|
|
{
|
|
|
|
if(!settings["session"]["headless"].Bool())
|
2023-12-25 22:26:59 +02:00
|
|
|
applyPackOnLobbyScreen(lobbyPack);
|
2023-02-12 09:23:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServerHandler::visitForClient(CPackForClient & clientPack)
|
|
|
|
{
|
|
|
|
client->handlePack(&clientPack);
|
|
|
|
}
|
|
|
|
|
2024-01-10 15:31:11 +02:00
|
|
|
void CServerHandler::threadRunServer(bool connectToLobby)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-02-27 12:00:13 +02:00
|
|
|
#if !defined(VCMI_MOBILE)
|
2023-08-20 23:55:11 +02:00
|
|
|
setThreadName("runServer");
|
2022-09-17 15:56:01 +02:00
|
|
|
const std::string logName = (VCMIDirs::get().userLogsPath() / "server_log.txt").string();
|
2018-01-05 19:21:07 +02:00
|
|
|
std::string comm = VCMIDirs::get().serverPath().string()
|
2023-12-26 19:27:47 +02:00
|
|
|
+ " --port=" + std::to_string(getLocalPort())
|
2024-01-10 15:31:11 +02:00
|
|
|
+ " --run-by-client";
|
|
|
|
if(connectToLobby)
|
|
|
|
comm += " --lobby";
|
2023-07-27 21:50:50 +02:00
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
comm += " > \"" + logName + '\"';
|
2023-09-23 14:51:39 +02:00
|
|
|
logGlobal->info("Server command line: %s", comm);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2022-09-10 15:03:54 +02:00
|
|
|
#ifdef VCMI_WINDOWS
|
|
|
|
int result = -1;
|
|
|
|
const auto bufSize = ::MultiByteToWideChar(CP_UTF8, 0, comm.c_str(), comm.size(), nullptr, 0);
|
|
|
|
if(bufSize > 0)
|
|
|
|
{
|
|
|
|
std::wstring wComm(bufSize, {});
|
|
|
|
const auto convertResult = ::MultiByteToWideChar(CP_UTF8, 0, comm.c_str(), comm.size(), &wComm[0], bufSize);
|
|
|
|
if(convertResult > 0)
|
|
|
|
result = ::_wsystem(wComm.c_str());
|
|
|
|
else
|
|
|
|
logNetwork->error("Error " + std::to_string(GetLastError()) + ": failed to convert server launch command to wide string: " + comm);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
logNetwork->error("Error " + std::to_string(GetLastError()) + ": failed to obtain buffer length to convert server launch command to wide string : " + comm);
|
|
|
|
#else
|
2018-01-05 19:21:07 +02:00
|
|
|
int result = std::system(comm.c_str());
|
2022-09-10 15:03:54 +02:00
|
|
|
#endif
|
2018-01-05 19:21:07 +02:00
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
logNetwork->info("Server closed correctly");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-04 19:56:04 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
|
|
|
|
if (getState() == EClientState::CONNECTING)
|
2023-12-26 21:13:46 +02:00
|
|
|
{
|
2024-02-04 19:56:04 +02:00
|
|
|
showServerError(CGI->generaltexth->translate("vcmi.server.errors.existingProcess"));
|
|
|
|
setState(EClientState::CONNECTION_CANCELLED); // stop attempts to reconnect
|
2023-12-26 21:13:46 +02:00
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
logNetwork->error("Error: server failed to close correctly or crashed!");
|
|
|
|
logNetwork->error("Check %s for more info", logName);
|
|
|
|
}
|
2022-09-16 15:43:21 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CServerHandler::sendLobbyPack(const CPackForLobby & pack) const
|
|
|
|
{
|
2024-02-04 19:56:04 +02:00
|
|
|
if(getState() != EClientState::STARTING)
|
2018-01-05 19:21:07 +02:00
|
|
|
c->sendPack(&pack);
|
|
|
|
}
|
2024-02-10 19:02:25 +02:00
|
|
|
|
|
|
|
bool CServerHandler::inLobbyRoom() const
|
|
|
|
{
|
|
|
|
return CSH->serverMode == EServerMode::LOBBY_HOST || CSH->serverMode == EServerMode::LOBBY_GUEST;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CServerHandler::inGame() const
|
|
|
|
{
|
|
|
|
return c != nullptr;
|
|
|
|
}
|