Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: server's client is shown incorrectly in some cases #12629

Merged
merged 2 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ static void NetworkInitGameInfo()
/* There should be always space for the server. */
assert(NetworkClientInfo::CanAllocateItem());
NetworkClientInfo *ci = new NetworkClientInfo(CLIENT_ID_SERVER);
ci->client_playas = _network_dedicated ? COMPANY_SPECTATOR : COMPANY_FIRST;
ci->client_playas = COMPANY_SPECTATOR;

ci->client_name = _settings_client.network.client_name;
}
Expand Down Expand Up @@ -987,6 +987,36 @@ bool NetworkServerStart()
return true;
}

/**
* Perform tasks when the server is started. This consists of things
* like putting the server's client in a valid company and resetting the restart time.
*/
void NetworkOnGameStart()
{
if (!_network_server) return;

/* Update the static game info to set the values from the new game. */
NetworkServerUpdateGameInfo();

ChangeNetworkRestartTime(true);

if (!_network_dedicated) {
Company *c = Company::GetIfValid(GetFirstPlayableCompanyID());
NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(CLIENT_ID_SERVER);
Fixed Show fixed Hide fixed
if (c != nullptr && ci != nullptr) {
ci->client_playas = c->index;

/*
* If the company has not been named yet, the company was just started.
* Otherwise it would have gotten a name already, so announce it as a new company.
*/
if (c->name_1 == STR_SV_UNNAMED && c->name.empty()) NetworkServerNewCompany(c, ci);
}

ShowClientList();
}
}

/* The server is rebooting...
* The only difference with NetworkDisconnect, is the packets that is sent */
void NetworkReboot()
Expand Down
2 changes: 2 additions & 0 deletions src/network/network_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ void NetworkPrintClients();
std::string_view NetworkGetPublicKeyOfClient(ClientID client_id);
void NetworkHandlePauseChange(PauseMode prev_mode, PauseMode changed_mode);

void NetworkOnGameStart();

/*** Commands ran by the server ***/
void NetworkServerSendConfigUpdate();
void NetworkServerUpdateGameInfo();
Expand Down
11 changes: 2 additions & 9 deletions src/openttd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#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 @@ -859,8 +858,8 @@ static void OnStartGame(bool dedicated_server)
* or in the case of a dedicated server, a spectator */
SetLocalCompany(dedicated_server ? COMPANY_SPECTATOR : GetFirstPlayableCompanyID());

/* Update the static game info to set the values from the new game. */
NetworkServerUpdateGameInfo();
NetworkOnGameStart();

/* Execute the game-start script */
IConsoleCmdExec("exec scripts/game_start.scr 0");
}
Expand Down Expand Up @@ -911,12 +910,6 @@ static void MakeNewGameDone()
CheckEngines();
CheckIndustries();
MarkWholeScreenDirty();

if (_network_server) {
ChangeNetworkRestartTime(true);

if (!_network_dedicated) ShowClientList();
}
}

static void MakeNewGame(bool from_heightmap, bool reset_settings)
Expand Down