Advance on components/game.js
[vchess.git] / sockets.js
index 11fe91c..21742d1 100644 (file)
@@ -13,13 +13,16 @@ function getJsonFromUrl(url) {
        return result;
 }
 
+// TODO: empêcher multi-log du même user (envoyer le user ID + secret en même temps que name et...)
+// --> si secret ne matche pas celui trouvé en DB, stop
+
 module.exports = function(wss) {
        db.serialize(function() {
                db.all("SELECT * FROM Variants", (err,variants) => {
                        let clients = { "index": {} };
                        let games = {}; //pending games (player sid)
                        for (const v of variants)
-                               clients[v.name] = {};
+                               clients[v.id] = {};
                        // No-op function as a callback when sending messages
                        const noop = () => { };
                        wss.on("connection", (socket, req) => {
@@ -41,7 +44,7 @@ module.exports = function(wss) {
                                        // Send counting info
                                        const countings = {};
                                        for (const v of variants)
-                                               countings[v.name] = Object.keys(clients[v.name]).length;
+                                               countings[v.id] = Object.keys(clients[v.id]).length;
                                        socket.send(JSON.stringify({code:"counts",counts:countings}));
                                }
                                else
@@ -49,7 +52,7 @@ module.exports = function(wss) {
                                        // Send to every client connected on index an update message for counts
                                        Object.keys(clients["index"]).forEach( k => {
                                                clients["index"][k].send(
-                                                       JSON.stringify({code:"increase",vname:page}), noop);
+                                                       JSON.stringify({code:"increase",vid:page}), noop);
                                        });
                                        // Also notify potential opponents:
                                        // hit all clients which check if sid corresponds
@@ -100,20 +103,21 @@ module.exports = function(wss) {
                                                                        // Start a new game
                                                                        const oppId = games[page]["id"];
                                                                        const fen = games[page]["fen"];
+                                                                       const gameId = games[page]["gameid"];
                                                                        delete games[page];
-                                                                       const mycolor = Math.random() < 0.5 ? 'w' : 'b';
+                                                                       const mycolor = (Math.random() < 0.5 ? 'w' : 'b');
                                                                        socket.send(JSON.stringify(
-                                                                               {code:"newgame",fen:fen,oppid:oppId,color:mycolor}));
+                                                                               {code:"newgame",fen:fen,oppid:oppId,color:mycolor,gameid:gameId}));
                                                                        if (!!clients[page][oppId])
                                                                        {
                                                                                clients[page][oppId].send(
                                                                                        JSON.stringify(
-                                                                                               {code:"newgame",fen:fen,oppid:sid,color:mycolor=="w"?"b":"w"}),
+                                                                                               {code:"newgame",fen:fen,oppid:sid,color:mycolor=="w"?"b":"w",gameid:gameId}),
                                                                                        noop);
                                                                        }
                                                                }
                                                                else
-                                                                       games[page] = {id:sid, fen:obj.fen}; //wait for opponent
+                                                                       games[page] = {id:sid, fen:obj.fen, gameid:obj.gameid}; //wait for opponent
                                                                break;
                                                        case "cancelnewgame": //if a user cancel his seek
                                                                delete games[page];
@@ -135,7 +139,7 @@ module.exports = function(wss) {
                                                // Send to every client connected on index an update message for counts
                                                Object.keys(clients["index"]).forEach( k => {
                                                        clients["index"][k].send(
-                                                               JSON.stringify({code:"decrease",vname:page}), noop);
+                                                               JSON.stringify({code:"decrease",vid:page}), noop);
                                                });
                                        }
                                        // Also notify potential opponents: