Draft game update. TODO: debug, finish view/Game.vue
[vchess.git] / server / models / Game.js
1 var db = require("../utils/database");
2
3 /*
4 * Structure table Games:
5 * id: game id (int)
6 * vid: integer (variant id)
7 * fenStart: varchar (initial position)
8 * fen: varchar (current position)
9 * timeControl: string
10 * score: varchar (result)
11 *
12 * Structure table Players:
13 * gid: ref game id
14 * uid: ref user id
15 * color: character
16 * rtime: real (remaining time)
17 *
18 * Structure table Moves:
19 * gid: ref game id
20 * move: varchar (description)
21 * message: text
22 * played: datetime
23 * idx: integer
24 * color: character
25 */
26
27 const GameModel =
28 {
29 create: function(vid, fen, timeControl, players, cb)
30 {
31 db.serialize(function() {
32 let query =
33 "INSERT INTO Games (vid, fenStart, score, timeControl) " +
34 "VALUES (" + vid + ",'" + fen + "','*','" + timeControl + "')";
35 db.run(query, function(err) {
36 if (!!err)
37 return cb(err);
38 players.forEach((p,idx) => {
39 const color = (idx==0 ? "w" : "b");
40 query =
41 "INSERT INTO Players VALUES " +
42 // Remaining time = -1 means "unstarted"
43 "(" + this.lastID + "," + p.id + ",'" + color + "', -1)";
44 db.run(query);
45 });
46 cb(null, {gid: this.lastID});
47 });
48 });
49 },
50
51 // TODO: queries here could be async, and wait for all to complete
52 getOne: function(id, cb)
53 {
54 db.serialize(function() {
55 let query =
56 "SELECT * " +
57 "FROM Games " +
58 "WHERE id = " + id;
59 db.get(query, (err,gameInfo) => {
60 if (!!err)
61 return cb(err);
62 query =
63 "SELECT uid, color, rtime " +
64 "FROM Players " +
65 "WHERE gid = " + id;
66 db.all(query, (err2,players) => {
67 if (!!err2)
68 return cb(err2);
69 query =
70 "SELECT move, message, played, idx, color " +
71 "FROM Moves " +
72 "WHERE gid = " + id;
73 db.all(query, (err3,moves) => {
74 if (!!err3)
75 return cb(err3);
76 const game = Object.assign({},
77 gameInfo,
78 {
79 players: players,
80 moves: moves
81 }
82 );
83 return cb(null, game);
84 });
85 });
86 });
87 });
88 },
89
90 getByUser: function(uid, excluded, cb)
91 {
92 db.serialize(function() {
93 // Next query is fine because a player appear at most once in a game
94 const query =
95 "SELECT gid " +
96 "FROM Players " +
97 "WHERE uid " + (excluded ? "<>" : "=") + " " + uid;
98 db.run(query, (err,gameIds) => {
99 if (!!err)
100 return cb(err);
101 gameIds = gameIds || []; //might be empty
102 let gameArray = [];
103 gameIds.forEach(gidRow => {
104 GameModel.getOne(gidRow["gid"], (err2,game) => {
105 if (!!err2)
106 return cb(err2);
107 gameArray.push(game);
108 });
109 });
110 return cb(null, gameArray);
111 });
112 });
113 },
114
115 // obj can have fields move, fen and/or score
116 update: function(id, obj, cb)
117 {
118 db.serialize(function() {
119 let query =
120 "UPDATE Games " +
121 "SET ";
122 if (!!obj.move)
123 query += "move = " + obj.move + ","; //TODO: already stringified?!
124 if (!!obj.fen)
125 query += "fen = " + obj.fen + ",";
126 if (!!obj.score)
127 query += "score = " + obj.score + ",";
128 query = query.slice(0,-1); //remove last comma
129 query += " WHERE gameId = " + id;
130 db.run(query, (err) => {
131 cb(err);
132 });
133 });
134 },
135
136 remove: function(id)
137 {
138 db.parallelize(function() {
139 let query =
140 "DELETE FROM Games " +
141 "WHERE id = " + id;
142 db.run(query);
143 query =
144 "DELETE FROM Players " +
145 "WHERE gid = " + id;
146 db.run(query);
147 query =
148 "DELETE FROM Moves " +
149 "WHERE gid = " + id;
150 db.run(query);
151 });
152 },
153 }
154
155 module.exports = GameModel;