Fix rules contents
[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
602d6bef 6 button(@click="clickReadRules") {{ st.tr["Rules"] }}
7aa548e7 7 button(v-show="!gameInProgress" @click="() => startGame('auto')")
602d6bef 8 | {{ st.tr["Sample game"] }}
7aa548e7 9 button(v-show="!gameInProgress" @click="() => startGame('versus')")
602d6bef 10 | {{ st.tr["Practice"] }}
7aa548e7 11 button(v-show="gameInProgress" @click="() => stopGame()")
602d6bef 12 | {{ st.tr["Stop game"] }}
4f518610
BA
13 button(v-if="display=='rules' && gameInfo.vname!='Dark'"
14 @click="gotoAnalyze")
0e16cb26 15 | {{ st.tr["Analyze"] }}
7aa548e7 16 .section-content(v-show="display=='rules'" v-html="content")
72ccbd67
BA
17 ComputerGame(v-show="display=='computer'" :game-info="gameInfo"
18 @game-over="stopGame" @game-stopped="gameStopped")
cf2343ce
BA
19</template>
20
21<script>
a6088c90 22import ComputerGame from "@/components/ComputerGame.vue";
cf2343ce 23import { store } from "@/store";
50aed5a1 24import { getDiagram } from "@/utils/printDiagram";
13aa5a44 25
cf2343ce
BA
26export default {
27 name: 'my-rules',
24340cae 28 components: {
a6088c90 29 ComputerGame,
24340cae 30 },
cf2343ce
BA
31 data: function() {
32 return {
33 st: store.state,
50aed5a1 34 content: "",
cf2343ce 35 display: "rules",
cf2343ce 36 gameInProgress: false,
6dd02928 37 // variables passed to ComputerGame:
834c202a
BA
38 gameInfo: {
39 vname: "_unknown",
40 mode: "versus",
41 fen: "",
41cb9b94 42 score: "*",
834c202a 43 }
cf2343ce
BA
44 };
45 },
42eb4eaf 46 watch: {
a6088c90 47 "$route": function(newRoute) {
42eb4eaf
BA
48 this.tryChangeVariant(newRoute.params["vname"]);
49 },
50 },
51 created: async function() {
e2732923 52 // NOTE: variant cannot be set before store is initialized
42eb4eaf 53 this.tryChangeVariant(this.$route.params["vname"]);
cf2343ce
BA
54 },
55 methods: {
41cb9b94
BA
56 clickReadRules: function() {
57 if (this.display != "rules")
58 this.display = "rules";
59 else if (this.gameInProgress)
60 this.display = "computer";
61 },
50aed5a1
BA
62 parseFen(fen) {
63 const fenParts = fen.split(" ");
64 return {
65 position: fenParts[0],
66 marks: fenParts[1],
67 orientation: fenParts[2],
68 shadow: fenParts[3],
69 };
70 },
42eb4eaf 71 tryChangeVariant: async function(vname) {
a6088c90 72 if (!vname || vname == "_unknown")
42eb4eaf 73 return;
834c202a 74 this.gameInfo.vname = vname;
42eb4eaf
BA
75 const vModule = await import("@/variants/" + vname + ".js");
76 window.V = vModule.VariantRules;
50aed5a1
BA
77 // Method to replace diagrams in loaded HTML
78 const replaceByDiag = (match, p1, p2) => {
79 const args = this.parseFen(p2);
80 return getDiagram(args);
81 };
82 // (AJAX) Request to get rules content (plain text, HTML)
83 this.content =
72ccbd67 84 require("raw-loader!@/translations/rules/" + vname + "/" + this.st.lang + ".pug")
2c6cb25e 85 // Next two lines fix a weird issue after last update (2019-11)
ea7adb58 86 .replace(/\\n/g, " ").replace(/\\"/g, '"')
2c6cb25e 87 .replace('module.exports = "', '').replace(/"$/, "")
50aed5a1 88 .replace(/(fen:)([^:]*):/g, replaceByDiag);
cf2343ce 89 },
834c202a 90 startGame: function(mode) {
cf2343ce
BA
91 if (this.gameInProgress)
92 return;
93 this.gameInProgress = true;
cf2343ce 94 this.display = "computer";
834c202a 95 this.gameInfo.mode = mode;
41cb9b94 96 this.gameInfo.score = "*";
834c202a 97 this.gameInfo.fen = V.GenRandInitFen();
cf2343ce 98 },
41cb9b94
BA
99 // user is willing to stop the game:
100 stopGame: function(score) {
101 this.gameInfo.score = score || "?";
cf2343ce 102 },
41cb9b94
BA
103 // The game is effectively stopped:
104 gameStopped: function() {
105 this.gameInProgress = false;
106 },
5157ce0b
BA
107 gotoAnalyze: function() {
108 this.$router.push("/analyze/" + this.gameInfo.vname
109 + "/?fen=" + V.GenRandInitFen());
110 },
cf2343ce
BA
111 },
112};
113</script>
50aed5a1 114
5bcc9b31
BA
115<!-- NOTE: not scoped here, because HTML is injected (TODO) -->
116<style lang="sass">
dcd68c41
BA
117//.section-content
118// *
119// margin-left: auto
120// margin-right: auto
121// max-width: 767px
122// figure.diagram-container
123// max-width: 1000px
124// @media screen and (max-width: 767px)
125// max-width: 100%
126// padding: 0 5px
127
50aed5a1
BA
128.warn
129 padding: 3px
130 color: red
131 background-color: lightgrey
132 font-weight: bold
133
134figure.diagram-container
135 margin: 15px 0 15px 0
136 text-align: center
137 width: 100%
138 display: block
139 .diagram
140 display: block
141 width: 40%
142 min-width: 240px
143 margin-left: auto
144 margin-right: auto
145 .diag12
146 float: left
147 margin-left: calc(10% - 20px)
148 margin-right: 40px
149 @media screen and (max-width: 630px)
150 float: none
151 margin: 0 auto 10px auto
152 .diag22
153 float: left
154 margin-right: calc(10% - 20px)
155 @media screen and (max-width: 630px)
156 float: none
157 margin: 0 auto
158 figcaption
159 display: block
160 clear: both
161 padding-top: 5px
162 font-size: 0.8em
163
92a523d1
BA
164p.boxed
165 background-color: #FFCC66
166 padding: 5px
50aed5a1 167
92a523d1
BA
168.stageDelimiter
169 color: purple
50aed5a1 170
92a523d1
BA
171// To show (new) pieces, and/or there values...
172figure.showPieces > img
173 width: 50px
50aed5a1 174
92a523d1
BA
175figure.showPieces > figcaption
176 color: #6C6C6C
50aed5a1 177
92a523d1
BA
178.section-title
179 padding: 0
50aed5a1 180
92a523d1
BA
181.section-title > h4
182 padding: 5px
50aed5a1 183
92a523d1
BA
184ol, ul:not(.browser-default)
185 padding-left: 20px
50aed5a1 186
92a523d1
BA
187ul:not(.browser-default)
188 margin-top: 5px
50aed5a1 189
92a523d1
BA
190ul:not(.browser-default) > li
191 list-style-type: disc
50aed5a1
BA
192
193.light-square-diag
194 background-color: #e5e5ca
195
196.dark-square-diag
197 background-color: #6f8f57
198
41c80bb6 199// TODO: following is duplicated (Board.vue)
50aed5a1
BA
200div.board
201 float: left
202 height: 0
203 display: inline-block
204 position: relative
205
206div.board8
207 width: 12.5%
208 padding-bottom: 12.5%
209
210div.board10
211 width: 10%
212 padding-bottom: 10%
213
214div.board11
215 width: 9.09%
216 padding-bottom: 9.1%
217
218img.piece
219 width: 100%
220
221img.piece, img.mark-square
222 max-width: 100%
223 height: auto
224 display: block
225
226img.mark-square
227 opacity: 0.6
228 width: 76%
229 position: absolute
230 top: 12%
231 left: 12%
232 opacity: .7
233
234.in-shadow
235 filter: brightness(50%)
236</style>