X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=game.js;h=bf74de46105aa62804c17b196c5df2fe4e9d3fd8;hb=e368873853353b4b8949bfdf3c560b484e4cfb88;hp=0946568e9bbd8adc5c99d82832261fa1ac254d5d;hpb=dbeeca917db24aaaffabc63072365f31ac2652ab;p=empires-server diff --git a/game.js b/game.js index 0946568..bf74de4 100644 --- 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). */