X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FGame.js;h=0949fc98712c870fae9dd7d13d549ec8b0eae3cb;hb=afd3240d89a2f6191fe9426960dc0c1667b40c77;hp=315b5e2b4b331f41ad59549a6ab5f405035f866a;hpb=3d55deea9a2011c38d8d0067bd57fc889958bec2;p=vchess.git diff --git a/server/models/Game.js b/server/models/Game.js index 315b5e2b..0949fc98 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -13,15 +13,13 @@ var db = require("../utils/database"); * gid: ref game id * uid: ref user id * color: character - * rtime: real (remaining time) * * Structure table Moves: * gid: ref game id - * move: varchar (description) + * squares: varchar (description) * message: text * played: datetime * idx: integer - * color: character */ const GameModel = @@ -30,17 +28,16 @@ const GameModel = { db.serialize(function() { let query = - "INSERT INTO Games (vid, fenStart, score, timeControl) " + - "VALUES (" + vid + ",'" + fen + "','*','" + timeControl + "')"; + "INSERT INTO Games (vid, fenStart, fen, score, timeControl) VALUES " + + "(" + vid + ",'" + fen + "','" + fen + "','*','" + timeControl + "')"; db.run(query, function(err) { - if (!!err) - return cb(err); + if (!!err) + return cb(err); players.forEach((p,idx) => { const color = (idx==0 ? "w" : "b"); query = "INSERT INTO Players VALUES " + - // Remaining time = -1 means "unstarted" - "(" + this.lastID + "," + p.id + ",'" + color + "', -1)"; + "(" + this.lastID + "," + p.id + ",'" + color + "')"; db.run(query); }); cb(null, {gid: this.lastID}); @@ -52,22 +49,28 @@ const GameModel = getOne: function(id, cb) { db.serialize(function() { + // TODO: optimize queries? let query = - "SELECT * " + - "FROM Games " + - "WHERE id = " + id; + "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " + + "v.name AS vname " + + "FROM Games g " + + "JOIN Variants v " + + " ON g.vid = v.id " + + "WHERE g.id = " + id; db.get(query, (err,gameInfo) => { if (!!err) return cb(err); query = - "SELECT uid, color, rtime " + - "FROM Players " + - "WHERE gid = " + id; + "SELECT p.uid, p.color, u.name " + + "FROM Players p " + + "JOIN Users u " + + " ON p.uid = u.id " + + "WHERE p.gid = " + id; db.all(query, (err2,players) => { if (!!err2) return cb(err2); query = - "SELECT move, message, played, idx, color " + + "SELECT squares, message, played, idx " + "FROM Moves " + "WHERE gid = " + id; db.all(query, (err3,moves) => { @@ -95,41 +98,62 @@ 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); + } }); }); }, + getPlayers: function(id, cb) + { + db.serialize(function() { + const query = + "SELECT id " + + "FROM Players " + + "WHERE gid = " + id; + db.all(query, (err,players) => { + return cb(err, players); + }); + }); + }, + // obj can have fields move, fen and/or score - update: function(id, obj, cb) + update: function(id, obj) { - db.serialize(function() { + db.parallelize(function() { let query = "UPDATE Games " + "SET "; - if (!!obj.move) - query += "move = " + obj.move + ","; //TODO: already stringified?! 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; - db.run(query, (err) => { - cb(err); - }); + query += " WHERE id = " + id; + db.run(query); + if (!!obj.move) + { + const m = obj.move; + query = + "INSERT INTO Moves (gid, squares, message, played, idx) VALUES " + + "(" + id + ",'" + JSON.stringify(m.squares) + "','" + m.message + + "'," + m.played + "," + m.idx + ")"; + db.run(query); + } }); },