Commit | Line | Data |
---|---|---|
8d7e2786 BA |
1 | var db = require("../utils/database"); |
2 | ||
3 | /* | |
00f2759e BA |
4 | * Structure table Challenges: |
5 | * id: integer | |
6 | * added: datetime | |
7 | * uid: user id (int) | |
8 | * vid: variant id (int) | |
9 | * nbPlayers: integer | |
ab4f4bf2 BA |
10 | * fen: varchar (optional) |
11 | * mainTime: integer | |
12 | * addTime: integer | |
00f2759e BA |
13 | * |
14 | * Structure table WillPlay: | |
15 | * cid: ref challenge id | |
16 | * uid: ref user id | |
8d7e2786 BA |
17 | */ |
18 | ||
ab4f4bf2 | 19 | const ChallengeModel = |
8d7e2786 | 20 | { |
98db2082 BA |
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 | ||
ab4f4bf2 BA |
54 | // fen cannot be undefined; TODO: generate fen on server instead |
55 | create: function(c, cb) | |
56 | { | |
57 | db.serialize(function() { | |
58 | let query = | |
74ea2e8d BA |
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 + ")"; | |
ab4f4bf2 BA |
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 " + | |
74ea2e8d | 69 | "(" + lastId["rowid"] + "," + c.uid + ")"; |
ab4f4bf2 BA |
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); | |
00f2759e | 90 | query = |
ab4f4bf2 BA |
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, | |
74ea2e8d | 101 | uid: challengeInfo.uid, |
ab4f4bf2 BA |
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); | |
00f2759e BA |
111 | }); |
112 | }); | |
113 | }); | |
ab4f4bf2 | 114 | }, |
8d7e2786 | 115 | |
ab4f4bf2 BA |
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); | |
00f2759e BA |
135 | }); |
136 | }); | |
ab4f4bf2 | 137 | }, |
8d7e2786 | 138 | |
ab4f4bf2 BA |
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 | }, | |
8d7e2786 | 152 | } |
ab4f4bf2 BA |
153 | |
154 | module.exports = ChallengeModel; |