Start server implementation for correspondance play (early debug stage)
[vchess.git] / models / Challenge.js
CommitLineData
8d7e2786
BA
1var 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 19const ChallengeModel =
8d7e2786 20{
ab4f4bf2
BA
21 // fen cannot be undefined; TODO: generate fen on server instead
22 create: function(c, cb)
23 {
24 db.serialize(function() {
25 let query =
26 "INSERT INTO Challenges (added, uid, vid, nbPlayers, fen, mainTime, addTime) " +
27 "VALUES (" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers + "," +
28 c.fen + "," + c.mainTime + "," + c.increment + ")";
29 db.run(query, err => {
30 if (!!err)
31 return cb(err);
32 db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
33 query =
34 "INSERT INTO WillPlay VALUES " +
35 "(" + lastId["rowid"] + "," + uid + ")";
36 db.run(query, (err,ret) => {
37 cb(err, lastId); //all we need is the challenge ID
38 });
39 });
40 });
41 });
42 },
43
44 getOne: function(id, cb)
45 {
46 db.serialize(function() {
47 let query =
48 "SELECT * " +
49 "FROM Challenges c " +
50 "JOIN Variants v " +
51 " ON c.vid = v.id "
52 "WHERE id = " + id;
53 db.get(query, (err,challengeInfo) => {
54 if (!!err)
55 return cb(err);
00f2759e 56 query =
ab4f4bf2
BA
57 "SELECT w.uid AS id, u.name " +
58 "FROM WillPlay w " +
59 "JOIN Users u " +
60 " ON w.uid = u.id " +
61 "WHERE w.cid = " + id;
62 db.run(query, (err2,players) => {
63 if (!!err2)
64 return cb(err2);
65 const challenge = {
66 id: id,
67 vname: challengeInfo.name,
68 added: challengeInfo.added,
69 nbPlayers: challengeInfo.nbPlayers,
70 players: players, //currently in
71 fen: challengeInfo.fen,
72 mainTime: challengeInfo.mainTime,
73 increment: challengeInfo.addTime,
74 };
75 return cb(null, challenge);
00f2759e
BA
76 });
77 });
78 });
ab4f4bf2 79 },
8d7e2786 80
ab4f4bf2
BA
81 getByUser: function(uid, cb)
82 {
83 db.serialize(function() {
84 const query =
85 "SELECT cid " +
86 "FROM WillPlay " +
87 "WHERE uid = " + uid;
88 db.run(query, (err,challIds) => {
89 if (!!err)
90 return cb(err);
91 let challenges = [];
92 challIds.forEach(cidRow => {
93 ChallengeModel.getOne(cidRow["cid"], (err2,chall) => {
94 if (!!err2)
95 return cb(err2);
96 challenges.push(chall);
97 });
98 });
99 return cb(null, challenges);
00f2759e
BA
100 });
101 });
ab4f4bf2 102 },
8d7e2786 103
ab4f4bf2
BA
104 remove: function(id)
105 {
106 db.parallelize(function() {
107 let query =
108 "DELETE FROM Challenges " +
109 "WHERE id = " + id;
110 db.run(query);
111 query =
112 "DELETE FROM WillPlay " +
113 "WHERE cid = " + id;
114 db.run(query);
115 });
116 },
8d7e2786 117}
ab4f4bf2
BA
118
119module.exports = ChallengeModel;