Ongoing work on challenges + games for correspondance play
authorBenjamin Auder <benjamin.auder@somewhere>
Wed, 13 Mar 2019 16:45:22 +0000 (17:45 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Wed, 13 Mar 2019 16:45:22 +0000 (17:45 +0100)
client/src/views/Hall.vue
server/models/Challenge.js
server/routes/challenges.js
server/routes/games.js

index 19f2e77..e499d7e 100644 (file)
@@ -127,20 +127,24 @@ export default {
     this.players.push(this.st.user);
     // Ask server for current corr games (all but mines)
 //    ajax(
-//      "",
+//      "/games",
 //      "GET",
+//      {excluded: this.st.user.id},
 //      response => {
-//
-//      }
-//    );
-    //    // Also ask for corr challenges (all) --> + accepted status if I play
-//    ajax(
-//      "",
-//      "GET",
-//      response => {
-//
+//        this.games = this.games.concat(response.games);
 //      }
 //    );
+    // Also ask for corr challenges (open + personal to me)
+    ajax(
+      "/challenges",
+      "GET",
+      {uid: this.st.user.id},
+      response => {
+        console.log(response.challenges);
+        // TODO: post-treatment on challenges ?
+        this.challenges = this.challenges.concat(response.challenges);
+      }
+    );
     // 0.1] Ask server for room composition:
     const socketOpenListener = () => {
       this.st.conn.send(JSON.stringify({code:"pollclients"}));
@@ -520,16 +524,15 @@ 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: action + "challenge",
+          code: (c.accepted ? "accept" : "refuse") + "challenge",
           cid: c.id, target: c.from.sid}));
-        if (c.type == "corr")
+        if (c.type == "corr" && c.accepted)
         {
           ajax(
             "/challenges",
             "PUT",
-            {action: action, id: this.challenges[cIdx].id}
+            {action: "accept", id: this.challenges[cIdx].id}
           );
         }
         if (!c.accepted)
index de2818b..6f8ba0b 100644 (file)
@@ -126,20 +126,61 @@ const ChallengeModel =
                });
        },
 
+  getSeatCount: function(id, cb)
+  {
+               db.serialize(function() {
+      let query =
+        "SELECT COUNT(*) AS scount " +
+        "FROM WillPlay " +
+        "WHERE cid = " + id;
+      db.get(query, (err,scRow) => {
+        if (!!err)
+          return cb(err);
+        query =
+          "SELECT nbPlayers " +
+          "FROM Challenges " +
+          "WHERE id = " + id;
+        db.get(query, (err2,chRow) => {
+          if (!!err2)
+            return cb(err2);
+          cb(chRow["nbPlayers"] - scRow["scount"]);
+        });
+      });
+    });
+  },
+
+  setSeat: function(id, uid)
+  {
+    // TODO: remove extra "db.serialize" (parallelize by default)
+    //db.serialize(function() {
+    const query =
+      "INSERT OR REPLACE INTO WillPlay " +
+      "VALUES (true," + id + "," + uid +")";
+    db.run(query);
+    //});
+  },
+
        remove: function(id, uid)
        {
                db.serialize(function() {
                        let query =
-                               "DELETE FROM Challenges " +
-                               "WHERE id = " + id + " AND uid = " + uid;
-                       db.run(query, (err,ret) => {
-                         if (!err && ret >= 1)
+        "SELECT 1 " +
+        "FROM Challenges " +
+        "WHERE id = " + id + " AND uid = " + uid;
+      db.run(query, (err,rows) => {
+        if (rows.length > 0) //it's my challenge
         {
-          // Also remove matching WillPlay entries if a challenge was deleted
-          query =
-                                   "DELETE FROM WillPlay " +
-                                   "WHERE cid = " + id;
-                           db.run(query);
+          db.parallelize(function() {
+            query =
+              "DELETE FROM Challenges " +
+              "WHERE id = " + id;
+            db.run(query);
+            // Also remove matching WillPlay entries if a challenge was deleted
+            query =
+              "DELETE FROM WillPlay " +
+              "WHERE cid = " + id;
+            db.run(query);
+          });
         }
       });
                });
index 40c50ec..84b2c83 100644 (file)
@@ -5,6 +5,12 @@ const access = require("../utils/access");
 const ChallengeModel = require("../models/Challenge");
 const UserModel = require("../models/User"); //for name check
 
+router.get("/challenges", access.logged, access.ajax, (req,res) => {
+  ChallengeModel.getByUser(req.query["uid"], (err,challenges) => {
+    res.json(err || {challenges:challenges});
+  });
+});
+
 router.post("/challenges", access.logged, access.ajax, (req,res) => {
   const error = ChallengeModel.checkChallenge(req.body.chall);
   if (!!error)
@@ -43,6 +49,32 @@ router.post("/challenges", access.logged, access.ajax, (req,res) => {
   });
 });
 
+// Nothing to do if challenge is refused (just removal)
+router.put("/challenges", access.logged, access.ajax, (req,res) => {
+  switch (req.body.action)
+  {
+    case "withdraw":
+      // turn WillPlay to false (TODO?)
+      break;
+    case "accept":
+      // turn WillPlay to true; if then challenge is full, launch game
+      ChallengeModel.getSeatCount(req.body.id, (scount) => {
+        if (scount == 1)
+          launchGame(req.body.id, req.userId);
+        else
+          ChallengeModel.setSeat(req.body.id, req.userId);
+      })
+      break;
+  }
+  res.json({});
+});
+
+function launchGame(cid, uid)
+{
+  // TODO: gather challenge infos + WillPlay
+  // Then create game, and remove challenge + WillPlay
+}
+
 //// index
 //router.get("/challenges", access.logged, access.ajax, (req,res) => {
 //  if (req.query["uid"] != req.user._id)
index 808c258..35beb25 100644 (file)
@@ -1,3 +1,7 @@
+router.get("/games", access.logged, access.ajax, (req,res) => {
+  const excluded = req.query["excluded"]; //TODO: think about query params here
+});
+
 // TODO: adapt for correspondance play
 
 var router = require("express").Router();