| 1 | // Load rules on variant page |
| 2 | Vue.component('my-rules', { |
| 3 | props: ["settings"], |
| 4 | data: function() { |
| 5 | return { |
| 6 | content: "", |
| 7 | display: "rules", |
| 8 | mode: "computer", |
| 9 | mycolor: "w", |
| 10 | allowMovelist: true, |
| 11 | fen: "", |
| 12 | }; |
| 13 | }, |
| 14 | |
| 15 | // TODO: third button "see a sample game" (comp VS comp) |
| 16 | |
| 17 | template: ` |
| 18 | <div class="col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2"> |
| 19 | <div class="button-group"> |
| 20 | <button @click="display='rules'"> |
| 21 | Read the rules |
| 22 | </button> |
| 23 | <button @click="startComputerGame()"> |
| 24 | Beat the computer! |
| 25 | </button> |
| 26 | </div> |
| 27 | <div v-show="display=='rules'" v-html="content" class="section-content"></div> |
| 28 | <my-game v-show="display=='computer'" :mycolor="mycolor" :settings="settings" |
| 29 | :allow-movelist="allowMovelist" :mode="mode" :fen="fen"> |
| 30 | </my-game> |
| 31 | </div> |
| 32 | `, |
| 33 | mounted: function() { |
| 34 | // AJAX request to get rules content (plain text, HTML) |
| 35 | ajax("/rules/" + variant.name, "GET", response => { |
| 36 | let replaceByDiag = (match, p1, p2) => { |
| 37 | const args = this.parseFen(p2); |
| 38 | return getDiagram(args); |
| 39 | }; |
| 40 | this.content = response.replace(/(fen:)([^:]*):/g, replaceByDiag); |
| 41 | }); |
| 42 | }, |
| 43 | methods: { |
| 44 | parseFen(fen) { |
| 45 | const fenParts = fen.split(" "); |
| 46 | return { |
| 47 | position: fenParts[0], |
| 48 | marks: fenParts[1], |
| 49 | orientation: fenParts[2], |
| 50 | shadow: fenParts[3], |
| 51 | }; |
| 52 | }, |
| 53 | startComputerGame: function() { |
| 54 | this.fen = V.GenRandInitFen(); |
| 55 | this.display = "computer"; |
| 56 | }, |
| 57 | }, |
| 58 | }) |