]> git.cworth.org Git - empires-server/commitdiff
Allow for games to opt in to allow "Guest" users
authorCarl Worth <cworth@cworth.org>
Sat, 6 Jun 2020 12:58:53 +0000 (05:58 -0700)
committerCarl Worth <cworth@cworth.org>
Sat, 6 Jun 2020 12:58:53 +0000 (05:58 -0700)
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.)

game.js
lmno.js
templates/choose-nickname.html
tictactoe.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). */
diff --git a/lmno.js b/lmno.js
index db0d35b9cc3a55fef821f1b1d2f75a5c9aee868d..ef76cb06918cce8a7f8b1318075aa908e701cf23 100644 (file)
--- 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) => {
index 8ffe761fa5e922185ac13fed67c32739dca72cdc..9817b1d901ad776949a137aec31599fad600f6ea 100644 (file)
@@ -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());
 <!-- The return false prevents the page from being reloaded -->
 <form id="nickname-form" onsubmit="set_nickname(this); return false">
   <div class="form-field large">
-    <label for="nickname">Nickname</label>
-    <input type="text" id="nickname" required>
+    <label for="nickname">Your name or nickname</label>
+    <input type="text" id="nickname"
+           {% if options.allow_guest %}
+           placeholder="Guest"
+           {% else %}
+           required
+           {% endif %}
+           >
   </div>
 
   <div class="form-field large">
index 476821eec57039d5735fab0b8f4f021f5d8c5cad..a7e6cfae290af760a9fedcef349b5a2f58136c44 100644 (file)
@@ -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;