summaryrefslogtreecommitdiff
path: root/network_server.c
diff options
context:
space:
mode:
authortruelight <truelight@openttd.org>2004-12-16 13:59:23 +0000
committertruelight <truelight@openttd.org>2004-12-16 13:59:23 +0000
commit5e28fb8f97f7f57be4f62676fd96bd7b97103419 (patch)
tree4e45f952d8f16c9ddc3ac358df5ad8668d9a599a /network_server.c
parent89fd12692f1999b1535d70589bab7366a9eee0f3 (diff)
downloadopenttd-5e28fb8f97f7f57be4f62676fd96bd7b97103419.tar.xz
(svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
When enabled, empty companies (companies with no active clients) with no password are declared bankrupt after 1 year of emptyness. For empty companies with password, the password is removed after 3 years of emptyness. The delay of removing company/password can be configured via: - 'set autoclean_protected <months>' - 'set autoclean_unprotected <months>'
Diffstat (limited to 'network_server.c')
-rw-r--r--network_server.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/network_server.c b/network_server.c
index c6f655d7b..26105940e 100644
--- a/network_server.c
+++ b/network_server.c
@@ -629,6 +629,10 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
}
}
+
+ /* Make sure companies to who people try to join are not autocleaned */
+ if (playas >= 1 && playas <= MAX_PLAYERS)
+ _network_player_info[playas-1].months_empty = 0;
}
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD)
@@ -1121,6 +1125,7 @@ void NetworkPopulateCompanyInfo(void)
ClientState *cs;
NetworkClientInfo *ci;
int i;
+ uint16 months_empty;
FOR_ALL_PLAYERS(p) {
if (!p->is_active) {
@@ -1130,7 +1135,9 @@ void NetworkPopulateCompanyInfo(void)
// Clean the info but not the password
ttd_strlcpy(password, _network_player_info[p->index].password, sizeof(password));
+ months_empty = _network_player_info[p->index].months_empty;
memset(&_network_player_info[p->index], 0, sizeof(NetworkPlayerInfo));
+ _network_player_info[p->index].months_empty = months_empty;
ttd_strlcpy(_network_player_info[p->index].password, password, sizeof(_network_player_info[p->index].password));
// Grap the company name
@@ -1228,6 +1235,67 @@ void NetworkUpdateClientInfo(uint16 client_index)
}
}
+/* 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) */
+static void NetworkAutoCleanCompanies()
+{
+ ClientState *cs;
+ NetworkClientInfo *ci;
+ Player *p;
+ bool clients_in_company[MAX_PLAYERS];
+
+ if (!_network_autoclean_companies)
+ return;
+
+ memset(clients_in_company, 0, sizeof(clients_in_company));
+
+ /* Detect the active companies */
+ FOR_ALL_CLIENTS(cs) {
+ ci = DEREF_CLIENT_INFO(cs);
+ if (ci->client_playas >= 1 && ci->client_playas <= MAX_PLAYERS) {
+ clients_in_company[ci->client_playas-1] = true;
+ }
+ }
+ if (!_network_dedicated) {
+ ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
+ if (ci->client_playas >= 1 && ci->client_playas <= MAX_PLAYERS) {
+ clients_in_company[ci->client_playas-1] = true;
+ }
+ }
+
+ /* Go through all the comapnies */
+ FOR_ALL_PLAYERS(p) {
+ /* Skip the non-active once */
+ if (!p->is_active || p->is_ai)
+ continue;
+
+ if (!clients_in_company[p->index]) {
+ /* The company is empty for one month more */
+ _network_player_info[p->index].months_empty++;
+
+ /* Is the company empty for autoclean_unprotected-months, and is there no protection? */
+ if (_network_player_info[p->index].months_empty > _network_autoclean_unprotected && _network_player_info[p->index].password[0] == '\0') {
+ /* Shut the company down */
+ DoCommandP(0, 2, p->index, NULL, CMD_PLAYER_CTRL);
+ IConsolePrintF(_iconsole_color_default, "Auto-cleaned company #%d", p->index+1);
+ }
+ /* Is the compnay empty for autoclean_protected-months, and there is a protection? */
+ if (_network_player_info[p->index].months_empty > _network_autoclean_protected && _network_player_info[p->index].password[0] != '\0') {
+ /* Unprotect the company */
+ _network_player_info[p->index].password[0] = '\0';
+ IConsolePrintF(_iconsole_color_default, "Auto-removed protection from company #%d", p->index+1);
+ _network_player_info[p->index].months_empty = 0;
+ }
+ } else {
+ /* It is not empty, reset the date */
+ _network_player_info[p->index].months_empty = 0;
+ }
+ }
+}
+
// This function changes new_name to a name that is unique (by adding #1 ...)
// and it returns true if that succeeded.
bool NetworkFindName(char new_name[NETWORK_NAME_LENGTH])
@@ -1385,4 +1453,9 @@ void NetworkServer_Tick(void)
NetworkUDPAdvertise();
}
+void NetworkServerMonthlyLoop()
+{
+ NetworkAutoCleanCompanies();
+}
+
#endif /* ENABLE_NETWORK */