From 07052665845283c65b50a76537669d0602ba436b Mon Sep 17 00:00:00 2001 From: Benjamin Auder Date: Mon, 6 Apr 2020 11:35:32 +0200 Subject: [PATCH] Experimental in-page analyze + show rules from Game page --- TODO | 11 -- client/src/App.vue | 9 +- client/src/components/BaseGame.vue | 66 ++++++--- client/src/components/ComputerGame.vue | 1 + client/src/components/MoveList.vue | 4 +- client/src/styles/TODO | 3 + client/src/translations/rules/Arena/es.pug | 2 +- client/src/utils/printDiagram.js | 11 ++ client/src/views/Analyse.vue | 4 +- client/src/views/Game.vue | 147 +++++++++++++++++++-- client/src/views/Rules.vue | 24 +--- 11 files changed, 211 insertions(+), 71 deletions(-) create mode 100644 client/src/styles/TODO diff --git a/TODO b/TODO index a4ffcfd7..219aa835 100644 --- a/TODO +++ b/TODO @@ -1,16 +1,5 @@ Chakart :) -// mode analyse + charger rules dans page modal - -+ bouton analyse en vert ! -// TODO: rules button in Game Page :: modal just show rules text, easy -// // TODO: analyse mode in Game Page :: just stay, pass in analyze, easy -// // --> indicateur isConnected: vérifie aussi que pas en mode analyse ?! non... -// // Attention si coup reçu pendant mode analyse faut d'abord sortir du mode -// // (qu'on soit joueur ou spectateur) - -Ball --> capture ballon prend à distance ?! bof, si on a ballon et capture ennemi : lui passe la balle ? - Ambiguous chess https://www.chessvariants.com/mvopponent.dir/ambiguous-chess.html Need special highlight square --> similar to enlightened for Dark, in Board.vue diff --git a/client/src/App.vue b/client/src/App.vue index def45262..eef7169f 100644 --- a/client/src/App.vue +++ b/client/src/App.vue @@ -116,6 +116,9 @@ body .row > div padding: 0 +a + text-decoration: underline + header width: 100% display: flex @@ -185,6 +188,7 @@ nav justify-content: flex-start & > a display: inline-block + text-decoration: none color: #2c3e50 &.router-link-exact-active color: #42b983 @@ -200,10 +204,6 @@ nav & > #leftMenu margin-top: 42px padding-bottom: 5px - & > a - color: #2c3e50 - &.router-link-exact-active - color: #42b983 & > #rightMenu padding-top: 5px border-top: 1px solid darkgrey @@ -266,6 +266,7 @@ footer align-self: center; &:link color: #2c3e50 + text-decoration: none &:visited, &:hover color: #2c3e50 text-decoration: none diff --git a/client/src/components/BaseGame.vue b/client/src/components/BaseGame.vue index 572c80ad..a0bcef0a 100644 --- a/client/src/components/BaseGame.vue +++ b/client/src/components/BaseGame.vue @@ -14,7 +14,7 @@ div#baseGame ref="board" :vr="vr" :last-move="lastMove" - :analyze="game.mode=='analyze'" + :analyze="mode=='analyze'" :score="game.score" :user-color="game.mycolor" :orientation="orientation" @@ -40,6 +40,7 @@ div#baseGame img.inline(src="/images/icons/play.svg") button(@click="gotoEnd()") img.inline(src="/images/icons/fast-forward.svg") + p(v-show="showFen") {{ (!!vr ? vr.getFen() : "") }} #movesList MoveList( :show="showMoves" @@ -52,7 +53,7 @@ div#baseGame :cursor="cursor" @download="download" @showrules="showRules" - @analyze="analyzePosition" + @analyze="toggleAnalyze" @goto-move="gotoMove" @reset-arrows="resetArrows" ) @@ -84,6 +85,7 @@ export default { vr: null, //VariantRules object, game state endgameMessage: "", orientation: "w", + mode: "", score: "*", //'*' means 'unfinished' moves: [], cursor: -1, //index of the move just played @@ -111,6 +113,12 @@ export default { : "" ); }, + showFen: function() { + return ( + this.mode == "analyze" && + this.$router.currentRoute.path.indexOf("/analyse") === -1 + ); + }, // TODO: is it OK to pass "computed" as properties? // Also, some are seemingly not recomputed when vr is initialized. showMoves: function() { @@ -192,14 +200,15 @@ export default { this.$refs["board"].cancelResetArrows(); }, showRules: function() { - //this.$router.push("/variants/" + this.game.vname); - window.open("#/variants/" + this.game.vname, "_blank"); //better + // The button is here only on Game page: + document.getElementById("modalRules").checked = true; }, re_setVariables: function(game) { if (!game) game = this.game; //in case of... this.endgameMessage = ""; // "w": default orientation for observed games this.orientation = game.mycolor || "w"; + this.mode = game.mode || game.type; //TODO: merge... this.moves = JSON.parse(JSON.stringify(game.moves || [])); // Post-processing: decorate each move with notation and FEN this.vr = new V(game.fenStart); @@ -217,6 +226,7 @@ export default { this.vr.play(m); const checkSquares = this.vr.getCheckSquares(); if (checkSquares.length > 0) m.notation += "+"; + if (idxM == Lm - 1) m.fen = this.vr.getFen(); if (idx == L - 1 && idxM == Lm - 1) { this.incheck = checkSquares; const score = this.vr.getCurrentScore(); @@ -243,14 +253,29 @@ export default { if (index >= 0) this.lastMove = this.moves[index]; else this.lastMove = null; }, - analyzePosition: function() { - let newUrl = - "/analyse/" + - this.game.vname + - "/?fen=" + - this.vr.getFen().replace(/ /g, "_"); - if (!!this.game.mycolor) newUrl += "&side=" + this.game.mycolor; - window.open("#" + newUrl); + toggleAnalyze: function() { + if (this.mode != "analyze") { + // Enter analyze mode: + this.gameMode = this.mode; //was not 'analyze' + this.mode = "analyze"; + this.gameCursor = this.cursor; + this.gameMoves = JSON.parse(JSON.stringify(this.moves)); + document.getElementById("analyzeBtn").classList.add("active"); + } + else { + // Exit analyze mode: + this.mode = this.gameMode ; + this.cursor = this.gameCursor; + this.moves = this.gameMoves; + let fen = this.game.fenStart; + if (this.cursor >= 0) { + let mv = this.moves[this.cursor]; + if (!Array.isArray(mv)) mv = [mv]; + fen = mv[mv.length-1].fen; + } + this.vr = new V(fen); + document.getElementById("analyzeBtn").classList.remove("active"); + } }, download: function() { const content = this.getPgn(); @@ -407,7 +432,7 @@ export default { smove.notation = this.vr.getNotation(smove); smove.unambiguous = V.GetUnambiguousNotation(smove); this.vr.play(smove); - if (!!this.lastMove) { + if (this.inMultimove && !!this.lastMove) { if (!Array.isArray(this.lastMove)) this.lastMove = [this.lastMove, smove]; else this.lastMove.push(smove); @@ -472,7 +497,7 @@ export default { else this.lastMove.notation += "#"; } } - if (score != "*" && this.game.mode == "analyze") { + if (score != "*" && this.mode == "analyze") { const message = getScoreMessage(score); // Just show score on screen (allow undo) this.showEndgameMsg(score + " . " + this.st.tr[message]); @@ -488,7 +513,7 @@ export default { this.emitFenIfAnalyze(); this.inMultimove = false; this.score = computeScore(); - if (this.game.mode != "analyze" && !navigate) { + if (this.mode != "analyze" && !navigate) { if (!noemit) { // Post-processing (e.g. computer play). const L = this.moves.length; @@ -526,16 +551,19 @@ export default { // Forbid playing outside analyze mode, except if move is received. // Sufficient condition because Board already knows which turn it is. if ( - this.game.mode != "analyze" && + this.mode != "analyze" && !navigate && !received && (this.game.score != "*" || this.cursor < this.moves.length - 1) ) { return; } - // To play a received move, cursor must be at the end of the game: - if (received && this.cursor < this.moves.length - 1) - this.gotoEnd(); + if (!!received) { + if (this.mode == "analyze") this.toggleAnalyze(); + if (this.cursor < this.moves.length - 1) + // To play a received move, cursor must be at the end of the game: + this.gotoEnd(); + } playMove(); }, cancelCurrentMultimove: function() { diff --git a/client/src/components/ComputerGame.vue b/client/src/components/ComputerGame.vue index 1cd37ab0..44bfb6a6 100644 --- a/client/src/components/ComputerGame.vue +++ b/client/src/components/ComputerGame.vue @@ -71,6 +71,7 @@ export default { game.players = [{ name: "Myself" }, { name: "Computer" }]; if (game.mycolor == "b") game.players = game.players.reverse(); game.score = "*"; //finished games are removed + game.mode = this.gameInfo.mode; this.currentUrl = document.location.href; //to avoid playing outside page this.game = game; this.$refs["basegame"].re_setVariables(game); diff --git a/client/src/components/MoveList.vue b/client/src/components/MoveList.vue index 6c999755..ccb07758 100644 --- a/client/src/components/MoveList.vue +++ b/client/src/components/MoveList.vue @@ -27,7 +27,7 @@ div :aria-label="st.tr['Resize board']" ) img.inline(src="/images/icons/resize.svg") - button.tooltip( + button#analyzeBtn.tooltip( v-if="canAnalyze" @click="$emit('analyze')" :aria-label="st.tr['Analyse']" @@ -240,6 +240,8 @@ span#rulesBtn button margin: 0 + &.active + background-color: #50E99A #aboveMoves button padding-bottom: 5px diff --git a/client/src/styles/TODO b/client/src/styles/TODO new file mode 100644 index 00000000..e3f897ee --- /dev/null +++ b/client/src/styles/TODO @@ -0,0 +1,3 @@ +@import "./styles/_variables.scss"; +https://css-tricks.com/how-to-import-a-sass-file-into-every-vue-component-in-an-app/ +--> Stop duplicating CSS diff --git a/client/src/translations/rules/Arena/es.pug b/client/src/translations/rules/Arena/es.pug index 67366aa3..999f861d 100644 --- a/client/src/translations/rules/Arena/es.pug +++ b/client/src/translations/rules/Arena/es.pug @@ -39,7 +39,7 @@ p. h3 Fuente p - | La + | La a(href="https://www.chessvariants.com/32turn.dir/arenachess.html") | variante Arena |  en chessvariants.com. diff --git a/client/src/utils/printDiagram.js b/client/src/utils/printDiagram.js index 9ebdeed6..00ff2c79 100644 --- a/client/src/utils/printDiagram.js +++ b/client/src/utils/printDiagram.js @@ -100,3 +100,14 @@ export function getDiagram(args) { } return boardDiv; } + +// Method to replace diagrams in loaded HTML +export function replaceByDiag(match, p1, p2) { + const diagParts = p2.split(" "); + return getDiagram({ + position: diagParts[0], + marks: diagParts[1], + orientation: diagParts[2], + shadow: diagParts[3] + }); +} diff --git a/client/src/views/Analyse.vue b/client/src/views/Analyse.vue index 2f087b99..39022e8a 100644 --- a/client/src/views/Analyse.vue +++ b/client/src/views/Analyse.vue @@ -64,7 +64,9 @@ export default { if (!routeFen) this.alertAndQuit("Missing FEN"); else { this.gameRef.fen = routeFen.replace(/_/g, " "); - // orientation is optional: taken from FEN if missing + // orientation is optional: taken from FEN if missing. + // NOTE: currently no internal usage of 'side', but could be used by + // manually settings the URL (TODO?). const orientation = this.$route.query["side"]; this.initialize(orientation); } diff --git a/client/src/views/Game.vue b/client/src/views/Game.vue index fe487653..0d7a4ee9 100644 --- a/client/src/views/Game.vue +++ b/client/src/views/Game.vue @@ -1,5 +1,14 @@