}
if (c.accepted)
{
- c.seat = this.people[0]; //avoid sending email
+ c.seat = this.people[0]; //== this.st.user, avoid revealing email
this.launchGame(c);
}
else
vid: c.vid,
timeControl: c.timeControl,
};
- this.st.conn.send(JSON.stringify({code:"newgame",
- gameInfo:gameInfo, target:c.from.sid, cid:c.id}));
+ let target = c.from.sid; //may not be defined if corr + offline opp
+ if (!target)
+ {
+ const opponent = this.people.find(p => p.id == c.from.id);
+ if (!!opponent)
+ target = opponent.sid
+ }
+ if (!!target) //opponent is online
+ {
+ this.st.conn.send(JSON.stringify({code:"newgame",
+ gameInfo:gameInfo, target:target, cid:c.id}));
+ }
if (c.type == "live")
this.startNewGame(gameInfo);
else //corr: game only on server
db.serialize(function() {
let query =
"INSERT INTO Games (vid, fen, timeControl) " +
- "VALUES (" + vid + ",'" + fen + "'," + timeControl + ")";
- db.run(insertQuery, err => {
+ "VALUES (" + vid + ",'" + fen + "','" + timeControl + "')";
+ db.run(query, function(err) {
if (!!err)
return cb(err);
- players.forEach(p => {
+ players.forEach((p,idx) => {
+ const color = (idx==0 ? "w" : "b");
query =
"INSERT INTO Players VALUES " +
// Remaining time = -1 means "unstarted"
- "(" + this.lastID + "," + p.id + "," + p.color + ", -1)";
+ "(" + this.lastID + "," + p.id + ",'" + color + "', -1)";
db.run(query);
});
cb(null, {gid: this.lastID});
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");
var params = require("../config/parameters");
-// Notify about a game (start, new move)
-function tryNotify(uid, gid, vname, subject)
-{
- UserModel.getOne("id", uid, (err,user) => {
- if (!!err && user.notify)
- {
- sendEmail(params.mailFrom, user.email, subject,
- params.siteURL + "?v=" + vname + "&g=" + gid, err => {
- res.json(err || {}); // TODO: log error somewhere.
- }
- );
- }
- });
-}
-
// From main hall, start game between players 0 and 1
router.post("/games", access.logged, access.ajax, (req,res) => {
- const gameInfo = JSON.parse(req.body.gameInfo);
- if (!gameInfo.players.some(p => p.id == req.user.id))
+ const gameInfo = req.body.gameInfo;
+ if (!gameInfo.players.some(p => p.id == req.userId))
return res.json({errmsg: "Cannot start someone else's game"});
- const cid = req.body.cid;
+ const cid = req.body.cid;
ChallengeModel.remove(cid);
const fen = req.body.fen;
GameModel.create(
gameInfo.vid, gameInfo.fen, gameInfo.timeControl, gameInfo.players,
(err,ret) => {
- access.checkRequest(res, err, game, "Cannot create game", () => {
- const oppIdx = gameInfo.players[0].id == req.user.id ? 1 : 0;
+ access.checkRequest(res, err, ret, "Cannot create game", () => {
+ const oppIdx = (gameInfo.players[0].id == req.userId ? 1 : 0);
const oppId = gameInfo.players[oppIdx].id;
UserModel.tryNotify(oppId,
- "New game: " + params.siteURL + "/game/" + gid);
+ "New game: " + params.siteURL + "/game/" + ret.gid);
res.json({gameId: ret.gid});
});
}
}
});
-// TODO:
+//////////////////////////////////
+
+// TODO: new move
router.put("/games", access.logged, access.ajax, (req,res) => {
let gid = ObjectId(req.body.gid);
let result = req.body.result;
// NOTE: only game-level life update is "gameover"
- GameModel.gameOver(gid, result, ObjectId(req.user._id), (err,game) => {
+ GameModel.gameOver(gid, result, ObjectId(req.userId), (err,game) => {
access.checkRequest(res, err, game, "Cannot find game", () => {
res.json({});
});