Revise server code + a few fixes in trnalsations and ComputerGame
[vchess.git] / client / src / views / Rules.vue
CommitLineData
cf2343ce 1<template lang="pug">
7aa548e7
BA
2main
3 .row
4 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
5 .button-group
9ddaf8da 6 button(@click="clickReadRules()") {{ st.tr["Rules"] }}
910d631b
BA
7 button(
8 v-show="!gameInProgress"
9 @click="startGame('auto')"
10 )
f44fd3bf 11 | {{ st.tr["Example game"] }}
910d631b
BA
12 button(
13 v-show="!gameInProgress"
14 @click="startGame('versus')"
15 )
602d6bef 16 | {{ st.tr["Practice"] }}
910d631b
BA
17 button(
18 v-show="gameInProgress"
19 @click="stopGame()"
20 )
602d6bef 21 | {{ st.tr["Stop game"] }}
910d631b 22 button(
20620465 23 v-if="showAnalyzeBtn"
910d631b
BA
24 @click="gotoAnalyze()"
25 )
677fe285 26 | {{ st.tr["Analyse"] }}
910d631b
BA
27 div(
28 v-show="display=='rules'"
29 v-html="content"
30 )
31 ComputerGame(
866842c3 32 ref="compgame"
910d631b
BA
33 v-show="display=='computer'"
34 :game-info="gameInfo"
910d631b
BA
35 @game-stopped="gameStopped"
36 )
cf2343ce
BA
37</template>
38
39<script>
a6088c90 40import ComputerGame from "@/components/ComputerGame.vue";
cf2343ce 41import { store } from "@/store";
50aed5a1 42import { getDiagram } from "@/utils/printDiagram";
cf2343ce 43export default {
6808d7a1 44 name: "my-rules",
24340cae 45 components: {
6808d7a1 46 ComputerGame
24340cae 47 },
cf2343ce
BA
48 data: function() {
49 return {
50 st: store.state,
cf2343ce 51 display: "rules",
cf2343ce 52 gameInProgress: false,
6dd02928 53 // variables passed to ComputerGame:
834c202a 54 gameInfo: {
fcbc92c2 55 vname: "",
834c202a 56 mode: "versus",
866842c3 57 fen: ""
834c202a 58 }
cf2343ce
BA
59 };
60 },
42eb4eaf 61 watch: {
6808d7a1 62 $route: function(newRoute) {
fcbc92c2 63 this.re_setVariant(newRoute.params["vname"]);
6808d7a1 64 }
42eb4eaf 65 },
6f093ada 66 created: function() {
e2732923 67 // NOTE: variant cannot be set before store is initialized
fcbc92c2
BA
68 this.re_setVariant(this.$route.params["vname"]);
69 },
70 computed: {
20620465
BA
71 showAnalyzeBtn: function() {
72 return (this.display=='rules' && (!window.V || V.CanAnalyse));
73 },
fcbc92c2 74 content: function() {
6808d7a1 75 if (!this.gameInfo.vname) return ""; //variant not set yet
fcbc92c2 76 // (AJAX) Request to get rules content (plain text, HTML)
6808d7a1
BA
77 return (
78 require("raw-loader!@/translations/rules/" +
79 this.gameInfo.vname +
80 "/" +
81 this.st.lang +
82 ".pug")
83 // Next two lines fix a weird issue after last update (2019-11)
84 .replace(/\\n/g, " ")
85 .replace(/\\"/g, '"')
86 .replace('module.exports = "', "")
87 .replace(/"$/, "")
88 .replace(/(fen:)([^:]*):/g, this.replaceByDiag)
89 );
90 }
cf2343ce
BA
91 },
92 methods: {
41cb9b94 93 clickReadRules: function() {
6808d7a1
BA
94 if (this.display != "rules") this.display = "rules";
95 else if (this.gameInProgress) this.display = "computer";
41cb9b94 96 },
50aed5a1
BA
97 parseFen(fen) {
98 const fenParts = fen.split(" ");
99 return {
100 position: fenParts[0],
101 marks: fenParts[1],
102 orientation: fenParts[2],
6808d7a1 103 shadow: fenParts[3]
50aed5a1
BA
104 };
105 },
fcbc92c2
BA
106 // Method to replace diagrams in loaded HTML
107 replaceByDiag: function(match, p1, p2) {
108 const args = this.parseFen(p2);
109 return getDiagram(args);
110 },
111 re_setVariant: async function(vname) {
42eb4eaf
BA
112 const vModule = await import("@/variants/" + vname + ".js");
113 window.V = vModule.VariantRules;
fcbc92c2 114 this.gameInfo.vname = vname;
cf2343ce 115 },
834c202a 116 startGame: function(mode) {
6808d7a1 117 if (this.gameInProgress) return;
cf2343ce 118 this.gameInProgress = true;
cf2343ce 119 this.display = "computer";
834c202a 120 this.gameInfo.mode = mode;
866842c3 121 this.$set(this.gameInfo, "fen", V.GenRandInitFen());
cf2343ce 122 },
41cb9b94 123 // user is willing to stop the game:
866842c3
BA
124 stopGame: function() {
125 this.$refs["compgame"].gameOver("?", "Undetermined result");
cf2343ce 126 },
41cb9b94
BA
127 // The game is effectively stopped:
128 gameStopped: function() {
129 this.gameInProgress = false;
130 },
5157ce0b 131 gotoAnalyze: function() {
6808d7a1
BA
132 this.$router.push(
133 "/analyse/" + this.gameInfo.vname + "/?fen=" + V.GenRandInitFen()
134 );
135 }
136 }
cf2343ce
BA
137};
138</script>
50aed5a1 139
5bcc9b31
BA
140<!-- NOTE: not scoped here, because HTML is injected (TODO) -->
141<style lang="sass">
50aed5a1
BA
142.warn
143 padding: 3px
144 color: red
145 background-color: lightgrey
146 font-weight: bold
147
148figure.diagram-container
149 margin: 15px 0 15px 0
150 text-align: center
151 width: 100%
152 display: block
153 .diagram
154 display: block
155 width: 40%
156 min-width: 240px
157 margin-left: auto
158 margin-right: auto
159 .diag12
160 float: left
161 margin-left: calc(10% - 20px)
162 margin-right: 40px
163 @media screen and (max-width: 630px)
164 float: none
165 margin: 0 auto 10px auto
166 .diag22
167 float: left
168 margin-right: calc(10% - 20px)
169 @media screen and (max-width: 630px)
170 float: none
171 margin: 0 auto
172 figcaption
173 display: block
174 clear: both
175 padding-top: 5px
176 font-size: 0.8em
177
92a523d1
BA
178p.boxed
179 background-color: #FFCC66
180 padding: 5px
50aed5a1 181
9a3049f3
BA
182.bigfont
183 font-size: 1.2em
184
185.bold
186 font-weight: bold
187
92a523d1
BA
188.stageDelimiter
189 color: purple
50aed5a1 190
92a523d1
BA
191// To show (new) pieces, and/or there values...
192figure.showPieces > img
193 width: 50px
50aed5a1 194
92a523d1
BA
195figure.showPieces > figcaption
196 color: #6C6C6C
50aed5a1 197
92a523d1
BA
198.section-title
199 padding: 0
50aed5a1 200
92a523d1
BA
201.section-title > h4
202 padding: 5px
50aed5a1 203
92a523d1
BA
204ol, ul:not(.browser-default)
205 padding-left: 20px
50aed5a1 206
92a523d1
BA
207ul:not(.browser-default)
208 margin-top: 5px
50aed5a1 209
92a523d1
BA
210ul:not(.browser-default) > li
211 list-style-type: disc
50aed5a1 212</style>