X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FGame.js;h=0949fc98712c870fae9dd7d13d549ec8b0eae3cb;hb=afd3240d89a2f6191fe9426960dc0c1667b40c77;hp=ced561127f8cbbf7645bd532bb8e4eaff1c16ca8;hpb=f41ce5806b989c06091a403d7e26ff3c457650c9;p=vchess.git diff --git a/server/models/Game.js b/server/models/Game.js index ced56112..0949fc98 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -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 { 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); } });