6d8f5c1e865d4f1d7d14f7d5af88ac8a4d41a28a
1 var db
= require("../utils/database");
4 * Structure table Challenges:
8 * vid: variant id (int)
10 * fen: varchar (optional)
11 * timeControl: string (3m+2s, 7d+1d ...)
13 * Structure table WillPlay:
14 * cid: ref challenge id
18 const ChallengeModel
=
20 checkChallenge: function(c
)
22 if (!c
.vid
.match(/^[0-9]+$/))
23 return "Wrong variant ID";
25 if (!c
.timeControl
.match(/^[0-9dhms
+]+$/))
26 return "Wrong characters in time control";
28 if (!c
.nbPlayers
.match(/^[0-9]+$/))
29 return "Wrong number of players";
31 if (!c
.fen
.match(/^[a-zA-Z0-9, /-]+$/))
32 return "Bad FEN string";
35 // fen cannot be undefined
36 create: function(c
, cb
)
38 db
.serialize(function() {
40 "INSERT INTO Challenges " +
41 "(added, uid, vid, nbPlayers, fen, timeControl) VALUES " +
42 "(" + Date
.now() + "," + c
.uid
+ "," + c
.vid
+ "," + c
.nbPlayers
+
43 ",'" + c
.fen
+ "'," + c
.timeControl
+ ")";
44 db
.run(query
, err
=> {
47 db
.get("SELECT last_insert_rowid() AS rowid", (err2
,lastId
) => {
49 // TODO: also insert "will play" "no" for other players ?
50 // willplay = "maybe" by default ?
53 "INSERT INTO WillPlay VALUES " +
54 "(true," + lastId
["rowid"] + "," + c
.uid
+ ")";
55 db
.run(query
, (err
,ret
) => {
56 cb(err
, lastId
); //all we need is the challenge ID
63 getOne: function(id
, cb
)
65 db
.serialize(function() {
68 "FROM Challenges c " +
72 db
.get(query
, (err
,challengeInfo
) => {
76 "SELECT w.uid AS id, u.name " +
80 "WHERE w.cid = " + id
;
81 db
.run(query
, (err2
,players
) => {
86 uid: challengeInfo
.uid
,
87 vname: challengeInfo
.name
,
88 added: challengeInfo
.added
,
89 nbPlayers: challengeInfo
.nbPlayers
,
90 players: players
, //currently in
91 fen: challengeInfo
.fen
,
92 timeControl: challengeInfo
.timeControl
,
94 return cb(null, challenge
);
100 getByUser: function(uid
, cb
)
102 db
.serialize(function() {
106 "WHERE uid = " + uid
;
107 db
.run(query
, (err
,challIds
) => {
111 challIds
.forEach(cidRow
=> {
112 ChallengeModel
.getOne(cidRow
["cid"], (err2
,chall
) => {
115 challenges
.push(chall
);
118 return cb(null, challenges
);
125 db
.parallelize(function() {
127 "DELETE FROM Challenges " +
131 "DELETE FROM WillPlay " +
138 module
.exports
= ChallengeModel
;