Draft Fullcavalry and Atomic2 (pawn removal). Fix Grand
[vchess.git] / client / src / views / Analyse.vue
CommitLineData
7f3484bd 1<template lang="pug">
7aa548e7 2main
26d8a01a
BA
3 input#modalRules.modal(type="checkbox")
4 div#rulesDiv(
5 role="dialog"
6 data-checkbox="modalRules"
7 )
8 .card
9 label.modal-close(for="modalRules")
1c15969e
BA
10 a#variantNameInAnalyze(:href="'/#/variants/'+game.vname")
11 | {{ game.vname }}
26d8a01a 12 div(v-html="rulesContent")
63ca2b89
BA
13 .row
14 .col-sm-12
9a3049f3 15 .text-center
910d631b
BA
16 input#fen(
17 v-model="curFen"
725da57f 18 @input="adjustFenSize(); tryGotoFen()"
910d631b 19 )
910d631b 20 BaseGame(
b1e46b33 21 ref="basegame"
910d631b 22 :game="game"
8055eabd 23 @fenchange="setFen"
910d631b 24 )
7f3484bd
BA
25</template>
26
27<script>
28import BaseGame from "@/components/BaseGame.vue";
26d8a01a
BA
29import { processModalClick } from "@/utils/modalClick";
30import { replaceByDiag } from "@/utils/printDiagram";
7f3484bd 31import { store } from "@/store";
70c9745d 32import afterRawLoad from "@/utils/afterRawLoad";
7f3484bd 33export default {
6808d7a1 34 name: "my-analyse",
5f918a27
BA
35 // TODO: game import ==> require some adjustments, like
36 // the ability to analyse from a list of moves...
7f3484bd 37 components: {
6808d7a1 38 BaseGame
7f3484bd
BA
39 },
40 // gameRef: to find the game in (potentially remote) storage
41 data: function() {
42 return {
43 st: store.state,
26d8a01a 44 rulesContent: "",
6808d7a1 45 gameRef: {
652f40de
BA
46 vname: "",
47 fen: ""
48 },
49 game: {
6808d7a1
BA
50 players: [{ name: "Analyse" }, { name: "Analyse" }],
51 mode: "analyze"
7f3484bd 52 },
6808d7a1 53 curFen: ""
7f3484bd
BA
54 };
55 },
b1e46b33
BA
56 watch: {
57 $route: function() {
58 this.initFromUrl();
a97bdbda 59 }
7f3484bd 60 },
b1e46b33
BA
61 created: function() {
62 this.initFromUrl();
63 },
26d8a01a
BA
64 mounted: function() {
65 document.getElementById("rulesDiv")
66 .addEventListener("click", processModalClick);
67 },
7f3484bd 68 methods: {
a97bdbda
BA
69 alertAndQuit: function(text, wrongVname) {
70 // Soon after component creation, st.tr might be uninitialized.
71 // Set a timeout to let a chance for the message to show translated.
2c5d7b20
BA
72 const newUrl =
73 "/variants" + (wrongVname ? "" : "/" + this.gameRef.vname);
a97bdbda
BA
74 setTimeout(() => {
75 alert(this.st.tr[text] || text);
76 this.$router.replace(newUrl);
77 }, 500);
78 },
b1e46b33
BA
79 initFromUrl: function() {
80 this.gameRef.vname = this.$route.params["vname"];
81 const routeFen = this.$route.query["fen"];
82 if (!routeFen) this.alertAndQuit("Missing FEN");
83 else {
84 this.gameRef.fen = routeFen.replace(/_/g, " ");
07052665
BA
85 // orientation is optional: taken from FEN if missing.
86 // NOTE: currently no internal usage of 'side', but could be used by
87 // manually settings the URL (TODO?).
b1e46b33
BA
88 const orientation = this.$route.query["side"];
89 this.initialize(orientation);
90 }
91 },
7ba4a5bc 92 initialize: async function(orientation) {
5157ce0b 93 // Obtain VariantRules object
a97bdbda
BA
94 await import("@/variants/" + this.gameRef.vname + ".js")
95 .then((vModule) => {
32f6285e 96 window.V = vModule[this.gameRef.vname + "Rules"];
a97bdbda
BA
97 if (!V.CanAnalyze)
98 // Late check, in case the user tried to enter URL by hand
99 this.alertAndQuit("Analysis disabled for this variant");
7ba4a5bc 100 else this.loadGame(orientation);
a97bdbda 101 })
0fb43db7 102 //.catch((err) => { this.alertAndQuit("Mispelled variant name", true); });
26d8a01a 103 this.rulesContent =
70c9745d
BA
104 afterRawLoad(
105 require(
106 "raw-loader!@/translations/rules/" +
107 this.gameRef.vname + "/" + this.st.lang + ".pug"
8b3b2151 108 ).default
70c9745d 109 ).replace(/(fen:)([^:]*):/g, replaceByDiag);
26d8a01a 110 },
7ba4a5bc 111 loadGame: function(orientation) {
652f40de 112 this.game.vname = this.gameRef.vname;
b1e46b33 113 this.game.fenStart = this.gameRef.fen;
652f40de 114 this.game.fen = this.gameRef.fen;
0234201f 115 this.game.score = "*"; //never change
5157ce0b
BA
116 this.curFen = this.game.fen;
117 this.adjustFenSize();
7ba4a5bc 118 this.game.mycolor = orientation || V.ParseFen(this.gameRef.fen).turn;
b1e46b33 119 this.$refs["basegame"].re_setVariables(this.game);
7f3484bd 120 },
8055eabd
BA
121 // Triggered by "fenchange" emitted in BaseGame:
122 setFen: function(fen) {
123 this.curFen = fen;
124 this.adjustFenSize();
125 },
5157ce0b
BA
126 adjustFenSize: function() {
127 let fenInput = document.getElementById("fen");
d54f6261 128 fenInput.style.width = (this.curFen.length+3) + "ch";
5157ce0b 129 },
725da57f 130 tryGotoFen: function() {
3a2a7b5f 131 if (V.IsGoodFen(this.curFen)) {
725da57f
BA
132 this.gameRef.fen = this.curFen;
133 this.loadGame();
134 }
6808d7a1
BA
135 }
136 }
7f3484bd
BA
137};
138</script>
d54f6261 139
26d8a01a
BA
140<style lang="sass">
141@import "@/styles/_board_squares_img.sass"
142@import "@/styles/_rules.sass"
143</style>
144
145<style lang="sass" scoped>
1c15969e
BA
146a#variantNameInAnalyze
147 color: var(--card-fore-color)
26d8a01a 148 text-align: center
26d8a01a 149 font-weight: bold
1c15969e
BA
150 font-size: calc(1rem * var(--heading-ratio))
151 line-height: 1.2
152 margin: calc(1.5 * var(--universal-margin))
26d8a01a
BA
153
154#rulesDiv > .card
155 padding: 5px 0
156 max-width: 50%
157 max-height: 100%
158 @media screen and (max-width: 1500px)
159 max-width: 67%
160 @media screen and (max-width: 1024px)
161 max-width: 85%
162 @media screen and (max-width: 767px)
163 max-width: 100%
164
d54f6261
BA
165input#fen
166 // Use a Monospace font for input FEN width adjustment
167 font-family: "Fira Code"
168</style>