- if (!c.nbPlayers.match(/^[0-9]+$/))
- return "Wrong number of players";
-
- if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/))
- return "Bad FEN string";
- },
-
- // fen cannot be undefined
- create: function(c, cb)
- {
- db.serialize(function() {
- let query =
- "INSERT INTO Challenges " +
- "(added, uid, vid, nbPlayers, fen, timeControl) VALUES " +
- "(" + Date.now() + "," + c.uid + "," + c.vid + "," + c.nbPlayers +
- ",'" + c.fen + "'," + c.timeControl + ")";
- db.run(query, err => {
- if (!!err)
- return cb(err);
- db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
-
- // TODO: also insert "will play" "no" for other players ?
- // willplay = "maybe" by default ?
-
- query =
- "INSERT INTO WillPlay VALUES " +
- "(true," + lastId["rowid"] + "," + c.uid + ")";
- db.run(query, (err,ret) => {
- cb(err, lastId); //all we need is the challenge ID
- });
- });
- });
- });
- },
+ // fen cannot be undefined
+ create: function(c, cb)
+ {
+ db.serialize(function() {
+ const query =
+ "INSERT INTO Challenges " +
+ "(added, uid, " + (!!c.to ? "target, " : "") +
+ "vid, fen, timeControl) VALUES " +
+ "(" + Date.now() + "," + c.uid + "," + (!!c.to ? c.to + "," : "") +
+ c.vid + ",'" + c.fen + "','" + c.timeControl + "')";
+ db.run(query, err => {
+ return cb(err);
+ });
+ });
+ },