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