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