From 092de30647eeb223b148a170615b4a715865fd7f Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Thu, 13 Feb 2020 14:42:51 +0100
Subject: [PATCH] Fix initial display on Hall. Understand the 'double close
 event bug'

---
 client/src/views/Hall.vue | 49 ++++++++++++++++++++-------------------
 server/sockets.js         |  4 +---
 2 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue
index 602238cc..de6d6d15 100644
--- a/client/src/views/Hall.vue
+++ b/client/src/views/Hall.vue
@@ -50,7 +50,7 @@ main
         #players
           p(v-for="sid in Object.keys(people)" v-if="!!people[sid].name")
             span {{ people[sid].name }}
-            button.player-action(v-if="showPlayerActionBtn(sid)" @click="challOrWatch(sid)")
+            button.player-action(v-if="sid!=st.user.sid || isGamer(sid)" @click="challOrWatch(sid)")
               | {{ getActionLabel(sid) }}
           p.anonymous @nonymous ({{ anonymousCount }})
         #chat
@@ -137,10 +137,16 @@ export default {
       {uid: this.st.user.id, excluded: true},
       response => {
         // Show corr tab with timeout, to let enough time for (socket) polling
-        setTimeout( () => {
-          if (this.games.length == response.games.length)
-            this.setDisplay('g', "corr");
-          }, 1000);
+        setTimeout(
+          () => {
+            if (response.games.length > 0 &&
+              this.games.length == response.games.length)
+            {
+              this.setDisplay('g', "corr");
+            }
+          },
+          1000
+        );
         this.games = this.games.concat(response.games.map(g => {
           const type = this.classifyObject(g);
           const vname = this.getVname(g.vid);
@@ -154,10 +160,16 @@ export default {
       "GET",
       {uid: this.st.user.id},
       response => {
-        setTimeout( () => {
-          if (this.challenges.length == response.challenges.length)
-            this.setDisplay('c', "corr");
-          }, 1000);
+        setTimeout(
+          () => {
+            if (response.challenges.length > 0 &&
+              this.challenges.length == response.challenges.length)
+            {
+              this.setDisplay('c', "corr");
+            }
+          },
+          1000
+        );
         // Gather all senders names, and then retrieve full identity:
         // (TODO [perf]: some might be online...)
         let names = {};
@@ -267,19 +279,6 @@ export default {
     isGamer: function(sid) {
       return this.people[sid].pages.some(p => p.indexOf("/game/") >= 0);
     },
-    showPlayerActionBtn: function(sid) {
-      // Do not show action btn if I'm anonymous and target isn't playing,
-      // or target is anonymous and not playing,
-      // or target is me and I don't play anywhere.
-      const targetIsGamer = this.isGamer(sid);
-      if ((!this.st.user.name && !targetIsGamer) ||
-        (!this.people[sid].name && !this.isGamer(this.st.user.sid)) ||
-        (sid == this.st.user.sid && !targetIsGamer))
-      {
-        return false;
-      }
-      return true;
-    },
     getActionLabel: function(sid) {
       return this.people[sid].pages.some(p => p == "/")
         ? "Challenge"
@@ -376,9 +375,11 @@ export default {
         }
         case "disconnect":
         case "gdisconnect":
+          // If the user reloads the page twice very quickly (experienced with Firefox),
+          // the first reload won't have time to connect but will trigger a "close" event anyway.
+          // ==> Next check is required.
           if (!this.people[data.from])
-            return; //TODO: solve this bug
-                    // (anonymous reloads page, onclose event triggered twice...)
+            return;
           // Disconnect means no more tmpIds:
           if (data.code == "disconnect")
           {
diff --git a/server/sockets.js b/server/sockets.js
index 175fe7e9..7f0d14ec 100644
--- a/server/sockets.js
+++ b/server/sockets.js
@@ -201,9 +201,7 @@ module.exports = function(wss) {
       }
     };
     const closeListener = () => {
-      // TODO: BUG: this is triggered twice when anonymous reloads page
-      // (+ registered users, everyone in Hall).
-      // For tab or browser closing:
+      // For browser or tab closing (including page reload):
       doDisconnect();
     };
     // Update clients object: add new connexion
-- 
2.44.0