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
) => {
94 const game
= Object
.assign({},
102 // Full game requested:
104 "SELECT squares, played, idx " +
107 db
.all(query
, (err3
,moves
) => {
109 "SELECT msg, name, added " +
112 db
.all(query
, (err4
,chats
) => {
113 const game
= Object
.assign({},
130 // For display on MyGames or Hall: no need for moves or chats
131 getByUser: function(uid
, excluded
, cb
)
133 db
.serialize(function() {
137 // Special case anonymous user: show all games
139 "SELECT id AS gid " +
149 "HAVING COUNT(uid = " + uid
+ " OR NULL) " +
150 (excluded
? " = 0" : " > 0");
152 db
.all(query
, (err
,gameIds
) => {
153 if (err
|| gameIds
.length
== 0)
159 for (let i
=0; i
<gameIds
.length
; i
++)
161 GameModel
.getOne(gameIds
[i
]["gid"], true, (err2
,game
) => {
162 gameArray
.push(game
);
163 gCounter
++; //TODO: let's hope this is atomic?!
164 // Call callback function only when gameArray is complete:
165 if (gCounter
== gameIds
.length
)
174 getPlayers: function(id
, cb
)
176 db
.serialize(function() {
181 db
.all(query
, (err
,players
) => {
182 return cb(err
, players
);
187 checkGameUpdate: function(obj
)
189 // Check all that is possible (required) in obj:
193 obj
.move.played
.toString().match(/^[0-9]+$/) &&
194 obj
.move.idx
.toString().match(/^[0-9]+$/)
197 !obj
.drawOffer
|| obj
.drawOffer
.match(/^[wbtn]$/)
199 !obj
.fen
|| obj
.fen
.match(/^[a-zA-Z0-9, /-]*$/)
201 !obj
.score
|| obj
.score
.match(/^[012?*\/-]+$/)
203 !obj
.scoreMsg
|| obj
.scoreMsg
.match(/^[a
-zA
-Z
]+$/)
205 !obj
.chat
|| UserModel
.checkNameEmail({name: obj
.chat
.name
})
210 // obj can have fields move, chat, fen, drawOffer and/or score + message
211 update: function(id
, obj
)
213 db
.parallelize(function() {
218 // NOTE: if drawOffer is set, we should check that it's player's turn
219 // A bit overcomplicated. Let's trust the client on that for now...
222 if (obj
.drawOffer
== "n") //Special "None" update
224 modifs
+= "drawOffer = '" + obj
.drawOffer
+ "',";
227 modifs
+= "fen = '" + obj
.fen
+ "',";
229 modifs
+= "score = '" + obj
.score
+ "',";
231 modifs
+= "scoreMsg = '" + obj
.scoreMsg
+ "',";
232 modifs
= modifs
.slice(0,-1); //remove last comma
233 if (modifs
.length
> 0)
235 query
+= modifs
+ " WHERE id = " + id
;
242 "INSERT INTO Moves (gid, squares, played, idx) VALUES " +
243 "(" + id
+ ",?," + m
.played
+ "," + m
.idx
+ ")";
244 db
.run(query
, JSON
.stringify(m
.squares
));
249 "INSERT INTO Chats (gid, msg, name, added) VALUES ("
250 + id
+ ",?,'" + obj
.chat
.name
+ "'," + Date
.now() + ")";
251 db
.run(query
, obj
.chat
.msg
);
258 db
.parallelize(function() {
260 "DELETE FROM Games " +
264 "DELETE FROM Players " +
268 "DELETE FROM Moves " +
272 "DELETE FROM Chats " +
278 cleanGamesDb: function()
280 const tsNow
= Date
.now();
281 // 86400000 = 24 hours in milliseconds
282 const day
= 86400000;
283 db
.serialize(function() {
285 "SELECT id, created " +
287 db
.all(query
, (err
,games
) => {
290 "SELECT count(*) as nbMoves, max(played) AS lastMaj " +
292 "WHERE gid = " + g
.id
;
293 db
.get(query
, (err2
,mstats
) => {
294 // Remove games still not really started,
295 // with no action in the last 3 months:
296 if ((mstats
.nbMoves
== 0 && tsNow
- g
.created
> 91*day
) ||
297 (mstats
.nbMoves
== 1 && tsNow
- mstats
.lastMaj
> 91*day
))
299 GameModel
.remove(g
.id
);
308 module
.exports
= GameModel
;