Add Orda variant. Some advances in Dynamo draft. Display error message when loading...
[vchess.git] / client / src / views / Rules.vue
1 <template lang="pug">
2 main
3 .row
4 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
5 .button-group
6 button(@click="clickReadRules()") {{ st.tr["Rules"] }}
7 button(
8 v-show="!gameInProgress"
9 @click="startGame('auto')"
10 )
11 | {{ st.tr["Example game"] }}
12 button(
13 v-show="!gameInProgress"
14 @click="startGame('versus')"
15 )
16 | {{ st.tr["Practice"] }}
17 button(
18 v-show="gameInProgress"
19 @click="stopGame()"
20 )
21 | {{ st.tr["Stop game"] }}
22 button(
23 v-if="showAnalyzeBtn"
24 @click="gotoAnalyze()"
25 )
26 | {{ st.tr["Analysis mode"] }}
27 .row
28 .col-sm-12.col-md-8.col-md-offset-2.col-lg-6.col-lg-offset-3
29 h4#variantName {{ gameInfo.vname }}
30 div(
31 v-show="display=='rules'"
32 v-html="content"
33 )
34 ComputerGame(
35 ref="compgame"
36 v-show="display=='computer'"
37 :game-info="gameInfo"
38 @game-stopped="gameStopped"
39 )
40 </template>
41
42 <script>
43 import ComputerGame from "@/components/ComputerGame.vue";
44 import { store } from "@/store";
45 import { getDiagram } from "@/utils/printDiagram";
46 import { CompgameStorage } from "@/utils/compgameStorage";
47 export default {
48 name: "my-rules",
49 components: {
50 ComputerGame
51 },
52 data: function() {
53 return {
54 st: store.state,
55 display: "rules",
56 gameInProgress: false,
57 // variables passed to ComputerGame:
58 gameInfo: {
59 vname: "",
60 mode: "versus",
61 },
62 V: null,
63 };
64 },
65 watch: {
66 $route: function(newRoute) {
67 this.re_setVariant(newRoute.params["vname"]);
68 }
69 },
70 created: function() {
71 // NOTE: variant cannot be set before store is initialized
72 this.re_setVariant(this.$route.params["vname"]);
73 },
74 computed: {
75 showAnalyzeBtn: function() {
76 return !!this.V && this.V.CanAnalyze;
77 },
78 content: function() {
79 if (!this.gameInfo.vname) return ""; //variant not set yet
80 // (AJAX) Request to get rules content (plain text, HTML)
81 return (
82 require(
83 "raw-loader!@/translations/rules/" +
84 this.gameInfo.vname + "/" +
85 this.st.lang + ".pug"
86 )
87 // Next two lines fix a weird issue after last update (2019-11)
88 .replace(/\\n/g, " ")
89 .replace(/\\"/g, '"')
90 .replace('module.exports = "', "")
91 .replace(/"$/, "")
92 .replace(/(fen:)([^:]*):/g, this.replaceByDiag)
93 );
94 }
95 },
96 methods: {
97 clickReadRules: function() {
98 if (this.display != "rules") this.display = "rules";
99 else if (this.gameInProgress) this.display = "computer";
100 },
101 parseFen(fen) {
102 const fenParts = fen.split(" ");
103 return {
104 position: fenParts[0],
105 marks: fenParts[1],
106 orientation: fenParts[2],
107 shadow: fenParts[3]
108 };
109 },
110 // Method to replace diagrams in loaded HTML
111 replaceByDiag: function(match, p1, p2) {
112 const args = this.parseFen(p2);
113 return getDiagram(args);
114 },
115 re_setVariant: async function(vname) {
116 await import("@/variants/" + vname + ".js")
117 .then((vModule) => {
118 this.V = window.V = vModule[vname + "Rules"];
119 this.gameInfo.vname = vname;
120 })
121 .catch((err) => {
122 // Soon after component creation, st.tr might be uninitialized.
123 // Set a timeout to let a chance for the message to show translated.
124 const text = "Mispelled variant name";
125 setTimeout(() => {
126 alert(this.st.tr[text] || text);
127 this.$router.replace("/variants");
128 }, 500);
129 });
130 },
131 startGame: function(mode) {
132 if (this.gameInProgress) return;
133 this.gameInProgress = true;
134 this.display = "computer";
135 this.gameInfo.mode = mode;
136 if (this.gameInfo.mode == "versus") {
137 CompgameStorage.get(this.gameInfo.vname, (game) => {
138 // NOTE: game might be null
139 this.$refs["compgame"].launchGame(game);
140 });
141 } else {
142 this.$refs["compgame"].launchGame();
143 }
144 },
145 // user wants to stop the game:
146 stopGame: function() {
147 this.$refs["compgame"].gameOver("?", "Undetermined result");
148 },
149 // The game is effectively stopped:
150 gameStopped: function() {
151 this.gameInProgress = false;
152 if (this.gameInfo.mode == "versus")
153 CompgameStorage.remove(this.gameInfo.vname);
154 },
155 gotoAnalyze: function() {
156 this.$router.push(
157 "/analyse/" + this.gameInfo.vname +
158 "/?fen=" + V.GenRandInitFen(this.st.settings.randomness)
159 );
160 }
161 }
162 };
163 </script>
164
165 <!-- NOTE: not scoped here, because HTML is injected (TODO) -->
166 <style lang="sass">
167 .warn
168 padding: 3px
169 color: red
170 background-color: lightgrey
171 font-weight: bold
172
173 h4#variantName
174 text-align: center
175 font-weight: bold
176
177 figure.diagram-container
178 margin: 15px 0 15px 0
179 text-align: center
180 width: 100%
181 display: block
182 .diagram
183 display: block
184 width: 50%
185 min-width: 240px
186 margin-left: auto
187 margin-right: auto
188 .diag12
189 float: left
190 width: 40%
191 margin-left: calc(10% - 20px)
192 margin-right: 40px
193 @media screen and (max-width: 630px)
194 float: none
195 margin: 0 auto 10px auto
196 .diag22
197 float: left
198 width: 40%
199 margin-right: calc(10% - 20px)
200 @media screen and (max-width: 630px)
201 float: none
202 margin: 0 auto
203 figcaption
204 display: block
205 clear: both
206 padding-top: 5px
207 font-size: 0.8em
208
209 p.boxed
210 background-color: #FFCC66
211 padding: 5px
212
213 .bigfont
214 font-size: 1.2em
215
216 .bold
217 font-weight: bold
218
219 .stageDelimiter
220 color: purple
221
222 // To show (new) pieces, and/or there values...
223 figure.showPieces > img
224 width: 50px
225
226 figure.showPieces > figcaption
227 color: #6C6C6C
228
229 .section-title
230 padding: 0
231
232 .section-title > h4
233 padding: 5px
234
235 ol, ul:not(.browser-default)
236 padding-left: 20px
237
238 ul:not(.browser-default)
239 margin-top: 5px
240
241 ul:not(.browser-default) > li
242 list-style-type: disc
243
244 table
245 margin: 15px auto
246
247 .italic
248 font-style: italic
249
250 img.img-center
251 display: block
252 margin: 0 auto 15px auto
253 </style>