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
.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";
52 create: function(vid
, fen
, timeControl
, players
, cb
)
54 db
.serialize(function() {
57 + " (vid, fenStart, fen, score, timeControl, created, drawOffer)"
58 + " VALUES (" + vid
+ ",'" + fen
+ "','" + fen
+ "','*','"
59 + timeControl
+ "'," + Date
.now() + ",'')";
60 db
.run(query
, function(err
) {
63 players
.forEach((p
,idx
) => {
64 const color
= (idx
==0 ? "w" : "b");
66 "INSERT INTO Players VALUES " +
67 "(" + this.lastID
+ "," + p
.id
+ ",'" + color
+ "')";
70 cb(null, {gid: this.lastID
});
75 // TODO: queries here could be async, and wait for all to complete
76 getOne: function(id
, cb
)
78 db
.serialize(function() {
79 // TODO: optimize queries?
81 // NOTE: g.scoreMsg can be NULL
82 // (in this case score = "*" and no reason to look at it)
83 "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " +
84 "g.scoreMsg, g.drawOffer, v.name AS vname " +
89 db
.get(query
, (err
,gameInfo
) => {
93 "SELECT p.uid, p.color, u.name " +
97 "WHERE p.gid = " + id
;
98 db
.all(query
, (err2
,players
) => {
102 "SELECT squares, played, idx " +
105 db
.all(query
, (err3
,moves
) => {
109 "SELECT msg, name, added " +
112 db
.all(query
, (err4
,chats
) => {
115 const game
= Object
.assign({},
123 return cb(null, game
);
131 getByUser: function(uid
, excluded
, cb
)
133 db
.serialize(function() {
135 "SELECT DISTINCT gid " +
137 "WHERE uid " + (excluded
? "<>" : "=") + " " + uid
;
138 db
.all(query
, (err
,gameIds
) => {
141 gameIds
= gameIds
|| []; //might be empty
143 for (let i
=0; i
<gameIds
.length
; i
++)
145 GameModel
.getOne(gameIds
[i
]["gid"], (err2
,game
) => {
148 gameArray
.push(game
);
149 // Call callback function only when gameArray is complete:
150 if (i
== gameIds
.length
- 1)
151 return cb(null, gameArray
);
158 getPlayers: function(id
, cb
)
160 db
.serialize(function() {
165 db
.all(query
, (err
,players
) => {
166 return cb(err
, players
);
171 checkGameUpdate: function(obj
)
173 // Check all that is possible (required) in obj:
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";
181 if (!!obj
.drawOffer
&& !obj
.drawOffer
.match(/^[wbtn]$/))
182 return "Wrong draw offer format";
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";
187 if (!!obj
.scoreMsg
&& !obj
.scoreMsg
.match(/^[a
-zA
-Z
]+$/))
188 return "Wrong characters in score message";
190 return UserModel
.checkNameEmail({name: obj
.chat
.name
});
194 // obj can have fields move, chat, fen, drawOffer and/or score
195 update: function(id
, obj
)
197 db
.parallelize(function() {
203 modifs
+= "message = message || ' ' || '" + obj
.message
+ "',";
204 // NOTE: if drawOffer is set, we should check that it's player's turn
205 // A bit overcomplicated. Let's trust the client on that for now...
208 if (obj
.drawOffer
== "n") //Special "None" update
210 modifs
+= "drawOffer = '" + obj
.drawOffer
+ "',";
213 modifs
+= "fen = '" + obj
.fen
+ "',";
215 modifs
+= "score = '" + obj
.score
+ "',";
217 modifs
+= "scoreMsg = '" + obj
.scoreMsg
+ "',";
218 modifs
= modifs
.slice(0,-1); //remove last comma
219 if (modifs
.length
> 0)
221 query
+= modifs
+ " WHERE id = " + id
;
228 "INSERT INTO Moves (gid, squares, played, idx) VALUES " +
229 "(" + id
+ ",?," + m
.played
+ "," + m
.idx
+ ")";
230 db
.run(query
, JSON
.stringify(m
.squares
));
235 "INSERT INTO Chats (gid, msg, name, added) VALUES ("
236 + id
+ ",?,'" + obj
.chat
.name
+ "'," + Date
.now() + ")";
237 db
.run(query
, obj
.chat
.msg
);
244 db
.parallelize(function() {
246 "DELETE FROM Games " +
250 "DELETE FROM Players " +
254 "DELETE FROM Moves " +
258 "DELETE FROM Chats " +
264 cleanGamesDb: function()
266 const tsNow
= Date
.now();
267 // 86400000 = 24 hours in milliseconds
268 const day
= 86400000;
269 db
.serialize(function() {
273 db
.all(query
, (err
,games
) => {
276 "SELECT max(played) AS lastMaj " +
278 "WHERE gid = " + g
.id
;
279 db
.get(query
, (err2
,updated
) => {
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
)
286 GameModel
.remove(g
.id
);
295 module
.exports
= GameModel
;