367963c5784536ec400d5f24c1b0f44a8a0f5581
[vchess.git] / Rules.vue
1 <template lang="pug">
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
6 button(@click="clickReadRules()") {{ st.tr["Rules"] }}
7 button(
8 v-show="!gameInProgress"
9 @click="startGame('auto')"
10 )
11 | {{ st.tr["Example game"] }}
12 button(
13 v-show="!gameInProgress"
14 @click="startGame('versus')"
15 )
16 | {{ st.tr["Practice"] }}
17 button(
18 v-show="gameInProgress"
19 @click="stopGame()"
20 )
21 | {{ st.tr["Stop game"] }}
22 button(
23 v-if="showAnalyzeBtn"
24 @click="gotoAnalyze()"
25 )
26 | {{ st.tr["Analysis mode"] }}
27 .row
28 .col-sm-12.col-md-8.col-md-offset-2.col-lg-6.col-lg-offset-3
29 h4#variantName {{ gameInfo.vname }}
30 div(
31 v-show="display=='rules'"
32 v-html="content"
33 )
34 ComputerGame(
35 ref="compgame"
36 v-show="display=='computer'"
37 :game-info="gameInfo"
38 @game-stopped="gameStopped"
39 )
40 </template>
41
42 <script>
43 import ComputerGame from "@/components/ComputerGame.vue";
44 import { store } from "@/store";
45 import { replaceByDiag } from "@/utils/printDiagram";
46 import { CompgameStorage } from "@/utils/compgameStorage";
47 export default {
48 name: "my-rules",
49 components: {
50 ComputerGame
51 },
52 data: function() {
53 return {
54 st: store.state,
55 display: "rules",
56 gameInProgress: false,
57 // variables passed to ComputerGame:
58 gameInfo: {
59 vname: "",
60 mode: "versus",
61 },
62 V: null,
63 };
64 },
65 watch: {
66 $route: function(newRoute) {
67 this.re_setVariant(newRoute.params["vname"]);
68 }
69 },
70 created: function() {
71 // NOTE: variant cannot be set before store is initialized
72 this.re_setVariant(this.$route.params["vname"]);
73 },
74 computed: {
75 showAnalyzeBtn: function() {
76 return !!this.V && this.V.CanAnalyze;
77 },
78 content: function() {
79 if (!this.gameInfo.vname) return ""; //variant not set yet
80 // (AJAX) Request to get rules content (plain text, HTML)
81 return (
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(/"$/, "")
92 .replace(/(fen:)([^:]*):/g, replaceByDiag)
93 );
94 }
95 },
96 methods: {
97 clickReadRules: function() {
98 if (this.display != "rules") this.display = "rules";
99 else if (this.gameInProgress) this.display = "computer";
100 },
101 re_setVariant: async function(vname) {
102 await import("@/variants/" + vname + ".js")
103 .then((vModule) => {
104 this.V = window.V = vModule[vname + "Rules"];
105 this.gameInfo.vname = vname;
106 })
107 .catch((err) => {
108 // Soon after component creation, st.tr might be uninitialized.
109 // Set a timeout to let a chance for the message to show translated.
110 const text = "Mispelled variant name";
111 setTimeout(() => {
112 alert(this.st.tr[text] || text);
113 this.$router.replace("/variants");
114 }, 500);
115 });
116 },
117 startGame: function(mode) {
118 if (this.gameInProgress) return;
119 this.gameInProgress = true;
120 this.display = "computer";
121 this.gameInfo.mode = mode;
122 if (this.gameInfo.mode == "versus") {
123 CompgameStorage.get(this.gameInfo.vname, (game) => {
124 // NOTE: game might be null
125 this.$refs["compgame"].launchGame(game);
126 });
127 } else {
128 this.$refs["compgame"].launchGame();
129 }
130 },
131 // user wants to stop the game:
132 stopGame: function() {
133 this.$refs["compgame"].gameOver("?", "Undetermined result");
134 },
135 // The game is effectively stopped:
136 gameStopped: function() {
137 this.gameInProgress = false;
138 if (this.gameInfo.mode == "versus")
139 CompgameStorage.remove(this.gameInfo.vname);
140 },
141 gotoAnalyze: function() {
142 this.$router.push(
143 "/analyse/" + this.gameInfo.vname +
144 "/?fen=" + V.GenRandInitFen(this.st.settings.randomness)
145 );
146 }
147 }
148 };
149 </script>
150
151 <!-- NOTE: not scoped here, because HTML is injected -->
152 <style lang="sass">
153 @import "@/styles/_board_squares_img.sass"
154 @import "@/styles/_rules.sass"
155 </style>
156
157 <style lang="sass" scoped>
158 h4#variantName
159 text-align: center
160 font-weight: bold
161 </style>