From 762b7c9ca36aedb3e720b31c103be429446060d3 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Fri, 16 Nov 2018 19:41:52 +0100
Subject: [PATCH] Add initial FEN to PGN; shorten end of game window

---
 public/javascripts/base_rules.js      |  5 +++--
 public/javascripts/components/game.js | 32 ++++++++++++++-------------
 2 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js
index 0a5c0e72..de1a0d0f 100644
--- a/public/javascripts/base_rules.js
+++ b/public/javascripts/base_rules.js
@@ -1012,7 +1012,7 @@ class ChessRules
 	}
 
 	// The score is already computed when calling this function
-	getPGN(mycolor, score)
+	getPGN(mycolor, score, fenStart)
 	{
 		let pgn = "";
 		pgn += '[Site "vchess.club"]<br>';
@@ -1020,7 +1020,8 @@ class ChessRules
 		pgn += '[Date "' + d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate() + '"]<br>';
 		pgn += '[White "' + (mycolor=='w'?'Myself':'Anonymous') + '"]<br>';
 		pgn += '[Black "' + (mycolor=='b'?'Myself':'Anonymous') + '"]<br>';
-		pgn += '[Result "' + score + '"]<br>';
+		pgn += '[Fen "' + fenStart + '"]<br>';
+		pgn += '[Result "' + score + '"]<br><br>';
 
 		for (let i=0; i<this.moves.length; i++)
 		{
diff --git a/public/javascripts/components/game.js b/public/javascripts/components/game.js
index 592a7001..81ced5b4 100644
--- a/public/javascripts/components/game.js
+++ b/public/javascripts/components/game.js
@@ -15,6 +15,7 @@ Vue.component('my-game', {
 			oppid: "", //opponent ID in case of HH game
 			oppConnected: false,
 			seek: false,
+			fenStart: "",
 		};
 	},
 	render(h) {
@@ -230,12 +231,6 @@ Vue.component('my-game', {
 					}
 				),
 				h('h3',
-					{
-						"class": { "section": true },
-						domProps: { innerHTML: "End of game" },
-					}
-				),
-				h('p',
 					{
 						"class": { "section": true },
 						domProps: { innerHTML: eogMessage },
@@ -247,7 +242,7 @@ Vue.component('my-game', {
 				elemsOfEog.push(
 					h('p', //'textarea', //TODO: selectable!
 						{
-							domProps: { innerHTML: this.vr.getPGN(this.mycolor, this.score) },
+							domProps: { innerHTML: this.vr.getPGN(this.mycolor, this.score, this.fenStart) },
 							//attrs: { "readonly": true },
 						}
 					)
@@ -430,22 +425,25 @@ Vue.component('my-game', {
 			}
 			this.endGame(this.mycolor=="w"?"0-1":"1-0");
 		},
-		updateStorage: function() {
-			if (!localStorage.getItem("myid"))
-			{
-				localStorage.setItem("myid", this.myid);
-				localStorage.setItem("variant", variant);
-				localStorage.setItem("mycolor", this.mycolor);
-				localStorage.setItem("oppid", this.oppid);
-			}
+		setStorage: function() {
+			localStorage.setItem("myid", this.myid);
+			localStorage.setItem("variant", variant);
+			localStorage.setItem("mycolor", this.mycolor);
+			localStorage.setItem("oppid", this.oppid);
+			localStorage.setItem("fenStart", this.fenStart);
+			localStorage.setItem("moves", JSON.stringify(this.vr.moves));
 			localStorage.setItem("fen", this.vr.getFen());
+		},
+		updateStorage: function() {
 			localStorage.setItem("moves", JSON.stringify(this.vr.moves));
+			localStorage.setItem("fen", this.vr.getFen());
 		},
 		clearStorage: function() {
 			delete localStorage["variant"];
 			delete localStorage["myid"];
 			delete localStorage["mycolor"];
 			delete localStorage["oppid"];
+			delete localStorage["fenStart"];
 			delete localStorage["fen"];
 			delete localStorage["moves"];
 		},
@@ -469,6 +467,9 @@ Vue.component('my-game', {
 			}
 			this.vr = new VariantRules(fen, moves || []);
 			this.mode = mode;
+			this.fenStart = continuation
+				? localStorage.getItem("fenStart")
+				: fen.split(" ")[0]; //Only the position matters
 			if (mode=="human")
 			{
 				// Opponent found!
@@ -483,6 +484,7 @@ Vue.component('my-game', {
 				this.mycolor = color;
 				this.seek = false;
 				delete localStorage["newgame"];
+				this.setStorage(); //in case of interruptions
 			}
 			else //against computer
 			{
-- 
2.44.0