Remove 'var' keyword from server code
[vchess.git] / server / models / Game.js
index 16c050c..fb249a7 100644 (file)
@@ -1,4 +1,4 @@
-var db = require("../utils/database");
+const db = require("../utils/database");
 const UserModel = require("./User");
 
 /*
@@ -36,8 +36,6 @@ const GameModel =
   checkGameInfo: function(g) {
     if (!g.vid.toString().match(/^[0-9]+$/))
       return "Wrong variant ID";
-    if (!g.vname.match(/^[a-zA-Z0-9]+$/))
-      return "Wrong variant name";
     if (!g.cadence.match(/^[0-9dhms +]+$/))
       return "Wrong characters in time control";
     if (!g.fen.match(/^[a-zA-Z0-9, /-]*$/))
@@ -79,7 +77,7 @@ const GameModel =
       let query =
         // NOTE: g.scoreMsg can be NULL
         // (in this case score = "*" and no reason to look at it)
-        "SELECT g.id, g.vid, g.fen, g.fenStart, g.cadence, g.score, " +
+        "SELECT g.id, g.vid, g.fen, g.fenStart, g.cadence, g.created, g.score, " +
           "g.scoreMsg, g.drawOffer, v.name AS vname " +
         "FROM Games g " +
         "JOIN Variants v " +
@@ -139,24 +137,38 @@ const GameModel =
   getByUser: function(uid, excluded, cb)
   {
     db.serialize(function() {
-      const query =
-        "SELECT DISTINCT gid " +
-        "FROM Players " +
-        "WHERE uid " + (excluded ? "<>" : "=") + " " + uid;
+      let query = "";
+      if (uid == 0)
+      {
+        // Special case anonymous user: show all games
+        query =
+          "SELECT id AS gid " +
+          "FROM Games";
+      }
+      else
+      {
+        // Registered user:
+        query =
+          "SELECT gid " +
+          "FROM Players " +
+          "GROUP BY gid " +
+          "HAVING COUNT(uid = " + uid + " OR NULL) " +
+          (excluded ? " = 0" : " > 0");
+      }
       db.all(query, (err,gameIds) => {
-        if (!!err)
-          return cb(err);
-        if (gameIds.length == 0)
-          return cb(null, []);
+        if (!!err || gameIds.length == 0)
+          return cb(err, []);
         let gameArray = [];
+        let kounter = 0;
         for (let i=0; i<gameIds.length; i++)
         {
           GameModel.getOne(gameIds[i]["gid"], true, (err2,game) => {
             if (!!err2)
               return cb(err2);
             gameArray.push(game);
+            kounter++; //TODO: let's hope this is atomic?!
             // Call callback function only when gameArray is complete:
-            if (i == gameIds.length - 1)
+            if (kounter == gameIds.length)
               return cb(null, gameArray);
           });
         }
@@ -168,7 +180,7 @@ const GameModel =
   {
     db.serialize(function() {
       const query =
-        "SELECT id " +
+        "SELECT uid " +
         "FROM Players " +
         "WHERE gid = " + id;
       db.all(query, (err,players) => {
@@ -277,28 +289,21 @@ const GameModel =
     const day = 86400000;
     db.serialize(function() {
       let query =
-        "SELECT id,score,created " +
+        "SELECT id, created " +
         "FROM Games ";
       db.all(query, (err,games) => {
         games.forEach(g => {
           query =
-            "SELECT max(played) AS lastMaj " +
+            "SELECT count(*) as nbMoves, max(played) AS lastMaj " +
             "FROM Moves " +
             "WHERE gid = " + g.id;
-          db.get(query, (err2,updated) => {
-            if (!updated.lastMaj)
-            {
-              if (tsNow - g.created > 7*day)
-                return GameModel.remove(g.id);
-            }
-            else //at least one move
+          db.get(query, (err2,mstats) => {
+            // Remove games still not really started,
+            // with no action in the last 3 months:
+            if ((mstats.nbMoves == 0 && tsNow - g.created > 91*day) ||
+              (mstats.nbMoves == 1 && tsNow - mstats.lastMaj > 91*day))
             {
-              const lastMaj = updated.lastMaj;
-              if (g.score != "*" && tsNow - lastMaj > 7*day ||
-                g.score == "*" && tsNow - lastMaj > 91*day)
-              {
-                GameModel.remove(g.id);
-              }
+              GameModel.remove(g.id);
             }
           });
         });