From: Carl Worth Date: Sat, 27 Jun 2020 17:36:39 +0000 (-0700) Subject: Allow a player to reclaim a disconnected player of the same name X-Git-Url: https://git.cworth.org/git?p=lmno-server;a=commitdiff_plain;h=3740e6f5ca87eac244bc2969b19a37939e9efc28 Allow a player to reclaim a disconnected player of the same name I've seen cases where players close their browser window, and then re-open it with the URL only to find that they aren't actually in the same session so they end up with an unwanted "Name01" name instead of "Name" and lose access to their score. We make things work the way users want here by letting them claim an existing player object by simply using the same name. Of course, this does assume nobody will want to do this nefariously. I'm perfectly fine with that, (since the whole point of all of this is to enable friendly games). If, at some point in the future somebody cared to lock people out from doign this, then we can imagine a future where: 1. Users could actually log in 2. Users could lock a game to only allow logged-in users --- diff --git a/game.js b/game.js index 0023257..f9e18cb 100644 --- a/game.js +++ b/game.js @@ -162,8 +162,18 @@ class Game { } add_player(session, connection) { + let nickname = session.nickname; + /* First see if we already have a player object for this session. */ - const existing = this.players_by_session[session.id]; + let existing = this.players_by_session[session.id]; + + /* Otherwise, see if we can match an inactive player by name. */ + if (! existing) { + existing = this.players.find(player => + player.name == nickname && + ! player.active); + } + if (existing) { if (! existing.active) { /* If we're re-activating a previously idled player, then we @@ -179,7 +189,6 @@ class Game { /* No existing player. Add a new one. */ const id = this.next_player_id; - let nickname = session.nickname; if (nickname === "") nickname = "Guest"; const nickname_orig = nickname;