From ecf44502c1ed7c408b8ea4d1be5234203aa3d314 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Mon, 19 Nov 2018 02:48:39 +0100
Subject: [PATCH] Fix lastate propagation

---
 public/javascripts/components/game.js | 24 ++++++++++++------------
 public/javascripts/index.js           |  1 -
 sockets.js                            |  3 ++-
 3 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/public/javascripts/components/game.js b/public/javascripts/components/game.js
index f0017a2d..7af9c8e3 100644
--- a/public/javascripts/components/game.js
+++ b/public/javascripts/components/game.js
@@ -207,7 +207,7 @@ Vue.component('my-game', {
 			[
 				h('label',
 					{
-						attrs: { "for": "modal-control" },
+						attrs: { "for": "modal-eog" },
 						"class": { "modal-close": true },
 					}
 				),
@@ -232,7 +232,7 @@ Vue.component('my-game', {
 			const modalEog = [
 				h('input',
 					{
-						attrs: { "id": "modal-control", type: "checkbox" },
+						attrs: { "id": "modal-eog", type: "checkbox" },
 						"class": { "modal": true },
 					}),
 				h('div',
@@ -254,7 +254,7 @@ Vue.component('my-game', {
 		const modalNewgame = [
 			h('input',
 				{
-					attrs: { "id": "modal-control2", type: "checkbox" },
+					attrs: { "id": "modal-newgame", type: "checkbox" },
 					"class": { "modal": true },
 				}),
 			h('div',
@@ -269,7 +269,7 @@ Vue.component('my-game', {
 						[
 							h('label',
 								{
-									attrs: { "id": "close-newgame", "for": "modal-control2" },
+									attrs: { "id": "close-newgame", "for": "modal-newgame" },
 									"class": { "modal-close": true },
 								}
 							),
@@ -339,7 +339,7 @@ Vue.component('my-game', {
 				const moves = JSON.parse(localStorage.getItem("moves"));
 				this.newGame("human", fen, mycolor, oppid, moves, true);
 				// Send ping to server (answer pong if opponent is connected)
-				this.conn.send(JSON.stringify({code:"ping",oppid:this.oppId}));
+				this.conn.send(JSON.stringify({code:"ping",oppid:this.oppid}));
 			}
 			else if (localStorage.getItem("newgame") === variant)
 			{
@@ -399,6 +399,7 @@ Vue.component('my-game', {
 					}
 					else if (data.movesCount > this.vr.moves.length) //just got last move from him
 						this.play(data.lastMove, "animate");
+					break;
 				case "resign": //..you won!
 					this.endGame(this.mycolor=="w"?"1-0":"0-1");
 					break;
@@ -411,7 +412,6 @@ Vue.component('my-game', {
 			}
 		};
 		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);
@@ -424,7 +424,7 @@ Vue.component('my-game', {
 	methods: {
 		endGame: function(score) {
 			this.score = score;
-			let modalBox = document.getElementById("modal-control");
+			let modalBox = document.getElementById("modal-eog");
 			modalBox.checked = true;
 			//setTimeout(() => { modalBox.checked = false; }, 2000); //disabled, to show PGN
 			if (this.mode == "human")
@@ -520,7 +520,7 @@ Vue.component('my-game', {
 				}
 				if (continuation !== "reconnect") //TODO: bad HACK...
 				{
-					let modalBox = document.getElementById("modal-control2");
+					let modalBox = document.getElementById("modal-newgame");
 					modalBox.checked = true;
 					setTimeout(() => { modalBox.checked = false; }, 2000);
 				}
@@ -537,9 +537,9 @@ Vue.component('my-game', {
 				// Opponent found!
 				if (!continuation)
 				{
-					// Playing sound fails on game continuation:
-					new Audio("/sounds/newgame.mp3").play();
-					document.getElementById("modal-control2").checked = false;
+					// Not playing sound on game continuation:
+					new Audio("/sounds/newgame.mp3").play().catch(err => {});
+					document.getElementById("modal-newgame").checked = false;
 				}
 				this.oppid = oppId;
 				this.oppConnected = true;
@@ -685,7 +685,7 @@ Vue.component('my-game', {
 			// Not programmatic, or animation is over
 			if (this.mode == "human" && this.vr.turn == this.mycolor)
 				this.conn.send(JSON.stringify({code:"newmove", move:move, oppid:this.oppid}));
-			new Audio("/sounds/chessmove1.mp3").play();
+			new Audio("/sounds/chessmove1.mp3").play().catch(err => {});
 			this.vr.play(move, "ingame");
 			if (this.mode == "human")
 				this.updateStorage(); //after our moves and opponent moves
diff --git a/public/javascripts/index.js b/public/javascripts/index.js
index e24995be..12bcf644 100644
--- a/public/javascripts/index.js
+++ b/public/javascripts/index.js
@@ -37,7 +37,6 @@ new Vue({
 				this.counts[data.vname]--;
 		};
 		const socketCloseListener = () => {
-			console.log("Lost connection -- reconnect");
 			this.conn = new WebSocket(url + "/?sid=" + sid + "&page=index");
 			this.conn.addEventListener('message', socketMessageListener);
 			this.conn.addEventListener('close', socketCloseListener);
diff --git a/sockets.js b/sockets.js
index 9e47513f..820a88c7 100644
--- a/sockets.js
+++ b/sockets.js
@@ -46,8 +46,9 @@ module.exports = function(wss) {
 					case "lastate":
 						if (!!clients[page][obj.oppid])
 						{
+							const oppId = obj.oppid;
 							obj.oppid = sid; //I'm oppid for my opponent
-							clients[page][obj.oppid].send(JSON.stringify(obj));
+							clients[page][oppId].send(JSON.stringify(obj));
 						}
 						break;
 					case "newgame":
-- 
2.44.0