Forgot to remove game seek on new game
[vchess.git] / public / javascripts / components / game.js
index 1338132..34b3633 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',
@@ -305,7 +317,7 @@ Vue.component('my-game', {
                        // random enough (TODO: function)
                        : (Date.now().toString(36) + Math.random().toString(36).substr(2, 7)).toUpperCase();
                this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant);
-               this.conn.onopen = () => {
+               const socketOpenListener = () => {
                        if (continuation)
                        {
                                // TODO: check FEN integrity with opponent
@@ -314,8 +326,13 @@ 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");
+                       }
                };
-               this.conn.onmessage = msg => {
+               const socketMessageListener = msg => {
                        const data = JSON.parse(msg.data);
                        switch (data.code)
                        {
@@ -339,6 +356,16 @@ Vue.component('my-game', {
                                        break;
                        }
                };
+               const socketCloseListener = () => {
+                       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);
+                       this.conn.addEventListener('close', socketCloseListener);
+               };
+               this.conn.onopen = socketOpenListener;
+               this.conn.onmessage = socketMessageListener;
+               this.conn.onclose = socketCloseListener;
        },
        methods: {
                endGame: function(message) {
@@ -351,7 +378,13 @@ Vue.component('my-game', {
                },
                resign: function() {
                        if (this.mode == "human" && this.oppConnected)
-                               this.conn.send(JSON.stringify({code: "resign", oppid: this.oppid}));
+                       {
+                               try {
+                                       this.conn.send(JSON.stringify({code: "resign", oppid: this.oppid}));
+                               } catch (INVALID_STATE_ERR) {
+                                       return; //resign failed for some reason...
+                               }
+                       }
                        this.endGame("Try again!");
                },
                updateStorage: function() {
@@ -378,7 +411,11 @@ Vue.component('my-game', {
                        {
                                // Send game request and wait..
                                this.clearStorage(); //in case of
-                               this.conn.send(JSON.stringify({code:"newgame", fen:fen}));
+                               try {
+                                       this.conn.send(JSON.stringify({code:"newgame", fen:fen}));
+                               } catch (INVALID_STATE_ERR) {
+                                       return; //nothing achieved
+                               }
                                document.getElementById("modal-control2").checked = true;
                                return;
                        }
@@ -396,6 +433,7 @@ Vue.component('my-game', {
                                this.oppid = oppId;
                                this.oppConnected = true;
                                this.mycolor = color;
+                               delete localStorage["newgame"]; //in case of
                        }
                        else //against computer
                        {
@@ -526,7 +564,11 @@ Vue.component('my-game', {
                        {
                                if (!this.oppConnected)
                                        return; //abort move if opponent is gone
-                               this.conn.send(JSON.stringify({code:"newmove", move:move, oppid:this.oppid}));
+                               try {
+                                       this.conn.send(JSON.stringify({code:"newmove", move:move, oppid:this.oppid}));
+                               } catch(INVALID_STATE_ERR) {
+                                       return; //abort also if sending failed
+                               }
                        }
                        new Audio("/sounds/chessmove1.mp3").play();
                        this.vr.play(move, "ingame");