954ffc402737239916718112a0f160dd8a95c1e5
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:
16 * rtime: real (remaining time)
18 * Structure table Moves:
20 * move: varchar (description)
29 create: function(vid
, fen
, timeControl
, players
, cb
)
31 db
.serialize(function() {
33 "INSERT INTO Games (vid, fen, timeControl) " +
34 "VALUES (" + vid
+ ",'" + fen
+ "'," + timeControl
+ ")";
35 db
.run(insertQuery
, err
=> {
38 db
.get("SELECT last_insert_rowid() AS rowid", (err2
,lastId
) => {
39 players
.forEach(p
=> {
41 "INSERT INTO Players VALUES " +
42 // Remaining time = -1 means "unstarted"
43 "(" + lastId
["rowid"] + "," + p
.id
+ "," + p
.color
+ ", -1)";
51 // TODO: queries here could be async, and wait for all to complete
52 getOne: function(id
, cb
)
54 db
.serialize(function() {
56 "SELECT v.name AS vname, g.fen, g.fenStart, g.score " +
61 db
.get(query
, (err
,gameInfo
) => {
65 "SELECT p.uid AS id, p.color, p.rtime, u.name " +
69 "WHERE p.gid = " + id
;
70 db
.run(query
, (err2
,players
) => {
74 "SELECT move AS desc, message, played, idx, color " +
77 db
.run(query
, (err3
,moves
) => {
82 vname: gameInfo
.vname
,
83 fenStart: gameInfo
.fenStart
,
85 score: gameInfo
.score
,
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
.run(query
, (err
,gameIds
) => {
107 gameIds
= gameIds
|| []; //might be empty
109 gameIds
.forEach(gidRow
=> {
110 GameModel
.getOne(gidRow
["gid"], (err2
,game
) => {
113 gameArray
.push(game
);
116 return cb(null, gameArray
);
123 db
.parallelize(function() {
125 "DELETE FROM Games " +
129 "DELETE FROM Players " +
133 "DELETE FROM Moves " +
140 module
.exports
= GameModel
;