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 | |
f76dd684 | 29 | h4#variantName(v-show="display=='rules'") {{ gameInfo.vname }} |
910d631b BA |
30 | div( |
31 | v-show="display=='rules'" | |
32 | v-html="content" | |
33 | ) | |
34 | ComputerGame( | |
866842c3 | 35 | ref="compgame" |
910d631b BA |
36 | v-show="display=='computer'" |
37 | :game-info="gameInfo" | |
910d631b BA |
38 | @game-stopped="gameStopped" |
39 | ) | |
cf2343ce BA |
40 | </template> |
41 | ||
42 | <script> | |
a6088c90 | 43 | import ComputerGame from "@/components/ComputerGame.vue"; |
cf2343ce | 44 | import { store } from "@/store"; |
07052665 | 45 | import { replaceByDiag } from "@/utils/printDiagram"; |
98b94cc3 | 46 | import { CompgameStorage } from "@/utils/compgameStorage"; |
cf2343ce | 47 | export default { |
6808d7a1 | 48 | name: "my-rules", |
24340cae | 49 | components: { |
6808d7a1 | 50 | ComputerGame |
24340cae | 51 | }, |
cf2343ce BA |
52 | data: function() { |
53 | return { | |
54 | st: store.state, | |
cf2343ce | 55 | display: "rules", |
cf2343ce | 56 | gameInProgress: false, |
6dd02928 | 57 | // variables passed to ComputerGame: |
834c202a | 58 | gameInfo: { |
fcbc92c2 | 59 | vname: "", |
834c202a | 60 | mode: "versus", |
b9139251 BA |
61 | }, |
62 | V: null, | |
cf2343ce BA |
63 | }; |
64 | }, | |
42eb4eaf | 65 | watch: { |
6808d7a1 | 66 | $route: function(newRoute) { |
fcbc92c2 | 67 | this.re_setVariant(newRoute.params["vname"]); |
6808d7a1 | 68 | } |
42eb4eaf | 69 | }, |
6f093ada | 70 | created: function() { |
e2732923 | 71 | // NOTE: variant cannot be set before store is initialized |
fcbc92c2 BA |
72 | this.re_setVariant(this.$route.params["vname"]); |
73 | }, | |
74 | computed: { | |
20620465 | 75 | showAnalyzeBtn: function() { |
84fc0f02 | 76 | return !!this.V && this.V.CanAnalyze; |
20620465 | 77 | }, |
fcbc92c2 | 78 | content: function() { |
6808d7a1 | 79 | if (!this.gameInfo.vname) return ""; //variant not set yet |
fcbc92c2 | 80 | // (AJAX) Request to get rules content (plain text, HTML) |
6808d7a1 | 81 | return ( |
e57c4de4 BA |
82 | require( |
83 | "raw-loader!@/translations/rules/" + | |
84 | this.gameInfo.vname + "/" + | |
85 | this.st.lang + ".pug" | |
86 | ) | |
87 | // Next two lines fix a weird issue after last update (2019-11) | |
88 | .replace(/\\n/g, " ") | |
89 | .replace(/\\"/g, '"') | |
90 | .replace('module.exports = "', "") | |
91 | .replace(/"$/, "") | |
07052665 | 92 | .replace(/(fen:)([^:]*):/g, replaceByDiag) |
6808d7a1 BA |
93 | ); |
94 | } | |
cf2343ce BA |
95 | }, |
96 | methods: { | |
41cb9b94 | 97 | clickReadRules: function() { |
6808d7a1 BA |
98 | if (this.display != "rules") this.display = "rules"; |
99 | else if (this.gameInProgress) this.display = "computer"; | |
41cb9b94 | 100 | }, |
fcbc92c2 | 101 | re_setVariant: async function(vname) { |
ba65b8de BA |
102 | const key = "rr_" + vname; |
103 | if (!localStorage.getItem(key)) | |
104 | // Mark rules as "read" | |
105 | localStorage.setItem(key, '1'); | |
a97bdbda BA |
106 | await import("@/variants/" + vname + ".js") |
107 | .then((vModule) => { | |
32f6285e | 108 | this.V = window.V = vModule[vname + "Rules"]; |
a97bdbda BA |
109 | this.gameInfo.vname = vname; |
110 | }) | |
111 | .catch((err) => { | |
112 | // Soon after component creation, st.tr might be uninitialized. | |
113 | // Set a timeout to let a chance for the message to show translated. | |
114 | const text = "Mispelled variant name"; | |
115 | setTimeout(() => { | |
116 | alert(this.st.tr[text] || text); | |
117 | this.$router.replace("/variants"); | |
118 | }, 500); | |
119 | }); | |
cf2343ce | 120 | }, |
834c202a | 121 | startGame: function(mode) { |
6808d7a1 | 122 | if (this.gameInProgress) return; |
cf2343ce | 123 | this.gameInProgress = true; |
cf2343ce | 124 | this.display = "computer"; |
834c202a | 125 | this.gameInfo.mode = mode; |
98b94cc3 BA |
126 | if (this.gameInfo.mode == "versus") { |
127 | CompgameStorage.get(this.gameInfo.vname, (game) => { | |
128 | // NOTE: game might be null | |
129 | this.$refs["compgame"].launchGame(game); | |
130 | }); | |
131 | } else { | |
132 | this.$refs["compgame"].launchGame(); | |
133 | } | |
cf2343ce | 134 | }, |
d2edca6d | 135 | // The user wants to stop the game: |
866842c3 BA |
136 | stopGame: function() { |
137 | this.$refs["compgame"].gameOver("?", "Undetermined result"); | |
cf2343ce | 138 | }, |
41cb9b94 BA |
139 | // The game is effectively stopped: |
140 | gameStopped: function() { | |
141 | this.gameInProgress = false; | |
98b94cc3 BA |
142 | if (this.gameInfo.mode == "versus") |
143 | CompgameStorage.remove(this.gameInfo.vname); | |
41cb9b94 | 144 | }, |
5157ce0b | 145 | gotoAnalyze: function() { |
6808d7a1 | 146 | this.$router.push( |
6b7b2cf7 BA |
147 | "/analyse/" + this.gameInfo.vname + |
148 | "/?fen=" + V.GenRandInitFen(this.st.settings.randomness) | |
6808d7a1 BA |
149 | ); |
150 | } | |
151 | } | |
cf2343ce BA |
152 | }; |
153 | </script> | |
50aed5a1 | 154 | |
26d8a01a | 155 | <!-- NOTE: not scoped here, because HTML is injected --> |
5bcc9b31 | 156 | <style lang="sass"> |
26d8a01a BA |
157 | @import "@/styles/_board_squares_img.sass" |
158 | @import "@/styles/_rules.sass" | |
159 | </style> | |
c7550017 | 160 | |
26d8a01a BA |
161 | <style lang="sass" scoped> |
162 | h4#variantName | |
50aed5a1 | 163 | text-align: center |
9a3049f3 | 164 | font-weight: bold |
50aed5a1 | 165 | </style> |