+ if (!!obj.fen) modifs += "fen = '" + obj.fen + "',";
+ if (!!obj.deletedBy) {
+ const myColor = obj.deletedBy == 'w' ? "White" : "Black";
+ modifs += "deletedBy" + myColor + " = true,";
+ }
+ if (!!obj.chatRead) {
+ const myColor = obj.chatRead == 'w' ? "White" : "Black";
+ modifs += "chatRead" + myColor + " = " + Date.now() + ",";
+ }
+ if (!!obj.score) {
+ modifs += "score = '" + obj.score + "'," +
+ "scoreMsg = '" + obj.scoreMsg + "',";
+ }
+ const finishAndSendQuery = () => {
+ modifs = modifs.slice(0, -1); //remove last comma
+ if (modifs.length > 0) {
+ updateQuery += modifs + " WHERE id = " + id;
+ db.run(updateQuery);
+ }
+ cb(null);
+ };
+ if (!!obj.move || (!!obj.score && obj.scoreMsg == "Time")) {
+ // Security: only update moves if index is right,
+ // and score with scoreMsg "Time" if really lost on time.
+ let query =
+ "SELECT MAX(idx) AS maxIdx, MAX(played) AS lastPlayed " +
+ "FROM Moves " +
+ "WHERE gid = " + id;
+ db.get(query, (err, ret) => {
+ if (!!obj.move ) {
+ if (!ret.maxIdx || ret.maxIdx + 1 == obj.move.idx) {
+ query =
+ "INSERT INTO Moves (gid, squares, played, idx) VALUES " +
+ "(" + id + ",?," + Date.now() + "," + obj.move.idx + ")";
+ db.run(query, JSON.stringify(obj.move.squares));
+ finishAndSendQuery();
+ } else cb({ errmsg: "Wrong move index" });
+ } else {
+ if (ret.maxIdx < 2) cb({ errmsg: "Time not over" });
+ else {
+ // We also need the game cadence
+ query =
+ "SELECT cadence " +
+ "FROM Games " +
+ "WHERE id = " + id;
+ db.get(query, (err2, ret2) => {
+ const daysTc = parseInt(ret2.cadence.match(/^[0-9]+/)[0]);
+ if (Date.now() - ret.lastPlayed > daysTc * 24 * 3600 * 1000)
+ finishAndSendQuery();
+ else cb({ errmsg: "Time not over" });
+ });
+ }
+ }
+ });
+ } else finishAndSendQuery();
+ // NOTE: chat and delchat are mutually exclusive
+ if (!!obj.chat) {
+ const query =