Small improvement in Hall
[vchess.git] / client / src / views / Hall.vue
index 95a9e95..afa2173 100644 (file)
@@ -141,7 +141,7 @@ export default {
         response => {
           console.log(response.challenges);
           // TODO: post-treatment on challenges ?
-          this.challenges = this.challenges.concat(response.challenges);
+          Array.prototype.push.apply(this.challenges, response.challenges);
         }
       );
     }
@@ -178,6 +178,7 @@ export default {
       // ==> Moves sent by connected remote player(s) if live game
       
 // TODO: this doesn't work: choose a SID at random
+      // --> do we have players' names ?
 
       let url = "/" + g.id;
       if (g.type == "live")
@@ -361,6 +362,7 @@ export default {
         }
         case "deletechallenge":
         {
+          // NOTE: the challenge may be already removed
           ArrayFun.remove(this.challenges, c => c.id == data.cid);
           break;
         }
@@ -464,12 +466,14 @@ export default {
       console.log("click challenge");
       console.log(c);
 
-      if (c.from.sid == this.st.user.sid
-        || (this.st.user.id > 0 && c.from.id == this.st.user.id))
+      // In all cases, the challenge is consumed:
+      ArrayFun.remove(this.challenges, ch => ch.id == c.id);
+
+      if (c.from.sid == this.st.user.sid //live
+        || (this.st.user.id > 0 && c.from.id == this.st.user.id)) //corr
       {
         // It's my challenge: cancel it
         this.sendSomethingTo(c.to, "deletechallenge", {cid:c.id});
-        ArrayFun.remove(this.challenges, ch => ch.id == c.id);
         if (c.type == "corr")
         {
           ajax(
@@ -482,7 +486,7 @@ export default {
       else //accept (or refuse) a challenge
       {
         c.accepted = true;
-        if (!!c.to[0])
+        if (!!c.to)
         {
           // TODO: if special FEN, show diagram after loading variant
           c.accepted = confirm("Accept challenge?");
@@ -490,26 +494,14 @@ export default {
         this.st.conn.send(JSON.stringify({
           code: (c.accepted ? "accept" : "refuse") + "challenge",
           cid: c.id, target: c.from.sid}));
-        if (c.type == "corr" && c.accepted)
+        if (c.type == "corr")
         {
           ajax(
             "/challenges",
-            "PUT",
+            accepted ? "PUT" : "DELETE",
             {id: this.challenges[cIdx].id}
           );
         }
-        if (!c.accepted)
-        {
-          ArrayFun.remove(this.challenges, ch => ch.id == c.id);
-          if (c.type == "corr")
-          {
-            ajax(
-              "/challenges",
-              "DELETE",
-              {id: this.challenges[cIdx].id}
-            );
-          }
-        }
       }
     },
     // NOTE: for live games only (corr games are launched on server)