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() {
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
) {
38 players
.forEach((p
,idx
) => {
39 const color
= (idx
==0 ? "w" : "b");
41 "INSERT INTO Players VALUES " +
42 "(" + this.lastID
+ "," + p
.id
+ ",'" + color
+ "')";
45 cb(null, {gid: this.lastID
});
50 // TODO: queries here could be async, and wait for all to complete
51 getOne: function(id
, cb
)
53 db
.serialize(function() {
54 // TODO: optimize queries?
56 "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " +
62 db
.get(query
, (err
,gameInfo
) => {
66 "SELECT p.uid, p.color, u.name " +
70 "WHERE p.gid = " + id
;
71 db
.all(query
, (err2
,players
) => {
75 "SELECT squares, message, played, idx " +
78 db
.all(query
, (err3
,moves
) => {
81 const game
= Object
.assign({},
88 return cb(null, game
);
95 getByUser: function(uid
, excluded
, cb
)
97 db
.serialize(function() {
98 // Next query is fine because a player appear at most once in a game
102 "WHERE uid " + (excluded
? "<>" : "=") + " " + uid
;
103 db
.all(query
, (err
,gameIds
) => {
106 gameIds
= gameIds
|| []; //might be empty
108 for (let i
=0; i
<gameIds
.length
; i
++)
110 GameModel
.getOne(gameIds
[i
]["gid"], (err2
,game
) => {
113 gameArray
.push(game
);
114 // Call callback function only when gameArray is complete:
115 if (i
== gameIds
.length
- 1)
116 return cb(null, gameArray
);
123 getPlayers: function(id
, cb
)
125 db
.serialize(function() {
130 db
.all(query
, (err
,players
) => {
131 return cb(err
, players
);
136 // obj can have fields move, fen and/or score
137 update: function(id
, obj
)
139 db
.parallelize(function() {
144 query
+= "fen = '" + obj
.fen
+ "',";
146 query
+= "score = '" + obj
.score
+ "',";
147 query
= query
.slice(0,-1); //remove last comma
148 query
+= " WHERE id = " + id
;
154 "INSERT INTO Moves (gid, squares, message, played, idx) VALUES " +
155 "(" + id
+ ",'" + JSON
.stringify(m
.squares
) + "','" + m
.message
+
156 "'," + m
.played
+ "," + m
.idx
+ ")";
164 db
.parallelize(function() {
166 "DELETE FROM Games " +
170 "DELETE FROM Players " +
174 "DELETE FROM Moves " +
180 cleanGamesDb: function()
182 const tsNow
= Date
.now();
183 // 86400000 = 24 hours in milliseconds
184 const day
= 86400000;
185 db
.serialize(function() {
189 db
.all(query
, (err
,games
) => {
192 "SELECT max(played) AS lastMaj " +
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
)
202 GameModel
.remove(g
.id
);
211 module
.exports
= GameModel
;