Fix challenge send+accept. Now debug game launch from server
authorBenjamin Auder <benjamin.auder@somewhere>
Fri, 29 Nov 2019 17:34:35 +0000 (18:34 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Fri, 29 Nov 2019 17:34:35 +0000 (18:34 +0100)
client/src/views/Hall.vue
server/models/Challenge.js
server/models/Game.js
server/routes/games.js
server/sockets.js

index 0467dab..2fbb6e6 100644 (file)
@@ -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
index c747629..f82f9e8 100644 (file)
@@ -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});
       });
     });
index e60bfe5..78827e2 100644 (file)
@@ -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});
index cc8a17b..24e9747 100644 (file)
@@ -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({});
                });
index 6e0451f..04422f4 100644 (file)
@@ -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":