From: Benjamin Auder Date: Tue, 3 Sep 2019 16:19:38 +0000 (+0200) Subject: Some adjustments, next: fix time control handling + finish corr play X-Git-Url: https://git.auder.net/variants/current/doc/css/rpsls.css?a=commitdiff_plain;h=5d04793e1bce0d448b4ffc532f1e8eb47a72e972;p=vchess.git Some adjustments, next: fix time control handling + finish corr play --- diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index a16b5b53..a6102fda 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -124,16 +124,16 @@ export default { } if (this.st.user.id > 0) { - // Ask server for current corr games (all but mines) -// ajax( -// "/games", -// "GET", -// {excluded: this.st.user.id}, -// response => { -// this.games = this.games.concat(response.games); -// } -// ); - // Also ask for corr challenges (open + sent to me) + // Ask server for current corr games (all but mines) + ajax( + "/games", + "GET", + {uid: this.st.user.id, excluded: true}, + response => { + this.games = this.games.concat(response.games); + } + ); + // Also ask for corr challenges (open + sent to me) ajax( "/challenges", "GET", @@ -288,6 +288,15 @@ export default { id: game.id, players: game.players.map(p => p.name), vname: game.vname, + + + + +// TODO: timeControl only in challenge - no need in game (info in mainTime + increment) + + + + timeControl: game.timeControl, }; this.st.conn.send(JSON.stringify({code:"game", @@ -331,10 +340,16 @@ export default { } case "newgame": { - // New game just started: data contain all informations - this.startNewGame(data.gameInfo); - // TODO: if live, redirect to game - // if corr, notify with link but do not redirect + // New game just started: data contain all information + if (data.gameInfo.type == "live") + { + this.startNewGame(data.gameInfo); + // TODO: redirect to game + } + else + { + // TODO: notify with game link but do not redirect + } break; } case "refusechallenge": @@ -481,17 +496,15 @@ export default { const vname = this.getVname(c.vid); const vModule = await import("@/variants/" + vname + ".js"); window.V = vModule.VariantRules; - let players = [c.from, c.seat]; // These game informations will be sent to other players const gameInfo = { gameId: getRandString(), fen: c.fen || V.GenRandInitFen(), - // Players' names may be required if game start when a player is offline - // Shuffle players order (white then black). - players: shuffle(players.map(p => { return {name:p.name, sid:p.sid} })), + players: shuffle([c.from, c.seat]), //white then black vid: c.vid, timeControl: c.timeControl, + type: c.type, }; this.st.conn.send(JSON.stringify({code:"newgame", gameInfo:gameInfo, target:c.seat.sid})); @@ -519,7 +532,7 @@ export default { vname: this.getVname(gameInfo.vid), fenStart: gameInfo.fen, players: gameInfo.players, - timeControl: gameInfo.timeControl, + mainTime: tc.mainTime, increment: tc.increment, mode: "live", //function for live games only // Game state: will be updated diff --git a/server/models/Game.js b/server/models/Game.js index 23a74e81..6f151375 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -94,14 +94,14 @@ const GameModel = }); }, - getByUser: function(uid, cb) + getByUser: function(uid, excluded, cb) { db.serialize(function() { // Next query is fine because a player appear at most once in a game const query = "SELECT gid " + "FROM Players " + - "WHERE uid = " + uid; + "WHERE uid " + (excluded ? "<>" : "=") + " " + uid; db.run(query, (err,gameIds) => { if (!!err) return cb(err); diff --git a/server/models/User.js b/server/models/User.js index 9e1adedb..7a0f70a7 100644 --- a/server/models/User.js +++ b/server/models/User.js @@ -124,6 +124,15 @@ const UserModel = db.run(query, cb); }); }, + + ///////////////// + // NOTIFICATIONS + + tryNotify: function(oppId, gid, vname, message) + { + // TODO: send email to oppId (request...) with title + // "vchess.club - vname" and content "message" + } } module.exports = UserModel; diff --git a/server/routes/games.js b/server/routes/games.js index 35beb250..ec824f73 100644 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -2,14 +2,13 @@ router.get("/games", access.logged, access.ajax, (req,res) => { const excluded = req.query["excluded"]; //TODO: think about query params here }); -// TODO: adapt for correspondance play - var router = require("express").Router(); var UserModel = require("../models/User"); +var sendEmail = require('../utils/mailer'); var GameModel = require('../models/Game'); var VariantModel = require('../models/Variant'); -var ObjectId = require("bson-objectid"); var access = require("../utils/access"); +var params = require("../config/parameters"); // Notify about a game (start, new move) function tryNotify(uid, gid, vname, subject) @@ -17,36 +16,28 @@ function tryNotify(uid, gid, vname, subject) UserModel.getOne("id", uid, (err,user) => { if (!!err && user.notify) { - maild.send({ - from: params.mailFrom, - to: user.email, - subject: subject, - body: params.siteURL + "?v=" + vname + "&g=" + gid - }, err => { - // TODO: log error somewhere. - }); + sendEmail(params.mailFrom, user.email, subject, + params.siteURL + "?v=" + vname + "&g=" + gid, err => { + res.json(err || {}); // TODO: log error somewhere. + } + ); } )}; } -// From variant page, start game between player 0 and 1 +// From main hall, start game between player 0 and 1 router.post("/games", access.logged, access.ajax, (req,res) => { - let variant = JSON.parse(req.body.variant); - let players = JSON.parse(req.body.players); - if (!players.includes(req.user._id.toString())) //TODO: should also check challenge... + 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; - // Randomly shuffle colors white/black - if (Math.random() < 0.5) - players = [players[1],players[0]]; - GameModel.create( - ObjectId(variant._id), [ObjectId(players[0]),ObjectId(players[1])], fen, + GameModel.create(gameInfo.vid, + gameInfo.fen, gameInfo.mainTime, gameInfo.increment, gameInfo.players, (err,game) => { access.checkRequest(res, err, game, "Cannot create game", () => { if (!!req.body.offlineOpp) - UserModel.tryNotify(ObjectId(req.body.offlineOpp), game._id, variant.name, "New game"); - game.movesLength = game.moves.length; //no need for all moves here - delete game["moves"]; + UserModel.tryNotify(req.body.offlineOpp, game.id, variant.name, + "New game: " + "game link"); //TODO: give game link res.json({game: game}); }); } @@ -55,14 +46,29 @@ router.post("/games", access.logged, access.ajax, (req,res) => { // game page router.get("/games", access.ajax, (req,res) => { - let gameID = req.query["gid"]; - GameModel.getById(ObjectId(gameID), (err,game) => { - access.checkRequest(res, err, game, "Game not found", () => { - res.json({game: game}); - }); - }); + const gameId = req.query["gid"]; + if (!!gameId) + { + GameModel.getOne(gameId, (err,game) => { + access.checkRequest(res, err, game, "Game not found", () => { + res.json({game: game}); + }); + }); + } + else + { + // Get by (non-)user ID: + const userId = req.query["uid"]; + const excluded = !!req.query["excluded"]; + GameModel.getByUser(userId, excluded, (err,games) => { + access.checkRequest(res, err, games, "Games not found", () => { + res.json({games: games}); + }); + }); + } }); +// TODO: router.put("/games", access.logged, access.ajax, (req,res) => { let gid = ObjectId(req.body.gid); let result = req.body.result;