* score: varchar (result)
* scoreMsg: varchar ("Time", "Mutual agreement"...)
* created: datetime
- * drawOffer: boolean
+ * drawOffer: char ('w','b' or '' for none)
*
* Structure table Players:
* gid: ref game id
"INSERT INTO Games"
+ " (vid, fenStart, fen, score, timeControl, created, drawOffer)"
+ " VALUES (" + vid + ",'" + fen + "','" + fen + "','*','"
- + timeControl + "'," + Date.now() + "," + false + ")";
+ + timeControl + "'," + Date.now() + ",'')";
db.run(query, function(err) {
if (!!err)
return cb(err);
// NOTE: g.scoreMsg can be NULL
// (in this case score = "*" and no reason to look at it)
"SELECT g.id, g.vid, g.fen, g.fenStart, g.timeControl, g.score, " +
- "g.scoreMsg, v.name AS vname " +
+ "g.scoreMsg, g.drawOffer, v.name AS vname " +
"FROM Games g " +
"JOIN Variants v " +
" ON g.vid = v.id " +
getByUser: function(uid, excluded, cb)
{
db.serialize(function() {
- // Next query is fine because a player appear at most once in a game
const query =
- "SELECT gid " +
+ "SELECT DISTINCT gid " +
"FROM Players " +
"WHERE uid " + (excluded ? "<>" : "=") + " " + uid;
db.all(query, (err,gameIds) => {
if (!obj.move.idx.toString().match(/^[0-9]+$/))
return "Wrong move index";
}
+ if (!!obj.drawOffer && !obj.drawOffer.match(/^[wbtn]$/))
+ return "Wrong draw offer format";
if (!!obj.fen && !obj.fen.match(/^[a-zA-Z0-9, /-]*$/))
return "Wrong FEN string";
if (!!obj.score && !obj.score.match(/^[012?*\/-]+$/))
let modifs = "";
if (!!obj.message)
modifs += "message = message || ' ' || '" + obj.message + "',";
- if ([true,false].includes(obj.drawOffer))
- modifs += "drawOffer = " + obj.drawOffer + ",";
+ // NOTE: if drawOffer is set, we should check that it's player's turn
+ // A bit overcomplicated. Let's trust the client on that for now...
+ if (!!obj.drawOffer)
+ {
+ if (obj.drawOffer == "n") //Special "None" update
+ obj.drawOffer = "";
+ modifs += "drawOffer = '" + obj.drawOffer + "',";
+ }
if (!!obj.fen)
modifs += "fen = '" + obj.fen + "',";
if (!!obj.score)