From: Benjamin Auder Date: Fri, 16 Nov 2018 16:33:27 +0000 (+0100) Subject: Store moves, show PGN at end of game X-Git-Url: https://git.auder.net/images/assets/doc/current/app_dev.php?a=commitdiff_plain;h=dfb4afc10d56715b80f753b1273738bd11a309dd;p=vchess.git Store moves, show PGN at end of game --- diff --git a/public/javascripts/base_rules.js b/public/javascripts/base_rules.js index 576bdd9b..0a5c0e72 100644 --- a/public/javascripts/base_rules.js +++ b/public/javascripts/base_rules.js @@ -47,9 +47,9 @@ class ChessRules // INITIALIZATION // fen = "position flags epSquare movesCount" - constructor(fen) + constructor(fen, moves) { - this.moves = []; + this.moves = moves; // Use fen string to initialize variables, flags and board this.initVariables(fen); this.flags = VariantRules.GetFlags(fen); @@ -665,12 +665,15 @@ class ChessRules move.flags = JSON.stringify(this.flags); //TODO: less costly this.updateVariables(move); + if (!!ingame) + { + move.notation = this.getNotation(move); + this.moves.push(move); + } + this.epSquares.push( this.getEpSquare(move) ); VariantRules.PlayOnBoard(this.board, move); this.movesCount++; - - if (!!ingame) - this.moves.push(move); } undo(move) @@ -1007,4 +1010,26 @@ class ChessRules return piece.toUpperCase() + (move.vanish.length > 1 ? "x" : "") + finalSquare; } } + + // The score is already computed when calling this function + getPGN(mycolor, score) + { + let pgn = ""; + pgn += '[Site "vchess.club"]
'; + const d = new Date(); + pgn += '[Date "' + d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate() + '"]
'; + pgn += '[White "' + (mycolor=='w'?'Myself':'Anonymous') + '"]
'; + pgn += '[Black "' + (mycolor=='b'?'Myself':'Anonymous') + '"]
'; + pgn += '[Result "' + score + '"]
'; + + for (let i=0; i { modalBox.checked = false; }, 2000); + //setTimeout(() => { modalBox.checked = false; }, 2000); //disabled, to show PGN if (this.mode == "human") this.clearStorage(); this.mode = "idle"; @@ -399,7 +428,7 @@ Vue.component('my-game', { return; //resign failed for some reason... } } - this.endGame("Try again!"); + this.endGame(this.mycolor=="w"?"0-1":"1-0"); }, updateStorage: function() { if (!localStorage.getItem("myid")) @@ -410,6 +439,7 @@ Vue.component('my-game', { localStorage.setItem("oppid", this.oppid); } localStorage.setItem("fen", this.vr.getFen()); + localStorage.setItem("moves", JSON.stringify(this.vr.moves)); }, clearStorage: function() { delete localStorage["variant"]; @@ -417,10 +447,12 @@ Vue.component('my-game', { delete localStorage["mycolor"]; delete localStorage["oppid"]; delete localStorage["fen"]; + delete localStorage["moves"]; }, - newGame: function(mode, fenInit, color, oppId, continuation) { + newGame: function(mode, fenInit, color, oppId, moves, continuation) { const fen = fenInit || VariantRules.GenRandInitFen(); console.log(fen); //DEBUG + this.score = "*"; if (mode=="human" && !oppId) { // Send game request and wait.. @@ -435,7 +467,7 @@ Vue.component('my-game', { setTimeout(() => { modalBox.checked = false; }, 2000); return; } - this.vr = new VariantRules(fen); + this.vr = new VariantRules(fen, moves || []); this.mode = mode; if (mode=="human") {