Changeset - r25233:c47c9757c370
[Not reviewed]
master
0 5 0
rubidium42 - 4 years ago 2021-04-22 06:01:52
rubidium@openttd.org
Feature: [Network] Ensure players fill in a name instead of defaulting to "Player"
5 files changed with 47 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/lang/english.txt
Show inline comments
 
@@ -2172,12 +2172,13 @@ STR_NETWORK_ERROR_DESYNC                
 
STR_NETWORK_ERROR_LOSTCONNECTION                                :{WHITE}Network-Game connection lost
 
STR_NETWORK_ERROR_SAVEGAMEERROR                                 :{WHITE}Could not load savegame
 
STR_NETWORK_ERROR_SERVER_START                                  :{WHITE}Could not start the server
 
STR_NETWORK_ERROR_CLIENT_START                                  :{WHITE}Could not connect
 
STR_NETWORK_ERROR_TIMEOUT                                       :{WHITE}Connection #{NUM} timed out
 
STR_NETWORK_ERROR_SERVER_ERROR                                  :{WHITE}A protocol error was detected and the connection was closed
 
STR_NETWORK_ERROR_BAD_PLAYER_NAME                               :{WHITE}Your player name has not been set. The name can be set at the top of the Multiplayer window
 
STR_NETWORK_ERROR_WRONG_REVISION                                :{WHITE}The revision of this client does not match the server's revision
 
STR_NETWORK_ERROR_WRONG_PASSWORD                                :{WHITE}Wrong password
 
STR_NETWORK_ERROR_SERVER_FULL                                   :{WHITE}The server is full
 
STR_NETWORK_ERROR_SERVER_BANNED                                 :{WHITE}You are banned from this server
 
STR_NETWORK_ERROR_KICKED                                        :{WHITE}You were kicked out of the game
 
STR_NETWORK_ERROR_KICK_MESSAGE                                  :{WHITE}Reason: {RAW_STRING}
src/network/network.cpp
Show inline comments
 
@@ -689,12 +689,14 @@ public:
 
void NetworkClientConnectGame(NetworkAddress address, CompanyID join_as, const char *join_server_password, const char *join_company_password)
 
{
 
	if (!_network_available) return;
 

	
 
	if (address.GetPort() == 0) return;
 

	
 
	if (!NetworkValidateClientName()) return;
 

	
 
	strecpy(_settings_client.network.last_host, address.GetHostname(), lastof(_settings_client.network.last_host));
 
	_settings_client.network.last_port = address.GetPort();
 
	_network_join_as = join_as;
 
	_network_join_server_password = join_server_password;
 
	_network_join_company_password = join_company_password;
 

	
src/network/network_client.cpp
Show inline comments
 
@@ -1262,12 +1262,48 @@ bool NetworkIsValidClientName(const char
 
	if (StrEmpty(client_name)) return false;
 
	if (*client_name == ' ') return false;
 
	return true;
 
}
 

	
 
/**
 
 * Trim the given client name in place, i.e. remove leading and trailing spaces.
 
 * After the trim check whether the client name is valid. A client name is valid
 
 * whenever the name is not empty and does not start with spaces. This check is
 
 * done via \c NetworkIsValidClientName.
 
 * When the client name is valid, this function returns true.
 
 * When the client name is not valid a GUI error message is shown telling the
 
 * user to set the client name and this function returns false.
 
 *
 
 * This function is not suitable for ensuring a valid client name at the server
 
 * as the error message will then be shown to the host instead of the client.
 
 * @param client_name The client name to validate. It will be trimmed of leading
 
 *                    and trailing spaces.
 
 * @return True iff the client name is valid.
 
 */
 
bool NetworkValidateClientName(char *client_name)
 
{
 
	StrTrimInPlace(client_name);
 
	if (NetworkIsValidClientName(client_name)) return true;
 

	
 
	ShowErrorMessage(STR_NETWORK_ERROR_BAD_PLAYER_NAME, INVALID_STRING_ID, WL_ERROR);
 
	return false;
 
}
 

	
 
/**
 
 * Convenience method for NetworkValidateClientName on _settings_client.network.client_name.
 
 * It trims the client name and checks whether it is empty. When it is empty
 
 * an error message is shown to the GUI user.
 
 * See \c NetworkValidateClientName(char*) for details about the functionality.
 
 * @return True iff the client name is valid.
 
 */
 
bool NetworkValidateClientName()
 
{
 
	return NetworkValidateClientName(_settings_client.network.client_name);
 
}
 

	
 
/**
 
 * Send the server our name.
 
 */
 
void NetworkUpdateClientName()
 
{
 
	NetworkClientInfo *ci = NetworkClientInfo::GetByClientID(_network_own_client_id);
 

	
src/network/network_func.h
Show inline comments
 
@@ -34,12 +34,14 @@ extern uint8 _network_reconnect;
 
extern StringList _network_bind_list;
 
extern StringList _network_host_list;
 
extern StringList _network_ban_list;
 

	
 
byte NetworkSpectatorCount();
 
bool NetworkIsValidClientName(const char *client_name);
 
bool NetworkValidateClientName();
 
bool NetworkValidateClientName(char *client_name);
 
void NetworkUpdateClientName();
 
bool NetworkCompanyHasClients(CompanyID company);
 
const char *NetworkChangeCompanyPassword(CompanyID company_id, const char *password);
 
void NetworkReboot();
 
void NetworkDisconnect(bool blocking = false, bool close_admins = true);
 
void NetworkGameLoop();
src/network/network_gui.cpp
Show inline comments
 
@@ -805,17 +805,15 @@ public:
 
				this->ScrollToSelectedServer();
 
				this->SetDirty();
 
				break;
 
			}
 

	
 
			case WID_NG_CLIENT:
 
				if (NetworkIsValidClientName(this->name_editbox.text.buf)) {
 
				/* Validation of the name will happen once the user tries to join or start a game, as getting
 
				 * error messages while typing (e.g. when you clear the name) defeats the purpose of the check. */
 
					strecpy(_settings_client.network.client_name, this->name_editbox.text.buf, lastof(_settings_client.network.client_name));
 
				} else {
 
					strecpy(_settings_client.network.client_name, "Player", lastof(_settings_client.network.client_name));
 
				}
 
				break;
 
		}
 
	}
 

	
 
	void OnQueryTextFinished(char *str) override
 
	{
 
@@ -1242,12 +1240,14 @@ static WindowDesc _network_start_server_
 
	0,
 
	_nested_network_start_server_window_widgets, lengthof(_nested_network_start_server_window_widgets)
 
);
 

	
 
static void ShowNetworkStartServerWindow()
 
{
 
	if (!NetworkValidateClientName()) return;
 

	
 
	DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
 
	DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_LOBBY);
 

	
 
	new NetworkStartServerWindow(&_network_start_server_window_desc);
 
}
 

	
 
@@ -1534,12 +1534,14 @@ static WindowDesc _network_lobby_window_
 
/**
 
 * Show the networklobbywindow with the selected server.
 
 * @param ngl Selected game pointer which is passed to the new window.
 
 */
 
static void ShowNetworkLobbyWindow(NetworkGameList *ngl)
 
{
 
	if (!NetworkValidateClientName()) return;
 

	
 
	DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
 
	DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_GAME);
 

	
 
	NetworkTCPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // company info
 
	NetworkUDPQueryServer(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port)); // general data
 

	
0 comments (0 inline, 0 general)