(svn r14712) -Codechange: split server and client side w.r.t. the storage of network related company information.

This commit is contained in:
rubidium 2008-12-22 18:40:57 +00:00
parent e6e32bad4d
commit abc903d625
13 changed files with 212 additions and 195 deletions

View File

@ -863,7 +863,7 @@ CommandCost CmdCompanyCtrl(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
if (IsValidCompanyID(ci->client_playas)) { if (IsValidCompanyID(ci->client_playas)) {
CompanyID company_backup = _local_company; CompanyID company_backup = _local_company;
_network_company_info[c->index].months_empty = 0; _network_company_states[c->index].months_empty = 0;
/* XXX - When a client joins, we automatically set its name to the /* XXX - When a client joins, we automatically set its name to the
* client's name (for some reason). As it stands now only the server * client's name (for some reason). As it stands now only the server

View File

@ -1146,21 +1146,26 @@ DEF_CONSOLE_CMD(ConCompanies)
IConsoleHelp("List the in-game details of all clients connected to the server. Usage 'companies'"); IConsoleHelp("List the in-game details of all clients connected to the server. Usage 'companies'");
return true; return true;
} }
NetworkPopulateCompanyInfo(); NetworkCompanyStats company_stats[MAX_COMPANIES];
NetworkPopulateCompanyStats(company_stats);
FOR_ALL_COMPANIES(c) { FOR_ALL_COMPANIES(c) {
char buffer[512]; /* Grab the company name */
char company_name[NETWORK_COMPANY_NAME_LENGTH];
SetDParam(0, c->index);
GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
const NetworkCompanyInfo *npi = &_network_company_info[c->index]; char buffer[512];
const NetworkCompanyStats *stats = &company_stats[c->index];
GetString(buffer, STR_00D1_DARK_BLUE + _company_colours[c->index], lastof(buffer)); GetString(buffer, STR_00D1_DARK_BLUE + _company_colours[c->index], lastof(buffer));
IConsolePrintF(CC_INFO, "#:%d(%s) Company Name: '%s' Year Founded: %d Money: %" OTTD_PRINTF64 "d Loan: %" OTTD_PRINTF64 "d Value: %" OTTD_PRINTF64 "d (T:%d, R:%d, P:%d, S:%d) %sprotected", IConsolePrintF(CC_INFO, "#:%d(%s) Company Name: '%s' Year Founded: %d Money: %" OTTD_PRINTF64 "d Loan: %" OTTD_PRINTF64 "d Value: %" OTTD_PRINTF64 "d (T:%d, R:%d, P:%d, S:%d) %sprotected",
c->index + 1, buffer, npi->company_name, c->inaugurated_year, (int64)c->money, (int64)c->current_loan, (int64)CalculateCompanyValue(c), c->index + 1, buffer, company_name, c->inaugurated_year, (int64)c->money, (int64)c->current_loan, (int64)CalculateCompanyValue(c),
/* trains */ npi->num_vehicle[0], /* trains */ stats->num_vehicle[0],
/* lorry + bus */ npi->num_vehicle[1] + npi->num_vehicle[2], /* lorry + bus */ stats->num_vehicle[1] + stats->num_vehicle[2],
/* planes */ npi->num_vehicle[3], /* planes */ stats->num_vehicle[3],
/* ships */ npi->num_vehicle[4], /* ships */ stats->num_vehicle[4],
/* protected */ StrEmpty(npi->password) ? "un" : ""); /* protected */ StrEmpty(_network_company_states[c->index].password) ? "un" : "");
} }
return true; return true;

View File

@ -8,9 +8,16 @@
#include "../../stdafx.h" #include "../../stdafx.h"
#include "../../debug.h" #include "../../debug.h"
#include "../../company_base.h"
#include "../../strings_func.h"
#include "../../string_func.h"
#include "../../date_func.h"
#include "os_abstraction.h" #include "os_abstraction.h"
#include "core.h" #include "core.h"
#include "packet.h" #include "packet.h"
#include "../network_func.h"
#include "table/strings.h"
#ifdef __MORPHOS__ #ifdef __MORPHOS__
/* the library base is required here */ /* the library base is required here */
@ -119,4 +126,45 @@ void NetworkSocketHandler::Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf)
} }
} }
void NetworkSocketHandler::Send_CompanyInformation(Packet *p, const Company *c, const NetworkCompanyStats *stats)
{
/* Grab the company name */
char company_name[NETWORK_COMPANY_NAME_LENGTH];
SetDParam(0, c->index);
GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
/* Get the income */
Money income = 0;
if (_cur_year - 1 == c->inaugurated_year) {
/* The company is here just 1 year, so display [2], else display[1] */
for (uint i = 0; i < lengthof(c->yearly_expenses[2]); i++) {
income -= c->yearly_expenses[2][i];
}
} else {
for (uint i = 0; i < lengthof(c->yearly_expenses[1]); i++) {
income -= c->yearly_expenses[1][i];
}
}
/* Send the information */
p->Send_uint8 (c->index);
p->Send_string(company_name);
p->Send_uint32(c->inaugurated_year);
p->Send_uint64(c->old_economy[0].company_value);
p->Send_uint64(c->money);
p->Send_uint64(income);
p->Send_uint16(c->old_economy[0].performance_history);
/* Send 1 if there is a passord for the company else send 0 */
p->Send_bool (!StrEmpty(_network_company_states[c->index].password));
for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++) {
p->Send_uint16(stats->num_vehicle[i]);
}
for (int i = 0; i < NETWORK_STATION_TYPES; i++) {
p->Send_uint16(stats->num_station[i]);
}
}
#endif /* ENABLE_NETWORK */ #endif /* ENABLE_NETWORK */

View File

@ -74,6 +74,7 @@ public:
void Send_GRFIdentifier(Packet *p, const GRFIdentifier *grf); void Send_GRFIdentifier(Packet *p, const GRFIdentifier *grf);
void Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf); void Recv_GRFIdentifier(Packet *p, GRFIdentifier *grf);
void Send_CompanyInformation(Packet *p, const struct Company *c, const struct NetworkCompanyStats *stats);
}; };
#endif /* ENABLE_NETWORK */ #endif /* ENABLE_NETWORK */

View File

@ -50,8 +50,8 @@ bool _network_available; ///< is network mode available?
bool _network_dedicated; ///< are we a dedicated server? bool _network_dedicated; ///< are we a dedicated server?
bool _is_network_server; ///< Does this client wants to be a network-server? bool _is_network_server; ///< Does this client wants to be a network-server?
NetworkServerGameInfo _network_game_info; NetworkServerGameInfo _network_game_info;
NetworkCompanyInfo _network_company_info[MAX_COMPANIES];
NetworkClientInfo _network_client_info[MAX_CLIENT_INFO]; NetworkClientInfo _network_client_info[MAX_CLIENT_INFO];
NetworkCompanyState _network_company_states[MAX_COMPANIES];
ClientID _network_own_client_id; ClientID _network_own_client_id;
ClientID _redirect_console_to_client; ClientID _redirect_console_to_client;
bool _network_need_advertise; bool _network_need_advertise;
@ -691,7 +691,7 @@ static void NetworkInitialize()
// Clean the client_info memory // Clean the client_info memory
memset(&_network_client_info, 0, sizeof(_network_client_info)); memset(&_network_client_info, 0, sizeof(_network_client_info));
memset(&_network_company_info, 0, sizeof(_network_company_info)); memset(&_network_company_states, 0, sizeof(_network_company_states));
_sync_frame = 0; _sync_frame = 0;
_network_first_time = true; _network_first_time = true;

View File

@ -85,7 +85,7 @@ void HashCurrentCompanyPassword(const char *password)
strecpy(_password_server_unique_id, _settings_client.network.network_id, lastof(_password_server_unique_id)); strecpy(_password_server_unique_id, _settings_client.network.network_id, lastof(_password_server_unique_id));
const char *new_pw = GenerateCompanyPasswordHash(password); const char *new_pw = GenerateCompanyPasswordHash(password);
strecpy(_network_company_info[_local_company].password, new_pw, lastof(_network_company_info[_local_company].password)); strecpy(_network_company_states[_local_company].password, new_pw, lastof(_network_company_states[_local_company].password));
} }
@ -359,19 +359,22 @@ DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO)
CompanyID current = (Owner)p->Recv_uint8(); CompanyID current = (Owner)p->Recv_uint8();
if (current >= MAX_COMPANIES) return NETWORK_RECV_STATUS_CLOSE_QUERY; if (current >= MAX_COMPANIES) return NETWORK_RECV_STATUS_CLOSE_QUERY;
p->Recv_string(_network_company_info[current].company_name, sizeof(_network_company_info[current].company_name)); NetworkCompanyInfo *company_info = GetLobbyCompanyInfo(current);
_network_company_info[current].inaugurated_year = p->Recv_uint32(); if (company_info == NULL) return NETWORK_RECV_STATUS_CLOSE_QUERY;
_network_company_info[current].company_value = p->Recv_uint64();
_network_company_info[current].money = p->Recv_uint64();
_network_company_info[current].income = p->Recv_uint64();
_network_company_info[current].performance = p->Recv_uint16();
_network_company_info[current].use_password = p->Recv_bool();
for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++)
_network_company_info[current].num_vehicle[i] = p->Recv_uint16();
for (int i = 0; i < NETWORK_STATION_TYPES; i++)
_network_company_info[current].num_station[i] = p->Recv_uint16();
p->Recv_string(_network_company_info[current].clients, sizeof(_network_company_info[current].clients)); p->Recv_string(company_info->company_name, sizeof(company_info->company_name));
company_info->inaugurated_year = p->Recv_uint32();
company_info->company_value = p->Recv_uint64();
company_info->money = p->Recv_uint64();
company_info->income = p->Recv_uint64();
company_info->performance = p->Recv_uint16();
company_info->use_password = p->Recv_bool();
for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++)
company_info->num_vehicle[i] = p->Recv_uint16();
for (int i = 0; i < NETWORK_STATION_TYPES; i++)
company_info->num_station[i] = p->Recv_uint16();
p->Recv_string(company_info->clients, sizeof(company_info->clients));
InvalidateWindow(WC_NETWORK_WINDOW, 0); InvalidateWindow(WC_NETWORK_WINDOW, 0);

View File

@ -11,8 +11,8 @@
#include "../console_type.h" #include "../console_type.h"
extern NetworkServerGameInfo _network_game_info; extern NetworkServerGameInfo _network_game_info;
extern NetworkCompanyInfo _network_company_info[MAX_COMPANIES];
extern NetworkClientInfo _network_client_info[MAX_CLIENT_INFO]; extern NetworkClientInfo _network_client_info[MAX_CLIENT_INFO];
extern NetworkCompanyState _network_company_states[MAX_COMPANIES];
extern ClientID _network_own_client_id; extern ClientID _network_own_client_id;
extern ClientID _redirect_console_to_client; extern ClientID _redirect_console_to_client;
@ -34,7 +34,7 @@ void NetworkUDPGameLoop();
void NetworkUDPCloseAll(); void NetworkUDPCloseAll();
void ParseConnectionString(const char **company, const char **port, char *connection_string); void ParseConnectionString(const char **company, const char **port, char *connection_string);
void NetworkStartDebugLog(const char *hostname, uint16 port); void NetworkStartDebugLog(const char *hostname, uint16 port);
void NetworkPopulateCompanyInfo(); void NetworkPopulateCompanyStats(NetworkCompanyStats *stats);
void NetworkUpdateClientInfo(ClientID client_id); void NetworkUpdateClientInfo(ClientID client_id);
bool NetworkClientConnectGame(const char *host, uint16 port); bool NetworkClientConnectGame(const char *host, uint16 port);

View File

@ -1136,18 +1136,6 @@ static void ShowNetworkStartServerWindow()
new NetworkStartServerWindow(&_network_start_server_window_desc); new NetworkStartServerWindow(&_network_start_server_window_desc);
} }
static CompanyID NetworkLobbyFindCompanyIndex(byte pos)
{
/* Scroll through all _network_company_info and get the 'pos' item that is not empty */
for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
if (!StrEmpty(_network_company_info[i].company_name)) {
if (pos-- == 0) return i;
}
}
return COMPANY_FIRST;
}
/** Enum for NetworkLobbyWindow, referring to _network_lobby_window_widgets */ /** Enum for NetworkLobbyWindow, referring to _network_lobby_window_widgets */
enum NetworkLobbyWindowWidgets { enum NetworkLobbyWindowWidgets {
NLWW_CLOSE = 0, ///< Close 'X' button NLWW_CLOSE = 0, ///< Close 'X' button
@ -1163,6 +1151,7 @@ enum NetworkLobbyWindowWidgets {
struct NetworkLobbyWindow : public Window { struct NetworkLobbyWindow : public Window {
CompanyID company; ///< Select company CompanyID company; ///< Select company
NetworkGameList *server; ///< Selected server NetworkGameList *server; ///< Selected server
NetworkCompanyInfo company_info[MAX_COMPANIES];
NetworkLobbyWindow(const WindowDesc *desc, NetworkGameList *ngl) : NetworkLobbyWindow(const WindowDesc *desc, NetworkGameList *ngl) :
Window(desc), company(INVALID_COMPANY), server(ngl) Window(desc), company(INVALID_COMPANY), server(ngl)
@ -1172,6 +1161,18 @@ struct NetworkLobbyWindow : public Window {
this->FindWindowPlacementAndResize(desc); this->FindWindowPlacementAndResize(desc);
} }
CompanyID NetworkLobbyFindCompanyIndex(byte pos)
{
/* Scroll through all this->company_info and get the 'pos' item that is not empty */
for (CompanyID i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
if (!StrEmpty(this->company_info[i].company_name)) {
if (pos-- == 0) return i;
}
}
return COMPANY_FIRST;
}
virtual void OnPaint() virtual void OnPaint()
{ {
const NetworkGameInfo *gi = &this->server->info; const NetworkGameInfo *gi = &this->server->info;
@ -1197,11 +1198,11 @@ struct NetworkLobbyWindow : public Window {
GfxFillRect(11, y - 1, 154, y + 10, 10); // show highlighted item with a different colour GfxFillRect(11, y - 1, 154, y + 10, 10); // show highlighted item with a different colour
} }
DoDrawStringTruncated(_network_company_info[company].company_name, 13, y, TC_BLACK, 135 - 13); DoDrawStringTruncated(this->company_info[company].company_name, 13, y, TC_BLACK, 135 - 13);
if (_network_company_info[company].use_password != 0) DrawSprite(SPR_LOCK, PAL_NONE, 135, y); if (this->company_info[company].use_password != 0) DrawSprite(SPR_LOCK, PAL_NONE, 135, y);
/* If the company's income was positive puts a green dot else a red dot */ /* If the company's income was positive puts a green dot else a red dot */
if (_network_company_info[company].income >= 0) income = true; if (this->company_info[company].income >= 0) income = true;
DrawSprite(SPR_BLOT, income ? PALETTE_TO_GREEN : PALETTE_TO_RED, 145, y); DrawSprite(SPR_BLOT, income ? PALETTE_TO_GREEN : PALETTE_TO_RED, 145, y);
pos++; pos++;
@ -1212,7 +1213,7 @@ struct NetworkLobbyWindow : public Window {
/* Draw info about selected company when it is selected in the left window */ /* Draw info about selected company when it is selected in the left window */
GfxFillRect(174, 39, 403, 75, 157); GfxFillRect(174, 39, 403, 75, 157);
DrawStringCentered(290, 50, STR_NETWORK_COMPANY_INFO, TC_FROMSTRING); DrawStringCentered(290, 50, STR_NETWORK_COMPANY_INFO, TC_FROMSTRING);
if (this->company != INVALID_COMPANY) { if (this->company != INVALID_COMPANY && !StrEmpty(this->company_info[this->company].company_name)) {
const uint x = 183; const uint x = 183;
const uint trunc_width = this->widget[NLWW_DETAILS].right - x; const uint trunc_width = this->widget[NLWW_DETAILS].right - x;
y = 80; y = 80;
@ -1224,47 +1225,47 @@ struct NetworkLobbyWindow : public Window {
DrawString(x, y, STR_NETWORK_CLIENTS, TC_GOLD); DrawString(x, y, STR_NETWORK_CLIENTS, TC_GOLD);
y += 10; y += 10;
SetDParamStr(0, _network_company_info[this->company].company_name); SetDParamStr(0, this->company_info[this->company].company_name);
DrawStringTruncated(x, y, STR_NETWORK_COMPANY_NAME, TC_GOLD, trunc_width); DrawStringTruncated(x, y, STR_NETWORK_COMPANY_NAME, TC_GOLD, trunc_width);
y += 10; y += 10;
SetDParam(0, _network_company_info[this->company].inaugurated_year); SetDParam(0, this->company_info[this->company].inaugurated_year);
DrawString(x, y, STR_NETWORK_INAUGURATION_YEAR, TC_GOLD); // inauguration year DrawString(x, y, STR_NETWORK_INAUGURATION_YEAR, TC_GOLD); // inauguration year
y += 10; y += 10;
SetDParam(0, _network_company_info[this->company].company_value); SetDParam(0, this->company_info[this->company].company_value);
DrawString(x, y, STR_NETWORK_VALUE, TC_GOLD); // company value DrawString(x, y, STR_NETWORK_VALUE, TC_GOLD); // company value
y += 10; y += 10;
SetDParam(0, _network_company_info[this->company].money); SetDParam(0, this->company_info[this->company].money);
DrawString(x, y, STR_NETWORK_CURRENT_BALANCE, TC_GOLD); // current balance DrawString(x, y, STR_NETWORK_CURRENT_BALANCE, TC_GOLD); // current balance
y += 10; y += 10;
SetDParam(0, _network_company_info[this->company].income); SetDParam(0, this->company_info[this->company].income);
DrawString(x, y, STR_NETWORK_LAST_YEARS_INCOME, TC_GOLD); // last year's income DrawString(x, y, STR_NETWORK_LAST_YEARS_INCOME, TC_GOLD); // last year's income
y += 10; y += 10;
SetDParam(0, _network_company_info[this->company].performance); SetDParam(0, this->company_info[this->company].performance);
DrawString(x, y, STR_NETWORK_PERFORMANCE, TC_GOLD); // performance DrawString(x, y, STR_NETWORK_PERFORMANCE, TC_GOLD); // performance
y += 10; y += 10;
SetDParam(0, _network_company_info[this->company].num_vehicle[0]); SetDParam(0, this->company_info[this->company].num_vehicle[0]);
SetDParam(1, _network_company_info[this->company].num_vehicle[1]); SetDParam(1, this->company_info[this->company].num_vehicle[1]);
SetDParam(2, _network_company_info[this->company].num_vehicle[2]); SetDParam(2, this->company_info[this->company].num_vehicle[2]);
SetDParam(3, _network_company_info[this->company].num_vehicle[3]); SetDParam(3, this->company_info[this->company].num_vehicle[3]);
SetDParam(4, _network_company_info[this->company].num_vehicle[4]); SetDParam(4, this->company_info[this->company].num_vehicle[4]);
DrawString(x, y, STR_NETWORK_VEHICLES, TC_GOLD); // vehicles DrawString(x, y, STR_NETWORK_VEHICLES, TC_GOLD); // vehicles
y += 10; y += 10;
SetDParam(0, _network_company_info[this->company].num_station[0]); SetDParam(0, this->company_info[this->company].num_station[0]);
SetDParam(1, _network_company_info[this->company].num_station[1]); SetDParam(1, this->company_info[this->company].num_station[1]);
SetDParam(2, _network_company_info[this->company].num_station[2]); SetDParam(2, this->company_info[this->company].num_station[2]);
SetDParam(3, _network_company_info[this->company].num_station[3]); SetDParam(3, this->company_info[this->company].num_station[3]);
SetDParam(4, _network_company_info[this->company].num_station[4]); SetDParam(4, this->company_info[this->company].num_station[4]);
DrawString(x, y, STR_NETWORK_STATIONS, TC_GOLD); // stations DrawString(x, y, STR_NETWORK_STATIONS, TC_GOLD); // stations
y += 10; y += 10;
SetDParamStr(0, _network_company_info[this->company].clients); SetDParamStr(0, this->company_info[this->company].clients);
DrawStringTruncated(x, y, STR_NETWORK_PLAYERS, TC_GOLD, trunc_width); // players DrawStringTruncated(x, y, STR_NETWORK_PLAYERS, TC_GOLD, trunc_width); // players
} }
} }
@ -1306,6 +1307,8 @@ struct NetworkLobbyWindow : public Window {
case NLWW_REFRESH: // Refresh case NLWW_REFRESH: // Refresh
NetworkTCPQueryServer(_settings_client.network.last_host, _settings_client.network.last_port); // company info NetworkTCPQueryServer(_settings_client.network.last_host, _settings_client.network.last_port); // company info
NetworkUDPQueryServer(_settings_client.network.last_host, _settings_client.network.last_port); // general data NetworkUDPQueryServer(_settings_client.network.last_host, _settings_client.network.last_port); // general data
/* Clear the information so removed companies don't remain */
memset(this->company_info, 0, sizeof(company_info));
break; break;
} }
} }
@ -1362,6 +1365,17 @@ static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
new NetworkLobbyWindow(&_network_lobby_window_desc, ngl); new NetworkLobbyWindow(&_network_lobby_window_desc, ngl);
} }
/**
* Get the company information of a given company to fill for the lobby.
* @param company the company to get the company info struct from.
* @return the company info struct to write the (downloaded) data to.
*/
NetworkCompanyInfo *GetLobbyCompanyInfo(CompanyID company)
{
NetworkLobbyWindow *lobby = dynamic_cast<NetworkLobbyWindow*>(FindWindowById(WC_NETWORK_WINDOW, 0));
return (lobby != NULL && company < MAX_COMPANIES) ? &lobby->company_info[company] : NULL;
}
// The window below gives information about the connected clients // The window below gives information about the connected clients
// and also makes able to give money to them, kick them (if server) // and also makes able to give money to them, kick them (if server)
// and stuff like that. // and stuff like that.

View File

@ -18,6 +18,21 @@ void ShowNetworkGameWindow();
void ShowClientList(); void ShowClientList();
void ShowNetworkCompanyPasswordWindow(Window *parent); void ShowNetworkCompanyPasswordWindow(Window *parent);
/** Company information stored at the client side */
struct NetworkCompanyInfo : NetworkCompanyStats {
char company_name[NETWORK_COMPANY_NAME_LENGTH]; ///< Company name
Year inaugurated_year; ///< What year the company started in
Money company_value; ///< The company value
Money money; ///< The amount of money the company has
Money income; ///< How much did the company earned last year
uint16 performance; ///< What was his performance last month?
bool use_password; ///< Is there a password
char clients[NETWORK_CLIENTS_LENGTH]; ///< The clients that control this company (Name1, name2, ..)
};
NetworkCompanyInfo *GetLobbyCompanyInfo(CompanyID company);
#else /* ENABLE_NETWORK */ #else /* ENABLE_NETWORK */
/* Network function stubs when networking is disabled */ /* Network function stubs when networking is disabled */

View File

@ -91,8 +91,6 @@ enum NetworkLanguage {
NETLANG_COUNT NETLANG_COUNT
}; };
extern NetworkCompanyInfo _network_company_info[MAX_COMPANIES];
extern uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode extern uint32 _frame_counter_server; // The frame_counter of the server, if in network-mode
extern uint32 _frame_counter_max; // To where we may go with our clients extern uint32 _frame_counter_max; // To where we may go with our clients

View File

@ -69,40 +69,52 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_COMPANY_INFO)
// Data: // Data:
// //
/* Fetch the latest version of the stats */
NetworkCompanyStats company_stats[MAX_COMPANIES];
NetworkPopulateCompanyStats(company_stats);
/* Make a list of all clients per company */
char clients[MAX_COMPANIES][NETWORK_CLIENTS_LENGTH];
NetworkTCPSocketHandler *csi;
memset(clients, 0, sizeof(clients));
/* Add the local player (if not dedicated) */
const NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER);
if (ci != NULL && IsValidCompanyID(ci->client_playas)) {
strecpy(clients[ci->client_playas], ci->client_name, lastof(clients[ci->client_playas]));
}
FOR_ALL_CLIENTS(csi) {
char client_name[NETWORK_CLIENT_NAME_LENGTH];
NetworkGetClientName(client_name, sizeof(client_name), csi);
ci = DEREF_CLIENT_INFO(csi);
if (ci != NULL && IsValidCompanyID(ci->client_playas)) {
if (!StrEmpty(clients[ci->client_playas])) {
strecat(clients[ci->client_playas], ", ", lastof(clients[ci->client_playas]));
}
strecat(clients[ci->client_playas], client_name, lastof(clients[ci->client_playas]));
}
}
/* Now send the data */
Company *company; Company *company;
Packet *p; Packet *p;
NetworkPopulateCompanyInfo();
FOR_ALL_COMPANIES(company) { FOR_ALL_COMPANIES(company) {
p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO); p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO);
p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION); p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
p->Send_bool (true); p->Send_bool (true);
p->Send_uint8 (company->index); cs->Send_CompanyInformation(p, company, &company_stats[company->index]);
p->Send_string(_network_company_info[company->index].company_name); if (StrEmpty(clients[company->index])) {
p->Send_uint32(_network_company_info[company->index].inaugurated_year);
p->Send_uint64(_network_company_info[company->index].company_value);
p->Send_uint64(_network_company_info[company->index].money);
p->Send_uint64(_network_company_info[company->index].income);
p->Send_uint16(_network_company_info[company->index].performance);
/* Send 1 if there is a passord for the company else send 0 */
p->Send_bool(!StrEmpty(_network_company_info[company->index].password));
for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++) {
p->Send_uint16(_network_company_info[company->index].num_vehicle[i]);
}
for (int i = 0; i < NETWORK_STATION_TYPES; i++) {
p->Send_uint16(_network_company_info[company->index].num_station[i]);
}
if (StrEmpty(_network_company_info[company->index].clients)) {
p->Send_string("<none>"); p->Send_string("<none>");
} else { } else {
p->Send_string(_network_company_info[company->index].clients); p->Send_string(clients[company->index]);
} }
cs->Send_Packet(p); cs->Send_Packet(p);
@ -603,7 +615,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)
if (!StrEmpty(_settings_client.network.server_password)) { if (!StrEmpty(_settings_client.network.server_password)) {
SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_GAME_PASSWORD); SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_GAME_PASSWORD);
} else { } else {
if (IsValidCompanyID(ci->client_playas) && _network_company_info[ci->client_playas].password[0] != '\0') { if (IsValidCompanyID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) {
SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD); SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD);
} else { } else {
SEND_COMMAND(PACKET_SERVER_WELCOME)(cs); SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
@ -681,7 +693,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
ci->client_lang = client_lang; ci->client_lang = client_lang;
/* Make sure companies to which people try to join are not autocleaned */ /* Make sure companies to which people try to join are not autocleaned */
if (IsValidCompanyID(playas)) _network_company_info[playas].months_empty = 0; if (IsValidCompanyID(playas)) _network_company_states[playas].months_empty = 0;
if (_grfconfig == NULL) { if (_grfconfig == NULL) {
RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)(cs, NULL); RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)(cs, NULL);
@ -709,7 +721,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD)
ci = DEREF_CLIENT_INFO(cs); ci = DEREF_CLIENT_INFO(cs);
if (IsValidCompanyID(ci->client_playas) && _network_company_info[ci->client_playas].password[0] != '\0') { if (IsValidCompanyID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) {
SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD); SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD);
return; return;
} }
@ -720,7 +732,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD)
} else if (cs->status == STATUS_AUTHORIZING && type == NETWORK_COMPANY_PASSWORD) { } else if (cs->status == STATUS_AUTHORIZING && type == NETWORK_COMPANY_PASSWORD) {
ci = DEREF_CLIENT_INFO(cs); ci = DEREF_CLIENT_INFO(cs);
if (strcmp(password, _network_company_info[ci->client_playas].password) != 0) { if (strcmp(password, _network_company_states[ci->client_playas].password) != 0) {
// Password is invalid // Password is invalid
SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD); SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
return; return;
@ -1180,7 +1192,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD)
ci = DEREF_CLIENT_INFO(cs); ci = DEREF_CLIENT_INFO(cs);
if (IsValidCompanyID(ci->client_playas)) { if (IsValidCompanyID(ci->client_playas)) {
strecpy(_network_company_info[ci->client_playas].password, password, lastof(_network_company_info[ci->client_playas].password)); strecpy(_network_company_states[ci->client_playas].password, password, lastof(_network_company_states[ci->client_playas].password));
} }
} }
@ -1281,54 +1293,18 @@ static NetworkServerPacket* const _network_server_packet[] = {
// If this fails, check the array above with network_data.h // If this fails, check the array above with network_data.h
assert_compile(lengthof(_network_server_packet) == PACKET_END); assert_compile(lengthof(_network_server_packet) == PACKET_END);
// This update the company_info-stuff /**
void NetworkPopulateCompanyInfo() * Populate the company stats.
* @param stats the stats to update
*/
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats)
{ {
char password[NETWORK_PASSWORD_LENGTH];
const Company *c;
const Vehicle *v; const Vehicle *v;
const Station *s; const Station *s;
NetworkTCPSocketHandler *cs;
const NetworkClientInfo *ci;
uint i;
uint16 months_empty;
for (CompanyID cid = COMPANY_FIRST; cid < MAX_COMPANIES; cid++) { memset(stats, 0, sizeof(*stats) * MAX_COMPANIES);
if (!IsValidCompanyID(cid)) memset(&_network_company_info[cid], 0, sizeof(NetworkCompanyInfo));
}
FOR_ALL_COMPANIES(c) { /* Go through all vehicles and count the type of vehicles */
// Clean the info but not the password
strecpy(password, _network_company_info[c->index].password, lastof(password));
months_empty = _network_company_info[c->index].months_empty;
memset(&_network_company_info[c->index], 0, sizeof(NetworkCompanyInfo));
_network_company_info[c->index].months_empty = months_empty;
strecpy(_network_company_info[c->index].password, password, lastof(_network_company_info[c->index].password));
// Grap the company name
SetDParam(0, c->index);
GetString(_network_company_info[c->index].company_name, STR_COMPANY_NAME, lastof(_network_company_info[c->index].company_name));
// Check the income
if (_cur_year - 1 == c->inaugurated_year) {
// The company is here just 1 year, so display [2], else display[1]
for (i = 0; i < lengthof(c->yearly_expenses[2]); i++) {
_network_company_info[c->index].income -= c->yearly_expenses[2][i];
}
} else {
for (i = 0; i < lengthof(c->yearly_expenses[1]); i++) {
_network_company_info[c->index].income -= c->yearly_expenses[1][i];
}
}
// Set some general stuff
_network_company_info[c->index].inaugurated_year = c->inaugurated_year;
_network_company_info[c->index].company_value = c->old_economy[0].company_value;
_network_company_info[c->index].money = c->money;
_network_company_info[c->index].performance = c->old_economy[0].performance_history;
}
// Go through all vehicles and count the type of vehicles
FOR_ALL_VEHICLES(v) { FOR_ALL_VEHICLES(v) {
if (!IsValidCompanyID(v->owner) || !v->IsPrimaryVehicle()) continue; if (!IsValidCompanyID(v->owner) || !v->IsPrimaryVehicle()) continue;
byte type = 0; byte type = 0;
@ -1339,13 +1315,13 @@ void NetworkPopulateCompanyInfo()
case VEH_SHIP: type = 4; break; case VEH_SHIP: type = 4; break;
default: continue; default: continue;
} }
_network_company_info[v->owner].num_vehicle[type]++; stats[v->owner].num_vehicle[type]++;
} }
// Go through all stations and count the types of stations // Go through all stations and count the types of stations
FOR_ALL_STATIONS(s) { FOR_ALL_STATIONS(s) {
if (IsValidCompanyID(s->owner)) { if (IsValidCompanyID(s->owner)) {
NetworkCompanyInfo *npi = &_network_company_info[s->owner]; NetworkCompanyStats *npi = &stats[s->owner];
if (s->facilities & FACIL_TRAIN) npi->num_station[0]++; if (s->facilities & FACIL_TRAIN) npi->num_station[0]++;
if (s->facilities & FACIL_TRUCK_STOP) npi->num_station[1]++; if (s->facilities & FACIL_TRUCK_STOP) npi->num_station[1]++;
@ -1354,26 +1330,6 @@ void NetworkPopulateCompanyInfo()
if (s->facilities & FACIL_DOCK) npi->num_station[4]++; if (s->facilities & FACIL_DOCK) npi->num_station[4]++;
} }
} }
ci = NetworkFindClientInfoFromIndex(CLIENT_ID_SERVER);
// Register local company (if not dedicated)
if (ci != NULL && IsValidCompanyID(ci->client_playas))
strecpy(_network_company_info[ci->client_playas].clients, ci->client_name, lastof(_network_company_info[ci->client_playas].clients));
FOR_ALL_CLIENTS(cs) {
char client_name[NETWORK_CLIENT_NAME_LENGTH];
NetworkGetClientName(client_name, sizeof(client_name), cs);
ci = DEREF_CLIENT_INFO(cs);
if (ci != NULL && IsValidCompanyID(ci->client_playas)) {
if (!StrEmpty(_network_company_info[ci->client_playas].clients)) {
strecat(_network_company_info[ci->client_playas].clients, ", ", lastof(_network_company_info[ci->client_playas].clients));
}
strecat(_network_company_info[ci->client_playas].clients, client_name, lastof(_network_company_info[ci->client_playas].clients));
}
}
} }
// Send a packet to all clients with updated info about this client_id // Send a packet to all clients with updated info about this client_id
@ -1433,24 +1389,24 @@ static void NetworkAutoCleanCompanies()
if (!clients_in_company[c->index]) { if (!clients_in_company[c->index]) {
/* The company is empty for one month more */ /* The company is empty for one month more */
_network_company_info[c->index].months_empty++; _network_company_states[c->index].months_empty++;
/* Is the company empty for autoclean_unprotected-months, and is there no protection? */ /* Is the company empty for autoclean_unprotected-months, and is there no protection? */
if (_settings_client.network.autoclean_unprotected != 0 && _network_company_info[c->index].months_empty > _settings_client.network.autoclean_unprotected && StrEmpty(_network_company_info[c->index].password)) { if (_settings_client.network.autoclean_unprotected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_unprotected && StrEmpty(_network_company_states[c->index].password)) {
/* Shut the company down */ /* Shut the company down */
DoCommandP(0, 2, c->index, NULL, CMD_COMPANY_CTRL); DoCommandP(0, 2, c->index, NULL, CMD_COMPANY_CTRL);
IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d", c->index + 1); IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d", c->index + 1);
} }
/* Is the company empty for autoclean_protected-months, and there is a protection? */ /* Is the company empty for autoclean_protected-months, and there is a protection? */
if (_settings_client.network.autoclean_protected != 0 && _network_company_info[c->index].months_empty > _settings_client.network.autoclean_protected && !StrEmpty(_network_company_info[c->index].password)) { if (_settings_client.network.autoclean_protected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_protected && !StrEmpty(_network_company_states[c->index].password)) {
/* Unprotect the company */ /* Unprotect the company */
_network_company_info[c->index].password[0] = '\0'; _network_company_states[c->index].password[0] = '\0';
IConsolePrintF(CC_DEFAULT, "Auto-removed protection from company #%d", c->index + 1); IConsolePrintF(CC_DEFAULT, "Auto-removed protection from company #%d", c->index + 1);
_network_company_info[c->index].months_empty = 0; _network_company_states[c->index].months_empty = 0;
} }
} else { } else {
/* It is not empty, reset the date */ /* It is not empty, reset the date */
_network_company_info[c->index].months_empty = 0; _network_company_states[c->index].months_empty = 0;
} }
} }
} }

View File

@ -41,18 +41,15 @@ enum ClientIndex {
MAX_CLIENT_INFO = MAX_CLIENTS + 1, MAX_CLIENT_INFO = MAX_CLIENTS + 1,
}; };
struct NetworkCompanyInfo { /** Simple calculated statistics of a company */
char company_name[NETWORK_COMPANY_NAME_LENGTH]; ///< Company name struct NetworkCompanyStats {
char password[NETWORK_PASSWORD_LENGTH]; ///< The password for the company
Year inaugurated_year; ///< What year the company started in
Money company_value; ///< The company value
Money money; ///< The amount of money the company has
Money income; ///< How much did the company earned last year
uint16 performance; ///< What was his performance last month?
bool use_password; ///< Is there a password
uint16 num_vehicle[NETWORK_VEHICLE_TYPES]; ///< How many vehicles are there of this type? uint16 num_vehicle[NETWORK_VEHICLE_TYPES]; ///< How many vehicles are there of this type?
uint16 num_station[NETWORK_STATION_TYPES]; ///< How many stations are there of this type? uint16 num_station[NETWORK_STATION_TYPES]; ///< How many stations are there of this type?
char clients[NETWORK_CLIENTS_LENGTH]; ///< The clients that control this company (Name1, name2, ..) };
/** Some state information of a company, especially for servers */
struct NetworkCompanyState {
char password[NETWORK_PASSWORD_LENGTH]; ///< The password for the company
uint16 months_empty; ///< How many months the company is empty uint16 months_empty; ///< How many months the company is empty
}; };

View File

@ -118,35 +118,15 @@ DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_DETAIL_INFO)
packet.Send_uint8 (NETWORK_COMPANY_INFO_VERSION); packet.Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
packet.Send_uint8 (ActiveCompanyCount()); packet.Send_uint8 (ActiveCompanyCount());
/* Fetch the latest version of everything */ /* Fetch the latest version of the stats */
NetworkPopulateCompanyInfo(); NetworkCompanyStats company_stats[MAX_COMPANIES];
NetworkPopulateCompanyStats(company_stats);
Company *company; Company *company;
byte current = 0;
/* Go through all the companies */ /* Go through all the companies */
FOR_ALL_COMPANIES(company) { FOR_ALL_COMPANIES(company) {
current++;
/* Send the information */ /* Send the information */
packet.Send_uint8 (current); this->Send_CompanyInformation(&packet, company, &company_stats[company->index]);
packet.Send_string(_network_company_info[company->index].company_name);
packet.Send_uint32(_network_company_info[company->index].inaugurated_year);
packet.Send_uint64(_network_company_info[company->index].company_value);
packet.Send_uint64(_network_company_info[company->index].money);
packet.Send_uint64(_network_company_info[company->index].income);
packet.Send_uint16(_network_company_info[company->index].performance);
/* Send 1 if there is a passord for the company else send 0 */
packet.Send_bool (!StrEmpty(_network_company_info[company->index].password));
for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++) {
packet.Send_uint16(_network_company_info[company->index].num_vehicle[i]);
}
for (int i = 0; i < NETWORK_STATION_TYPES; i++) {
packet.Send_uint16(_network_company_info[company->index].num_station[i]);
}
} }
this->SendPacket(&packet, client_addr); this->SendPacket(&packet, client_addr);