Small fixes + add some debug traces
[vchess.git] / server / models / Challenge.js
index 96db0a2..a5fbf63 100644 (file)
@@ -8,35 +8,59 @@ var db = require("../utils/database");
  *   vid: variant id (int)
  *   nbPlayers: integer
  *   fen: varchar (optional)
- *   mainTime: integer
- *   addTime: integer
+ *   timeControl: string (3m+2s, 7d+1d ...)
  *
  * Structure table WillPlay:
  *   cid: ref challenge id
  *   uid: ref user id
+ *   yes: boolean (false means "not decided yet")
  */
 
 const ChallengeModel =
 {
-       // fen cannot be undefined; TODO: generate fen on server instead
+       checkChallenge: function(c)
+       {
+    if (!c.vid.match(/^[0-9]+$/))
+                       return "Wrong variant ID";
+
+    if (!c.timeControl.match(/^[0-9dhms +]+$/))
+      return "Wrong characters in time control";
+
+               if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/))
+                       return "Bad FEN string";
+       },
+
+  initializeWillPlay: function(uids, cid, cb)
+  {
+    let query = "INSERT INTO WillPlay VALUES ";
+    for (let i=0; i<uids.length; i++)
+    {
+      query += "(false," + cid + "," + uids[i] + ")";
+      if (i < uids.length-1)
+        query += ",";
+    }
+    db.run(query, cb);
+  },
+
+       // fen cannot be undefined
        create: function(c, cb)
        {
                db.serialize(function() {
                        let query =
                                "INSERT INTO Challenges " +
-                               "(added, uid, vid, nbPlayers, fen, mainTime, addTime) VALUES " +
+                               "(added, uid, vid, nbPlayers, fen, timeControl) VALUES " +
                                "(" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers +
-                                       ",'" + c.fen + "'," + c.mainTime + "," + c.increment + ")";
+                                       ",'" + c.fen + "'," + c.timeControl + ")";
                        db.run(query, err => {
                                if (!!err)
                                        return cb(err);
                                db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
-                                       query =
+          query =
                                                "INSERT INTO WillPlay VALUES " +
-                                               "(" + lastId["rowid"] + "," + c.uid + ")";
-                                               db.run(query, (err,ret) => {
-                                                       cb(err, lastId); //all we need is the challenge ID
-                                               });
+                                               "(true," + lastId["rowid"] + "," + c.uid + ")";
+                                       db.run(query, (err,ret) => {
+                                               cb(err, lastId); //all we need is the challenge ID
+                                       });
                                });
                        });
                });
@@ -54,12 +78,15 @@ const ChallengeModel =
                        db.get(query, (err,challengeInfo) => {
                                if (!!err)
                                        return cb(err);
+        let condition = "";
+        if (!!challengeInfo.to[0])
+          condition = " AND u.name in (" + challengeInfo.to.join(",") + ")";
                                query =
                                        "SELECT w.uid AS id, u.name " +
                                        "FROM WillPlay w " +
                                        "JOIN Users u " +
                                        "  ON w.uid = u.id " +
-                                       "WHERE w.cid = " + id;
+                                       "WHERE w.cid = " + id + condition;
                                db.run(query, (err2,players) => {
                                        if (!!err2)
                                                return cb(err2);
@@ -71,8 +98,7 @@ const ChallengeModel =
                                                nbPlayers: challengeInfo.nbPlayers,
                                                players: players, //currently in
                                                fen: challengeInfo.fen,
-                                               mainTime: challengeInfo.mainTime,
-                                               increment: challengeInfo.addTime,
+                                               timeControl: challengeInfo.timeControl,
                                        };
                                        return cb(null, challenge);
                                });
@@ -90,6 +116,7 @@ const ChallengeModel =
                        db.run(query, (err,challIds) => {
                                if (!!err)
                                        return cb(err);
+        challIds = challIds || [];
                                let challenges = [];
                                challIds.forEach(cidRow => {
                                        ChallengeModel.getOne(cidRow["cid"], (err2,chall) => {
@@ -103,17 +130,63 @@ const ChallengeModel =
                });
        },
 
-       remove: function(id)
+  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.parallelize(function() {
+               db.serialize(function() {
                        let query =
-                               "DELETE FROM Challenges " +
-                               "WHERE id = " + id;
-                       db.run(query);
-                       query =
-                               "DELETE FROM WillPlay " +
-                               "WHERE cid = " + id;
-                       db.run(query);
+        "SELECT 1 " +
+        "FROM Challenges " +
+        "WHERE id = " + id + " AND uid = " + uid;
+      db.run(query, (err,rows) => {
+        if (rows.length > 0) //it's my challenge
+        {
+          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);
+          });
+        }
+      });
                });
        },
 }