Fix notation and pieces setup for random asymmetric in Eightpieces
[vchess.git] / client / src / views / MyGames.vue
index 6078394..9403528 100644 (file)
@@ -53,21 +53,24 @@ export default {
         corr: Number.MAX_SAFE_INTEGER
       },
       // hasMore == TRUE: a priori there could be more games to load
-      hasMore: { live: true, corr: true },
+      hasMore: { live: true, corr: store.state.user.id > 0 },
       conn: null,
       connexionString: ""
     };
   },
+  watch: {
+    $route: function(to, from) {
+      if (to.path != "/mygames") this.cleanBeforeDestroy();
+    }
+  },
   created: function() {
+    window.addEventListener("beforeunload", this.cleanBeforeDestroy);
     // Initialize connection
     this.connexionString =
       params.socketUrl +
-      "/?sid=" +
-      this.st.user.sid +
-      "&id=" +
-      this.st.user.id +
-      "&tmpId=" +
-      getRandString() +
+      "/?sid=" + this.st.user.sid +
+      "&id=" + this.st.user.id +
+      "&tmpId=" + getRandString() +
       "&page=" +
       encodeURIComponent(this.$route.path);
     this.conn = new WebSocket(this.connexionString);
@@ -123,18 +126,20 @@ export default {
             }
           }
         );
-      } else {
-        this.loadMore(
-          "live",
-          () => this.loadMore("corr", adjustAndSetDisplay)
-        );
-      }
+      } else this.loadMore("live", adjustAndSetDisplay);
     });
   },
   beforeDestroy: function() {
-    this.conn.send(JSON.stringify({code: "disconnect"}));
+    this.cleanBeforeDestroy();
   },
   methods: {
+    cleanBeforeDestroy: function() {
+      window.removeEventListener("beforeunload", this.cleanBeforeDestroy);
+      this.conn.removeEventListener("message", this.socketMessageListener);
+      this.conn.removeEventListener("close", this.socketCloseListener);
+      this.conn.send(JSON.stringify({code: "disconnect"}));
+      this.conn = null;
+    },
     setDisplay: function(type, e) {
       this.display = type;
       localStorage.setItem("type-myGames", type);
@@ -172,6 +177,7 @@ export default {
       });
     },
     socketMessageListener: function(msg) {
+      if (!this.conn) return;
       const data = JSON.parse(msg.data);
       let gamesArrays = {
         "corr": this.corrGames,
@@ -252,16 +258,18 @@ export default {
           game.players[0].sid == this.st.user.sid
             ? game.players[1].sid
             : game.players[0].sid;
-        this.conn.send(
-          JSON.stringify(
-            {
-              code: "mabort",
-              gid: game.id,
-              // NOTE: target might not be online
-              target: oppsid
-            }
-          )
-        );
+        if (!!this.conn) {
+          this.conn.send(
+            JSON.stringify(
+              {
+                code: "mabort",
+                gid: game.id,
+                // NOTE: target might not be online
+                target: oppsid
+              }
+            )
+          );
+        }
       }
       else if (!game.deletedByWhite || !game.deletedByBlack) {
         // Set score if game isn't deleted on server:
@@ -281,7 +289,7 @@ export default {
       }
     },
     loadMore: function(type, cb) {
-      if (type == "corr") {
+      if (type == "corr" && this.st.user.id > 0) {
         ajax(
           "/completedgames",
           "GET",