| 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 | * created: datetime |
| 12 | * |
| 13 | * Structure table Players: |
| 14 | * gid: ref game id |
| 15 | * uid: ref user id |
| 16 | * color: character |
| 17 | * |
| 18 | * Structure table Moves: |
| 19 | * gid: ref game id |
| 20 | * squares: varchar (description) |
| 21 | * message: text |
| 22 | * played: datetime |
| 23 | * idx: integer |
| 24 | */ |
| 25 | |
| 26 | const GameModel = |
| 27 | { |
| 28 | create: function(vid, fen, timeControl, players, cb) |
| 29 | { |
| 30 | db.serialize(function() { |
| 31 | let query = |
| 32 | "INSERT INTO Games" |
| 33 | + " (vid, fenStart, fen, score, timeControl, created, drawOffer)" |
| 34 | + " VALUES (" + vid + ",'" + fen + "','" + fen + "','*','" |
| 35 | + timeControl + "'," + Date.now() + "," + false + ")"; |
| 36 | db.run(query, function(err) { |
| 37 | if (!!err) |
| 38 | return cb(err); |
| 39 | players.forEach((p,idx) => { |
| 40 | const color = (idx==0 ? "w" : "b"); |
| 41 | query = |
| 42 | "INSERT INTO Players VALUES " + |
| 43 | "(" + this.lastID + "," + p.id + ",'" + color + "')"; |
| 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 | // TODO: optimize queries? |
| 56 | let query = |
| 57 | "SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " + |
| 58 | "v.name AS vname " + |
| 59 | "FROM Games g " + |
| 60 | "JOIN Variants v " + |
| 61 | " ON g.vid = v.id " + |
| 62 | "WHERE g.id = " + id; |
| 63 | db.get(query, (err,gameInfo) => { |
| 64 | if (!!err) |
| 65 | return cb(err); |
| 66 | query = |
| 67 | "SELECT p.uid, p.color, u.name " + |
| 68 | "FROM Players p " + |
| 69 | "JOIN Users u " + |
| 70 | " ON p.uid = u.id " + |
| 71 | "WHERE p.gid = " + id; |
| 72 | db.all(query, (err2,players) => { |
| 73 | if (!!err2) |
| 74 | return cb(err2); |
| 75 | query = |
| 76 | "SELECT squares, message, played, idx " + |
| 77 | "FROM Moves " + |
| 78 | "WHERE gid = " + id; |
| 79 | db.all(query, (err3,moves) => { |
| 80 | if (!!err3) |
| 81 | return cb(err3); |
| 82 | const game = Object.assign({}, |
| 83 | gameInfo, |
| 84 | { |
| 85 | players: players, |
| 86 | moves: moves |
| 87 | } |
| 88 | ); |
| 89 | return cb(null, game); |
| 90 | }); |
| 91 | }); |
| 92 | }); |
| 93 | }); |
| 94 | }, |
| 95 | |
| 96 | getByUser: function(uid, excluded, cb) |
| 97 | { |
| 98 | db.serialize(function() { |
| 99 | // Next query is fine because a player appear at most once in a game |
| 100 | const query = |
| 101 | "SELECT gid " + |
| 102 | "FROM Players " + |
| 103 | "WHERE uid " + (excluded ? "<>" : "=") + " " + uid; |
| 104 | db.all(query, (err,gameIds) => { |
| 105 | if (!!err) |
| 106 | return cb(err); |
| 107 | gameIds = gameIds || []; //might be empty |
| 108 | let gameArray = []; |
| 109 | for (let i=0; i<gameIds.length; i++) |
| 110 | { |
| 111 | GameModel.getOne(gameIds[i]["gid"], (err2,game) => { |
| 112 | if (!!err2) |
| 113 | return cb(err2); |
| 114 | gameArray.push(game); |
| 115 | // Call callback function only when gameArray is complete: |
| 116 | if (i == gameIds.length - 1) |
| 117 | return cb(null, gameArray); |
| 118 | }); |
| 119 | } |
| 120 | }); |
| 121 | }); |
| 122 | }, |
| 123 | |
| 124 | getPlayers: function(id, cb) |
| 125 | { |
| 126 | db.serialize(function() { |
| 127 | const query = |
| 128 | "SELECT id " + |
| 129 | "FROM Players " + |
| 130 | "WHERE gid = " + id; |
| 131 | db.all(query, (err,players) => { |
| 132 | return cb(err, players); |
| 133 | }); |
| 134 | }); |
| 135 | }, |
| 136 | |
| 137 | // obj can have fields move, fen, drawOffer and/or score |
| 138 | update: function(id, obj) |
| 139 | { |
| 140 | db.parallelize(function() { |
| 141 | let query = |
| 142 | "UPDATE Games " + |
| 143 | "SET "; |
| 144 | if (!!obj.drawOffer) |
| 145 | query += "drawOffer = " + obj.drawOffer + ","; |
| 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 id = " + id; |
| 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) VALUES " + |
| 158 | "(" + id + ",'" + JSON.stringify(m.squares) + "','" + m.message + |
| 159 | "'," + m.played + "," + m.idx + ")"; |
| 160 | db.run(query); |
| 161 | } |
| 162 | }); |
| 163 | }, |
| 164 | |
| 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 | }, |
| 182 | |
| 183 | cleanGamesDb: function() |
| 184 | { |
| 185 | const tsNow = Date.now(); |
| 186 | // 86400000 = 24 hours in milliseconds |
| 187 | const day = 86400000; |
| 188 | db.serialize(function() { |
| 189 | let query = |
| 190 | "SELECT id,score " + |
| 191 | "FROM Games "; |
| 192 | db.all(query, (err,games) => { |
| 193 | games.forEach(g => { |
| 194 | query = |
| 195 | "SELECT max(played) AS lastMaj " + |
| 196 | "FROM Moves " + |
| 197 | "WHERE gid = " + g.id; |
| 198 | db.get(query, (err2,updated) => { |
| 199 | if (!updated && tsNow - g.created > 7*day) |
| 200 | return GameModel.remove(g.id); |
| 201 | const lastMaj = updated.lastMaj; |
| 202 | if (g.score != "*" && tsNow - lastMaj > 7*day || |
| 203 | g.score == "*" && tsNow - lastMaj > 91*day) |
| 204 | { |
| 205 | GameModel.remove(g.id); |
| 206 | } |
| 207 | }); |
| 208 | }); |
| 209 | }); |
| 210 | }); |
| 211 | }, |
| 212 | } |
| 213 | |
| 214 | module.exports = GameModel; |