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