X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=server%2Fmodels%2FGame.js;h=4a8c517a19b136e612977aba5853cb12427ddb32;hb=4a2093139089632727de4f510127ef186cab528e;hp=ed3fb436976cd2ac82be2de9b1cf82cd4176eda3;hpb=077ba3446c4ea3c6553c325fc8d16a96b7a9ff4f;p=vchess.git diff --git a/server/models/Game.js b/server/models/Game.js index ed3fb436..4a8c517a 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -34,8 +34,8 @@ const UserModel = require("./User"); * added: datetime */ -const GameModel = -{ +const GameModel = { + checkGameInfo: function(g) { return ( g.vid.toString().match(/^[0-9]+$/) && @@ -47,6 +47,16 @@ const GameModel = ); }, + incrementCounter: function(vid, cb) { + db.serialize(function() { + let query = + "UPDATE GameStat " + + "SET total = total + 1 " + + "WHERE vid = " + vid; + db.run(query, cb); + }); + }, + create: function(vid, fen, randomness, cadence, players, cb) { db.serialize(function() { let query = @@ -128,12 +138,13 @@ const GameModel = db.serialize(function() { let query = "SELECT id, vid, cadence, created, score, white, black " + - "FROM Games "; - if (uid > 0) query += - "WHERE " + - " created < " + cursor + " AND " + - " white <> " + uid + " AND " + - " black <> " + uid + " "; + "FROM Games " + + "WHERE created < " + cursor + " "; + if (uid > 0) { + query += + " AND white <> " + uid + " " + + " AND black <> " + uid + " "; + } query += "ORDER BY created DESC " + "LIMIT 20"; //TODO: 20 hard-coded... @@ -309,7 +320,8 @@ const GameModel = ) && ( !obj.rematchOffer || !!(obj.rematchOffer.match(/^[wbn]$/)) ) && ( - !obj.fen || !!(obj.fen.match(/^[a-zA-Z0-9, /-]*$/)) + // TODO: check if commas are still used (probably not) + !obj.fen || !!(obj.fen.match(/^[a-zA-Z0-9,. /-]*$/)) ) && ( !obj.score || !!(obj.score.match(/^[012?*\/-]+$/)) ) && ( @@ -379,8 +391,10 @@ const GameModel = "(" + id + ",?," + Date.now() + "," + obj.move.idx + ")"; db.run(query, JSON.stringify(obj.move.squares)); finishAndSendQuery(); - } else cb({ errmsg: "Wrong move index" }); - } else { + } + else cb({ errmsg: "Wrong move index" }); + } + else { if (ret.maxIdx < 2) cb({ errmsg: "Time not over" }); else { // We also need the game cadence @@ -397,14 +411,16 @@ const GameModel = } } }); - } else finishAndSendQuery(); + } + else finishAndSendQuery(); // NOTE: chat and delchat are mutually exclusive if (!!obj.chat) { const query = "INSERT INTO Chats (gid, msg, name, added) VALUES (" + id + ",?,'" + obj.chat.name + "'," + Date.now() + ")"; db.run(query, obj.chat.msg); - } else if (obj.delchat) { + } + else if (obj.delchat) { const query = "DELETE " + "FROM Chats " + @@ -483,6 +499,7 @@ const GameModel = ) || ( + !!movesGroups[g.id] && movesGroups[g.id].nbMoves == 1 && tsNow - movesGroups[g.id].lastMaj > 14*day ) @@ -495,6 +512,7 @@ const GameModel = }); }); } -} + +}; module.exports = GameModel;