Drop 'WillPlay' table (no multiplayers games)
[vchess.git] / server / models / Challenge.js
CommitLineData
8d7e2786
BA
1var db = require("../utils/database");
2
3/*
bf20f404 4 * Structure:
00f2759e
BA
5 * id: integer
6 * added: datetime
7 * uid: user id (int)
bf20f404 8 * target: recipient id (optional)
00f2759e 9 * vid: variant id (int)
ab4f4bf2 10 * fen: varchar (optional)
052d17ea 11 * timeControl: string (3m+2s, 7d+1d ...)
8d7e2786
BA
12 */
13
ab4f4bf2 14const ChallengeModel =
8d7e2786 15{
98db2082
BA
16 checkChallenge: function(c)
17 {
052d17ea
BA
18 if (!c.vid.match(/^[0-9]+$/))
19 return "Wrong variant ID";
98db2082 20
052d17ea
BA
21 if (!c.timeControl.match(/^[0-9dhms +]+$/))
22 return "Wrong characters in time control";
98db2082 23
052d17ea 24 if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/))
98db2082
BA
25 return "Bad FEN string";
26 },
27
052d17ea 28 // fen cannot be undefined
ab4f4bf2
BA
29 create: function(c, cb)
30 {
31 db.serialize(function() {
32 let query =
74ea2e8d 33 "INSERT INTO Challenges " +
bf20f404
BA
34 "(added, uid, " + (!!c.to ? "target, " : "") +
35 "vid, fen, timeControl) VALUES " +
36 "(" + Date.now() + "," + c.uid + "," + (!!c.to ? c.to + "," : "") +
37 c.vid + ",'" + c.fen + "','" + c.timeControl + "')";
ab4f4bf2
BA
38 db.run(query, err => {
39 if (!!err)
40 return cb(err);
41 db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
bf20f404
BA
42 return cb(err2, lastId);
43 });
44 });
ab4f4bf2
BA
45 });
46 },
47
48 getOne: function(id, cb)
49 {
50 db.serialize(function() {
51 let query =
52 "SELECT * " +
bf20f404 53 "FROM Challenges " +
ab4f4bf2
BA
54 "WHERE id = " + id;
55 db.get(query, (err,challengeInfo) => {
56 if (!!err)
57 return cb(err);
f05815d7 58 let condition = "";
bf20f404
BA
59 if (!!challengeInfo.to)
60 condition = "IN (" + challengeInfo.uid + "," + challengeInfo.to + ")";
61 else
62 condition = "= " + challengeInfo.uid;
00f2759e 63 query =
bf20f404
BA
64 "SELECT id, name " +
65 "FROM Users " +
66 "WHERE id " + condition;
ab4f4bf2
BA
67 db.run(query, (err2,players) => {
68 if (!!err2)
69 return cb(err2);
70 const challenge = {
71 id: id,
bf20f404
BA
72 uid: challengeInfo.uid, //sender (but we don't know who ask)
73 vid: challengeInfo.vid,
ab4f4bf2 74 added: challengeInfo.added,
bf20f404 75 players: players, //sender + potential receiver
ab4f4bf2 76 fen: challengeInfo.fen,
052d17ea 77 timeControl: challengeInfo.timeControl,
ab4f4bf2
BA
78 };
79 return cb(null, challenge);
00f2759e
BA
80 });
81 });
82 });
ab4f4bf2 83 },
8d7e2786 84
ab4f4bf2
BA
85 getByUser: function(uid, cb)
86 {
87 db.serialize(function() {
88 const query =
89 "SELECT cid " +
90 "FROM WillPlay " +
91 "WHERE uid = " + uid;
92 db.run(query, (err,challIds) => {
93 if (!!err)
94 return cb(err);
f05815d7 95 challIds = challIds || [];
ab4f4bf2
BA
96 let challenges = [];
97 challIds.forEach(cidRow => {
98 ChallengeModel.getOne(cidRow["cid"], (err2,chall) => {
99 if (!!err2)
100 return cb(err2);
101 challenges.push(chall);
102 });
103 });
104 return cb(null, challenges);
00f2759e
BA
105 });
106 });
ab4f4bf2 107 },
8d7e2786 108
1f49533d 109 remove: function(id, uid)
ab4f4bf2 110 {
1f49533d 111 db.serialize(function() {
ab4f4bf2 112 let query =
4edfed6c
BA
113 "SELECT 1 " +
114 "FROM Challenges " +
115 "WHERE id = " + id + " AND uid = " + uid;
116 db.run(query, (err,rows) => {
bf20f404
BA
117 if (rows.length == 0)
118 return res.json({errmsg: "Not your challenge"});
119 query =
120 "DELETE FROM Challenges " +
121 "WHERE id = " + id;
122 db.run(query);
36093eba 123 });
ab4f4bf2
BA
124 });
125 },
8d7e2786 126}
ab4f4bf2
BA
127
128module.exports = ChallengeModel;