Commit | Line | Data |
---|---|---|
00f2759e | 1 | var db = require("../utils/database"); |
58e7b94e | 2 | const UserModel = require("./User"); |
00f2759e BA |
3 | |
4 | /* | |
5 | * Structure table Games: | |
6 | * id: game id (int) | |
7 | * vid: integer (variant id) | |
ab4f4bf2 BA |
8 | * fenStart: varchar (initial position) |
9 | * fen: varchar (current position) | |
a9b131f1 | 10 | * timeControl: string |
00f2759e | 11 | * score: varchar (result) |
dcd68c41 | 12 | * scoreMsg: varchar ("Time", "Mutual agreement"...) |
83494c7f | 13 | * created: datetime |
dfeb96ea | 14 | * drawOffer: char ('w','b' or '' for none) |
00f2759e BA |
15 | * |
16 | * Structure table Players: | |
17 | * gid: ref game id | |
18 | * uid: ref user id | |
19 | * color: character | |
20 | * | |
21 | * Structure table Moves: | |
ab4f4bf2 | 22 | * gid: ref game id |
f41ce580 | 23 | * squares: varchar (description) |
00f2759e BA |
24 | * played: datetime |
25 | * idx: integer | |
3837d4f7 BA |
26 | * |
27 | * Structure table Chats: | |
28 | * gid: game id (int) | |
29 | * msg: varchar | |
30 | * name: varchar | |
3837d4f7 | 31 | * added: datetime |
00f2759e BA |
32 | */ |
33 | ||
ab4f4bf2 | 34 | const GameModel = |
00f2759e | 35 | { |
58e7b94e | 36 | checkGameInfo: function(g) { |
58e7b94e BA |
37 | if (!g.vid.toString().match(/^[0-9]+$/)) |
38 | return "Wrong variant ID"; | |
39 | if (!g.vname.match(/^[a-zA-Z0-9]+$/)) | |
40 | return "Wrong variant name"; | |
41 | if (!g.timeControl.match(/^[0-9dhms +]+$/)) | |
42 | return "Wrong characters in time control"; | |
43 | if (!g.fen.match(/^[a-zA-Z0-9, /-]*$/)) | |
44 | return "Bad FEN string"; | |
45 | if (g.players.length != 2) | |
46 | return "Need exactly 2 players"; | |
47 | if (g.players.some(p => !p.id.toString().match(/^[0-9]+$/))) | |
48 | return "Wrong characters in player ID"; | |
49 | return ""; | |
50 | }, | |
51 | ||
dac39588 BA |
52 | create: function(vid, fen, timeControl, players, cb) |
53 | { | |
54 | db.serialize(function() { | |
55 | let query = | |
56 | "INSERT INTO Games" | |
b7cbbda1 | 57 | + " (vid, fenStart, fen, score, timeControl, created, drawOffer)" |
d431028c | 58 | + " VALUES (" + vid + ",'" + fen + "','" + fen + "','*','" |
dfeb96ea | 59 | + timeControl + "'," + Date.now() + ",'')"; |
8c564f46 | 60 | db.run(query, function(err) { |
cf742aaf BA |
61 | if (!!err) |
62 | return cb(err); | |
8c564f46 BA |
63 | players.forEach((p,idx) => { |
64 | const color = (idx==0 ? "w" : "b"); | |
2be5d614 BA |
65 | query = |
66 | "INSERT INTO Players VALUES " + | |
f41ce580 | 67 | "(" + this.lastID + "," + p.id + ",'" + color + "')"; |
2be5d614 BA |
68 | db.run(query); |
69 | }); | |
70 | cb(null, {gid: this.lastID}); | |
dac39588 BA |
71 | }); |
72 | }); | |
73 | }, | |
00f2759e | 74 | |
dac39588 BA |
75 | // TODO: queries here could be async, and wait for all to complete |
76 | getOne: function(id, cb) | |
77 | { | |
78 | db.serialize(function() { | |
f41ce580 | 79 | // TODO: optimize queries? |
dac39588 | 80 | let query = |
dcd68c41 BA |
81 | // NOTE: g.scoreMsg can be NULL |
82 | // (in this case score = "*" and no reason to look at it) | |
dac39588 | 83 | "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " + |
633959bf | 84 | "g.scoreMsg, g.drawOffer, v.name AS vname " + |
dac39588 | 85 | "FROM Games g " + |
f41ce580 BA |
86 | "JOIN Variants v " + |
87 | " ON g.vid = v.id " + | |
dac39588 BA |
88 | "WHERE g.id = " + id; |
89 | db.get(query, (err,gameInfo) => { | |
90 | if (!!err) | |
91 | return cb(err); | |
92 | query = | |
93 | "SELECT p.uid, p.color, u.name " + | |
94 | "FROM Players p " + | |
f41ce580 BA |
95 | "JOIN Users u " + |
96 | " ON p.uid = u.id " + | |
dac39588 BA |
97 | "WHERE p.gid = " + id; |
98 | db.all(query, (err2,players) => { | |
99 | if (!!err2) | |
100 | return cb(err2); | |
101 | query = | |
102 | "SELECT squares, played, idx " + | |
103 | "FROM Moves " + | |
104 | "WHERE gid = " + id; | |
105 | db.all(query, (err3,moves) => { | |
106 | if (!!err3) | |
107 | return cb(err3); | |
108 | query = | |
dcd68c41 | 109 | "SELECT msg, name, added " + |
3837d4f7 BA |
110 | "FROM Chats " + |
111 | "WHERE gid = " + id; | |
dac39588 BA |
112 | db.all(query, (err4,chats) => { |
113 | if (!!err4) | |
114 | return cb(err4); | |
115 | const game = Object.assign({}, | |
3837d4f7 BA |
116 | gameInfo, |
117 | { | |
118 | players: players, | |
119 | moves: moves, | |
120 | chats: chats, | |
121 | } | |
122 | ); | |
dac39588 | 123 | return cb(null, game); |
3837d4f7 | 124 | }); |
dac39588 BA |
125 | }); |
126 | }); | |
127 | }); | |
128 | }); | |
129 | }, | |
00f2759e | 130 | |
dac39588 BA |
131 | getByUser: function(uid, excluded, cb) |
132 | { | |
133 | db.serialize(function() { | |
dac39588 | 134 | const query = |
77c50966 | 135 | "SELECT DISTINCT gid " + |
dac39588 BA |
136 | "FROM Players " + |
137 | "WHERE uid " + (excluded ? "<>" : "=") + " " + uid; | |
138 | db.all(query, (err,gameIds) => { | |
139 | if (!!err) | |
140 | return cb(err); | |
098cd7f1 | 141 | gameIds = gameIds || []; //might be empty |
dac39588 BA |
142 | let gameArray = []; |
143 | for (let i=0; i<gameIds.length; i++) | |
144 | { | |
145 | GameModel.getOne(gameIds[i]["gid"], (err2,game) => { | |
146 | if (!!err2) | |
147 | return cb(err2); | |
148 | gameArray.push(game); | |
149 | // Call callback function only when gameArray is complete: | |
150 | if (i == gameIds.length - 1) | |
151 | return cb(null, gameArray); | |
152 | }); | |
153 | } | |
154 | }); | |
155 | }); | |
156 | }, | |
ab4f4bf2 | 157 | |
411d23cd BA |
158 | getPlayers: function(id, cb) |
159 | { | |
160 | db.serialize(function() { | |
161 | const query = | |
162 | "SELECT id " + | |
163 | "FROM Players " + | |
164 | "WHERE gid = " + id; | |
165 | db.all(query, (err,players) => { | |
166 | return cb(err, players); | |
167 | }); | |
168 | }); | |
169 | }, | |
170 | ||
58e7b94e BA |
171 | checkGameUpdate: function(obj) |
172 | { | |
173 | // Check all that is possible (required) in obj: | |
174 | if (!!obj.move) | |
175 | { | |
176 | if (!obj.move.played.toString().match(/^[0-9]+$/)) | |
177 | return "Wrong move played time"; | |
178 | if (!obj.move.idx.toString().match(/^[0-9]+$/)) | |
179 | return "Wrong move index"; | |
180 | } | |
633959bf BA |
181 | if (!!obj.drawOffer && !obj.drawOffer.match(/^[wbtn]$/)) |
182 | return "Wrong draw offer format"; | |
58e7b94e BA |
183 | if (!!obj.fen && !obj.fen.match(/^[a-zA-Z0-9, /-]*$/)) |
184 | return "Wrong FEN string"; | |
185 | if (!!obj.score && !obj.score.match(/^[012?*\/-]+$/)) | |
186 | return "Wrong characters in score"; | |
dcd68c41 BA |
187 | if (!!obj.scoreMsg && !obj.scoreMsg.match(/^[a-zA-Z ]+$/)) |
188 | return "Wrong characters in score message"; | |
58e7b94e | 189 | if (!!obj.chat) |
58e7b94e | 190 | return UserModel.checkNameEmail({name: obj.chat.name}); |
58e7b94e BA |
191 | return ""; |
192 | }, | |
193 | ||
3837d4f7 | 194 | // obj can have fields move, chat, fen, drawOffer and/or score |
f41ce580 | 195 | update: function(id, obj) |
3d55deea | 196 | { |
dac39588 | 197 | db.parallelize(function() { |
3d55deea BA |
198 | let query = |
199 | "UPDATE Games " + | |
200 | "SET "; | |
3837d4f7 | 201 | let modifs = ""; |
63ca2b89 | 202 | if (!!obj.message) |
3837d4f7 | 203 | modifs += "message = message || ' ' || '" + obj.message + "',"; |
633959bf | 204 | // NOTE: if drawOffer is set, we should check that it's player's turn |
dfeb96ea BA |
205 | // A bit overcomplicated. Let's trust the client on that for now... |
206 | if (!!obj.drawOffer) | |
633959bf BA |
207 | { |
208 | if (obj.drawOffer == "n") //Special "None" update | |
209 | obj.drawOffer = ""; | |
210 | modifs += "drawOffer = '" + obj.drawOffer + "',"; | |
211 | } | |
3d55deea | 212 | if (!!obj.fen) |
3837d4f7 | 213 | modifs += "fen = '" + obj.fen + "',"; |
3d55deea | 214 | if (!!obj.score) |
3837d4f7 | 215 | modifs += "score = '" + obj.score + "',"; |
dcd68c41 BA |
216 | if (!!obj.scoreMsg) |
217 | modifs += "scoreMsg = '" + obj.scoreMsg + "',"; | |
3837d4f7 BA |
218 | modifs = modifs.slice(0,-1); //remove last comma |
219 | if (modifs.length > 0) | |
220 | { | |
221 | query += modifs + " WHERE id = " + id; | |
222 | db.run(query); | |
223 | } | |
f41ce580 BA |
224 | if (!!obj.move) |
225 | { | |
afc42601 | 226 | const m = obj.move; |
f41ce580 | 227 | query = |
63ca2b89 | 228 | "INSERT INTO Moves (gid, squares, played, idx) VALUES " + |
58e7b94e BA |
229 | "(" + id + ",?," + m.played + "," + m.idx + ")"; |
230 | db.run(query, JSON.stringify(m.squares)); | |
f41ce580 | 231 | } |
3837d4f7 BA |
232 | if (!!obj.chat) |
233 | { | |
dac39588 BA |
234 | query = |
235 | "INSERT INTO Chats (gid, msg, name, added) VALUES (" | |
41c80bb6 | 236 | + id + ",?,'" + obj.chat.name + "'," + Date.now() + ")"; |
58e7b94e | 237 | db.run(query, obj.chat.msg); |
3837d4f7 | 238 | } |
3d55deea BA |
239 | }); |
240 | }, | |
241 | ||
dac39588 BA |
242 | remove: function(id) |
243 | { | |
244 | db.parallelize(function() { | |
245 | let query = | |
246 | "DELETE FROM Games " + | |
247 | "WHERE id = " + id; | |
248 | db.run(query); | |
249 | query = | |
250 | "DELETE FROM Players " + | |
251 | "WHERE gid = " + id; | |
252 | db.run(query); | |
253 | query = | |
254 | "DELETE FROM Moves " + | |
255 | "WHERE gid = " + id; | |
256 | db.run(query); | |
257 | query = | |
258 | "DELETE FROM Chats " + | |
259 | "WHERE gid = " + id; | |
260 | db.run(query); | |
261 | }); | |
262 | }, | |
d431028c BA |
263 | |
264 | cleanGamesDb: function() | |
265 | { | |
266 | const tsNow = Date.now(); | |
267 | // 86400000 = 24 hours in milliseconds | |
268 | const day = 86400000; | |
269 | db.serialize(function() { | |
270 | let query = | |
271 | "SELECT id,score " + | |
272 | "FROM Games "; | |
273 | db.all(query, (err,games) => { | |
274 | games.forEach(g => { | |
275 | query = | |
276 | "SELECT max(played) AS lastMaj " + | |
277 | "FROM Moves " + | |
278 | "WHERE gid = " + g.id; | |
f21cd6d9 | 279 | db.get(query, (err2,updated) => { |
d431028c BA |
280 | if (!updated && tsNow - g.created > 7*day) |
281 | return GameModel.remove(g.id); | |
282 | const lastMaj = updated.lastMaj; | |
283 | if (g.score != "*" && tsNow - lastMaj > 7*day || | |
284 | g.score == "*" && tsNow - lastMaj > 91*day) | |
285 | { | |
286 | GameModel.remove(g.id); | |
287 | } | |
288 | }); | |
289 | }); | |
290 | }); | |
291 | }); | |
292 | }, | |
00f2759e | 293 | } |
ab4f4bf2 BA |
294 | |
295 | module.exports = GameModel; |