Fixed corr challenge disappear when player quit
[vchess.git] / client / src / views / Hall.vue
index 1031e49..8d8b311 100644 (file)
@@ -55,7 +55,7 @@ main
             placeholder="5+0, 1h+30s, 5d ..."
           )
         fieldset
-          label(for="selectRandomLevel") {{ st.tr["Randomness"] }}
+          label(for="selectRandomLevel") {{ st.tr["Randomness"] }} *
           select#selectRandomLevel(v-model="newchallenge.randomness")
             option(value="0") {{ st.tr["Deterministic"] }}
             option(value="1") {{ st.tr["Symmetric random"] }}
@@ -93,10 +93,15 @@ main
           )
             span {{ people[sid].name }}
             button.player-action(
-              v-if="isGamer(sid) || (st.user.id > 0 && sid!=st.user.sid)"
-              @click="challOrWatch(sid)"
+              v-if="isGamer(sid)"
+              @click="watchGame(sid)"
             )
-              | {{ getActionLabel(sid) }}
+              | {{ st.tr["Observe"] }}
+            button.player-action(
+              v-else-if="st.user.id > 0 && sid!=st.user.sid"
+              @click="challenge(sid)"
+            )
+              | {{ st.tr["Challenge"] }}
           p.anonymous @nonymous ({{ anonymousCount }})
         #chat
           Chat(
@@ -249,7 +254,7 @@ export default {
       let names = {};
       response.challenges.forEach(c => {
         if (c.uid != this.st.user.id) names[c.uid] = "";
-        else if (!!c.target && c.target != this.st.user.id)
+        else if (c.target && c.target != this.st.user.id)
           names[c.target] = "";
       });
       const addChallenges = () => {
@@ -369,29 +374,23 @@ export default {
     isGamer: function(sid) {
       return this.people[sid].pages.some(p => p.indexOf("/game/") >= 0);
     },
-    getActionLabel: function(sid) {
-      return this.people[sid].pages.some(p => p == "/")
-        ? "Challenge"
-        : "Observe";
+    challenge: function(sid) {
+      // Available, in Hall (only)
+      this.newchallenge.to = this.people[sid].name;
+      document.getElementById("modalPeople").checked = false;
+      window.doClick("modalNewgame");
     },
-    challOrWatch: function(sid) {
-      if (this.people[sid].pages.some(p => p == "/")) {
-        // Available, in Hall
-        this.newchallenge.to = this.people[sid].name;
-        document.getElementById("modalPeople").checked = false;
-        window.doClick("modalNewgame");
-      } else {
-        // In some game, maybe playing maybe not: show a random one
-        let gids = [];
-        this.people[sid].pages.forEach(p => {
-          const matchGid = p.match(/[a-zA-Z0-9]+$/);
-          if (matchGid) gids.push(matchGid[0]);
-        });
-        const gid = gids[Math.floor(Math.random() * gids.length)];
-        const game = this.games.find(g => g.id == gid);
-        if (game) this.showGame(game);
-        else this.$router.push("/game/" + gid); //game vs. me
-      }
+    watchGame: function(sid) {
+      // In some game, maybe playing maybe not: show a random one
+      let gids = [];
+      this.people[sid].pages.forEach(p => {
+        const matchGid = p.match(/[a-zA-Z0-9]+$/);
+        if (matchGid) gids.push(matchGid[0]);
+      });
+      const gid = gids[Math.floor(Math.random() * gids.length)];
+      const game = this.games.find(g => g.id == gid);
+      if (game) this.showGame(game);
+      else this.$router.push("/game/" + gid); //game vs. me
     },
     showGame: function(g) {
       // NOTE: we are an observer, since only games I don't play are shown here
@@ -408,6 +407,15 @@ export default {
     processChat: function(chat) {
       this.send("newchat", { data: chat });
     },
+    getOppsid: function(c) {
+      let oppsid = c.from.sid; //may not be defined if corr + offline opp
+      if (!oppsid) {
+        oppsid = Object.keys(this.people).find(
+          sid => this.people[sid].id == c.from.id
+        );
+      }
+      return oppsid;
+    },
     // Messaging center:
     socketMessageListener: function(msg) {
       if (!this.conn) return;
@@ -468,7 +476,10 @@ export default {
           // Disconnect means no more tmpIds:
           if (data.code == "disconnect") {
             // Remove the live challenge sent by this player:
-            ArrayFun.remove(this.challenges, c => c.from.sid == data.from);
+            ArrayFun.remove(
+              this.challenges,
+              c => c.type == "live" && c.from.sid == data.from
+            );
           } else {
             // Remove the matching live game if now unreachable
             const gid = data.page.match(/[a-zA-Z0-9]+$/)[0];
@@ -600,7 +611,8 @@ export default {
           // NOTE: it may be live or correspondance
           const game = data.data;
           // Ignore games where I play (corr games)
-          if (game.players.every(p => p.id != this.st.user.id))
+          if (game.players.every(p =>
+            p.sid != this.st.user.sid || p.id != this.st.user.id))
           {
             let locGame = this.games.find(g => g.id == game.id);
             if (!locGame) {
@@ -797,7 +809,9 @@ export default {
         };
         this.launchGame(c);
       } else {
-        this.send("refusechallenge", { data: c.id, target: c.from.sid });
+        const oppsid = this.getOppsid(c);
+        if (oppsid)
+          this.send("refusechallenge", { data: c.id, target: oppsid });
       }
       this.send("deletechallenge", { data: c.id });
     },
@@ -857,13 +871,8 @@ export default {
         vid: c.vid,
         cadence: c.cadence
       };
-      let oppsid = c.from.sid; //may not be defined if corr + offline opp
-      if (!oppsid) {
-        oppsid = Object.keys(this.people).find(
-          sid => this.people[sid].id == c.from.id
-        );
-      }
       const notifyNewgame = () => {
+        const oppsid = this.getOppsid(c);
         if (oppsid)
           //opponent is online
           this.send("startgame", { data: gameInfo, target: oppsid });