]> git.cworth.org Git - empires-server/commitdiff
Allow a player to reclaim a disconnected player of the same name
authorCarl Worth <cworth@cworth.org>
Sat, 27 Jun 2020 17:36:39 +0000 (10:36 -0700)
committerCarl Worth <cworth@cworth.org>
Sat, 27 Jun 2020 17:36:39 +0000 (10:36 -0700)
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

game.js

diff --git a/game.js b/game.js
index 0023257e796c1f56e49e2fe62742c6ec1f35454b..f9e18cb760725eb9d05a993115bf286f5f0b2e85 100644 (file)
--- 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;