1 var db
= require("../utils/database");
4 * Structure table Games:
6 * vid: integer (variant id)
7 * fenStart: varchar (initial position)
8 * fen: varchar (current position)
10 * score: varchar (result)
13 * Structure table Players:
18 * Structure table Moves:
20 * squares: varchar (description)
28 create: function(vid
, fen
, timeControl
, players
, cb
)
30 db
.serialize(function() {
33 + " (vid, fenStart, fen, score, timeControl, created, drawOffer)"
34 + " VALUES (" + vid
+ ",'" + fen
+ "','" + fen
+ "','*','"
35 + timeControl
+ "'," + Date
.now() + "," + false + ")";
36 db
.run(query
, function(err
) {
39 players
.forEach((p
,idx
) => {
40 const color
= (idx
==0 ? "w" : "b");
42 "INSERT INTO Players VALUES " +
43 "(" + this.lastID
+ "," + p
.id
+ ",'" + color
+ "')";
46 cb(null, {gid: this.lastID
});
51 // TODO: queries here could be async, and wait for all to complete
52 getOne: function(id
, cb
)
54 db
.serialize(function() {
55 // TODO: optimize queries?
57 "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " +
63 db
.get(query
, (err
,gameInfo
) => {
67 "SELECT p.uid, p.color, u.name " +
71 "WHERE p.gid = " + id
;
72 db
.all(query
, (err2
,players
) => {
76 "SELECT squares, message, played, idx " +
79 db
.all(query
, (err3
,moves
) => {
82 const game
= Object
.assign({},
89 return cb(null, game
);
96 getByUser: function(uid
, excluded
, cb
)
98 db
.serialize(function() {
99 // Next query is fine because a player appear at most once in a game
103 "WHERE uid " + (excluded
? "<>" : "=") + " " + uid
;
104 db
.all(query
, (err
,gameIds
) => {
107 gameIds
= gameIds
|| []; //might be empty
109 for (let i
=0; i
<gameIds
.length
; i
++)
111 GameModel
.getOne(gameIds
[i
]["gid"], (err2
,game
) => {
114 gameArray
.push(game
);
115 // Call callback function only when gameArray is complete:
116 if (i
== gameIds
.length
- 1)
117 return cb(null, gameArray
);
124 getPlayers: function(id
, cb
)
126 db
.serialize(function() {
131 db
.all(query
, (err
,players
) => {
132 return cb(err
, players
);
137 // obj can have fields move, message, fen, drawOffer and/or score
138 update: function(id
, obj
)
140 db
.parallelize(function() {
145 query
+= "message = message || ' ' || '" + obj
.message
+ "',";
147 query
+= "drawOffer = " + obj
.drawOffer
+ ",";
149 query
+= "fen = '" + obj
.fen
+ "',";
151 query
+= "score = '" + obj
.score
+ "',";
152 query
= query
.slice(0,-1); //remove last comma
153 query
+= " WHERE id = " + id
;
159 "INSERT INTO Moves (gid, squares, played, idx) VALUES " +
160 "(" + id
+ ",'" + JSON
.stringify(m
.squares
) + "',"
161 + m
.played
+ "," + m
.idx
+ ")";
169 db
.parallelize(function() {
171 "DELETE FROM Games " +
175 "DELETE FROM Players " +
179 "DELETE FROM Moves " +
185 cleanGamesDb: function()
187 const tsNow
= Date
.now();
188 // 86400000 = 24 hours in milliseconds
189 const day
= 86400000;
190 db
.serialize(function() {
194 db
.all(query
, (err
,games
) => {
197 "SELECT max(played) AS lastMaj " +
199 "WHERE gid = " + g
.id
;
200 db
.get(query
, (err2
,updated
) => {
201 if (!updated
&& tsNow
- g
.created
> 7*day
)
202 return GameModel
.remove(g
.id
);
203 const lastMaj
= updated
.lastMaj
;
204 if (g
.score
!= "*" && tsNow
- lastMaj
> 7*day
||
205 g
.score
== "*" && tsNow
- lastMaj
> 91*day
)
207 GameModel
.remove(g
.id
);
216 module
.exports
= GameModel
;