Fix moves transmission for Synchrone1/2
[vchess.git] / server / models / Game.js
index 02ef2d1..5f9ab73 100644 (file)
@@ -47,6 +47,16 @@ const GameModel = {
     );
   },
 
+  incrementCounter: function(vid, cb) {
+    db.serialize(function() {
+      let query =
+        "UPDATE GameStat " +
+        "SET total = total + 1 " +
+        "WHERE vid = " + vid;
+      db.run(query, cb);
+    });
+  },
+
   create: function(vid, fen, randomness, cadence, players, cb) {
     db.serialize(function() {
       let query =
@@ -75,9 +85,10 @@ const GameModel = {
       let query =
         "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 " +
+          "g.white, g.black, g.randomness, g.score, g.scoreMsg, " +
+          "g.chatReadWhite, g.chatReadBlack, g.drawOffer, " +
+          // TODO: vid and vname are redundant
+          "g.rematchOffer, v.id as vid, v.name AS vname " +
         "FROM Games g " +
         "JOIN Variants v " +
         "  ON g.vid = v.id " +
@@ -310,8 +321,7 @@ const GameModel = {
       ) && (
         !obj.rematchOffer || !!(obj.rematchOffer.match(/^[wbn]$/))
       ) && (
-        // TODO: check if commas are still used (probably not)
-        !obj.fen || !!(obj.fen.match(/^[a-zA-Z0-9,. /-]*$/))
+        !obj.fen || !!(obj.fen.match(/^[a-zA-Z0-9,.:{}\[\]" /-]*$/))
       ) && (
         !obj.score || !!(obj.score.match(/^[012?*\/-]+$/))
       ) && (
@@ -462,7 +472,7 @@ const GameModel = {
     const day = 86400000;
     db.serialize(function() {
       let query =
-        "SELECT id, created " +
+        "SELECT id, created, cadence, score " +
         "FROM Games";
       db.all(query, (err, games) => {
         query =
@@ -479,25 +489,51 @@ const GameModel = {
             };
           });
           // Remove games still not really started,
-          // with no action in the last 2 weeks:
+          // with no action in the last 2 weeks, or result != '*':
           let toRemove = [];
+          let lostOnTime = [ [], [] ];
           games.forEach(g => {
             if (
               (
                 !movesGroups[g.id] &&
-                tsNow - g.created > 14*day
+                (g.score != '*' || tsNow - g.created > 14*day)
               )
               ||
               (
                 !!movesGroups[g.id] &&
                 movesGroups[g.id].nbMoves == 1 &&
-                tsNow - movesGroups[g.id].lastMaj > 14*day
+                (g.score != '*' || tsNow - movesGroups[g.id].lastMaj > 14*day)
               )
             ) {
               toRemove.push(g.id);
             }
+            // Set score if lost on time and >= 2 moves:
+            else if (
+              g.score == '*' &&
+              !!movesGroups[g.id] &&
+              movesGroups[g.id].nbMoves >= 2 &&
+              tsNow - movesGroups[g.id].lastMaj >
+                // cadence in days * nb seconds per day:
+                parseInt(g.cadence.slice(0, -1), 10) * day
+            ) {
+              lostOnTime[movesGroups[g.id].nbMoves % 2].push(g.id);
+            }
           });
           if (toRemove.length > 0) GameModel.remove(toRemove);
+          if (lostOnTime.some(l => l.length > 0)) {
+            db.parallelize(function() {
+              for (let i of [0, 1]) {
+                if (lostOnTime[i].length > 0) {
+                  const score = (i == 0 ? "0-1" : "1-0");
+                  const query =
+                    "UPDATE Games " +
+                    "SET score = '" + score + "', scoreMsg = 'Time' " +
+                    "WHERE id IN (" + lostOnTime[i].join(',') + ")";
+                  db.run(query);
+                }
+              }
+            });
+          }
         });
       });
     });