summaryrefslogtreecommitdiff
path: root/src/network/network_client.cpp
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-04-22 08:01:52 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-04-24 08:02:54 +0200
commitbfb0ab3e2f5a5a09b4bf7f5584199425ffb1de50 (patch)
tree47f3e37e8d3a46096964ad32aa642b3f20ab24ea /src/network/network_client.cpp
parent5202869f0fec5512211988b35b1ee4b29a33686d (diff)
downloadopenttd-bfb0ab3e2f5a5a09b4bf7f5584199425ffb1de50.tar.xz
Feature: [Network] Ensure players fill in a name instead of defaulting to "Player"
Diffstat (limited to 'src/network/network_client.cpp')
-rw-r--r--src/network/network_client.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp
index a12215bbc..35c52d3bc 100644
--- a/src/network/network_client.cpp
+++ b/src/network/network_client.cpp
@@ -1265,6 +1265,42 @@ bool NetworkIsValidClientName(const char *client_name)
}
/**
+ * 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()