- db.get(query, (err,ret) => {
- const m = obj.move;
- if (!ret.maxIdx || ret.maxIdx + 1 == m.idx) {
- query =
- "INSERT INTO Moves (gid, squares, played, idx) VALUES " +
- "(" + id + ",?," + m.played + "," + m.idx + ")";
- db.run(query, JSON.stringify(m.squares));
- cb(null);
+ 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" });
+ });
+ }