A few fixes, drop planned problems support (replaced by forum + mode analyze)
[vchess.git] / server / models / Game.js
index ced5611..0949fc9 100644 (file)
@@ -20,7 +20,6 @@ var db = require("../utils/database");
  *   message: text
  *   played: datetime
  *   idx: integer
- *   color: character
  */
 
 const GameModel =
@@ -71,7 +70,7 @@ const GameModel =
                                        if (!!err2)
                                                return cb(err2);
                                        query =
-                                               "SELECT squares, message, played, idx, color " +
+                                               "SELECT squares, message, played, idx " +
                                                "FROM Moves " +
                                                "WHERE gid = " + id;
                                        db.all(query, (err3,moves) => {
@@ -99,19 +98,22 @@ const GameModel =
                                "SELECT gid " +
                                "FROM Players " +
                                "WHERE uid " + (excluded ? "<>" : "=") + " " + uid;
-                       db.run(query, (err,gameIds) => {
+                       db.all(query, (err,gameIds) => {
                                if (!!err)
                                        return cb(err);
         gameIds = gameIds || []; //might be empty
                                let gameArray = [];
-                               gameIds.forEach(gidRow => {
-                                       GameModel.getOne(gidRow["gid"], (err2,game) => {
+                               for (let i=0; i<gameIds.length; i++)
+                               {
+                                       GameModel.getOne(gameIds[i]["gid"], (err2,game) => {
                                                if (!!err2)
                                                        return cb(err2);
                                                gameArray.push(game);
+                                               // Call callback function only when gameArray is complete:
+                                               if (i == gameIds.length - 1)
+                                                       return cb(null, gameArray);
                                        });
-                               });
-                               return cb(null, gameArray);
+                               }
                        });
                });
        },
@@ -132,31 +134,24 @@ const GameModel =
   // obj can have fields move, fen and/or score
   update: function(id, obj)
   {
-
-
-
-console.log(id);
-    console.log(obj);
-
-
                db.parallelize(function() {
       let query =
         "UPDATE Games " +
         "SET ";
       if (!!obj.fen)
-        query += "fen = " + obj.fen + ",";
+        query += "fen = '" + obj.fen + "',";
       if (!!obj.score)
-        query += "score = " + obj.score + ",";
+        query += "score = '" + obj.score + "',";
       query = query.slice(0,-1); //remove last comma
-      query += " WHERE gameId = " + id;
+      query += " WHERE id = " + id;
       db.run(query);
       if (!!obj.move)
       {
-        const m  =obj.move;
+        const m obj.move;
         query =
-          "INSERT INTO Moves (gid,squares,message,played,idx,color) VALUES " +
+          "INSERT INTO Moves (gid, squares, message, played, idx) VALUES " +
           "(" + id + ",'" + JSON.stringify(m.squares) + "','" + m.message +
-            "'" + m.played + "," + m.idx + ",'" + m.color + "')";
+            "'," + m.played + "," + m.idx + ")";
         db.run(query);
       }
     });