Attempt to clarify installation instructions a little
[vchess.git] / server / models / Game.js
... / ...
CommitLineData
1const db = require("../utils/database");
2const UserModel = require("./User");
3
4/*
5 * Structure table Games:
6 * id: game id (int)
7 * vid: integer (variant id)
8 * fenStart: varchar (initial position)
9 * fen: varchar (current position)
10 * white: integer
11 * black: integer
12 * cadence: string
13 * score: varchar (result)
14 * scoreMsg: varchar ("Time", "Mutual agreement"...)
15 * created: datetime
16 * drawOffer: char ('w','b' or '' for none)
17 * rematchOffer: char (similar to drawOffer)
18 * options: varchar
19 * deletedByWhite: boolean
20 * deletedByBlack: boolean
21 * chatReadWhite: datetime
22 * chatReadBlack: datetime
23 *
24 * Structure table Moves:
25 * gid: ref game id
26 * squares: varchar (description)
27 * played: datetime
28 * idx: integer
29 *
30 * Structure table Chats:
31 * gid: game id (int)
32 * msg: varchar
33 * name: varchar
34 * added: datetime
35 */
36
37const GameModel = {
38
39 checkGameInfo: function(g) {
40 return (
41 g.vid.toString().match(/^[0-9]+$/) &&
42 g.cadence.match(/^[0-9dhms +]+$/) &&
43 g.fen.match(/^[a-zA-Z0-9, /-]*$/) &&
44 g.players.length == 2 &&
45 g.players.every(p => p.id.toString().match(/^[0-9]+$/))
46 );
47 },
48
49 incrementCounter: function(vid, cb) {
50 db.serialize(function() {
51 let query =
52 "UPDATE GameStat " +
53 "SET total = total + 1 " +
54 "WHERE vid = " + vid;
55 db.run(query, cb);
56 });
57 },
58
59 create: function(vid, fen, options, cadence, players, cb) {
60 db.serialize(function() {
61 let query =
62 "INSERT INTO Games " +
63 "(" +
64 "vid, fenStart, fen, options, " +
65 "white, black, " +
66 "cadence, created" +
67 ") " +
68 "VALUES " +
69 "(" +
70 vid + ",'" + fen + "','" + fen + "',?," +
71 players[0].id + "," + players[1].id + "," +
72 "'" + cadence + "'," + Date.now() +
73 ")";
74 db.run(query, options, function(err) {
75 cb(err, { id: this.lastID });
76 });
77 });
78 },
79
80 // TODO: some queries here could be async
81 getOne: function(id, cb) {
82 // NOTE: ignoring errors (shouldn't happen at this stage)
83 db.serialize(function() {
84 let query =
85 "SELECT " +
86 "id, vid, fen, fenStart, cadence, created, " +
87 "white, black, options, score, scoreMsg, " +
88 "chatReadWhite, chatReadBlack, drawOffer, rematchOffer " +
89 "FROM Games " +
90 "WHERE id = " + id;
91 db.get(query, (err, gameInfo) => {
92 if (!gameInfo) {
93 cb(err || { errmsg: "Game not found" }, undefined);
94 return;
95 }
96 query =
97 "SELECT id, name " +
98 "FROM Users " +
99 "WHERE id IN (" + gameInfo.white + "," + gameInfo.black + ")";
100 db.all(query, (err2, players) => {
101 if (players[0].id == gameInfo.black) players = players.reverse();
102 // The original players' IDs info isn't required anymore
103 delete gameInfo["white"];
104 delete gameInfo["black"];
105 query =
106 "SELECT squares, played, idx " +
107 "FROM Moves " +
108 "WHERE gid = " + id;
109 db.all(query, (err3, moves) => {
110 query =
111 "SELECT msg, name, added " +
112 "FROM Chats " +
113 "WHERE gid = " + id;
114 db.all(query, (err4, chats) => {
115 const game = Object.assign(
116 {
117 players: players,
118 moves: moves,
119 chats: chats
120 },
121 gameInfo
122 );
123 cb(null, game);
124 });
125 });
126 });
127 });
128 });
129 },
130
131 // For display on Hall: no need for moves or chats
132 getObserved: function(uid, cursor, cb) {
133 db.serialize(function() {
134 let query =
135 "SELECT id, vid, cadence, options, created, score, white, black " +
136 "FROM Games " +
137 "WHERE created < " + cursor + " ";
138 if (uid > 0) {
139 query +=
140 " AND white <> " + uid + " " +
141 " AND black <> " + uid + " ";
142 }
143 query +=
144 "ORDER BY created DESC " +
145 "LIMIT 20"; //TODO: 20 hard-coded...
146 db.all(query, (err, games) => {
147 games = games || [];
148 // Query players names
149 let pids = {};
150 games.forEach(g => {
151 if (!pids[g.white]) pids[g.white] = true;
152 if (!pids[g.black]) pids[g.black] = true;
153 });
154 UserModel.getByIds(Object.keys(pids), (err2, users) => {
155 users = users || [];
156 let names = {};
157 users.forEach(u => { names[u.id] = u.name; });
158 cb(
159 err,
160 games.map(
161 g => {
162 return {
163 id: g.id,
164 vid: g.vid,
165 cadence: g.cadence,
166 options: g.options,
167 created: g.created,
168 score: g.score,
169 players: [
170 { id: g.white, name: names[g.white] },
171 { id: g.black, name: names[g.black] }
172 ]
173 };
174 }
175 )
176 );
177 });
178 });
179 });
180 },
181
182 // For display on MyGames: registered user only
183 getRunning: function(uid, cb) {
184 db.serialize(function() {
185 let query =
186 "SELECT id, vid, cadence, options, created, white, black " +
187 "FROM Games " +
188 "WHERE score = '*' AND (white = " + uid + " OR black = " + uid + ")";
189 db.all(query, (err, games) => {
190 games = games || [];
191 // Get movesCount (could be done in // with next query)
192 query =
193 "SELECT gid, COUNT(*) AS nbMoves " +
194 "FROM Moves " +
195 "WHERE gid IN " + "(" + games.map(g => g.id).join(",") + ") " +
196 "GROUP BY gid";
197 db.all(query, (err, mstats) => {
198 let movesCounts = {};
199 mstats.forEach(ms => { movesCounts[ms.gid] = ms.nbMoves; });
200 // Query player names
201 let pids = {};
202 games.forEach(g => {
203 if (!pids[g.white]) pids[g.white] = true;
204 if (!pids[g.black]) pids[g.black] = true;
205 });
206 UserModel.getByIds(Object.keys(pids), (err2, users) => {
207 users = users || [];
208 let names = {};
209 users.forEach(u => { names[u.id] = u.name; });
210 cb(
211 null,
212 games.map(
213 g => {
214 return {
215 id: g.id,
216 vid: g.vid,
217 cadence: g.cadence,
218 options: g.options,
219 created: g.created,
220 score: g.score,
221 movesCount: movesCounts[g.id] || 0,
222 players: [
223 { id: g.white, name: names[g.white] },
224 { id: g.black, name: names[g.black] }
225 ]
226 };
227 }
228 )
229 );
230 });
231 });
232 });
233 });
234 },
235
236 // These games could be deleted on some side. movesCount not required
237 getCompleted: function(uid, cursor, cb) {
238 db.serialize(function() {
239 let query =
240 "SELECT id, vid, cadence, options, created, score, scoreMsg, " +
241 "white, black, deletedByWhite, deletedByBlack " +
242 "FROM Games " +
243 "WHERE " +
244 " score <> '*' AND" +
245 " created < " + cursor + " AND" +
246 " (" +
247 " (" +
248 " white = " + uid + " AND" +
249 " (deletedByWhite IS NULL OR NOT deletedByWhite)" +
250 " )" +
251 " OR " +
252 " (" +
253 " black = " + uid + " AND" +
254 " (deletedByBlack IS NULL OR NOT deletedByBlack)" +
255 " )" +
256 " ) ";
257 query +=
258 "ORDER BY created DESC " +
259 "LIMIT 20";
260 db.all(query, (err, games) => {
261 games = games || [];
262 // Query player names
263 let pids = {};
264 games.forEach(g => {
265 if (!pids[g.white]) pids[g.white] = true;
266 if (!pids[g.black]) pids[g.black] = true;
267 });
268 UserModel.getByIds(Object.keys(pids), (err2, users) => {
269 users = users || [];
270 let names = {};
271 users.forEach(u => { names[u.id] = u.name; });
272 cb(
273 null,
274 games.map(
275 g => {
276 return {
277 id: g.id,
278 vid: g.vid,
279 cadence: g.cadence,
280 options: g.options,
281 created: g.created,
282 score: g.score,
283 scoreMsg: g.scoreMsg,
284 players: [
285 { id: g.white, name: names[g.white] },
286 { id: g.black, name: names[g.black] }
287 ],
288 deletedByWhite: g.deletedByWhite,
289 deletedByBlack: g.deletedByBlack
290 };
291 }
292 )
293 );
294 });
295 });
296 });
297 },
298
299 getPlayers: function(id, cb) {
300 db.serialize(function() {
301 const query =
302 "SELECT white, black " +
303 "FROM Games " +
304 "WHERE id = " + id;
305 db.get(query, (err, players) => {
306 return cb(err, players);
307 });
308 });
309 },
310
311 checkGameUpdate: function(obj) {
312 // Check all that is possible (required) in obj:
313 return (
314 (
315 !obj.move || !!(obj.move.idx.toString().match(/^[0-9]+$/))
316 ) && (
317 !obj.drawOffer || !!(obj.drawOffer.match(/^[wbtn]$/))
318 ) && (
319 !obj.rematchOffer || !!(obj.rematchOffer.match(/^[wbn]$/))
320 ) && (
321 !obj.fen || !!(obj.fen.match(/^[a-zA-Z0-9,.:{}\[\]" /-]*$/))
322 ) && (
323 !obj.score || !!(obj.score.match(/^[012?*\/-]+$/))
324 ) && (
325 !obj.chatRead || ['w','b'].includes(obj.chatRead)
326 ) && (
327 !obj.scoreMsg || !!(obj.scoreMsg.match(/^[a-zA-Z ]+$/))
328 ) && (
329 !obj.chat || UserModel.checkNameEmail({name: obj.chat.name})
330 )
331 );
332 },
333
334 // obj can have fields move, chat, fen, drawOffer and/or score + message
335 update: function(id, obj, cb) {
336 db.parallelize(function() {
337 let updateQuery =
338 "UPDATE Games " +
339 "SET ";
340 let modifs = "";
341 // NOTE: if drawOffer is set, we should check that it's player's turn
342 // A bit overcomplicated. Let's trust the client on that for now...
343 if (!!obj.drawOffer) {
344 if (obj.drawOffer == "n")
345 // Special "None" update
346 obj.drawOffer = "";
347 modifs += "drawOffer = '" + obj.drawOffer + "',";
348 }
349 if (!!obj.rematchOffer) {
350 if (obj.rematchOffer == "n")
351 // Special "None" update
352 obj.rematchOffer = "";
353 modifs += "rematchOffer = '" + obj.rematchOffer + "',";
354 }
355 if (!!obj.fen) modifs += "fen = '" + obj.fen + "',";
356 if (!!obj.deletedBy) {
357 const myColor = obj.deletedBy == 'w' ? "White" : "Black";
358 modifs += "deletedBy" + myColor + " = true,";
359 }
360 if (!!obj.chatRead) {
361 const myColor = obj.chatRead == 'w' ? "White" : "Black";
362 modifs += "chatRead" + myColor + " = " + Date.now() + ",";
363 }
364 if (!!obj.score) {
365 modifs += "score = '" + obj.score + "'," +
366 "scoreMsg = '" + obj.scoreMsg + "',";
367 }
368 const finishAndSendQuery = () => {
369 modifs = modifs.slice(0, -1); //remove last comma
370 if (modifs.length > 0) {
371 updateQuery += modifs + " WHERE id = " + id;
372 db.run(updateQuery);
373 }
374 cb(null);
375 };
376 if (!!obj.move || (!!obj.score && obj.scoreMsg == "Time")) {
377 // Security: only update moves if index is right,
378 // and score with scoreMsg "Time" if really lost on time.
379 let query =
380 "SELECT MAX(idx) AS maxIdx, MAX(played) AS lastPlayed " +
381 "FROM Moves " +
382 "WHERE gid = " + id;
383 db.get(query, (err, ret) => {
384 if (!!obj.move ) {
385 if (!ret.maxIdx || ret.maxIdx + 1 == obj.move.idx) {
386 query =
387 "INSERT INTO Moves (gid, squares, played, idx) VALUES " +
388 "(" + id + ",?," + Date.now() + "," + obj.move.idx + ")";
389 db.run(query, JSON.stringify(obj.move.squares));
390 finishAndSendQuery();
391 }
392 else cb({ errmsg: "Wrong move index" });
393 }
394 else {
395 if (ret.maxIdx < 2) cb({ errmsg: "Time not over" });
396 else {
397 // We also need the game cadence
398 query =
399 "SELECT cadence " +
400 "FROM Games " +
401 "WHERE id = " + id;
402 db.get(query, (err2, ret2) => {
403 const daysTc = parseInt(ret2.cadence.match(/^[0-9]+/)[0]);
404 if (Date.now() - ret.lastPlayed > daysTc * 24 * 3600 * 1000)
405 finishAndSendQuery();
406 else cb({ errmsg: "Time not over" });
407 });
408 }
409 }
410 });
411 }
412 else finishAndSendQuery();
413 // NOTE: chat and delchat are mutually exclusive
414 if (!!obj.chat) {
415 const query =
416 "INSERT INTO Chats (gid, msg, name, added) VALUES ("
417 + id + ",?,'" + obj.chat.name + "'," + Date.now() + ")";
418 db.run(query, obj.chat.msg);
419 }
420 else if (obj.delchat) {
421 const query =
422 "DELETE " +
423 "FROM Chats " +
424 "WHERE gid = " + id;
425 db.run(query);
426 }
427 if (!!obj.deletedBy) {
428 // Did my opponent delete it too?
429 let selection =
430 "deletedBy" +
431 (obj.deletedBy == 'w' ? "Black" : "White") +
432 " AS deletedByOpp";
433 const query =
434 "SELECT " + selection + " " +
435 "FROM Games " +
436 "WHERE id = " + id;
437 db.get(query, (err,ret) => {
438 // If yes: just remove game
439 if (!!ret.deletedByOpp) GameModel.remove(id);
440 });
441 }
442 });
443 },
444
445 remove: function(id_s) {
446 const suffix =
447 Array.isArray(id_s)
448 ? " IN (" + id_s.join(",") + ")"
449 : " = " + id_s;
450 db.parallelize(function() {
451 let query =
452 "DELETE FROM Games " +
453 "WHERE id " + suffix;
454 db.run(query);
455 query =
456 "DELETE FROM Moves " +
457 "WHERE gid " + suffix;
458 db.run(query);
459 query =
460 "DELETE FROM Chats " +
461 "WHERE gid " + suffix;
462 db.run(query);
463 });
464 },
465
466 cleanGamesDb: function() {
467 const tsNow = Date.now();
468 // 86400000 = 24 hours in milliseconds
469 const day = 86400000;
470 db.serialize(function() {
471 let query =
472 "SELECT id, created, cadence, score " +
473 "FROM Games";
474 db.all(query, (err, games) => {
475 query =
476 "SELECT gid, count(*) AS nbMoves, MAX(played) AS lastMaj " +
477 "FROM Moves " +
478 "GROUP BY gid";
479 db.all(query, (err2, mstats) => {
480 // Reorganize moves data to avoid too many array lookups:
481 let movesGroups = {};
482 mstats.forEach(ms => {
483 movesGroups[ms.gid] = {
484 nbMoves: ms.nbMoves,
485 lastMaj: ms.lastMaj
486 };
487 });
488 // Remove games still not really started,
489 // with no action in the last 2 weeks, or result != '*':
490 let toRemove = [];
491 let lostOnTime = [ [], [] ];
492 games.forEach(g => {
493 if (
494 (
495 !movesGroups[g.id] &&
496 (g.score != '*' || tsNow - g.created > 14*day)
497 )
498 ||
499 (
500 !!movesGroups[g.id] &&
501 movesGroups[g.id].nbMoves == 1 &&
502 (g.score != '*' || tsNow - movesGroups[g.id].lastMaj > 14*day)
503 )
504 ) {
505 toRemove.push(g.id);
506 }
507 // Set score if lost on time and >= 2 moves:
508 else if (
509 g.score == '*' &&
510 !!movesGroups[g.id] &&
511 movesGroups[g.id].nbMoves >= 2 &&
512 tsNow - movesGroups[g.id].lastMaj >
513 // cadence in days * nb seconds per day:
514 parseInt(g.cadence.slice(0, -1), 10) * day
515 ) {
516 lostOnTime[movesGroups[g.id].nbMoves % 2].push(g.id);
517 }
518 });
519 if (toRemove.length > 0) GameModel.remove(toRemove);
520 if (lostOnTime.some(l => l.length > 0)) {
521 db.parallelize(function() {
522 for (let i of [0, 1]) {
523 if (lostOnTime[i].length > 0) {
524 const score = (i == 0 ? "0-1" : "1-0");
525 const query =
526 "UPDATE Games " +
527 "SET score = '" + score + "', scoreMsg = 'Time' " +
528 "WHERE id IN (" + lostOnTime[i].join(',') + ")";
529 db.run(query);
530 }
531 }
532 });
533 }
534 });
535 });
536 });
537 }
538
539};
540
541module.exports = GameModel;