Corr games: almost there. Then remote games + abort/resign/draw/message
[vchess.git] / server / models / Game.js
CommitLineData
00f2759e
BA
1var db = require("../utils/database");
2
3/*
4 * Structure table Games:
5 * id: game id (int)
6 * vid: integer (variant id)
ab4f4bf2
BA
7 * fenStart: varchar (initial position)
8 * fen: varchar (current position)
a9b131f1 9 * timeControl: string
00f2759e
BA
10 * score: varchar (result)
11 *
12 * Structure table Players:
13 * gid: ref game id
14 * uid: ref user id
15 * color: character
16 *
17 * Structure table Moves:
ab4f4bf2 18 * gid: ref game id
f41ce580 19 * squares: varchar (description)
ab4f4bf2 20 * message: text
00f2759e
BA
21 * played: datetime
22 * idx: integer
23 * color: character
24 */
25
ab4f4bf2 26const GameModel =
00f2759e 27{
a9b131f1 28 create: function(vid, fen, timeControl, players, cb)
ab4f4bf2 29 {
88667455 30 db.serialize(function() {
ab4f4bf2 31 let query =
f41ce580
BA
32 "INSERT INTO Games (vid, fenStart, fen, score, timeControl) VALUES " +
33 "(" + vid + ",'" + fen + "','" + fen + "','*','" + timeControl + "')";
8c564f46 34 db.run(query, function(err) {
cf742aaf
BA
35 if (!!err)
36 return cb(err);
8c564f46
BA
37 players.forEach((p,idx) => {
38 const color = (idx==0 ? "w" : "b");
2be5d614
BA
39 query =
40 "INSERT INTO Players VALUES " +
f41ce580 41 "(" + this.lastID + "," + p.id + ",'" + color + "')";
2be5d614
BA
42 db.run(query);
43 });
44 cb(null, {gid: this.lastID});
00f2759e
BA
45 });
46 });
ab4f4bf2 47 },
00f2759e 48
ab4f4bf2
BA
49 // TODO: queries here could be async, and wait for all to complete
50 getOne: function(id, cb)
51 {
52 db.serialize(function() {
f41ce580 53 // TODO: optimize queries?
ab4f4bf2 54 let query =
f41ce580
BA
55 "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " +
56 "v.name AS vname " +
57 "FROM Games g " +
58 "JOIN Variants v " +
59 " ON g.vid = v.id " +
60 "WHERE g.id = " + id;
ab4f4bf2
BA
61 db.get(query, (err,gameInfo) => {
62 if (!!err)
63 return cb(err);
00f2759e 64 query =
f41ce580
BA
65 "SELECT p.uid, p.color, u.name " +
66 "FROM Players p " +
67 "JOIN Users u " +
68 " ON p.uid = u.id " +
69 "WHERE p.gid = " + id;
fd7aea36 70 db.all(query, (err2,players) => {
ab4f4bf2
BA
71 if (!!err2)
72 return cb(err2);
73 query =
f41ce580 74 "SELECT squares, message, played, idx, color " +
ab4f4bf2
BA
75 "FROM Moves " +
76 "WHERE gid = " + id;
fd7aea36 77 db.all(query, (err3,moves) => {
ab4f4bf2
BA
78 if (!!err3)
79 return cb(err3);
fd7aea36
BA
80 const game = Object.assign({},
81 gameInfo,
82 {
83 players: players,
84 moves: moves
85 }
86 );
ab4f4bf2
BA
87 return cb(null, game);
88 });
00f2759e
BA
89 });
90 });
91 });
ab4f4bf2 92 },
00f2759e 93
5d04793e 94 getByUser: function(uid, excluded, cb)
ab4f4bf2
BA
95 {
96 db.serialize(function() {
97 // Next query is fine because a player appear at most once in a game
98 const query =
99 "SELECT gid " +
100 "FROM Players " +
5d04793e 101 "WHERE uid " + (excluded ? "<>" : "=") + " " + uid;
ab4f4bf2
BA
102 db.run(query, (err,gameIds) => {
103 if (!!err)
104 return cb(err);
098cd7f1 105 gameIds = gameIds || []; //might be empty
ab4f4bf2
BA
106 let gameArray = [];
107 gameIds.forEach(gidRow => {
108 GameModel.getOne(gidRow["gid"], (err2,game) => {
109 if (!!err2)
110 return cb(err2);
111 gameArray.push(game);
112 });
113 });
114 return cb(null, gameArray);
115 });
116 });
117 },
118
411d23cd
BA
119 getPlayers: function(id, cb)
120 {
121 db.serialize(function() {
122 const query =
123 "SELECT id " +
124 "FROM Players " +
125 "WHERE gid = " + id;
126 db.all(query, (err,players) => {
127 return cb(err, players);
128 });
129 });
130 },
131
3d55deea 132 // obj can have fields move, fen and/or score
f41ce580 133 update: function(id, obj)
3d55deea 134 {
f41ce580
BA
135
136
137
138console.log(id);
139 console.log(obj);
140
141
142 db.parallelize(function() {
3d55deea
BA
143 let query =
144 "UPDATE Games " +
145 "SET ";
3d55deea
BA
146 if (!!obj.fen)
147 query += "fen = " + obj.fen + ",";
148 if (!!obj.score)
149 query += "score = " + obj.score + ",";
150 query = query.slice(0,-1); //remove last comma
151 query += " WHERE gameId = " + id;
f41ce580
BA
152 db.run(query);
153 if (!!obj.move)
154 {
155 const m =obj.move;
156 query =
157 "INSERT INTO Moves (gid,squares,message,played,idx,color) VALUES " +
158 "(" + id + ",'" + JSON.stringify(m.squares) + "','" + m.message +
159 "'" + m.played + "," + m.idx + ",'" + m.color + "')";
160 db.run(query);
161 }
3d55deea
BA
162 });
163 },
164
ab4f4bf2
BA
165 remove: function(id)
166 {
167 db.parallelize(function() {
168 let query =
169 "DELETE FROM Games " +
170 "WHERE id = " + id;
171 db.run(query);
172 query =
173 "DELETE FROM Players " +
174 "WHERE gid = " + id;
175 db.run(query);
176 query =
177 "DELETE FROM Moves " +
178 "WHERE gid = " + id;
179 db.run(query);
180 });
181 },
00f2759e 182}
ab4f4bf2
BA
183
184module.exports = GameModel;