Skip to content

Commit

Permalink
Change: Use minutes instead of dates for automatic server restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
2TallTyler committed Jul 16, 2023
1 parent a54c28f commit 5d143a2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 30 deletions.
59 changes: 35 additions & 24 deletions src/network/network_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "../rev.h"
#include "../timer/timer.h"
#include "../timer/timer_game_calendar.h"
#include "../timer/timer_game_realtime.h"
#include <mutex>
#include <condition_variable>

Expand Down Expand Up @@ -1490,29 +1491,6 @@ void NetworkUpdateClientInfo(ClientID client_id)
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)
Expand Down Expand Up @@ -1813,12 +1791,45 @@ void NetworkServer_Tick(bool send_frame)
}
}

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

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

Debug(net, 3, "Auto-restarting map: {} minutes played", _settings_client.network.restart_minutes);

_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;
}
});

/** Reset the automatic network restart time interval. */
void ChangeNetworkRestartTime()
{
if (!_network_server) return;

_network_restart_map.SetInterval({ std::chrono::minutes(_settings_client.network.restart_minutes), TimerGameRealtime::UNPAUSED }, true);
}

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

NetworkCheckRestartMap();
NetworkAdminUpdate(ADMIN_FREQUENCY_ANUALLY);
});

Expand Down
1 change: 1 addition & 0 deletions src/network/network_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class ServerNetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<
};

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

Expand Down
7 changes: 6 additions & 1 deletion src/openttd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#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"
Expand Down Expand Up @@ -893,7 +894,11 @@ static void MakeNewGameDone()
CheckIndustries();
MarkWholeScreenDirty();

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

if (!_network_dedicated) ShowClientList();
}
}

static void MakeNewGame(bool from_heightmap, bool reset_settings)
Expand Down
2 changes: 1 addition & 1 deletion src/settings_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ struct NetworkSettings {
uint8 autoclean_novehicles; ///< remove companies with no vehicles after this many months
uint8 max_companies; ///< maximum amount of companies
uint8 max_clients; ///< maximum amount of clients
TimerGameCalendar::Year restart_game_year; ///< year the server restarts
uint32 restart_minutes; ///< number of minutes to run the server before automatic restart
uint8 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
Expand Down
10 changes: 6 additions & 4 deletions src/table/settings/network_settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

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

Expand Down Expand Up @@ -233,13 +234,14 @@ post_cb = [](auto) { UpdateClientConfigValues(); }
cat = SC_BASIC

[SDTC_VAR]
var = network.restart_game_year
type = SLE_INT32
var = network.restart_minutes
type = SLE_UINT32
flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC | SF_GUI_0_IS_SPECIAL | SF_NETWORK_ONLY
def = 0
min = MIN_YEAR
max = MAX_YEAR
min = 0
max = UINT32_MAX
interval = 1
post_cb = [](auto) {ChangeNetworkRestartTime(); }

[SDTC_VAR]
var = network.min_active_clients
Expand Down

0 comments on commit 5d143a2

Please sign in to comment.