92c013016034416daf8f27772acac4ae5553ecb0
1 const 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
) {
38 g
.vid
.toString().match(/^[0-9]+$/) &&
39 g
.cadence
.match(/^[0-9dhms
+]+$/) &&
40 g
.fen
.match(/^[a-zA-Z0-9, /-]*$/) &&
41 g
.players
.length
== 2 &&
42 g
.players
.every(p
=> p
.id
.toString().match(/^[0-9]+$/))
46 create: function(vid
, fen
, cadence
, players
, cb
)
48 db
.serialize(function() {
50 "INSERT INTO Games " +
51 "(vid, fenStart, fen, score, cadence, created, drawOffer) " +
53 "(" + vid
+ ",'" + fen
+ "','" + fen
+ "','*','" + cadence
+ "'," + Date
.now() + ",'')";
54 db
.run(query
, function(err
) {
59 players
.forEach((p
,idx
) => {
60 const color
= (idx
==0 ? "w" : "b");
62 "INSERT INTO Players VALUES " +
63 "(" + this.lastID
+ "," + p
.id
+ ",'" + color
+ "')";
66 cb(null, {gid: this.lastID
});
72 // TODO: some queries here could be async
73 getOne: function(id
, light
, cb
)
75 // NOTE: ignoring errors (shouldn't happen at this stage)
76 db
.serialize(function() {
78 "SELECT g.id, g.vid, g.fen, g.fenStart, g.cadence, g.created, g.score, " +
79 "g.scoreMsg, g.drawOffer, v.name AS vname " +
84 db
.get(query
, (err
,gameInfo
) => {
86 "SELECT p.uid, p.color, u.name " +
90 "WHERE p.gid = " + id
;
91 db
.all(query
, (err2
,players
) => {
95 "SELECT COUNT(*) AS nbMoves " +
98 db
.get(query
, (err
,ret
) => {
99 const game
= Object
.assign({},
102 {movesCount: ret
.nbMoves
}
109 // Full game requested:
111 "SELECT squares, played, idx " +
114 db
.all(query
, (err3
,moves
) => {
116 "SELECT msg, name, added " +
119 db
.all(query
, (err4
,chats
) => {
120 const game
= Object
.assign({},
137 // For display on MyGames or Hall: no need for moves or chats
138 getByUser: function(uid
, excluded
, cb
)
140 db
.serialize(function() {
144 // Special case anonymous user: show all games
146 "SELECT id AS gid " +
156 "HAVING COUNT(uid = " + uid
+ " OR NULL) " +
157 (excluded
? " = 0" : " > 0");
159 db
.all(query
, (err
,gameIds
) => {
160 if (err
|| gameIds
.length
== 0)
166 for (let i
=0; i
<gameIds
.length
; i
++)
168 GameModel
.getOne(gameIds
[i
]["gid"], true, (err2
,game
) => {
169 gameArray
.push(game
);
170 gCounter
++; //TODO: let's hope this is atomic?!
171 // Call callback function only when gameArray is complete:
172 if (gCounter
== gameIds
.length
)
181 getPlayers: function(id
, cb
)
183 db
.serialize(function() {
188 db
.all(query
, (err
,players
) => {
189 return cb(err
, players
);
194 checkGameUpdate: function(obj
)
196 // Check all that is possible (required) in obj:
200 obj
.move.played
.toString().match(/^[0-9]+$/) &&
201 obj
.move.idx
.toString().match(/^[0-9]+$/)
204 !obj
.drawOffer
|| obj
.drawOffer
.match(/^[wbtn]$/)
206 !obj
.fen
|| obj
.fen
.match(/^[a-zA-Z0-9, /-]*$/)
208 !obj
.score
|| obj
.score
.match(/^[012?*\/-]+$/)
210 !obj
.scoreMsg
|| obj
.scoreMsg
.match(/^[a
-zA
-Z
]+$/)
212 !obj
.chat
|| UserModel
.checkNameEmail({name: obj
.chat
.name
})
217 // obj can have fields move, chat, fen, drawOffer and/or score + message
218 update: function(id
, obj
, cb
)
220 db
.parallelize(function() {
225 // NOTE: if drawOffer is set, we should check that it's player's turn
226 // A bit overcomplicated. Let's trust the client on that for now...
229 if (obj
.drawOffer
== "n") //Special "None" update
231 modifs
+= "drawOffer = '" + obj
.drawOffer
+ "',";
234 modifs
+= "fen = '" + obj
.fen
+ "',";
236 modifs
+= "score = '" + obj
.score
+ "',";
238 modifs
+= "scoreMsg = '" + obj
.scoreMsg
+ "',";
239 modifs
= modifs
.slice(0,-1); //remove last comma
240 if (modifs
.length
> 0)
242 query
+= modifs
+ " WHERE id = " + id
;
245 // NOTE: move, chat and delchat are mutually exclusive
248 // Security: only update moves if index is right
250 "SELECT MAX(idx) AS maxIdx " +
253 db
.get(query
, (err
,ret
) => {
256 return cb({errmsg: ret
.maxIdx
+ " " + m
.idx
+ " " + (!ret
.maxIdx
|| ret
.maxIdx
+ 1 == m
.idx
) + " " + query
});
259 if (!ret
.maxIdx
|| ret
.maxIdx
+ 1 == m
.idx
) {
261 "INSERT INTO Moves (gid, squares, played, idx) VALUES " +
262 "(" + id
+ ",?," + m
.played
+ "," + m
.idx
+ ")";
263 db
.run(query
, JSON
.stringify(m
.squares
));
266 else cb({errmsg:"Wrong move index"});
273 "INSERT INTO Chats (gid, msg, name, added) VALUES ("
274 + id
+ ",?,'" + obj
.chat
.name
+ "'," + Date
.now() + ")";
275 db
.run(query
, obj
.chat
.msg
);
277 else if (obj
.delchat
)
290 db
.parallelize(function() {
292 "DELETE FROM Games " +
296 "DELETE FROM Players " +
300 "DELETE FROM Moves " +
304 "DELETE FROM Chats " +
310 cleanGamesDb: function()
312 const tsNow
= Date
.now();
313 // 86400000 = 24 hours in milliseconds
314 const day
= 86400000;
315 db
.serialize(function() {
317 "SELECT id, created " +
319 db
.all(query
, (err
,games
) => {
322 "SELECT count(*) as nbMoves, max(played) AS lastMaj " +
324 "WHERE gid = " + g
.id
;
325 db
.get(query
, (err2
,mstats
) => {
326 // Remove games still not really started,
327 // with no action in the last 3 months:
328 if ((mstats
.nbMoves
== 0 && tsNow
- g
.created
> 91*day
) ||
329 (mstats
.nbMoves
== 1 && tsNow
- mstats
.lastMaj
> 91*day
))
331 GameModel
.remove(g
.id
);
340 module
.exports
= GameModel
;