3d2184b8ab97586c5623ea1dff6c034c833219af
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)
12 * Structure table Players:
17 * Structure table Moves:
19 * squares: varchar (description)
27 create: function(vid
, fen
, timeControl
, players
, cb
)
29 db
.serialize(function() {
31 "INSERT INTO Games (vid, fenStart, fen, score, timeControl, created)"
32 + " VALUES (" + vid
+ ",'" + fen
+ "','" + fen
+ "','*','"
33 + timeControl
+ "'," + Date
.now() + ")";
34 db
.run(query
, function(err
) {
37 players
.forEach((p
,idx
) => {
38 const color
= (idx
==0 ? "w" : "b");
40 "INSERT INTO Players VALUES " +
41 "(" + this.lastID
+ "," + p
.id
+ ",'" + color
+ "')";
44 cb(null, {gid: this.lastID
});
49 // TODO: queries here could be async, and wait for all to complete
50 getOne: function(id
, cb
)
52 db
.serialize(function() {
53 // TODO: optimize queries?
55 "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " +
61 db
.get(query
, (err
,gameInfo
) => {
65 "SELECT p.uid, p.color, u.name " +
69 "WHERE p.gid = " + id
;
70 db
.all(query
, (err2
,players
) => {
74 "SELECT squares, message, played, idx " +
77 db
.all(query
, (err3
,moves
) => {
80 const game
= Object
.assign({},
87 return cb(null, game
);
94 getByUser: function(uid
, excluded
, cb
)
96 db
.serialize(function() {
97 // Next query is fine because a player appear at most once in a game
101 "WHERE uid " + (excluded
? "<>" : "=") + " " + uid
;
102 db
.all(query
, (err
,gameIds
) => {
105 gameIds
= gameIds
|| []; //might be empty
107 for (let i
=0; i
<gameIds
.length
; i
++)
109 GameModel
.getOne(gameIds
[i
]["gid"], (err2
,game
) => {
112 gameArray
.push(game
);
113 // Call callback function only when gameArray is complete:
114 if (i
== gameIds
.length
- 1)
115 return cb(null, gameArray
);
122 getPlayers: function(id
, cb
)
124 db
.serialize(function() {
129 db
.all(query
, (err
,players
) => {
130 return cb(err
, players
);
135 // obj can have fields move, fen and/or score
136 update: function(id
, obj
)
138 db
.parallelize(function() {
143 query
+= "fen = '" + obj
.fen
+ "',";
145 query
+= "score = '" + obj
.score
+ "',";
146 query
= query
.slice(0,-1); //remove last comma
147 query
+= " WHERE id = " + id
;
153 "INSERT INTO Moves (gid, squares, message, played, idx) VALUES " +
154 "(" + id
+ ",'" + JSON
.stringify(m
.squares
) + "','" + m
.message
+
155 "'," + m
.played
+ "," + m
.idx
+ ")";
163 db
.parallelize(function() {
165 "DELETE FROM Games " +
169 "DELETE FROM Players " +
173 "DELETE FROM Moves " +
179 cleanGamesDb: function()
181 const tsNow
= Date
.now();
182 // 86400000 = 24 hours in milliseconds
183 const day
= 86400000;
184 db
.serialize(function() {
188 db
.all(query
, (err
,games
) => {
191 "SELECT max(played) AS lastMaj " +
193 "WHERE gid = " + g
.id
;
194 db
.get(query
, (err2
,updated
) {
195 if (!updated
&& tsNow
- g
.created
> 7*day
)
196 return GameModel
.remove(g
.id
);
197 const lastMaj
= updated
.lastMaj
;
198 if (g
.score
!= "*" && tsNow
- lastMaj
> 7*day
||
199 g
.score
== "*" && tsNow
- lastMaj
> 91*day
)
201 GameModel
.remove(g
.id
);
210 module
.exports
= GameModel
;