Work on correspondance challenges system
authorBenjamin Auder <benjamin.auder@somewhere>
Tue, 12 Mar 2019 16:12:55 +0000 (17:12 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Tue, 12 Mar 2019 16:12:55 +0000 (17:12 +0100)
client/src/main.js
client/src/views/Hall.vue
server/models/Challenge.js
server/models/User.js
server/routes/challenges.js

index 22fe0cd..6547865 100644 (file)
@@ -37,13 +37,6 @@ new Vue({
   },
 }).$mount("#app");
 
-// TODO: get rules, dynamic import
-// Load a rules page (AJAX)
-// router.get("/rules/:vname([a-zA-Z0-9]+)", access.ajax, (req,res) => {
-//  const lang = selectLanguage(req, res);
-//  res.render("rules/" + req.params["vname"] + "/" + lang);
-// });
-//
 // board2, 3, 4 automatiquement, mais rules separement (les 3 pour une)
 // game : aussi systématique
 // problems: on-demand
index c91fb1e..1d842d5 100644 (file)
@@ -125,6 +125,10 @@ export default {
   created: function() {
     // Always add myself to players' list
     this.players.push(this.st.user);
+
+    console.log("created");
+ajax("/testtest", "DELETE");
+
     // Ask server for current corr games (all but mines)
 //    ajax(
 //      "",
@@ -208,7 +212,7 @@ export default {
           {target: sid}
         )));
       };
-      else if (!!to[0])
+      if (!!to[0])
       {
         to.forEach(pname => {
           // Challenge with targeted players
@@ -483,30 +487,34 @@ export default {
 // *  - prepare and start new game (if challenge is full after acceptation)
 // *    --> include challenge ID (so that opponents can delete the challenge too)
     clickChallenge: function(c) {
-      switch (c.type)
+      if (!!c.accepted)
       {
-        case "live":
-          if (!!c.accepted)
-          {
-            this.st.conn.send(JSON.stringify({code: "withdrawchallenge",
-              cid: c.id, target: c.from.sid}));
-          
-          break;
-        case "corr":
+        this.st.conn.send(JSON.stringify({code: "withdrawchallenge",
+          cid: c.id, target: c.from.sid}));
+        if (c.type == "corr")
+        {
           ajax(
             "/challenges",
             "PUT",
             {action:"withdraw", id: this.challenges[cIdx].id}
           );
-          break;
-      }
-      c.accepted = false;
-      {
+        }
+        c.accepted = false;
       }
-      else if (c.from.sid == this.st.user.sid) //it's my challenge: cancel it
+      else if (c.from.sid == this.st.user.sid
+        || (this.st.user.id > 0 && c.from.id == this.st.user.id))
       {
+        // 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(
+            "/challenges",
+            "DELETE",
+            {id: this.challenges[cIdx].id}
+          );
+        }
       }
       else //accept (or refuse) a challenge
       {
@@ -516,16 +524,35 @@ export default {
           // TODO: if special FEN, show diagram after loading variant
           c.accepted = confirm("Accept challenge?");
         }
+        const action = (c.accepted ? "accept" : "refuse");
         this.st.conn.send(JSON.stringify({
-          code: (c.accepted ? "accept" : "refuse") + "challenge",
+          code: action + "challenge",
           cid: c.id, target: c.from.sid}));
+        if (c.type == "corr")
+        {
+          ajax(
+            "/challenges",
+            "PUT",
+            {action: action, 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}
+            );
+          }
+        }
       }
     },
     // c.type == corr alors use id...sinon sid (figés)
     // NOTE: only for live games ?
-    launchGame: function(c) {
+    launchGame: async function(c) {
       // Just assign colors and pass the message
       const vname = this.getVname(c.vid);
       const vModule = await import("@/variants/" + vname + ".js");
@@ -537,7 +564,7 @@ export default {
         fen: c.fen || V.GenRandInitFen(),
         // Shuffle players order (white then black then other colors).
         // Players' names may be required if game start when a player is offline
-        players: shuffle(players).map(p => {name:p.name, sid:p.sid},
+        players: shuffle(players).map(p => { return {name:p.name, sid:p.sid} }),
         vid: c.vid,
         timeControl: c.timeControl,
       };
index e977267..8adf3ca 100644 (file)
@@ -126,6 +126,15 @@ const ChallengeModel =
                });
        },
 
+  testfunc: function()
+  {
+    db.serialize(function() {
+      db.run("DELETE * FROM TableTest", (err,ret) => {
+        console.log(ret);
+      });
+    });
+  },
+
        remove: function(id, uid)
        {
                db.serialize(function() {
@@ -133,10 +142,15 @@ const ChallengeModel =
                                "DELETE FROM Challenges " +
                                "WHERE id = " + id + " AND uid = " + uid;
                        db.run(query, (err,ret) => {
-                         if (!!err && query = //TODO
-                               "DELETE FROM WillPlay " +
-                               "WHERE cid = " + id;
-                       db.run(query);
+                         if (!err && ret >= 1)
+        {
+          // Also remove matching WillPlay entries if a challenge was deleted
+          query =
+                                   "DELETE FROM WillPlay " +
+                                   "WHERE cid = " + id;
+                           db.run(query);
+        }
+      });
                });
        },
 }
index 9401133..9e1aded 100644 (file)
@@ -66,9 +66,9 @@ const UserModel =
   getByName: function(names, cb) {
                db.serialize(function() {
                        const query =
-                               "SELECT id
+                               "SELECT id " +
         "FROM Users " +
-                               "WHERE name IN ('" + names.join("','") + "')";;
+                               "WHERE name IN ('" + names.join("','") + "')";
                        db.all(query, cb);
                });
   },
index b1e0694..99f055f 100644 (file)
@@ -5,6 +5,11 @@ const access = require("../utils/access");
 const ChallengeModel = require("../models/Challenge");
 const UserModel = require("../models/User"); //for name check
 
+router.delete("/testtest", access.ajax, (req,res) => {
+  console.log("passag");
+  ChallengeModel.testfunc();
+});
+
 router.post("/challenges", access.logged, access.ajax, (req,res) => {
   const error = ChallengeModel.checkChallenge(req.body.chall);
   if (!!error)