Experimental simplification in Game.vue (next step: check move validity when receiving)
[vchess.git] / server / models / Game.js
index 8cc526f..f05d2fe 100644 (file)
@@ -132,7 +132,7 @@ const GameModel = {
   getObserved: function(uid, cursor, cb) {
     db.serialize(function() {
       let query =
-        "SELECT id, vid, cadence, created, score, white, black " +
+        "SELECT id, vid, cadence, options, created, score, white, black " +
         "FROM Games " +
         "WHERE created < " + cursor + " ";
       if (uid > 0) {
@@ -144,6 +144,7 @@ const GameModel = {
         "ORDER BY created DESC " +
         "LIMIT 20"; //TODO: 20 hard-coded...
       db.all(query, (err, games) => {
+        games = games || [];
         // Query players names
         let pids = {};
         games.forEach(g => {
@@ -151,6 +152,7 @@ const GameModel = {
           if (!pids[g.black]) pids[g.black] = true;
         });
         UserModel.getByIds(Object.keys(pids), (err2, users) => {
+          users = users || [];
           let names = {};
           users.forEach(u => { names[u.id] = u.name; });
           cb(
@@ -161,6 +163,7 @@ const GameModel = {
                   id: g.id,
                   vid: g.vid,
                   cadence: g.cadence,
+                  options: g.options,
                   created: g.created,
                   score: g.score,
                   players: [
@@ -180,10 +183,11 @@ const GameModel = {
   getRunning: function(uid, cb) {
     db.serialize(function() {
       let query =
-        "SELECT id, vid, cadence, created, white, black " +
+        "SELECT id, vid, cadence, options, created, white, black " +
         "FROM Games " +
         "WHERE score = '*' AND (white = " + uid + " OR black = " + uid + ")";
       db.all(query, (err, games) => {
+        games = games || [];
         // Get movesCount (could be done in // with next query)
         query =
           "SELECT gid, COUNT(*) AS nbMoves " +
@@ -200,6 +204,7 @@ const GameModel = {
             if (!pids[g.black]) pids[g.black] = true;
           });
           UserModel.getByIds(Object.keys(pids), (err2, users) => {
+            users = users || [];
             let names = {};
             users.forEach(u => { names[u.id] = u.name; });
             cb(
@@ -210,6 +215,7 @@ const GameModel = {
                     id: g.id,
                     vid: g.vid,
                     cadence: g.cadence,
+                    options: g.options,
                     created: g.created,
                     score: g.score,
                     movesCount: movesCounts[g.id] || 0,
@@ -231,7 +237,7 @@ const GameModel = {
   getCompleted: function(uid, cursor, cb) {
     db.serialize(function() {
       let query =
-        "SELECT id, vid, cadence, created, score, scoreMsg, " +
+        "SELECT id, vid, cadence, options, created, score, scoreMsg, " +
           "white, black, deletedByWhite, deletedByBlack " +
         "FROM Games " +
         "WHERE " +
@@ -252,6 +258,7 @@ const GameModel = {
         "ORDER BY created DESC " +
         "LIMIT 20";
       db.all(query, (err, games) => {
+        games = games || [];
         // Query player names
         let pids = {};
         games.forEach(g => {
@@ -259,6 +266,7 @@ const GameModel = {
           if (!pids[g.black]) pids[g.black] = true;
         });
         UserModel.getByIds(Object.keys(pids), (err2, users) => {
+          users = users || [];
           let names = {};
           users.forEach(u => { names[u.id] = u.name; });
           cb(
@@ -269,6 +277,7 @@ const GameModel = {
                   id: g.id,
                   vid: g.vid,
                   cadence: g.cadence,
+                  options: g.options,
                   created: g.created,
                   score: g.score,
                   scoreMsg: g.scoreMsg,