Some more cleaning + fixes
[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
BA
22 button(
23 v-if="display=='rules' && gameInfo.vname!='Dark'"
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(
32 v-show="display=='computer'"
33 :game-info="gameInfo"
34 @game-over="stopGame"
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
BA
56 mode: "versus",
57 fen: "",
6808d7a1 58 score: "*"
834c202a 59 }
cf2343ce
BA
60 };
61 },
42eb4eaf 62 watch: {
6808d7a1 63 $route: function(newRoute) {
fcbc92c2 64 this.re_setVariant(newRoute.params["vname"]);
6808d7a1 65 }
42eb4eaf 66 },
6f093ada 67 created: function() {
e2732923 68 // NOTE: variant cannot be set before store is initialized
fcbc92c2
BA
69 this.re_setVariant(this.$route.params["vname"]);
70 },
71 computed: {
72 content: function() {
6808d7a1 73 if (!this.gameInfo.vname) return ""; //variant not set yet
fcbc92c2 74 // (AJAX) Request to get rules content (plain text, HTML)
6808d7a1
BA
75 return (
76 require("raw-loader!@/translations/rules/" +
77 this.gameInfo.vname +
78 "/" +
79 this.st.lang +
80 ".pug")
81 // Next two lines fix a weird issue after last update (2019-11)
82 .replace(/\\n/g, " ")
83 .replace(/\\"/g, '"')
84 .replace('module.exports = "', "")
85 .replace(/"$/, "")
86 .replace(/(fen:)([^:]*):/g, this.replaceByDiag)
87 );
88 }
cf2343ce
BA
89 },
90 methods: {
41cb9b94 91 clickReadRules: function() {
6808d7a1
BA
92 if (this.display != "rules") this.display = "rules";
93 else if (this.gameInProgress) this.display = "computer";
41cb9b94 94 },
50aed5a1
BA
95 parseFen(fen) {
96 const fenParts = fen.split(" ");
97 return {
98 position: fenParts[0],
99 marks: fenParts[1],
100 orientation: fenParts[2],
6808d7a1 101 shadow: fenParts[3]
50aed5a1
BA
102 };
103 },
fcbc92c2
BA
104 // Method to replace diagrams in loaded HTML
105 replaceByDiag: function(match, p1, p2) {
106 const args = this.parseFen(p2);
107 return getDiagram(args);
108 },
109 re_setVariant: async function(vname) {
42eb4eaf
BA
110 const vModule = await import("@/variants/" + vname + ".js");
111 window.V = vModule.VariantRules;
fcbc92c2 112 this.gameInfo.vname = vname;
cf2343ce 113 },
834c202a 114 startGame: function(mode) {
6808d7a1 115 if (this.gameInProgress) return;
cf2343ce 116 this.gameInProgress = true;
cf2343ce 117 this.display = "computer";
834c202a 118 this.gameInfo.mode = mode;
41cb9b94 119 this.gameInfo.score = "*";
834c202a 120 this.gameInfo.fen = V.GenRandInitFen();
cf2343ce 121 },
41cb9b94
BA
122 // user is willing to stop the game:
123 stopGame: function(score) {
124 this.gameInfo.score = score || "?";
cf2343ce 125 },
41cb9b94
BA
126 // The game is effectively stopped:
127 gameStopped: function() {
128 this.gameInProgress = false;
129 },
5157ce0b 130 gotoAnalyze: function() {
6808d7a1
BA
131 this.$router.push(
132 "/analyse/" + this.gameInfo.vname + "/?fen=" + V.GenRandInitFen()
133 );
134 }
135 }
cf2343ce
BA
136};
137</script>
50aed5a1 138
5bcc9b31
BA
139<!-- NOTE: not scoped here, because HTML is injected (TODO) -->
140<style lang="sass">
50aed5a1
BA
141.warn
142 padding: 3px
143 color: red
144 background-color: lightgrey
145 font-weight: bold
146
147figure.diagram-container
148 margin: 15px 0 15px 0
149 text-align: center
150 width: 100%
151 display: block
152 .diagram
153 display: block
154 width: 40%
155 min-width: 240px
156 margin-left: auto
157 margin-right: auto
158 .diag12
159 float: left
160 margin-left: calc(10% - 20px)
161 margin-right: 40px
162 @media screen and (max-width: 630px)
163 float: none
164 margin: 0 auto 10px auto
165 .diag22
166 float: left
167 margin-right: calc(10% - 20px)
168 @media screen and (max-width: 630px)
169 float: none
170 margin: 0 auto
171 figcaption
172 display: block
173 clear: both
174 padding-top: 5px
175 font-size: 0.8em
176
92a523d1
BA
177p.boxed
178 background-color: #FFCC66
179 padding: 5px
50aed5a1 180
9a3049f3
BA
181.bigfont
182 font-size: 1.2em
183
184.bold
185 font-weight: bold
186
92a523d1
BA
187.stageDelimiter
188 color: purple
50aed5a1 189
92a523d1
BA
190// To show (new) pieces, and/or there values...
191figure.showPieces > img
192 width: 50px
50aed5a1 193
92a523d1
BA
194figure.showPieces > figcaption
195 color: #6C6C6C
50aed5a1 196
92a523d1
BA
197.section-title
198 padding: 0
50aed5a1 199
92a523d1
BA
200.section-title > h4
201 padding: 5px
50aed5a1 202
92a523d1
BA
203ol, ul:not(.browser-default)
204 padding-left: 20px
50aed5a1 205
92a523d1
BA
206ul:not(.browser-default)
207 margin-top: 5px
50aed5a1 208
92a523d1
BA
209ul:not(.browser-default) > li
210 list-style-type: disc
50aed5a1 211</style>