From: Carl Worth Date: Sat, 6 Jun 2020 12:58:53 +0000 (-0700) Subject: Allow for games to opt in to allow "Guest" users X-Git-Url: https://git.cworth.org/git?p=lmno-server;a=commitdiff_plain;h=e368873853353b4b8949bfdf3c560b484e4cfb88 Allow for games to opt in to allow "Guest" users Here we allow a new object to the game metadata called "options" and a new optional property in that called "allow_guest". If this option is present and true, then the choose-nickname form does not require the user to enter a name, but will use "Guest" by default. This is convenient in a 2-player game, for example, where the two players are the only ones present so there's no ambiguity over who is who. But this could be very confusing for a party game like Empires. That is why, in this commit, tictactoe.js sets allow_guest to true while empires.js does not. Also, now that it's even more likely for there to be name collisions, (such as multiple people entering the game as "Guest"), when adding a player we not automatically append a numeric suffix to their name to make it unique within the game. (Note: The name in the session is left untouched so if a user with the same session joins a different game and the session name is unique there, it won't get any suffix.) --- 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). */ diff --git a/lmno.js b/lmno.js index db0d35b..ef76cb0 100644 --- a/lmno.js +++ b/lmno.js @@ -285,10 +285,14 @@ for (let key in engines) { router.get('/', (request, response) => { const game = request.game; - if (! request.session.nickname) - response.render('choose-nickname.html', { game_name: game.meta.name }); - else + if (! request.session.nickname) { + response.render('choose-nickname.html', { + game_name: game.meta.name, + options: game.meta.options + }); + } else { response.render(`${game.meta.identifier}-game.html`); + } }); router.put('/player', (request, response) => { diff --git a/templates/choose-nickname.html b/templates/choose-nickname.html index 8ffe761..9817b1d 100644 --- a/templates/choose-nickname.html +++ b/templates/choose-nickname.html @@ -9,7 +9,9 @@ } function set_nickname(form) { - const nickname = form.nickname.value; + let nickname = form.nickname.value; + if (nickname === "") + nickname = "Guest"; var request = new XMLHttpRequest(); request.addEventListener("loadend", () => set_nickname_loadend()); @@ -32,8 +34,14 @@
- - + +
diff --git a/tictactoe.js b/tictactoe.js index 476821e..a7e6cfa 100644 --- a/tictactoe.js +++ b/tictactoe.js @@ -54,7 +54,10 @@ TicTacToe.router = express.Router(); TicTacToe.meta = { name: "Tic Tac Toe", - identifier: "tictactoe" + identifier: "tictactoe", + options: { + allow_guest: true + } }; exports.Game = TicTacToe;