Robustify game seek
authorBenjamin Auder <benjamin.auder@somewhere>
Fri, 16 Nov 2018 00:29:09 +0000 (01:29 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Fri, 16 Nov 2018 00:29:09 +0000 (01:29 +0100)
public/javascripts/components/game.js
sockets.js

index c5d858f..2a5b570 100644 (file)
@@ -1,3 +1,5 @@
+// TODO: use indexedDB instead of localStorage: more flexible.
+
 Vue.component('my-game', {
        data: function() {
                return {
@@ -27,7 +29,17 @@ Vue.component('my-game', {
                let actionArray = [
                        h('button',
                                {
-                                       on: { click: () => this.newGame("human") },
+                                       on: {
+                                               click: () => {
+                                                       if (localStorage.getItem("newgame") === variant)
+                                                               delete localStorage["newgame"]; //cancel game seek
+                                                       else
+                                                       {
+                                                               localStorage["newgame"] = variant;
+                                                               this.newGame("human");
+                                                       }
+                                               }
+                                       },
                                        attrs: { "aria-label": 'New game VS human' },
                                        'class': { "tooltip":true },
                                },
@@ -137,7 +149,7 @@ Vue.component('my-game', {
                                                                        )
                                                                );
                                                        }
-                                                       const lm = this.vr.lastMove; //TODO: interruptions (FEN local storage..)
+                                                       const lm = this.vr.lastMove;
                                                        const highlight = !!lm && _.isMatch(lm.end, {x:ci,y:cj}); //&& _.isMatch(lm.start, {x:ci,y:cj})
                                                        return h(
                                                                'div',
@@ -314,6 +326,11 @@ Vue.component('my-game', {
                                // Send ping to server, which answers pong if opponent is connected
                                this.conn.send(JSON.stringify({code:"ping", oppid:this.oppId}));
                        }
+                       else if (localStorage.getItem("newgame") === variant)
+                       {
+                               // New game request has been cancelled on disconnect
+                               this.newGame("human");
+                       }
                };
                const socketMessageListener = msg => {
                        const data = JSON.parse(msg.data);
@@ -340,7 +357,7 @@ Vue.component('my-game', {
                        }
                };
                const socketCloseListener = () => {
-                       console.log("Lost connection -- reconnect"); //TODO: be more subtle than that, reconnect only when needed!
+                       console.log("Lost connection -- reconnect");
                        this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant);
                        this.conn.addEventListener('open', socketOpenListener);
                        this.conn.addEventListener('message', socketMessageListener);
index 6dbb24a..c3190aa 100644 (file)
@@ -18,6 +18,9 @@ module.exports = function(wss) {
        for (const v of Variants)
                clients[v.name] = {};
 
+       // TODO: when relaying to opponent, check readyState, potential setTimeout()? + send opponent (re)disconnect
+       // (resign, newgame, newmove). See https://github.com/websockets/ws/blob/master/lib/websocket.js around line 313
+
        wss.on("connection", (socket, req) => {
                //const params = new URL("http://localhost" + req.url).searchParams;
                var query = getJsonFromUrl(req.url);
@@ -47,10 +50,7 @@ module.exports = function(wss) {
                                switch (obj.code)
                                {
                                        case "newmove":
-                                               // TODO: adjust with readyState? (+ setTimeout()?) + send opponent (re)disconnect
-                                               // https://github.com/websockets/ws/blob/master/lib/websocket.js line 313
-                                               if (!!clients[page][obj.oppid])
-                                                       clients[page][obj.oppid].send(JSON.stringify({code:"newmove",move:obj.move}));
+                                               clients[page][obj.oppid].send(JSON.stringify({code:"newmove",move:obj.move}));
                                                break;
                                        case "ping":
                                                if (!!clients[page][obj.oppid])
@@ -65,7 +65,6 @@ module.exports = function(wss) {
                                                        delete games[page];
                                                        const mycolor = Math.random() < 0.5 ? 'w' : 'b';
                                                        socket.send(JSON.stringify({code:"newgame",fen:fen,oppid:oppId,color:mycolor}));
-                                                       // TODO: check readyState, potential setTimeout()? + send opponent (re)disconnect
                                                        clients[page][oppId].send(JSON.stringify({code:"newgame",fen:fen,oppid:sid,color:mycolor=="w"?"b":"w"}));
                                                }
                                                else