'update'
[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{
b1aa927b
BA
16 checkChallenge: function(c)
17 {
bebcc8d4 18 if (!c.vid.toString().match(/^[0-9]+$/))
b1aa927b 19 return "Wrong variant ID";
052d17ea
BA
20 if (!c.timeControl.match(/^[0-9dhms +]+$/))
21 return "Wrong characters in time control";
bebcc8d4 22 if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/))
b1aa927b 23 return "Bad FEN string";
bebcc8d4 24 return "";
b1aa927b 25 },
98db2082 26
b1aa927b
BA
27 // fen cannot be undefined
28 create: function(c, cb)
29 {
30 db.serialize(function() {
bebcc8d4 31 const query =
b1aa927b
BA
32 "INSERT INTO Challenges " +
33 "(added, uid, " + (!!c.to ? "target, " : "") +
bf20f404 34 "vid, fen, timeControl) VALUES " +
b1aa927b 35 "(" + Date.now() + "," + c.uid + "," + (!!c.to ? c.to + "," : "") +
bf20f404 36 c.vid + ",'" + c.fen + "','" + c.timeControl + "')";
8c564f46 37 db.run(query, function(err) {
b4de2e73 38 return cb(err, {cid: this.lastID});
bf20f404 39 });
b1aa927b
BA
40 });
41 },
ab4f4bf2 42
b1aa927b
BA
43 getOne: function(id, cb)
44 {
45 db.serialize(function() {
bebcc8d4 46 const query =
b1aa927b
BA
47 "SELECT * " +
48 "FROM Challenges " +
49 "WHERE id = " + id;
50 db.get(query, (err,challenge) => {
51 return cb(err, challenge);
52 });
53 });
54 },
8d7e2786 55
b1aa927b
BA
56 // all challenges except where target is defined and not me
57 getByUser: function(uid, cb)
58 {
59 db.serialize(function() {
60 const query =
61 "SELECT * " +
62 "FROM Challenges " +
63 "WHERE target IS NULL OR target = " + uid;
bebcc8d4 64 db.all(query, (err,challenges) => {
b1aa927b
BA
65 return cb(err, challenges);
66 });
67 });
68 },
8d7e2786 69
2be5d614 70 remove: function(id)
b1aa927b
BA
71 {
72 db.serialize(function() {
2be5d614
BA
73 const query =
74 "DELETE FROM Challenges " +
75 "WHERE id = " + id;
76 db.run(query);
77 });
78 },
79
80 safeRemove: function(id, uid, cb)
81 {
82 db.serialize(function() {
83 const query =
4edfed6c
BA
84 "SELECT 1 " +
85 "FROM Challenges " +
86 "WHERE id = " + id + " AND uid = " + uid;
bebcc8d4
BA
87 db.get(query, (err,chall) => {
88 if (!chall)
89 return cb({errmsg: "Not your challenge"});
2be5d614 90 ChallengeModel.remove(id);
bebcc8d4 91 cb(null);
36093eba 92 });
b1aa927b
BA
93 });
94 },
ab4f4bf2 95
83494c7f
BA
96 // Remove challenges older than 1 month, and 1to1 older than 2 days
97 removeOld: function()
98 {
99 const tsNow = Date.now();
100 // 86400000 = 24 hours in milliseconds
101 const day = 86400000;
102 db.serialize(function() {
103 const query =
104 "SELECT id, target " +
105 "FROM Challenges";
106 db.all(query, (err, challenges) => {
107 challenges.forEach(c => {
108 if ((!c.target && tsNow - c.added > 30*day) ||
109 (!!c.target && tsNow - c.added > 2*day))
110 {
111 db.run("DELETE FROM CHallenges WHERE id = " + c.id);
112 }
113 });
114 });
115 });
116 },
117}
d431028c 118
ab4f4bf2 119module.exports = ChallengeModel;