1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Store and load last difficulty setting

This commit is contained in:
Tomasz Zieliński 2024-03-01 10:57:48 +01:00
parent c87b0e6d65
commit 5f95955535
5 changed files with 35 additions and 6 deletions

View File

@ -140,6 +140,12 @@ CServerHandler::CServerHandler()
{
uuid = boost::uuids::to_string(boost::uuids::random_generator()());
registerTypesLobbyPacks(*applier);
auto lastDifficulty = settings["general"]["lastDifficulty"];
if (lastDifficulty.isNumber())
{
si->difficulty = lastDifficulty.Integer();
}
}
void CServerHandler::threadRunNetwork()
@ -193,8 +199,16 @@ void CServerHandler::startLocalServerAndConnect(bool connectToLobby)
serverRunner.reset(new ServerThreadRunner());
#endif
auto si = std::make_shared<StartInfo>();
auto lastDifficulty = settings["general"]["lastDifficulty"];
if (lastDifficulty.isNumber())
{
si->difficulty = lastDifficulty.Integer();
}
logNetwork->trace("\tStarting local server");
serverRunner->start(getLocalPort(), connectToLobby);
serverRunner->start(getLocalPort(), connectToLobby, si);
logNetwork->trace("\tConnecting to local server");
connectToServer(getLocalHostname(), getLocalPort());
logNetwork->trace("\tWaiting for connection");

View File

@ -23,10 +23,15 @@
ServerThreadRunner::ServerThreadRunner() = default;
ServerThreadRunner::~ServerThreadRunner() = default;
void ServerThreadRunner::start(uint16_t port, bool connectToLobby)
void ServerThreadRunner::start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
{
server = std::make_unique<CVCMIServer>(port, connectToLobby, true);
if (startingInfo)
{
server->si = startingInfo; //Else use default
}
threadRunLocalServer = boost::thread([this]{
setThreadName("runServer");
server->run();
@ -68,7 +73,7 @@ int ServerProcessRunner::exitCode()
return child->exit_code();
}
void ServerProcessRunner::start(uint16_t port, bool connectToLobby)
void ServerProcessRunner::start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
{
boost::filesystem::path serverPath = VCMIDirs::get().serverPath();
boost::filesystem::path logPath = VCMIDirs::get().userLogsPath() / "server_log.txt";

View File

@ -10,11 +10,12 @@
#pragma once
class CVCMIServer;
struct StartInfo;
class IServerRunner
{
public:
virtual void start(uint16_t port, bool connectToLobby) = 0;
virtual void start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) = 0;
virtual void shutdown() = 0;
virtual void wait() = 0;
virtual int exitCode() = 0;
@ -28,7 +29,7 @@ class ServerThreadRunner : public IServerRunner, boost::noncopyable
std::unique_ptr<CVCMIServer> server;
boost::thread threadRunLocalServer;
public:
void start(uint16_t port, bool connectToLobby) override;
void start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) override;
void shutdown() override;
void wait() override;
int exitCode() override;
@ -50,7 +51,7 @@ class ServerProcessRunner : public IServerRunner, boost::noncopyable
std::unique_ptr<boost::process::child> child;
public:
void start(uint16_t port, bool connectToLobby) override;
void start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) override;
void shutdown() override;
void wait() override;
int exitCode() override;

View File

@ -162,6 +162,10 @@ void CLobbyScreen::startScenario(bool allowOnlyAI)
tabRand->saveOptions(*CSH->si->mapGenOptions);
}
// Save chosen difficulty
Settings lastDifficulty = settings.write["general"]["lastDifficulty"];
lastDifficulty->Integer() = getCurrentDifficulty();
if (CSH->validateGameStart(allowOnlyAI))
{
CSH->sendStartGame(allowOnlyAI);

View File

@ -28,6 +28,7 @@
"lastSave",
"lastSettingsTab",
"lastCampaign",
"lastDifficulty",
"saveFrequency",
"notifications",
"extraDump",
@ -85,6 +86,10 @@
"type" : "string",
"default" : ""
},
"lastDifficulty" : {
"type" : "number",
"default" : 1
},
"saveFrequency" : {
"type" : "number",
"default" : 1