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