| 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, message, 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.message) |
| 145 | query += "message = message || ' ' || '" + obj.message + "',"; |
| 146 | if (!!obj.drawOffer) |
| 147 | query += "drawOffer = " + obj.drawOffer + ","; |
| 148 | if (!!obj.fen) |
| 149 | query += "fen = '" + obj.fen + "',"; |
| 150 | if (!!obj.score) |
| 151 | query += "score = '" + obj.score + "',"; |
| 152 | query = query.slice(0,-1); //remove last comma |
| 153 | query += " WHERE id = " + id; |
| 154 | db.run(query); |
| 155 | if (!!obj.move) |
| 156 | { |
| 157 | const m = obj.move; |
| 158 | query = |
| 159 | "INSERT INTO Moves (gid, squares, played, idx) VALUES " + |
| 160 | "(" + id + ",'" + JSON.stringify(m.squares) + "'," |
| 161 | + m.played + "," + m.idx + ")"; |
| 162 | db.run(query); |
| 163 | } |
| 164 | }); |
| 165 | }, |
| 166 | |
| 167 | remove: function(id) |
| 168 | { |
| 169 | db.parallelize(function() { |
| 170 | let query = |
| 171 | "DELETE FROM Games " + |
| 172 | "WHERE id = " + id; |
| 173 | db.run(query); |
| 174 | query = |
| 175 | "DELETE FROM Players " + |
| 176 | "WHERE gid = " + id; |
| 177 | db.run(query); |
| 178 | query = |
| 179 | "DELETE FROM Moves " + |
| 180 | "WHERE gid = " + id; |
| 181 | db.run(query); |
| 182 | }); |
| 183 | }, |
| 184 | |
| 185 | cleanGamesDb: function() |
| 186 | { |
| 187 | const tsNow = Date.now(); |
| 188 | // 86400000 = 24 hours in milliseconds |
| 189 | const day = 86400000; |
| 190 | db.serialize(function() { |
| 191 | let query = |
| 192 | "SELECT id,score " + |
| 193 | "FROM Games "; |
| 194 | db.all(query, (err,games) => { |
| 195 | games.forEach(g => { |
| 196 | query = |
| 197 | "SELECT max(played) AS lastMaj " + |
| 198 | "FROM Moves " + |
| 199 | "WHERE gid = " + g.id; |
| 200 | db.get(query, (err2,updated) => { |
| 201 | if (!updated && tsNow - g.created > 7*day) |
| 202 | return GameModel.remove(g.id); |
| 203 | const lastMaj = updated.lastMaj; |
| 204 | if (g.score != "*" && tsNow - lastMaj > 7*day || |
| 205 | g.score == "*" && tsNow - lastMaj > 91*day) |
| 206 | { |
| 207 | GameModel.remove(g.id); |
| 208 | } |
| 209 | }); |
| 210 | }); |
| 211 | }); |
| 212 | }); |
| 213 | }, |
| 214 | } |
| 215 | |
| 216 | module.exports = GameModel; |