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 * addTime: integer (increment)
11 * score: varchar (result)
13 * Structure table Players:
17 * rtime: real (remaining time)
19 * Structure table Moves:
21 * move: varchar (description)
30 // mainTime and increment in milliseconds
31 create: function(vid
, fen
, mainTime
, increment
, players
, cb
)
35 "INSERT INTO Games (vid, fen, mainTime, addTime) " +
36 "VALUES (" + vid
+ ",'" + fen
+ "'," + mainTime
+ "," + increment
+ ")";
37 db
.run(insertQuery
, err
=> {
40 db
.get("SELECT last_insert_rowid() AS rowid", (err2
,lastId
) => {
41 players
.forEach(p
=> {
43 "INSERT INTO Players VALUES " +
44 "(" + lastId
["rowid"] + "," + p
.id
+ "," + p
.color
+ "," + mainTime
+ ")";
52 // TODO: queries here could be async, and wait for all to complete
53 getOne: function(id
, cb
)
55 db
.serialize(function() {
57 "SELECT v.name AS vname, g.fen, g.fenStart, g.score " +
62 db
.get(query
, (err
,gameInfo
) => {
66 "SELECT p.uid AS id, p.color, p.rtime, u.name " +
70 "WHERE p.gid = " + id
;
71 db
.run(query
, (err2
,players
) => {
75 "SELECT move AS desc, message, played, idx, color " +
78 db
.run(query
, (err3
,moves
) => {
83 vname: gameInfo
.vname
,
84 fenStart: gameInfo
.fenStart
,
86 score: gameInfo
.score
,
90 return cb(null, game
);
97 getByUser: function(uid
, cb
)
99 db
.serialize(function() {
100 // Next query is fine because a player appear at most once in a game
104 "WHERE uid = " + uid
;
105 db
.run(query
, (err
,gameIds
) => {
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
;