X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=models%2FGame.js;h=23a74e81ed7140baadd5aabac8c569f5eb16e39c;hb=b955c65b942d09d24b5c3bed0d755d4f2f8f71f1;hp=d279514fe8cec84acf610bc051c8b95d28c4a4b1;hpb=fd08ab2c5b8931bb8c95cf7e9f2f95122647f991;p=vchess.git diff --git a/models/Game.js b/models/Game.js index d279514f..23a74e81 100644 --- a/models/Game.js +++ b/models/Game.js @@ -1 +1,140 @@ -//TODO: +var db = require("../utils/database"); + +/* + * Structure table Games: + * id: game id (int) + * vid: integer (variant id) + * fenStart: varchar (initial position) + * fen: varchar (current position) + * mainTime: integer + * addTime: integer (increment) + * score: varchar (result) + * + * Structure table Players: + * gid: ref game id + * uid: ref user id + * color: character + * rtime: real (remaining time) + * + * Structure table Moves: + * gid: ref game id + * move: varchar (description) + * message: text + * played: datetime + * idx: integer + * color: character + */ + +const GameModel = +{ + // mainTime and increment in milliseconds + create: function(vid, fen, mainTime, increment, players, cb) + { + db.serialize({ + let query = + "INSERT INTO Games (vid, fen, mainTime, addTime) " + + "VALUES (" + vid + ",'" + fen + "'," + mainTime + "," + increment + ")"; + db.run(insertQuery, err => { + if (!!err) + return cb(err); + db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => { + players.forEach(p => { + query = + "INSERT INTO Players VALUES " + + "(" + lastId["rowid"] + "," + p.id + "," + p.color + "," + mainTime + ")"; + db.run(query, cb); + }); + }); + }); + }); + }, + + // TODO: queries here could be async, and wait for all to complete + getOne: function(id, cb) + { + db.serialize(function() { + let query = + "SELECT v.name AS vname, g.fen, g.fenStart, g.score " + + "FROM Games g " + + "JOIN Variants v " + + " ON g.vid = v.id " + "WHERE id = " + id; + db.get(query, (err,gameInfo) => { + if (!!err) + return cb(err); + query = + "SELECT p.uid AS id, p.color, p.rtime, u.name " + + "FROM Players p " + + "JOIN Users u " + + " ON p.uid = u.id " + + "WHERE p.gid = " + id; + db.run(query, (err2,players) => { + if (!!err2) + return cb(err2); + query = + "SELECT move AS desc, message, played, idx, color " + + "FROM Moves " + + "WHERE gid = " + id; + db.run(query, (err3,moves) => { + if (!!err3) + return cb(err3); + const game = { + id: id, + vname: gameInfo.vname, + fenStart: gameInfo.fenStart, + fen: gameInfo.fen, + score: gameInfo.score, + players: players, + moves: moves, + }; + return cb(null, game); + }); + }); + }); + }); + }, + + getByUser: function(uid, cb) + { + db.serialize(function() { + // Next query is fine because a player appear at most once in a game + const query = + "SELECT gid " + + "FROM Players " + + "WHERE uid = " + uid; + db.run(query, (err,gameIds) => { + if (!!err) + return cb(err); + let gameArray = []; + gameIds.forEach(gidRow => { + GameModel.getOne(gidRow["gid"], (err2,game) => { + if (!!err2) + return cb(err2); + gameArray.push(game); + }); + }); + return cb(null, gameArray); + }); + }); + }, + + remove: function(id) + { + db.parallelize(function() { + let query = + "DELETE FROM Games " + + "WHERE id = " + id; + db.run(query); + query = + "DELETE FROM Players " + + "WHERE gid = " + id; + db.run(query); + query = + "DELETE FROM Moves " + + "WHERE gid = " + id; + db.run(query); + }); + }, +} + +module.exports = GameModel;