Computer mode in rules section almost OK
[vchess.git] / public / javascripts / components / rules.js
CommitLineData
92342261 1// Load rules on variant page
1d184b4c 2Vue.component('my-rules', {
582df349 3 props: ["settings"],
1d184b4c 4 data: function() {
582df349
BA
5 return {
6 content: "",
7 display: "rules",
8 mode: "computer",
9 mycolor: "w",
10 allowMovelist: true,
11 fen: "",
12 };
1d184b4c 13 },
582df349
BA
14
15 // TODO: third button "see a sample game" (comp VS comp)
16
a5d56686
BA
17 template: `
18 <div class="col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2">
582df349
BA
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>
a5d56686
BA
31 </div>
32 `,
1d184b4c
BA
33 mounted: function() {
34 // AJAX request to get rules content (plain text, HTML)
8d7e2786 35 ajax("/rules/" + variant.name, "GET", response => {
da06a6eb 36 let replaceByDiag = (match, p1, p2) => {
7931e479 37 const args = this.parseFen(p2);
da06a6eb
BA
38 return getDiagram(args);
39 };
7931e479
BA
40 this.content = response.replace(/(fen:)([^:]*):/g, replaceByDiag);
41 });
1d184b4c
BA
42 },
43 methods: {
da06a6eb
BA
44 parseFen(fen) {
45 const fenParts = fen.split(" ");
46 return {
47 position: fenParts[0],
48 marks: fenParts[1],
49 orientation: fenParts[2],
5915f720 50 shadow: fenParts[3],
da06a6eb 51 };
1d184b4c 52 },
582df349
BA
53 startComputerGame: function() {
54 this.fen = V.GenRandInitFen();
55 this.display = "computer";
56 },
1d184b4c
BA
57 },
58})