const tc = extractTime(c.timeControl);
if (!tc)
return "Wrong time control";
- // Less than 3 days ==> live game (TODO: heuristic... 40 moves also)
- c.liveGame = (tc.mainTime + 40 * tc.increment < 3*24*60*60);
- // Basic alphanumeric check for players names
- let playerCount = 0;
- for (const pname of c.to)
+ // Basic alphanumeric check for opponent name
+ if (!!c.to)
{
- if (pname.length > 0)
- {
- // TODO: slightly redundant (see data/userCheck.js)
- if (!pname.match(/^[\w]+$/))
- return "Wrong characters in players names";
- playerCount++;
- }
+ // TODO: slightly redundant (see data/userCheck.js)
+ if (!c.to.match(/^[\w]+$/))
+ return "Wrong characters in opponent name";
}
- if (playerCount > 0 && playerCount != c.nbPlayers-1)
- return "None, or all of the opponent names must be filled"
-
// Allow custom FEN (and check it) only for individual challenges
- if (c.fen.length > 0 && playerCount > 0)
+ if (c.fen.length > 0 && !!c.to)
{
if (!V.IsGoodFen(c.fen))
return "Bad FEN string";
created: function() {
window.doClick = (elemId) => { document.getElementById(elemId).click() };
store.initialize();
+ // NOTE: at this point, variants and tr(anslations) might be uninitialized
},
}).$mount("#app");
"GET",
{uid: this.st.user.id},
response => {
- console.log(response.challenges);
- // TODO: post-treatment on challenges ?
- Array.prototype.push.apply(this.challenges, response.challenges);
+ // Gather all senders names, and then retrieve full identity:
+ // (TODO [perf]: some might be online...)
+ const uids = response.challenges.map(c => { return c.uid });
+ ajax("/users",
+ "GET",
+ { ids: uids },
+ names => {
+ this.challenges = this.challenges.concat(
+ response.challenges.map(c => {
+ // (just players names in fact)
+ const from = {name: names[c.uid], id: c.uid};
+ const type = this.classifyObject(c);
+ const vname = this.getVname(c.vid);
+ return Object.assign({}, c, {type: type, vname: vname, from: from});
+ })
+ )
+ }
+ );
}
);
}
ajax(
"/challenges",
"POST",
- chall,
+ { chall: chall },
response => { finishAddChallenge(response.cid); }
);
}
);
-- All the following tables are for correspondance play only
--- (Live games are stored only in browsers)
+-- (Live games are stored in browser)
create table Challenges (
id integer primary key,
fenStart varchar, --initial state
fen varchar, --current state
score varchar,
- mainTime integer,
- addTime integer,
+ timeControl varchar,
foreign key (vid) references Variants(id)
);
{
checkChallenge: function(c)
{
- if (!c.vid.match(/^[0-9]+$/))
+ if (!c.vid.toString().match(/^[0-9]+$/))
return "Wrong variant ID";
-
if (!c.timeControl.match(/^[0-9dhms +]+$/))
return "Wrong characters in time control";
-
- if (!c.fen.match(/^[a-zA-Z0-9, /-]+$/))
+ if (!c.fen.match(/^[a-zA-Z0-9, /-]*$/))
return "Bad FEN string";
+ return "";
},
// fen cannot be undefined
create: function(c, cb)
{
db.serialize(function() {
- let query =
+ const query =
"INSERT INTO Challenges " +
"(added, uid, " + (!!c.to ? "target, " : "") +
"vid, fen, timeControl) VALUES " +
getOne: function(id, cb)
{
db.serialize(function() {
- let query =
+ const query =
"SELECT * " +
"FROM Challenges " +
"WHERE id = " + id;
"SELECT * " +
"FROM Challenges " +
"WHERE target IS NULL OR target = " + uid;
- db.run(query, (err,challenges) => {
+ db.all(query, (err,challenges) => {
return cb(err, challenges);
});
});
},
- remove: function(id, uid)
+ remove: function(id, uid, cb)
{
db.serialize(function() {
let query =
"SELECT 1 " +
"FROM Challenges " +
"WHERE id = " + id + " AND uid = " + uid;
- db.run(query, (err,rows) => {
- if (rows.length == 0)
- return res.json({errmsg: "Not your challenge"});
+ db.get(query, (err,chall) => {
+ if (!chall)
+ return cb({errmsg: "Not your challenge"});
query =
"DELETE FROM Challenges " +
"WHERE id = " + id;
db.run(query);
+ cb(null);
});
});
},
const VariantModel =
{
- // This is duplicated in client. TODO: really required here?
- NbPlayers:
- {
- "Alice": [2,3,4],
- "Antiking": [2,3,4],
- "Atomic": [2,3,4],
- "Baroque": [2,3,4],
- "Berolina": [2,4],
- "Checkered": [2,3,4],
- "Chess960": [2,3,4],
- "Crazyhouse": [2,3,4],
- "Dark": [2,3,4],
- "Extinction": [2,3,4],
- "Grand": [2],
- "Losers": [2,3,4],
- "Magnetic": [2],
- "Marseille": [2],
- "Switching": [2,3,4],
- "Upsidedown": [2],
- "Wildebeest": [2],
- "Zen": [2,3,4],
- },
-
getByName: function(name, callback)
{
db.serialize(function() {
if (!!err || !user)
return res.json(err | {errmsg: "Typo in player name"});
challenge.to = user.id; //ready now to insert challenge
+ insertChallenge();
});
- insertChallenge();
}
else
insertChallenge();
});
});
+router.get("/users", access.ajax, (req,res) => {
+ // TODO: list all names + id for users of given ID (query "ids")
+});
+
// to: object user (to who we send an email)
function setAndSendLoginToken(subject, to, res)
{