X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FGame.js;h=da59b4a6dbe6433e84beeceaa501b13fc12f0f0d;hb=dac395887d96e2d642b209c6db6aaacc3ffacb34;hp=5b225ce6a68bb82fea6f64aa50e2528f7e4ed3e0;hpb=58e7b94e6e1a8d5721b9211b45c40e65fc13f600;p=vchess.git diff --git a/server/models/Game.js b/server/models/Game.js index 5b225ce6..da59b4a6 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -9,6 +9,7 @@ const UserModel = require("./User"); * fen: varchar (current position) * timeControl: string * score: varchar (result) + * scoreMsg: varchar ("Time", "Mutual agreement"...) * created: datetime * drawOffer: boolean * @@ -27,15 +28,12 @@ const UserModel = require("./User"); * gid: game id (int) * msg: varchar * name: varchar - * sid: varchar (socket ID when sending message) * added: datetime */ const GameModel = { checkGameInfo: function(g) { - if (!g.id.toString().match(/^[0-9]+$/)) - return "Wrong game ID"; if (!g.vid.toString().match(/^[0-9]+$/)) return "Wrong variant ID"; if (!g.vname.match(/^[a-zA-Z0-9]+$/)) @@ -51,11 +49,11 @@ const GameModel = return ""; }, - create: function(vid, fen, timeControl, players, cb) - { - db.serialize(function() { - let query = - "INSERT INTO Games" + create: function(vid, fen, timeControl, players, cb) + { + db.serialize(function() { + let query = + "INSERT INTO Games" + " (vid, fenStart, fen, score, timeControl, created, drawOffer)" + " VALUES (" + vid + ",'" + fen + "','" + fen + "','*','" + timeControl + "'," + Date.now() + "," + false + ")"; @@ -70,49 +68,51 @@ const GameModel = db.run(query); }); cb(null, {gid: this.lastID}); - }); - }); - }, + }); + }); + }, - // TODO: queries here could be async, and wait for all to complete - getOne: function(id, cb) - { - db.serialize(function() { + // TODO: queries here could be async, and wait for all to complete + getOne: function(id, cb) + { + db.serialize(function() { // TODO: optimize queries? - let query = - "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " + - "v.name AS vname " + - "FROM Games g " + + let query = + // NOTE: g.scoreMsg can be NULL + // (in this case score = "*" and no reason to look at it) + "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " + + "g.scoreMsg, 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 p.uid, p.color, u.name " + - "FROM Players p " + + "WHERE g.id = " + id; + db.get(query, (err,gameInfo) => { + if (!!err) + return cb(err); + query = + "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 squares, played, idx " + - "FROM Moves " + - "WHERE gid = " + id; - db.all(query, (err3,moves) => { - if (!!err3) - return cb(err3); - query = - "SELECT msg, name, sid, added " + + "WHERE p.gid = " + id; + db.all(query, (err2,players) => { + if (!!err2) + return cb(err2); + query = + "SELECT squares, played, idx " + + "FROM Moves " + + "WHERE gid = " + id; + db.all(query, (err3,moves) => { + if (!!err3) + return cb(err3); + query = + "SELECT msg, name, added " + "FROM Chats " + "WHERE gid = " + id; - db.all(query, (err4,chats) => { - if (!!err4) - return cb(err4); - const game = Object.assign({}, + db.all(query, (err4,chats) => { + if (!!err4) + return cb(err4); + const game = Object.assign({}, gameInfo, { players: players, @@ -120,41 +120,41 @@ const GameModel = chats: chats, } ); - return cb(null, game); + return cb(null, game); }); - }); - }); - }); - }); - }, + }); + }); + }); + }); + }, - getByUser: function(uid, excluded, 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 " + (excluded ? "<>" : "=") + " " + uid; - db.all(query, (err,gameIds) => { - if (!!err) - return cb(err); + getByUser: function(uid, excluded, 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 " + (excluded ? "<>" : "=") + " " + uid; + db.all(query, (err,gameIds) => { + if (!!err) + return cb(err); gameIds = gameIds || []; //might be empty - let gameArray = []; - 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); - }); - } - }); - }); - }, + let gameArray = []; + 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); + }); + } + }); + }); + }, getPlayers: function(id, cb) { @@ -183,19 +183,17 @@ const GameModel = return "Wrong FEN string"; if (!!obj.score && !obj.score.match(/^[012?*\/-]+$/)) return "Wrong characters in score"; + if (!!obj.scoreMsg && !obj.scoreMsg.match(/^[a-zA-Z ]+$/)) + return "Wrong characters in score message"; if (!!obj.chat) - { - if (!obj.chat.sid.match(/^[a-zA-Z0-9]+$/)) - return "Wrong user SID"; return UserModel.checkNameEmail({name: obj.chat.name}); - } return ""; }, // obj can have fields move, chat, fen, drawOffer and/or score update: function(id, obj) { - db.parallelize(function() { + db.parallelize(function() { let query = "UPDATE Games " + "SET "; @@ -208,6 +206,8 @@ const GameModel = modifs += "fen = '" + obj.fen + "',"; if (!!obj.score) modifs += "score = '" + obj.score + "',"; + if (!!obj.scoreMsg) + modifs += "scoreMsg = '" + obj.scoreMsg + "',"; modifs = modifs.slice(0,-1); //remove last comma if (modifs.length > 0) { @@ -224,36 +224,35 @@ const GameModel = } if (!!obj.chat) { - query = - "INSERT INTO Chats (gid, msg, name, sid, added) VALUES " + - "(" + id + ",?,'" + obj.chat.name + "','" - + obj.chat.sid + "'," + Date.now() + ")"; + query = + "INSERT INTO Chats (gid, msg, name, added) VALUES (" + + id + ",?,'" + obj.chat.name + "'," + Date.now() + ")"; db.run(query, obj.chat.msg); } }); }, - 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); - query = - "DELETE FROM Chats " + - "WHERE gid = " + id; - db.run(query); - }); - }, + 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); + query = + "DELETE FROM Chats " + + "WHERE gid = " + id; + db.run(query); + }); + }, cleanGamesDb: function() {