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