cid: c.id, target: c.from.sid}));
}
}
- else
- localStorage.removeItem("challenge");
- if (c.type == "corr")
+ else //my challenge
{
- ajax(
- "/challenges",
- "DELETE",
- {id: c.id}
- );
+ localStorage.removeItem("challenge");
+ if (c.type == "corr")
+ {
+ ajax(
+ "/challenges",
+ "DELETE",
+ {id: c.id}
+ );
+ }
}
},
// NOTE: when launching game, the challenge is already deleted
ajax(
"/games",
"POST",
- {gameInfo: gameInfo}
+ {gameInfo: gameInfo, cid: c.id}, //cid useful to delete challenge
+ response => { this.$router.push("/game/" + response.gameId); }
);
- // TODO: redirection here
}
},
// NOTE: for live games only (corr games start on the server)
});
},
- remove: function(id, uid, cb)
+ remove: function(id)
{
db.serialize(function() {
- let query =
+ const query =
+ "DELETE FROM Challenges " +
+ "WHERE id = " + id;
+ db.run(query);
+ });
+ },
+
+ safeRemove: function(id, uid, cb)
+ {
+ db.serialize(function() {
+ const query =
"SELECT 1 " +
"FROM Challenges " +
"WHERE id = " + id + " AND uid = " + uid;
db.get(query, (err,chall) => {
if (!chall)
return cb({errmsg: "Not your challenge"});
- query =
- "DELETE FROM Challenges " +
- "WHERE id = " + id;
- db.run(query);
+ ChallengeModel.remove(id);
cb(null);
});
});
db.run(insertQuery, err => {
if (!!err)
return cb(err);
- db.get("SELECT last_insert_rowid() AS rowid", (err2,lastId) => {
- players.forEach(p => {
- query =
- "INSERT INTO Players VALUES " +
- // Remaining time = -1 means "unstarted"
- "(" + lastId["rowid"] + "," + p.id + "," + p.color + ", -1)";
- db.run(query, cb);
- });
- });
+ players.forEach(p => {
+ query =
+ "INSERT INTO Players VALUES " +
+ // Remaining time = -1 means "unstarted"
+ "(" + this.lastID + "," + p.id + "," + p.color + ", -1)";
+ db.run(query);
+ });
+ cb(null, {gid: this.lastID});
});
});
},
var db = require("../utils/database");
-var maild = require("../utils/mailer.js");
var genToken = require("../utils/tokenGenerator");
var params = require("../config/parameters");
+var sendEmail = require('../utils/mailer');
/*
* Structure:
/////////////////
// NOTIFICATIONS
- tryNotify: function(oppId, gid, vname, message)
+ tryNotify: function(oppId, message)
{
- // TODO: send email to oppId (request...) with title
- // "vchess.club - vname" and content "message"
+ UserModel.getOne("id", oppId, (err,opp) => {
+ if (!err || !opp.notify)
+ return; //error is ignored here (TODO: should be logged)
+ const subject = "vchess.club - notification";
+ const body = "Hello " + opp.name + "!\n" + message;
+ sendEmail(params.mail.noreply, opp.email, subject, body, err => {
+ res.json(err || {});
+ });
+ });
}
}
router.delete("/challenges", access.logged, access.ajax, (req,res) => {
const cid = req.query.id;
- ChallengeModel.remove(cid, req.userId, err => {
+ ChallengeModel.safeRemove(cid, req.userId, err => {
res.json(err || {}); //TODO: just "return err" because is empty if no errors
});
});
var router = require("express").Router();
var UserModel = require("../models/User");
var sendEmail = require('../utils/mailer');
+var ChallengeModel = require('../models/Challenge');
var GameModel = require('../models/Game');
var VariantModel = require('../models/Variant');
var access = require("../utils/access");
const gameInfo = JSON.parse(req.body.gameInfo);
if (!gameInfo.players.some(p => p.id == req.user.id))
return res.json({errmsg: "Cannot start someone else's game"});
- let fen = req.body.fen;
+ const cid = req.body.cid;
+ ChallengeModel.remove(cid);
+ const fen = req.body.fen;
GameModel.create(
gameInfo.vid, gameInfo.fen, gameInfo.timeControl, gameInfo.players,
- (err,game) => {
+ (err,ret) => {
access.checkRequest(res, err, game, "Cannot create game", () => {
- if (!!req.body.offlineOpp)
- UserModel.tryNotify(req.body.offlineOpp, game.id, variant.name,
- "New game: " + "game link"); //TODO: give game link
- res.json({game: game});
+ const oppIdx = gameInfo.players[0].id == req.user.id ? 1 : 0;
+ const oppId = gameInfo.players[oppIdx].id;
+ UserModel.tryNotify(oppId,
+ "New game: " + params.siteURL + "/game/" + gid);
+ res.json({gameId: ret.gid});
});
}
);
if (!!err)
return res.json({errmsg: err.toString()});
const body =
- "Hello " + to.name + "!\n" +
+ "Hello " + to.name + "!\\n" +
"Access your account here: " +
params.siteURL + "/#/authenticate/" + token + "\\n" +
"Token will expire in " + params.token.expire/(1000*60) + " minutes."