1 var db
= require("../utils/database");
2 const UserModel
= require("./User");
5 * Structure table Games:
7 * vid: integer (variant id)
8 * fenStart: varchar (initial position)
9 * fen: varchar (current position)
11 * score: varchar (result)
12 * scoreMsg: varchar ("Time", "Mutual agreement"...)
14 * drawOffer: char ('w','b' or '' for none)
16 * Structure table Players:
21 * Structure table Moves:
23 * squares: varchar (description)
27 * Structure table Chats:
36 checkGameInfo: function(g
) {
37 if (!g
.vid
.toString().match(/^[0-9]+$/))
38 return "Wrong variant ID";
39 if (!g
.cadence
.match(/^[0-9dhms
+]+$/))
40 return "Wrong characters in time control";
41 if (!g
.fen
.match(/^[a-zA-Z0-9, /-]*$/))
42 return "Bad FEN string";
43 if (g
.players
.length
!= 2)
44 return "Need exactly 2 players";
45 if (g
.players
.some(p
=> !p
.id
.toString().match(/^[0-9]+$/)))
46 return "Wrong characters in player ID";
50 create: function(vid
, fen
, cadence
, players
, cb
)
52 db
.serialize(function() {
54 "INSERT INTO Games " +
55 "(vid, fenStart, fen, score, cadence, created, drawOffer) " +
57 "(" + vid
+ ",'" + fen
+ "','" + fen
+ "','*','" + cadence
+ "'," + Date
.now() + ",'')";
58 db
.run(query
, function(err
) {
61 players
.forEach((p
,idx
) => {
62 const color
= (idx
==0 ? "w" : "b");
64 "INSERT INTO Players VALUES " +
65 "(" + this.lastID
+ "," + p
.id
+ ",'" + color
+ "')";
68 cb(null, {gid: this.lastID
});
73 // TODO: some queries here could be async
74 getOne: function(id
, light
, cb
)
76 db
.serialize(function() {
78 // NOTE: g.scoreMsg can be NULL
79 // (in this case score = "*" and no reason to look at it)
80 "SELECT g.id, g.vid, g.fen, g.fenStart, g.cadence, g.score, " +
81 "g.scoreMsg, g.drawOffer, v.name AS vname " +
86 db
.get(query
, (err
,gameInfo
) => {
90 "SELECT p.uid, p.color, u.name " +
94 "WHERE p.gid = " + id
;
95 db
.all(query
, (err2
,players
) => {
100 const game
= Object
.assign({},
104 return cb(null, game
);
107 "SELECT squares, played, idx " +
110 db
.all(query
, (err3
,moves
) => {
114 "SELECT msg, name, added " +
117 db
.all(query
, (err4
,chats
) => {
120 const game
= Object
.assign({},
128 return cb(null, game
);
136 // For display on MyGames or Hall: no need for moves or chats
137 getByUser: function(uid
, excluded
, cb
)
139 db
.serialize(function() {
141 "SELECT DISTINCT gid " +
143 "WHERE uid " + (excluded
? "<>" : "=") + " " + uid
;
144 db
.all(query
, (err
,gameIds
) => {
147 if (gameIds
.length
== 0)
150 for (let i
=0; i
<gameIds
.length
; i
++)
152 GameModel
.getOne(gameIds
[i
]["gid"], true, (err2
,game
) => {
155 gameArray
.push(game
);
156 // Call callback function only when gameArray is complete:
157 if (i
== gameIds
.length
- 1)
158 return cb(null, gameArray
);
165 getPlayers: function(id
, cb
)
167 db
.serialize(function() {
172 db
.all(query
, (err
,players
) => {
173 return cb(err
, players
);
178 checkGameUpdate: function(obj
)
180 // Check all that is possible (required) in obj:
183 if (!obj
.move.played
.toString().match(/^[0-9]+$/))
184 return "Wrong move played time";
185 if (!obj
.move.idx
.toString().match(/^[0-9]+$/))
186 return "Wrong move index";
188 if (!!obj
.drawOffer
&& !obj
.drawOffer
.match(/^[wbtn]$/))
189 return "Wrong draw offer format";
190 if (!!obj
.fen
&& !obj
.fen
.match(/^[a-zA-Z0-9, /-]*$/))
191 return "Wrong FEN string";
192 if (!!obj
.score
&& !obj
.score
.match(/^[012?*\/-]+$/))
193 return "Wrong characters in score";
194 if (!!obj
.scoreMsg
&& !obj
.scoreMsg
.match(/^[a
-zA
-Z
]+$/))
195 return "Wrong characters in score message";
197 return UserModel
.checkNameEmail({name: obj
.chat
.name
});
201 // obj can have fields move, chat, fen, drawOffer and/or score
202 update: function(id
, obj
)
204 db
.parallelize(function() {
210 modifs
+= "message = message || ' ' || '" + obj
.message
+ "',";
211 // NOTE: if drawOffer is set, we should check that it's player's turn
212 // A bit overcomplicated. Let's trust the client on that for now...
215 if (obj
.drawOffer
== "n") //Special "None" update
217 modifs
+= "drawOffer = '" + obj
.drawOffer
+ "',";
220 modifs
+= "fen = '" + obj
.fen
+ "',";
222 modifs
+= "score = '" + obj
.score
+ "',";
224 modifs
+= "scoreMsg = '" + obj
.scoreMsg
+ "',";
225 modifs
= modifs
.slice(0,-1); //remove last comma
226 if (modifs
.length
> 0)
228 query
+= modifs
+ " WHERE id = " + id
;
235 "INSERT INTO Moves (gid, squares, played, idx) VALUES " +
236 "(" + id
+ ",?," + m
.played
+ "," + m
.idx
+ ")";
237 db
.run(query
, JSON
.stringify(m
.squares
));
242 "INSERT INTO Chats (gid, msg, name, added) VALUES ("
243 + id
+ ",?,'" + obj
.chat
.name
+ "'," + Date
.now() + ")";
244 db
.run(query
, obj
.chat
.msg
);
251 db
.parallelize(function() {
253 "DELETE FROM Games " +
257 "DELETE FROM Players " +
261 "DELETE FROM Moves " +
265 "DELETE FROM Chats " +
271 cleanGamesDb: function()
273 const tsNow
= Date
.now();
274 // 86400000 = 24 hours in milliseconds
275 const day
= 86400000;
276 db
.serialize(function() {
278 "SELECT id, created " +
280 db
.all(query
, (err
,games
) => {
283 "SELECT count(*) as nbMoves, max(played) AS lastMaj " +
285 "WHERE gid = " + g
.id
;
286 db
.get(query
, (err2
,mstats
) => {
287 // Remove games still not really started,
288 // with no action in the last 3 months:
289 if ((mstats
.nbMoves
== 0 && tsNow
- g
.created
> 91*day
) ||
290 (mstats
.nbMoves
== 1 && tsNow
- mstats
.lastMaj
> 91*day
))
292 GameModel
.remove(g
.id
);
301 module
.exports
= GameModel
;