瀏覽代碼

Store and load last difficulty setting

Tomasz Zieliński 1 年之前
父節點
當前提交
5f95955535
共有 5 個文件被更改,包括 35 次插入6 次删除
  1. 15 1
      client/CServerHandler.cpp
  2. 7 2
      client/ServerRunner.cpp
  3. 4 3
      client/ServerRunner.h
  4. 4 0
      client/lobby/CLobbyScreen.cpp
  5. 5 0
      config/schemas/settings.json

+ 15 - 1
client/CServerHandler.cpp

@@ -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");

+ 7 - 2
client/ServerRunner.cpp

@@ -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";

+ 4 - 3
client/ServerRunner.h

@@ -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;

+ 4 - 0
client/lobby/CLobbyScreen.cpp

@@ -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);

+ 5 - 0
config/schemas/settings.json

@@ -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