96db0a2b431ad5237d8b08b4bcfceca3c4155212
1 var db
= require("../utils/database");
4 * Structure table Challenges:
8 * vid: variant id (int)
10 * fen: varchar (optional)
14 * Structure table WillPlay:
15 * cid: ref challenge id
19 const ChallengeModel
=
21 // fen cannot be undefined; TODO: generate fen on server instead
22 create: function(c
, cb
)
24 db
.serialize(function() {
26 "INSERT INTO Challenges " +
27 "(added, uid, vid, nbPlayers, fen, mainTime, addTime) VALUES " +
28 "(" + Date
.now() + "," + c
.uid
+ "," + c
.vid
+ "," + c
.nbPlayers
+
29 ",'" + c
.fen
+ "'," + c
.mainTime
+ "," + c
.increment
+ ")";
30 db
.run(query
, err
=> {
33 db
.get("SELECT last_insert_rowid() AS rowid", (err2
,lastId
) => {
35 "INSERT INTO WillPlay VALUES " +
36 "(" + lastId
["rowid"] + "," + c
.uid
+ ")";
37 db
.run(query
, (err
,ret
) => {
38 cb(err
, lastId
); //all we need is the challenge ID
45 getOne: function(id
, cb
)
47 db
.serialize(function() {
50 "FROM Challenges c " +
54 db
.get(query
, (err
,challengeInfo
) => {
58 "SELECT w.uid AS id, u.name " +
62 "WHERE w.cid = " + id
;
63 db
.run(query
, (err2
,players
) => {
68 uid: challengeInfo
.uid
,
69 vname: challengeInfo
.name
,
70 added: challengeInfo
.added
,
71 nbPlayers: challengeInfo
.nbPlayers
,
72 players: players
, //currently in
73 fen: challengeInfo
.fen
,
74 mainTime: challengeInfo
.mainTime
,
75 increment: challengeInfo
.addTime
,
77 return cb(null, challenge
);
83 getByUser: function(uid
, cb
)
85 db
.serialize(function() {
90 db
.run(query
, (err
,challIds
) => {
94 challIds
.forEach(cidRow
=> {
95 ChallengeModel
.getOne(cidRow
["cid"], (err2
,chall
) => {
98 challenges
.push(chall
);
101 return cb(null, challenges
);
108 db
.parallelize(function() {
110 "DELETE FROM Challenges " +
114 "DELETE FROM WillPlay " +
121 module
.exports
= ChallengeModel
;