network_server.c
changeset 690 3afcad69d4f7
parent 686 719f01ca0175
child 692 b9e99d207ca6
equal deleted inserted replaced
689:5a4b1536db82 690:3afcad69d4f7
   627 		}
   627 		}
   628 		else {
   628 		else {
   629 			SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
   629 			SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
   630 		}
   630 		}
   631 	}
   631 	}
       
   632 
       
   633 	/* Make sure companies to who people try to join are not autocleaned */
       
   634 	if (playas >= 1 && playas <= MAX_PLAYERS)
       
   635 		_network_player_info[playas-1].months_empty = 0;
   632 }
   636 }
   633 
   637 
   634 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD)
   638 DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD)
   635 {
   639 {
   636 	NetworkPasswordType type;
   640 	NetworkPasswordType type;
  1119 	Vehicle *v;
  1123 	Vehicle *v;
  1120 	Station *s;
  1124 	Station *s;
  1121 	ClientState *cs;
  1125 	ClientState *cs;
  1122 	NetworkClientInfo *ci;
  1126 	NetworkClientInfo *ci;
  1123 	int i;
  1127 	int i;
       
  1128 	uint16 months_empty;
  1124 
  1129 
  1125 	FOR_ALL_PLAYERS(p) {
  1130 	FOR_ALL_PLAYERS(p) {
  1126 		if (!p->is_active) {
  1131 		if (!p->is_active) {
  1127 			memset(&_network_player_info[p->index], 0, sizeof(NetworkPlayerInfo));
  1132 			memset(&_network_player_info[p->index], 0, sizeof(NetworkPlayerInfo));
  1128 			continue;
  1133 			continue;
  1129 		}
  1134 		}
  1130 
  1135 
  1131 		// Clean the info but not the password
  1136 		// Clean the info but not the password
  1132 		ttd_strlcpy(password, _network_player_info[p->index].password, sizeof(password));
  1137 		ttd_strlcpy(password, _network_player_info[p->index].password, sizeof(password));
       
  1138 		months_empty = _network_player_info[p->index].months_empty;
  1133 		memset(&_network_player_info[p->index], 0, sizeof(NetworkPlayerInfo));
  1139 		memset(&_network_player_info[p->index], 0, sizeof(NetworkPlayerInfo));
       
  1140 		_network_player_info[p->index].months_empty = months_empty;
  1134 		ttd_strlcpy(_network_player_info[p->index].password, password, sizeof(_network_player_info[p->index].password));
  1141 		ttd_strlcpy(_network_player_info[p->index].password, password, sizeof(_network_player_info[p->index].password));
  1135 
  1142 
  1136 		// Grap the company name
  1143 		// Grap the company name
  1137 		GetString(_network_player_info[p->index].company_name, p->name_1);
  1144 		GetString(_network_player_info[p->index].company_name, p->name_1);
  1138 
  1145 
  1226 	FOR_ALL_CLIENTS(cs) {
  1233 	FOR_ALL_CLIENTS(cs) {
  1227 		SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, ci);
  1234 		SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, ci);
  1228 	}
  1235 	}
  1229 }
  1236 }
  1230 
  1237 
       
  1238 /* Check if the server has autoclean_companies activated
       
  1239     Two things happen:
       
  1240       1) If a company is not protected, it is closed after 1 year (for example)
       
  1241       2) If a company is protected, protection is disabled after 3 years (for example)
       
  1242            (and item 1. happens a year later) */
       
  1243 static void NetworkAutoCleanCompanies()
       
  1244 {
       
  1245 	ClientState *cs;
       
  1246 	NetworkClientInfo *ci;
       
  1247 	Player *p;
       
  1248 	bool clients_in_company[MAX_PLAYERS];
       
  1249 
       
  1250 	if (!_network_autoclean_companies)
       
  1251 		return;
       
  1252 
       
  1253 	memset(clients_in_company, 0, sizeof(clients_in_company));
       
  1254 
       
  1255 	/* Detect the active companies */
       
  1256 	FOR_ALL_CLIENTS(cs) {
       
  1257 		ci = DEREF_CLIENT_INFO(cs);
       
  1258 		if (ci->client_playas >= 1 && ci->client_playas <= MAX_PLAYERS) {
       
  1259 			clients_in_company[ci->client_playas-1] = true;
       
  1260 		}
       
  1261 	}
       
  1262 	if (!_network_dedicated) {
       
  1263 		ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
       
  1264 		if (ci->client_playas >= 1 && ci->client_playas <= MAX_PLAYERS) {
       
  1265 			clients_in_company[ci->client_playas-1] = true;
       
  1266 		}
       
  1267 	}
       
  1268 
       
  1269 	/* Go through all the comapnies */
       
  1270 	FOR_ALL_PLAYERS(p) {
       
  1271 		/* Skip the non-active once */
       
  1272 		if (!p->is_active || p->is_ai)
       
  1273 			continue;
       
  1274 
       
  1275 		if (!clients_in_company[p->index]) {
       
  1276 			/* The company is empty for one month more */
       
  1277 			_network_player_info[p->index].months_empty++;
       
  1278 
       
  1279 			/* Is the company empty for autoclean_unprotected-months, and is there no protection? */
       
  1280 			if (_network_player_info[p->index].months_empty > _network_autoclean_unprotected && _network_player_info[p->index].password[0] == '\0') {
       
  1281 				/* Shut the company down */
       
  1282 				DoCommandP(0, 2, p->index, NULL, CMD_PLAYER_CTRL);
       
  1283 				IConsolePrintF(_iconsole_color_default, "Auto-cleaned company #%d", p->index+1);
       
  1284 			}
       
  1285 			/* Is the compnay empty for autoclean_protected-months, and there is a protection? */
       
  1286 			if (_network_player_info[p->index].months_empty > _network_autoclean_protected && _network_player_info[p->index].password[0] != '\0') {
       
  1287 				/* Unprotect the company */
       
  1288 				_network_player_info[p->index].password[0] = '\0';
       
  1289 				IConsolePrintF(_iconsole_color_default, "Auto-removed protection from company #%d", p->index+1);
       
  1290 				_network_player_info[p->index].months_empty = 0;
       
  1291 			}
       
  1292 		} else {
       
  1293 			/* It is not empty, reset the date */
       
  1294 			_network_player_info[p->index].months_empty = 0;
       
  1295 		}
       
  1296 	}
       
  1297 }
       
  1298 
  1231 // This function changes new_name to a name that is unique (by adding #1 ...)
  1299 // This function changes new_name to a name that is unique (by adding #1 ...)
  1232 //  and it returns true if that succeeded.
  1300 //  and it returns true if that succeeded.
  1233 bool NetworkFindName(char new_name[NETWORK_NAME_LENGTH])
  1301 bool NetworkFindName(char new_name[NETWORK_NAME_LENGTH])
  1234 {
  1302 {
  1235 	ClientState *new_cs;
  1303 	ClientState *new_cs;
  1383 
  1451 
  1384 	/* See if we need to advertise */
  1452 	/* See if we need to advertise */
  1385 	NetworkUDPAdvertise();
  1453 	NetworkUDPAdvertise();
  1386 }
  1454 }
  1387 
  1455 
       
  1456 void NetworkServerMonthlyLoop()
       
  1457 {
       
  1458 	NetworkAutoCleanCompanies();
       
  1459 }
       
  1460 
  1388 #endif /* ENABLE_NETWORK */
  1461 #endif /* ENABLE_NETWORK */