Some fixes + first draft of Chakart (just thoughts for now)
[vchess.git] / server / models / Game.js
index aec80a0..1d2cd68 100644 (file)
@@ -18,6 +18,8 @@ const UserModel = require("./User");
  *   randomness: integer
  *   deletedByWhite: boolean
  *   deletedByBlack: boolean
+ *   chatReadWhite: datetime
+ *   chatReadBlack: datetime
  *
  * Structure table Moves:
  *   gid: ref game id
@@ -74,6 +76,7 @@ const GameModel =
         "SELECT " +
           "g.id, g.fen, g.fenStart, g.cadence, g.created, " +
           "g.white, g.black, g.score, g.scoreMsg, " +
+          "g.chatReadWhite, g.chatReadBlack, " +
           "g.drawOffer, g.rematchOffer, v.name AS vname " +
         "FROM Games g " +
         "JOIN Variants v " +
@@ -125,12 +128,13 @@ const GameModel =
     db.serialize(function() {
       let query =
         "SELECT id, vid, cadence, created, score, white, black " +
-        "FROM Games ";
-      if (uid > 0) query +=
-        "WHERE " +
-        "  created < " + cursor + " AND " +
-        "  white <> " + uid + " AND " +
-        "  black <> " + uid + " ";
+        "FROM Games " +
+        "WHERE created < " + cursor + " ";
+      if (uid > 0) {
+        query +=
+          "  AND white <> " + uid + " " +
+          "  AND black <> " + uid + " ";
+      }
       query +=
         "ORDER BY created DESC " +
         "LIMIT 20"; //TODO: 20 hard-coded...
@@ -306,9 +310,12 @@ const GameModel =
       ) && (
         !obj.rematchOffer || !!(obj.rematchOffer.match(/^[wbn]$/))
       ) && (
-        !obj.fen || !!(obj.fen.match(/^[a-zA-Z0-9, /-]*$/))
+        // TODO: check if commas are still used (probably not)
+        !obj.fen || !!(obj.fen.match(/^[a-zA-Z0-9,. /-]*$/))
       ) && (
         !obj.score || !!(obj.score.match(/^[012?*\/-]+$/))
+      ) && (
+        !obj.chatRead || ['w','b'].includes(obj.chatRead)
       ) && (
         !obj.scoreMsg || !!(obj.scoreMsg.match(/^[a-zA-Z ]+$/))
       ) && (
@@ -343,6 +350,10 @@ const GameModel =
         const myColor = obj.deletedBy == 'w' ? "White" : "Black";
         modifs += "deletedBy" + myColor + " = true,";
       }
+      if (!!obj.chatRead) {
+        const myColor = obj.chatRead == 'w' ? "White" : "Black";
+        modifs += "chatRead" + myColor + " = " + Date.now() + ",";
+      }
       if (!!obj.score) {
         modifs += "score = '" + obj.score + "'," +
                   "scoreMsg = '" + obj.scoreMsg + "',";
@@ -474,6 +485,7 @@ const GameModel =
               )
               ||
               (
+                !!movesGroups[g.id] &&
                 movesGroups[g.id].nbMoves == 1 &&
                 tsNow - movesGroups[g.id].lastMaj > 14*day
               )