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 (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
=> {
32 db
.get("SELECT last_insert_rowid() AS rowid", (err2
,lastId
) => {
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
44 getOne: function(id
, cb
)
46 db
.serialize(function() {
49 "FROM Challenges c " +
53 db
.get(query
, (err
,challengeInfo
) => {
57 "SELECT w.uid AS id, u.name " +
61 "WHERE w.cid = " + id
;
62 db
.run(query
, (err2
,players
) => {
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
,
75 return cb(null, challenge
);
81 getByUser: function(uid
, cb
)
83 db
.serialize(function() {
88 db
.run(query
, (err
,challIds
) => {
92 challIds
.forEach(cidRow
=> {
93 ChallengeModel
.getOne(cidRow
["cid"], (err2
,chall
) => {
96 challenges
.push(chall
);
99 return cb(null, challenges
);
106 db
.parallelize(function() {
108 "DELETE FROM Challenges " +
112 "DELETE FROM WillPlay " +
119 module
.exports
= ChallengeModel
;