Improve notification system after a move or at new game
authorBenjamin Auder <benjamin.auder@somewhere>
Tue, 3 Dec 2019 17:45:44 +0000 (18:45 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Tue, 3 Dec 2019 17:45:44 +0000 (18:45 +0100)
client/src/views/Game.vue
client/src/views/Hall.vue
server/models/Game.js
server/routes/games.js

index 67af3b9..923e940 100644 (file)
@@ -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
index 82330ef..433151c 100644 (file)
@@ -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);
+          }
         );
       }
     },
index 315b5e2..d15072d 100644 (file)
@@ -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)
   {
index 73a8bf7..3b91e8b 100644 (file)
@@ -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({});
        });
 });