| 1 | var db = require("../utils/database"); |
| 2 | |
| 3 | /* |
| 4 | * Structure table Challenges: |
| 5 | * id: integer |
| 6 | * added: datetime |
| 7 | * uid: user id (int) |
| 8 | * vid: variant id (int) |
| 9 | * nbPlayers: integer |
| 10 | * fen: varchar (optional) |
| 11 | * mainTime: integer |
| 12 | * addTime: integer |
| 13 | * |
| 14 | * Structure table WillPlay: |
| 15 | * cid: ref challenge id |
| 16 | * uid: ref user id |
| 17 | */ |
| 18 | |
| 19 | const ChallengeModel = |
| 20 | { |
| 21 | checkChallenge: function(c) |
| 22 | { |
| 23 | const vid = parseInt(c.vid); |
| 24 | if (isNaN(vid) || vid <= 0) |
| 25 | return "Please select a variant"; |
| 26 | |
| 27 | const mainTime = parseInt(c.mainTime); |
| 28 | const increment = parseInt(c.increment); |
| 29 | if (isNaN(mainTime) || mainTime <= 0) |
| 30 | return "Main time should be strictly positive"; |
| 31 | if (isNaN(increment) || increment < 0) |
| 32 | return "Increment must be positive"; |
| 33 | |
| 34 | // Basic alphanumeric check for players names |
| 35 | let playerCount = 0; |
| 36 | for (p of c.players) |
| 37 | { |
| 38 | if (p.name.length > 0) |
| 39 | { |
| 40 | if (!p.name.match(/^[\w]+$/)) |
| 41 | return "Wrong characters in players names"; |
| 42 | playerCount++; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if (playerCount > 0 && playerCount != c.nbPlayers-1) |
| 47 | return "None, or all of the opponent names must be filled" |
| 48 | |
| 49 | // Just characters check on server: |
| 50 | if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/)) |
| 51 | return "Bad FEN string"; |
| 52 | }, |
| 53 | |
| 54 | // fen cannot be undefined; TODO: generate fen on server instead |
| 55 | create: function(c, cb) |
| 56 | { |
| 57 | db.serialize(function() { |
| 58 | let query = |
| 59 | "INSERT INTO Challenges " + |
| 60 | "(added, uid, vid, nbPlayers, fen, mainTime, addTime) VALUES " + |
| 61 | "(" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers + |
| 62 | ",'" + c.fen + "'," + c.mainTime + "," + c.increment + ")"; |
| 63 | db.run(query, err => { |
| 64 | if (!!err) |
| 65 | return cb(err); |
| 66 | db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => { |
| 67 | query = |
| 68 | "INSERT INTO WillPlay VALUES " + |
| 69 | "(" + lastId["rowid"] + "," + c.uid + ")"; |
| 70 | db.run(query, (err,ret) => { |
| 71 | cb(err, lastId); //all we need is the challenge ID |
| 72 | }); |
| 73 | }); |
| 74 | }); |
| 75 | }); |
| 76 | }, |
| 77 | |
| 78 | getOne: function(id, cb) |
| 79 | { |
| 80 | db.serialize(function() { |
| 81 | let query = |
| 82 | "SELECT * " + |
| 83 | "FROM Challenges c " + |
| 84 | "JOIN Variants v " + |
| 85 | " ON c.vid = v.id " |
| 86 | "WHERE id = " + id; |
| 87 | db.get(query, (err,challengeInfo) => { |
| 88 | if (!!err) |
| 89 | return cb(err); |
| 90 | query = |
| 91 | "SELECT w.uid AS id, u.name " + |
| 92 | "FROM WillPlay w " + |
| 93 | "JOIN Users u " + |
| 94 | " ON w.uid = u.id " + |
| 95 | "WHERE w.cid = " + id; |
| 96 | db.run(query, (err2,players) => { |
| 97 | if (!!err2) |
| 98 | return cb(err2); |
| 99 | const challenge = { |
| 100 | id: id, |
| 101 | uid: challengeInfo.uid, |
| 102 | vname: challengeInfo.name, |
| 103 | added: challengeInfo.added, |
| 104 | nbPlayers: challengeInfo.nbPlayers, |
| 105 | players: players, //currently in |
| 106 | fen: challengeInfo.fen, |
| 107 | mainTime: challengeInfo.mainTime, |
| 108 | increment: challengeInfo.addTime, |
| 109 | }; |
| 110 | return cb(null, challenge); |
| 111 | }); |
| 112 | }); |
| 113 | }); |
| 114 | }, |
| 115 | |
| 116 | getByUser: function(uid, cb) |
| 117 | { |
| 118 | db.serialize(function() { |
| 119 | const query = |
| 120 | "SELECT cid " + |
| 121 | "FROM WillPlay " + |
| 122 | "WHERE uid = " + uid; |
| 123 | db.run(query, (err,challIds) => { |
| 124 | if (!!err) |
| 125 | return cb(err); |
| 126 | let challenges = []; |
| 127 | challIds.forEach(cidRow => { |
| 128 | ChallengeModel.getOne(cidRow["cid"], (err2,chall) => { |
| 129 | if (!!err2) |
| 130 | return cb(err2); |
| 131 | challenges.push(chall); |
| 132 | }); |
| 133 | }); |
| 134 | return cb(null, challenges); |
| 135 | }); |
| 136 | }); |
| 137 | }, |
| 138 | |
| 139 | remove: function(id) |
| 140 | { |
| 141 | db.parallelize(function() { |
| 142 | let query = |
| 143 | "DELETE FROM Challenges " + |
| 144 | "WHERE id = " + id; |
| 145 | db.run(query); |
| 146 | query = |
| 147 | "DELETE FROM WillPlay " + |
| 148 | "WHERE cid = " + id; |
| 149 | db.run(query); |
| 150 | }); |
| 151 | }, |
| 152 | } |
| 153 | |
| 154 | module.exports = ChallengeModel; |