From: Benjamin Auder Date: Tue, 3 Dec 2019 17:45:44 +0000 (+0100) Subject: Improve notification system after a move or at new game X-Git-Url: https://git.auder.net/variants/Dynamo/%7B%7B%20asset%28%27mixstore/images/current/gitweb.js?a=commitdiff_plain;h=411d23cd80a2dbf53d21008976d34e7f450154bf;p=vchess.git Improve notification system after a move or at new game --- diff --git a/client/src/views/Game.vue b/client/src/views/Game.vue index 67af3b9f..923e9400 100644 --- a/client/src/views/Game.vue +++ b/client/src/views/Game.vue @@ -61,7 +61,7 @@ export default { rid: "" }, game: { }, //passed to BaseGame - oppConnected: false, + oppConnected: false, //TODO: use for styling corrMsg: "", //to send offline messages in corr games virtualClocks: [0, 0], //initialized with true game.clocks vr: null, //"variant rules" object initialized from FEN @@ -152,18 +152,21 @@ export default { case "pong": //received if we sent a ping (game still alive on our side) { this.oppConnected = true; - // Send our "last state" informations to opponent(s) - const L = this.game.moves.length; - this.st.conn.send(JSON.stringify({ - code: "lastate", - target: this.game.oppid, - gameId: this.gameRef.id, - lastMove: (L>0 ? this.game.moves[L-1] : undefined), - score: this.game.score, - movesCount: L, - drawOffer: this.drawOffer, - clocks: this.game.clocks, - })); + if (this.game.type == "live") //corr games are always complete + { + // Send our "last state" informations to opponent(s) + const L = this.game.moves.length; + this.st.conn.send(JSON.stringify({ + code: "lastate", + target: this.game.oppid, + gameId: this.gameRef.id, + lastMove: (L>0 ? this.game.moves[L-1] : undefined), + score: this.game.score, + movesCount: L, + drawOffer: this.drawOffer, + clocks: this.game.clocks, + })); + } break; } case "lastate": //got opponent infos about last move diff --git a/client/src/views/Hall.vue b/client/src/views/Hall.vue index 82330ef1..433151c7 100644 --- a/client/src/views/Hall.vue +++ b/client/src/views/Hall.vue @@ -550,20 +550,29 @@ export default { 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})); - } + const tryNotifyOpponent = () => { + if (!!target) //opponent is online + { + this.st.conn.send(JSON.stringify({code:"newgame", + gameInfo:gameInfo, target:target, cid:c.id})); + } + }; if (c.type == "live") + { + tryNotifyOpponent(); this.startNewGame(gameInfo); + } else //corr: game only on server { ajax( "/games", "POST", {gameInfo: gameInfo, cid: c.id}, //cid useful to delete challenge - response => { this.$router.push("/game/" + response.gameId); } + response => { + gameInfo.gameId = response.gameId; + tryNotifyOpponent(); + this.$router.push("/game/" + response.gameId); + } ); } }, diff --git a/server/models/Game.js b/server/models/Game.js index 315b5e2b..d15072df 100644 --- a/server/models/Game.js +++ b/server/models/Game.js @@ -112,6 +112,19 @@ const GameModel = }); }, + getPlayers: function(id, cb) + { + db.serialize(function() { + const query = + "SELECT id " + + "FROM Players " + + "WHERE gid = " + id; + db.all(query, (err,players) => { + return cb(err, players); + }); + }); + }, + // obj can have fields move, fen and/or score update: function(id, obj, cb) { diff --git a/server/routes/games.js b/server/routes/games.js index 73a8bf70..3b91e8b7 100644 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -55,12 +55,19 @@ router.get("/games", access.ajax, (req,res) => { // TODO: if newmove fail, takeback in GUI router.put("/games", access.logged, access.ajax, (req,res) => { const gid = req.body.gid; + const oppId = req.body.oppId; const obj = req.body.newObj; GameModel.update(gid, obj, (err) => { if (!!err) return res.json(err); - if (!!req.body.offlineOpp) //TODO: refresh this... - UserModel.tryNotify(req.body.offlineOpp, "New move in game " + gid); + // Notify opponent if he enabled notifications: + GameModel.getPlayers(gid, (err2,players) => { + if (!!err2) + return res.json(err); + const oppid = (players[0].id == req.userId ? players[1].id : players[0].id); + UserModel.tryNotify(oppid, + "New move in game: " + params.siteURL + "/game/" + gid); + }); res.json({}); }); });