From: Benjamin Auder Date: Fri, 29 Nov 2019 17:34:35 +0000 (+0100) Subject: Fix challenge send+accept. Now debug game launch from server X-Git-Url: https://git.auder.net/%7B%7B%20path%28%27mixstore_static_policy%27%29%20%7D%7D?a=commitdiff_plain;h=8c564f462f5406fcd311d4733f851daf6ada665d;p=vchess.git Fix challenge send+accept. Now debug game launch from server --- diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index 0467dab9..2fbb6e6d 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -499,7 +499,7 @@ export default { } 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 @@ -535,8 +535,18 @@ export default { 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 diff --git a/server/models/Challenge.js b/server/models/Challenge.js index c7476299..f82f9e81 100644 --- a/server/models/Challenge.js +++ b/server/models/Challenge.js @@ -34,7 +34,7 @@ const ChallengeModel = "vid, fen, timeControl) VALUES " + "(" + Date.now() + "," + c.uid + "," + (!!c.to ? c.to + "," : "") + c.vid + ",'" + c.fen + "','" + c.timeControl + "')"; - db.run(query, err => { + db.run(query, function(err) { return cb(err, {cid: this.lastID}); }); }); diff --git a/server/models/Game.js b/server/models/Game.js index e60bfe51..78827e29 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -31,15 +31,16 @@ const GameModel = 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}); diff --git a/server/routes/games.js b/server/routes/games.js index cc8a17bb..24e97471 100644 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -1,43 +1,27 @@ 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}); }); } @@ -67,12 +51,14 @@ router.get("/games", access.ajax, (req,res) => { } }); -// 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({}); }); diff --git a/server/sockets.js b/server/sockets.js index 6e0451f5..04422f46 100644 --- a/server/sockets.js +++ b/server/sockets.js @@ -30,7 +30,6 @@ module.exports = function(wss) { let obj = JSON.parse(objtxt); if (!!obj.target && !clients[obj.target]) return; //receiver not connected, nothing we can do - //console.log(obj.code); switch (obj.code) { case "pollclients":