Finish Pacosako + add GameStat table to know how many live games are played
[vchess.git] / server / models / Game.js
index 1d2cd68..4a8c517 100644 (file)
@@ -34,8 +34,8 @@ const UserModel = require("./User");
  *   added: datetime
  */
 
-const GameModel =
-{
+const GameModel = {
+
   checkGameInfo: function(g) {
     return (
       g.vid.toString().match(/^[0-9]+$/) &&
@@ -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 =
@@ -381,8 +391,10 @@ const GameModel =
                 "(" + id + ",?," + Date.now() + "," + obj.move.idx + ")";
               db.run(query, JSON.stringify(obj.move.squares));
               finishAndSendQuery();
-            } else cb({ errmsg: "Wrong move index" });
-          } else {
+            }
+            else cb({ errmsg: "Wrong move index" });
+          }
+          else {
             if (ret.maxIdx < 2) cb({ errmsg: "Time not over" });
             else {
               // We also need the game cadence
@@ -399,14 +411,16 @@ const GameModel =
             }
           }
         });
-      } else finishAndSendQuery();
+      }
+      else finishAndSendQuery();
       // NOTE: chat and delchat are mutually exclusive
       if (!!obj.chat) {
         const query =
           "INSERT INTO Chats (gid, msg, name, added) VALUES ("
             + id + ",?,'" + obj.chat.name + "'," + Date.now() + ")";
         db.run(query, obj.chat.msg);
-      } else if (obj.delchat) {
+      }
+      else if (obj.delchat) {
         const query =
           "DELETE " +
           "FROM Chats " +
@@ -498,6 +512,7 @@ const GameModel =
       });
     });
   }
-}
+
+};
 
 module.exports = GameModel;