Fix games vs comp + auto
[vchess.git] / client / src / views / Rules.vue
CommitLineData
cf2343ce
BA
1<template lang="pug">
2.row
4473050c 3 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
cf2343ce 4 .button-group
41cb9b94 5 button(@click="clickReadRules") Read the rules
834c202a 6 button(v-show="!gameInProgress" @click="() => startGame('auto')")
cf2343ce 7 | Observe a sample game
834c202a 8 button(v-show="!gameInProgress" @click="() => startGame('versus')")
cf2343ce 9 | Beat the computer!
41cb9b94 10 button(v-show="gameInProgress" @click="() => stopGame()")
cf2343ce 11 | Stop game
50aed5a1 12 .section-content(v-show="display=='rules'" v-html="content")
6dd02928 13 ComputerGame(v-show="display=='computer'" :game-info="gameInfo"
af32cf62 14 @game-over="stopGame" @game-stopped="gameStopped")
cf2343ce
BA
15</template>
16
17<script>
a6088c90 18import ComputerGame from "@/components/ComputerGame.vue";
cf2343ce 19import { store } from "@/store";
50aed5a1 20import { getDiagram } from "@/utils/printDiagram";
13aa5a44 21
cf2343ce
BA
22export default {
23 name: 'my-rules',
24340cae 24 components: {
a6088c90 25 ComputerGame,
24340cae 26 },
cf2343ce
BA
27 data: function() {
28 return {
29 st: store.state,
50aed5a1 30 content: "",
cf2343ce 31 display: "rules",
cf2343ce 32 gameInProgress: false,
6dd02928 33 // variables passed to ComputerGame:
834c202a
BA
34 gameInfo: {
35 vname: "_unknown",
36 mode: "versus",
37 fen: "",
41cb9b94 38 score: "*",
834c202a 39 }
cf2343ce
BA
40 };
41 },
42eb4eaf 42 watch: {
a6088c90 43 "$route": function(newRoute) {
42eb4eaf
BA
44 this.tryChangeVariant(newRoute.params["vname"]);
45 },
46 },
47 created: async function() {
e2732923 48 // NOTE: variant cannot be set before store is initialized
42eb4eaf 49 this.tryChangeVariant(this.$route.params["vname"]);
cf2343ce
BA
50 },
51 methods: {
41cb9b94
BA
52 clickReadRules: function() {
53 if (this.display != "rules")
54 this.display = "rules";
55 else if (this.gameInProgress)
56 this.display = "computer";
57 },
50aed5a1
BA
58 parseFen(fen) {
59 const fenParts = fen.split(" ");
60 return {
61 position: fenParts[0],
62 marks: fenParts[1],
63 orientation: fenParts[2],
64 shadow: fenParts[3],
65 };
66 },
42eb4eaf 67 tryChangeVariant: async function(vname) {
a6088c90 68 if (!vname || vname == "_unknown")
42eb4eaf 69 return;
834c202a 70 this.gameInfo.vname = vname;
42eb4eaf
BA
71 const vModule = await import("@/variants/" + vname + ".js");
72 window.V = vModule.VariantRules;
50aed5a1
BA
73 // Method to replace diagrams in loaded HTML
74 const replaceByDiag = (match, p1, p2) => {
75 const args = this.parseFen(p2);
76 return getDiagram(args);
77 };
78 // (AJAX) Request to get rules content (plain text, HTML)
79 this.content =
80 require("raw-loader!@/rules/" + vname + "/" + this.st.lang + ".pug")
2c6cb25e
BA
81 // Next two lines fix a weird issue after last update (2019-11)
82 .replace(/\\[n"]/g, " ")
83 .replace('module.exports = "', '').replace(/"$/, "")
50aed5a1 84 .replace(/(fen:)([^:]*):/g, replaceByDiag);
cf2343ce 85 },
834c202a 86 startGame: function(mode) {
cf2343ce
BA
87 if (this.gameInProgress)
88 return;
89 this.gameInProgress = true;
cf2343ce 90 this.display = "computer";
834c202a 91 this.gameInfo.mode = mode;
41cb9b94 92 this.gameInfo.score = "*";
834c202a 93 this.gameInfo.fen = V.GenRandInitFen();
cf2343ce 94 },
41cb9b94
BA
95 // user is willing to stop the game:
96 stopGame: function(score) {
97 this.gameInfo.score = score || "?";
834c202a 98 this.gameInfo.mode = "analyze";
cf2343ce 99 },
41cb9b94
BA
100 // The game is effectively stopped:
101 gameStopped: function() {
102 this.gameInProgress = false;
103 },
cf2343ce
BA
104 },
105};
106</script>
50aed5a1
BA
107
108<style lang="sass">
109.warn
110 padding: 3px
111 color: red
112 background-color: lightgrey
113 font-weight: bold
114
115figure.diagram-container
116 margin: 15px 0 15px 0
117 text-align: center
118 width: 100%
119 display: block
120 .diagram
121 display: block
122 width: 40%
123 min-width: 240px
124 margin-left: auto
125 margin-right: auto
126 .diag12
127 float: left
128 margin-left: calc(10% - 20px)
129 margin-right: 40px
130 @media screen and (max-width: 630px)
131 float: none
132 margin: 0 auto 10px auto
133 .diag22
134 float: left
135 margin-right: calc(10% - 20px)
136 @media screen and (max-width: 630px)
137 float: none
138 margin: 0 auto
139 figcaption
140 display: block
141 clear: both
142 padding-top: 5px
143 font-size: 0.8em
144
92a523d1
BA
145p.boxed
146 background-color: #FFCC66
147 padding: 5px
50aed5a1 148
92a523d1
BA
149.stageDelimiter
150 color: purple
50aed5a1 151
92a523d1
BA
152// To show (new) pieces, and/or there values...
153figure.showPieces > img
154 width: 50px
50aed5a1 155
92a523d1
BA
156figure.showPieces > figcaption
157 color: #6C6C6C
50aed5a1 158
92a523d1
BA
159.section-title
160 padding: 0
50aed5a1 161
92a523d1
BA
162.section-title > h4
163 padding: 5px
50aed5a1 164
92a523d1
BA
165ol, ul:not(.browser-default)
166 padding-left: 20px
50aed5a1 167
92a523d1
BA
168ul:not(.browser-default)
169 margin-top: 5px
50aed5a1 170
92a523d1
BA
171ul:not(.browser-default) > li
172 list-style-type: disc
50aed5a1
BA
173
174.light-square-diag
175 background-color: #e5e5ca
176
177.dark-square-diag
178 background-color: #6f8f57
179
180// TODO: following is duplicated
181div.board
182 float: left
183 height: 0
184 display: inline-block
185 position: relative
186
187div.board8
188 width: 12.5%
189 padding-bottom: 12.5%
190
191div.board10
192 width: 10%
193 padding-bottom: 10%
194
195div.board11
196 width: 9.09%
197 padding-bottom: 9.1%
198
199img.piece
200 width: 100%
201
202img.piece, img.mark-square
203 max-width: 100%
204 height: auto
205 display: block
206
207img.mark-square
208 opacity: 0.6
209 width: 76%
210 position: absolute
211 top: 12%
212 left: 12%
213 opacity: .7
214
215.in-shadow
216 filter: brightness(50%)
217</style>