Some debug, plan several short + long term TODOs
[vchess.git] / sockets.js
index c130dd7..e411050 100644 (file)
@@ -1,6 +1,17 @@
 const url = require('url');
 const sqlite3 = require('sqlite3');
-const db = new sqlite3.Database('db/vchess.sqlite');
+const db = new sqlite3.Database(__dirname + "/db/vchess.sqlite");
+
+// Node version in Ubuntu 16.04 does not know about URL class
+function getJsonFromUrl(url) {
+       var query = url.substr(2); //starts with "/?"
+       var result = {};
+       query.split("&").forEach(function(part) {
+               var item = part.split("=");
+               result[item[0]] = decodeURIComponent(item[1]);
+       });
+       return result;
+}
 
 module.exports = function(wss) {
        db.serialize(function() {
@@ -12,9 +23,12 @@ module.exports = function(wss) {
                        // No-op function as a callback when sending messages
                        const noop = () => { };
                        wss.on("connection", (socket, req) => {
-                               const params = new URL("http://localhost" + req.url).searchParams;
-                               const sid = params.get("sid");
-                               const page = params.get("page");
+//                             const params = new URL("http://localhost" + req.url).searchParams;
+//                             const sid = params.get("sid");
+//                             const page = params.get("page");
+                               var query = getJsonFromUrl(req.url);
+                               const sid = query["sid"];
+                               const page = query["page"];
                                // Ignore duplicate connections:
                                if (!!clients[page][sid])
                                {
@@ -86,20 +100,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];