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.)
/* No existing player. Add a new one. */
const id = this.next_player_id;
/* 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). */
/* Broadcast before adding player to list (to avoid announcing the
* new player to itself). */
router.get('/', (request, response) => {
const game = request.game;
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`);
response.render(`${game.meta.identifier}-game.html`);
});
router.put('/player', (request, response) => {
});
router.put('/player', (request, response) => {
}
function set_nickname(form) {
}
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());
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">
<!-- 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">
</div>
<div class="form-field large">
TicTacToe.meta = {
name: "Tic Tac Toe",
TicTacToe.meta = {
name: "Tic Tac Toe",
- identifier: "tictactoe"
+ identifier: "tictactoe",
+ options: {
+ allow_guest: true
+ }
};
exports.Game = TicTacToe;
};
exports.Game = TicTacToe;