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