~martijnbraam/public-inbox

among-sus: disallow duplicate names v1 PROPOSED

~wenngle
~wenngle: 1
 disallow duplicate names

 1 files changed, 7 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~martijnbraam/public-inbox/patches/19598/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH among-sus] disallow duplicate names Export this patch

~wenngle
From: wenngle <wenngle@disroot.org>

---
 main.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/main.c b/main.c
index 9a25284..83dc7b1 100644
--- a/main.c
+++ b/main.c
@@ -1273,6 +1273,13 @@ handle_input(int fd)
				write(fd, buf, strlen(buf));
				return 0;
			}
			for (int i= 0; i < NUM_PLAYERS; i++) {
				if (strcmp(players[i].name, buf) == 0) {
					snprintf(buf, sizeof(buf), "Taken, pick another name\n> ");
					write(fd, buf, strlen(buf));
					return 0;
				}
			}
			for (size_t i = 0; i < strlen(buf); i++) {
				if(!isprint(buf[i])) {
					snprintf(buf, sizeof(buf), "Invalid char, pick another name\n> ");
-- 
2.26.2
> From: wenngle <wenngle@disroot.org>
> 
> ---
> main.c | 7 +++++++
> 1 file changed, 7 insertions(+)
> 
> diff --git a/main.c b/main.c
> index 9a25284..83dc7b1 100644
> --- a/main.c
> +++ b/main.c
> @@ -1273,6 +1273,13 @@ handle_input(int fd)
> write(fd, buf, strlen(buf));
> return 0;
> }
> + for (int i= 0; i < NUM_PLAYERS; i++) {
Style: add a space between i and =
> + if (strcmp(players[i].name, buf) == 0) {
> + snprintf(buf, sizeof(buf), "Taken, pick another name\n> ");
> + write(fd, buf, strlen(buf));
> + return 0;
> + }
> + }
We'll need to clear players' names on disconnect as well
(players[i].name[0] = '\0' should be enough), this doesn't allow you to
connect, disconnect, and reconnect under the same name.