Update TODO
[vchess.git] / server / models / Game.js
index 21ece2f..3945917 100644 (file)
@@ -91,11 +91,18 @@ const GameModel =
         db.all(query, (err2,players) => {
           if (light)
           {
-            const game = Object.assign({},
-              gameInfo,
-              {players: players}
-            );
-            cb(null, game);
+            query =
+              "SELECT COUNT(*) AS nbMoves " +
+              "FROM Moves " +
+              "WHERE gid = " + id;
+            db.get(query, (err,ret) => {
+              const game = Object.assign({},
+                gameInfo,
+                {players: players},
+                {movesCount: ret.nbMoves}
+              );
+              cb(null, game);
+            });
           }
           else
           {
@@ -190,17 +197,17 @@ const GameModel =
     return (
       (
         !obj.move || (
-          obj.move.played.toString().match(/^[0-9]+$/) &&
-          obj.move.idx.toString().match(/^[0-9]+$/)
+          !!(obj.move.played.toString().match(/^[0-9]+$/)) &&
+          !!(obj.move.idx.toString().match(/^[0-9]+$/))
         )
       ) && (
-        !obj.drawOffer || obj.drawOffer.match(/^[wbtn]$/)
+        !obj.drawOffer || !!(obj.drawOffer.match(/^[wbtn]$/))
       ) && (
-        !obj.fen || obj.fen.match(/^[a-zA-Z0-9, /-]*$/)
+        !obj.fen || !!(obj.fen.match(/^[a-zA-Z0-9, /-]*$/))
       ) && (
-        !obj.score || obj.score.match(/^[012?*\/-]+$/)
+        !obj.score || !!(obj.score.match(/^[012?*\/-]+$/))
       ) && (
-        !obj.scoreMsg || obj.scoreMsg.match(/^[a-zA-Z ]+$/)
+        !obj.scoreMsg || !!(obj.scoreMsg.match(/^[a-zA-Z ]+$/))
       ) && (
         !obj.chat || UserModel.checkNameEmail({name: obj.chat.name})
       )
@@ -235,8 +242,8 @@ const GameModel =
         query += modifs + " WHERE id = " + id;
         db.run(query);
       }
-      let wrongMoveIndex = false;
-      if (obj.move)
+      // NOTE: move, chat and delchat are mutually exclusive
+      if (!!obj.move)
       {
         // Security: only update moves if index is right
         query =
@@ -263,6 +270,14 @@ const GameModel =
             + id + ",?,'" + obj.chat.name + "'," + Date.now() + ")";
         db.run(query, obj.chat.msg);
       }
+      else if (obj.delchat)
+      {
+        query =
+          "DELETE " +
+          "FROM Chats " +
+          "WHERE gid = " + id;
+        db.run(query);
+      }
     });
   },