Implement cleaning methods (CRON tasks)
[vchess.git] / server / models / Game.js
1 var db = require("../utils/database");
2
3 /*
4 * Structure table Games:
5 * id: game id (int)
6 * vid: integer (variant id)
7 * fenStart: varchar (initial position)
8 * fen: varchar (current position)
9 * timeControl: string
10 * score: varchar (result)
11 * created: datetime
12 *
13 * Structure table Players:
14 * gid: ref game id
15 * uid: ref user id
16 * color: character
17 *
18 * Structure table Moves:
19 * gid: ref game id
20 * squares: varchar (description)
21 * message: text
22 * played: datetime
23 * idx: integer
24 */
25
26 const GameModel =
27 {
28 create: function(vid, fen, timeControl, players, cb)
29 {
30 db.serialize(function() {
31 let query =
32 "INSERT INTO Games (vid, fenStart, fen, score, timeControl, created)"
33 + " VALUES (" + vid + ",'" + fen + "','" + fen + "','*','"
34 + timeControl + "'," + Date.now() + ")";
35 db.run(query, function(err) {
36 if (!!err)
37 return cb(err);
38 players.forEach((p,idx) => {
39 const color = (idx==0 ? "w" : "b");
40 query =
41 "INSERT INTO Players VALUES " +
42 "(" + this.lastID + "," + p.id + ",'" + color + "')";
43 db.run(query);
44 });
45 cb(null, {gid: this.lastID});
46 });
47 });
48 },
49
50 // TODO: queries here could be async, and wait for all to complete
51 getOne: function(id, cb)
52 {
53 db.serialize(function() {
54 // TODO: optimize queries?
55 let query =
56 "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " +
57 "v.name AS vname " +
58 "FROM Games g " +
59 "JOIN Variants v " +
60 " ON g.vid = v.id " +
61 "WHERE g.id = " + id;
62 db.get(query, (err,gameInfo) => {
63 if (!!err)
64 return cb(err);
65 query =
66 "SELECT p.uid, p.color, u.name " +
67 "FROM Players p " +
68 "JOIN Users u " +
69 " ON p.uid = u.id " +
70 "WHERE p.gid = " + id;
71 db.all(query, (err2,players) => {
72 if (!!err2)
73 return cb(err2);
74 query =
75 "SELECT squares, message, played, idx " +
76 "FROM Moves " +
77 "WHERE gid = " + id;
78 db.all(query, (err3,moves) => {
79 if (!!err3)
80 return cb(err3);
81 const game = Object.assign({},
82 gameInfo,
83 {
84 players: players,
85 moves: moves
86 }
87 );
88 return cb(null, game);
89 });
90 });
91 });
92 });
93 },
94
95 getByUser: function(uid, excluded, cb)
96 {
97 db.serialize(function() {
98 // Next query is fine because a player appear at most once in a game
99 const query =
100 "SELECT gid " +
101 "FROM Players " +
102 "WHERE uid " + (excluded ? "<>" : "=") + " " + uid;
103 db.all(query, (err,gameIds) => {
104 if (!!err)
105 return cb(err);
106 gameIds = gameIds || []; //might be empty
107 let gameArray = [];
108 for (let i=0; i<gameIds.length; i++)
109 {
110 GameModel.getOne(gameIds[i]["gid"], (err2,game) => {
111 if (!!err2)
112 return cb(err2);
113 gameArray.push(game);
114 // Call callback function only when gameArray is complete:
115 if (i == gameIds.length - 1)
116 return cb(null, gameArray);
117 });
118 }
119 });
120 });
121 },
122
123 getPlayers: function(id, cb)
124 {
125 db.serialize(function() {
126 const query =
127 "SELECT id " +
128 "FROM Players " +
129 "WHERE gid = " + id;
130 db.all(query, (err,players) => {
131 return cb(err, players);
132 });
133 });
134 },
135
136 // obj can have fields move, fen and/or score
137 update: function(id, obj)
138 {
139 db.parallelize(function() {
140 let query =
141 "UPDATE Games " +
142 "SET ";
143 if (!!obj.fen)
144 query += "fen = '" + obj.fen + "',";
145 if (!!obj.score)
146 query += "score = '" + obj.score + "',";
147 query = query.slice(0,-1); //remove last comma
148 query += " WHERE id = " + id;
149 db.run(query);
150 if (!!obj.move)
151 {
152 const m = obj.move;
153 query =
154 "INSERT INTO Moves (gid, squares, message, played, idx) VALUES " +
155 "(" + id + ",'" + JSON.stringify(m.squares) + "','" + m.message +
156 "'," + m.played + "," + m.idx + ")";
157 db.run(query);
158 }
159 });
160 },
161
162 remove: function(id)
163 {
164 db.parallelize(function() {
165 let query =
166 "DELETE FROM Games " +
167 "WHERE id = " + id;
168 db.run(query);
169 query =
170 "DELETE FROM Players " +
171 "WHERE gid = " + id;
172 db.run(query);
173 query =
174 "DELETE FROM Moves " +
175 "WHERE gid = " + id;
176 db.run(query);
177 });
178 },
179
180 cleanGamesDb: function()
181 {
182 const tsNow = Date.now();
183 // 86400000 = 24 hours in milliseconds
184 const day = 86400000;
185 db.serialize(function() {
186 let query =
187 "SELECT id,score " +
188 "FROM Games ";
189 db.all(query, (err,games) => {
190 games.forEach(g => {
191 query =
192 "SELECT max(played) AS lastMaj " +
193 "FROM Moves " +
194 "WHERE gid = " + g.id;
195 db.get(query, (err2,updated) {
196 if (!updated && tsNow - g.created > 7*day)
197 return GameModel.remove(g.id);
198 const lastMaj = updated.lastMaj;
199 if (g.score != "*" && tsNow - lastMaj > 7*day ||
200 g.score == "*" && tsNow - lastMaj > 91*day)
201 {
202 GameModel.remove(g.id);
203 }
204 });
205 });
206 });
207 });
208 },
209 }
210
211 module.exports = GameModel;