Play against computer almost OK: need to fix Board component
[vchess.git] / client / src / views / Rules.vue
CommitLineData
cf2343ce
BA
1<template lang="pug">
2.row
3 .col-sm-12 col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2
4 .button-group
5 button(@click="display='rules'") Read the rules
6 button(v-show="!gameInProgress" @click="watchComputerGame")
7 | Observe a sample game
8 button(v-show="!gameInProgress" @click="playAgainstComputer")
9 | Beat the computer!
10 button(v-show="gameInProgress" @click="stopGame")
11 | Stop game
12 div(v-show="display=='rules'" v-html="content" class="section-content")
c75838d9 13 Game(v-show="display=='computer'" :gid-or-fen="fen"
24340cae 14 :mode="mode" :sub-mode="subMode" :variant="variant"
cf2343ce
BA
15 @computer-think="gameInProgress=false" @game-over="stopGame")
16</template>
17
18<script>
19import Game from "@/components/Game.vue";
20import { store } from "@/store";
e2732923 21import { getDiagram } from "@/utils/printDiagram";
cf2343ce
BA
22export default {
23 name: 'my-rules',
24340cae
BA
24 components: {
25 Game,
26 },
cf2343ce
BA
27 data: function() {
28 return {
29 st: store.state,
e2732923 30 variant: {id: 0, name: "Unknown"}, //...yet
cf2343ce
BA
31 content: "",
32 display: "rules",
33 mode: "computer",
34 subMode: "", //'auto' for game CPU vs CPU
35 gameInProgress: false,
36 mycolor: "w",
37 fen: "",
38 };
39 },
e2732923
BA
40 mounted: async function() {
41 // NOTE: variant cannot be set before store is initialized
24340cae 42 const vname = this.$route.params["vname"];
e2732923
BA
43 //const idxOfVar = this.st.variants.indexOf(e => e.name == vname);
44 //this.variant = this.st.variants[idxOfVar]; //TODO: is it the right timing?!
45 this.variant.name = vname;
46 const vModule = await import("@/variants/" + vname + ".js");
47 window.V = vModule.VariantRules;
cf2343ce
BA
48 // Method to replace diagrams in loaded HTML
49 const replaceByDiag = (match, p1, p2) => {
50 const args = this.parseFen(p2);
51 return getDiagram(args);
52 };
53 // (AJAX) Request to get rules content (plain text, HTML)
54 this.content =
c75838d9 55 // TODO: why doesn't this work? require("raw-loader!pug-plain-loader!@/rules/"...)
e2732923 56 require("raw-loader!@/rules/" + vname + "/" + this.st.lang + ".pug")
cf2343ce
BA
57 .replace(/(fen:)([^:]*):/g, replaceByDiag);
58 },
59 methods: {
60 parseFen(fen) {
61 const fenParts = fen.split(" ");
62 return {
63 position: fenParts[0],
64 marks: fenParts[1],
65 orientation: fenParts[2],
66 shadow: fenParts[3],
67 };
68 },
69 startGame: function() {
70 if (this.gameInProgress)
71 return;
72 this.gameInProgress = true;
73 this.mode = "computer";
74 this.display = "computer";
75 this.fen = V.GenRandInitFen();
76 },
77 stopGame: function() {
78 this.gameInProgress = false;
79 this.mode = "analyze";
80 },
81 playAgainstComputer: function() {
82 this.subMode = "";
83 this.startGame();
84 },
85 watchComputerGame: function() {
86 this.subMode = "auto";
87 this.startGame();
88 },
89 },
90};
91</script>