]> git.cworth.org Git - empires-server/blobdiff - game.js
Allow for games to opt in to allow "Guest" users
[empires-server] / game.js
diff --git a/game.js b/game.js
index 0946568e9bbd8adc5c99d82832261fa1ac254d5d..bf74de46105aa62804c17b196c5df2fe4e9d3fd8 100644 (file)
--- a/game.js
+++ b/game.js
@@ -158,7 +158,20 @@ class Game {
 
     /* No existing player. Add a new one. */
     const id = this.next_player_id;
-    const player = new Player(id, session.id, session.nickname, connection);
+    let nickname = session.nickname;
+    if (nickname === "")
+      nickname = "Guest";
+    const nickname_orig = nickname;
+
+    /* Ensure we don't have a name collision with a previous player. */
+    let unique_suffix = 1;
+    while (this.players.find(player => player.name === nickname))
+    {
+      nickname = `${nickname_orig}${unique_suffix.toString().padStart(2, '0')}`;
+      unique_suffix++;
+    }
+
+    const player = new Player(id, session.id, nickname, connection);
 
     /* Broadcast before adding player to list (to avoid announcing the
      * new player to itself). */