Changeset - r28569:181c923a13a0
[Not reviewed]
master
0 5 0
Tyler Trahan - 4 months ago 2024-01-26 15:25:25
tyler@tylertrahan.com
Feature: Setting to automatically restart server based on hours played (#11142)
5 files changed with 75 insertions and 25 deletions:
0 comments (0 inline, 0 general)
src/network/network_server.cpp
Show inline comments
 
@@ -30,12 +30,13 @@
 
#include "../core/random_func.hpp"
 
#include "../company_cmd.h"
 
#include "../rev.h"
 
#include "../timer/timer.h"
 
#include "../timer/timer_game_calendar.h"
 
#include "../timer/timer_game_economy.h"
 
#include "../timer/timer_game_realtime.h"
 
#include <mutex>
 
#include <condition_variable>
 

	
 
#include "../safeguards.h"
 

	
 

	
 
@@ -1488,35 +1489,12 @@ void NetworkUpdateClientInfo(ClientID cl
 
		}
 
	}
 

	
 
	NetworkAdminClientUpdate(ci);
 
}
 

	
 
/** Check if we want to restart the map */
 
static void NetworkCheckRestartMap()
 
{
 
	if (_settings_client.network.restart_game_year != 0 && TimerGameCalendar::year >= _settings_client.network.restart_game_year) {
 
		Debug(net, 3, "Auto-restarting map: year {} reached", TimerGameCalendar::year);
 

	
 
		_settings_newgame.game_creation.generation_seed = GENERATE_NEW_SEED;
 
		switch(_file_to_saveload.abstract_ftype) {
 
			case FT_SAVEGAME:
 
			case FT_SCENARIO:
 
				_switch_mode = SM_LOAD_GAME;
 
				break;
 

	
 
			case FT_HEIGHTMAP:
 
				_switch_mode = SM_START_HEIGHTMAP;
 
				break;
 

	
 
			default:
 
				_switch_mode = SM_NEWGAME;
 
		}
 
	}
 
}
 

	
 
/** Check if the server has autoclean_companies activated
 
 * Two things happen:
 
 *     1) If a company is not protected, it is closed after 1 year (for example)
 
 *     2) If a company is protected, protection is disabled after 3 years (for example)
 
 *          (and item 1. happens a year later)
 
 */
 
@@ -1808,17 +1786,71 @@ void NetworkServer_Tick(bool send_frame)
 
			if (send_sync) cs->SendSync();
 
#endif
 
		}
 
	}
 
}
 

	
 
/** Helper function to restart the map. */
 
static void NetworkRestartMap()
 
{
 
	_settings_newgame.game_creation.generation_seed = GENERATE_NEW_SEED;
 
	switch (_file_to_saveload.abstract_ftype) {
 
		case FT_SAVEGAME:
 
		case FT_SCENARIO:
 
			_switch_mode = SM_LOAD_GAME;
 
			break;
 

	
 
		case FT_HEIGHTMAP:
 
			_switch_mode = SM_START_HEIGHTMAP;
 
			break;
 

	
 
		default:
 
			_switch_mode = SM_NEWGAME;
 
	}
 
}
 

	
 
/** Timer to restart a network server automatically based on real-time hours played. Initialized at zero to disable until settings are loaded. */
 
static IntervalTimer<TimerGameRealtime> _network_restart_map_timer({std::chrono::hours::zero(), TimerGameRealtime::UNPAUSED}, [](auto)
 
{
 
	if (!_network_server) return;
 

	
 
	/* If setting is 0, this feature is disabled. */
 
	if (_settings_client.network.restart_hours == 0) return;
 

	
 
	Debug(net, 3, "Auto-restarting map: {} hours played", _settings_client.network.restart_hours);
 
	NetworkRestartMap();
 
});
 

	
 
/**
 
 * Reset the automatic network restart time interval.
 
 * @param reset Whether to reset the timer to zero.
 
 */
 
void ChangeNetworkRestartTime(bool reset)
 
{
 
	if (!_network_server) return;
 

	
 
	_network_restart_map_timer.SetInterval({ std::chrono::hours(_settings_client.network.restart_hours), TimerGameRealtime::UNPAUSED }, reset);
 
}
 

	
 
/** Check if we want to restart the map based on the year. */
 
static void NetworkCheckRestartMapYear()
 
{
 
	/* If setting is 0, this feature is disabled. */
 
	if (_settings_client.network.restart_game_year == 0) return;
 

	
 
	if (TimerGameCalendar::year >= _settings_client.network.restart_game_year) {
 
		Debug(net, 3, "Auto-restarting map: year {} reached", TimerGameCalendar::year);
 
		NetworkRestartMap();
 
	}
 
}
 

	
 
/** Calendar yearly "callback". Called whenever the calendar year changes. */
 
static IntervalTimer<TimerGameCalendar> _calendar_network_yearly({ TimerGameCalendar::YEAR, TimerGameCalendar::Priority::NONE }, [](auto) {
 
	if (!_network_server) return;
 

	
 
	NetworkCheckRestartMap();
 
	NetworkCheckRestartMapYear();
 
});
 

	
 
/** Economy yearly "callback". Called whenever the economy year changes. */
 
static IntervalTimer<TimerGameEconomy> _economy_network_yearly({TimerGameEconomy::YEAR, TimerGameEconomy::Priority::NONE}, [](auto)
 
{
 
	if (!_network_server) return;
src/network/network_server.h
Show inline comments
 
@@ -117,10 +117,11 @@ public:
 
	const std::string &GetClientIP();
 

	
 
	static ServerNetworkGameSocketHandler *GetByClientID(ClientID client_id);
 
};
 

	
 
void NetworkServer_Tick(bool send_frame);
 
void ChangeNetworkRestartTime(bool reset);
 
void NetworkServerSetCompanyPassword(CompanyID company_id, const std::string &password, bool already_hashed = true);
 
void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded);
 

	
 
#endif /* NETWORK_SERVER_H */
src/openttd.cpp
Show inline comments
 
@@ -31,12 +31,13 @@
 
#include "roadveh.h"
 
#include "train.h"
 
#include "ship.h"
 
#include "console_func.h"
 
#include "screenshot.h"
 
#include "network/network.h"
 
#include "network/network_server.h"
 
#include "network/network_func.h"
 
#include "ai/ai.hpp"
 
#include "ai/ai_config.hpp"
 
#include "settings_func.h"
 
#include "genworld.h"
 
#include "progress.h"
 
@@ -914,13 +915,17 @@ static void MakeNewGameDone()
 
	if (_settings_client.gui.pause_on_newgame) Command<CMD_PAUSE>::Post(PM_PAUSED_NORMAL, true);
 

	
 
	CheckEngines();
 
	CheckIndustries();
 
	MarkWholeScreenDirty();
 

	
 
	if (_network_server && !_network_dedicated) ShowClientList();
 
	if (_network_server) {
 
		ChangeNetworkRestartTime(true);
 

	
 
		if (!_network_dedicated) ShowClientList();
 
	}
 
}
 

	
 
static void MakeNewGame(bool from_heightmap, bool reset_settings)
 
{
 
	_game_mode = GM_NORMAL;
 
	if (!from_heightmap) {
src/settings_type.h
Show inline comments
 
@@ -322,12 +322,13 @@ struct NetworkSettings {
 
	uint8_t       autoclean_unprotected;                    ///< remove passwordless companies after this many months
 
	uint8_t       autoclean_protected;                      ///< remove the password from passworded companies after this many months
 
	uint8_t       autoclean_novehicles;                     ///< remove companies with no vehicles after this many months
 
	uint8_t       max_companies;                            ///< maximum amount of companies
 
	uint8_t       max_clients;                              ///< maximum amount of clients
 
	TimerGameCalendar::Year restart_game_year;            ///< year the server restarts
 
	uint16_t      restart_hours;                          ///< number of hours to run the server before automatic restart
 
	uint8_t       min_active_clients;                       ///< minimum amount of active clients to unpause the game
 
	bool        reload_cfg;                               ///< reload the config file before restarting
 
	std::string last_joined;                              ///< Last joined server
 
	bool        no_http_content_downloads;                ///< do not do content downloads over HTTP
 
	UseRelayService use_relay_service;                    ///< Use relay service?
 
	ParticipateSurvey participate_survey;                 ///< Participate in the automated survey
src/table/settings/network_settings.ini
Show inline comments
 
@@ -5,12 +5,13 @@
 
;
 

	
 
; Network settings as stored in the main configuration file ("openttd.cfg").
 

	
 
[pre-amble]
 
static void UpdateClientConfigValues();
 
void ChangeNetworkRestartTime(bool reset);
 

	
 
static constexpr std::initializer_list<const char*> _server_game_type{"local", "public", "invite-only"};
 

	
 
static const SettingVariant _network_settings_table[] = {
 
[post-amble]
 
};
 
@@ -239,12 +240,22 @@ flags    = SF_NOT_IN_SAVE | SF_NO_NETWOR
 
def      = 0
 
min      = CalendarTime::MIN_YEAR
 
max      = CalendarTime::MAX_YEAR
 
interval = 1
 

	
 
[SDTC_VAR]
 
var      = network.restart_hours
 
type     = SLE_UINT16
 
flags    = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC | SF_GUI_0_IS_SPECIAL | SF_NETWORK_ONLY
 
def      = 0
 
min      = 0
 
max      = UINT16_MAX
 
interval = 1
 
post_cb  = [](auto) { ChangeNetworkRestartTime(false); }
 

	
 
[SDTC_VAR]
 
var      = network.min_active_clients
 
type     = SLE_UINT8
 
flags    = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC | SF_NETWORK_ONLY
 
def      = 0
 
min      = 0
 
max      = MAX_CLIENTS
0 comments (0 inline, 0 general)