Add 'display' DB field for nicer variants display. Remove join on Variants table...
[vchess.git] / client / src / views / Rules.vue
CommitLineData
cf2343ce 1<template lang="pug">
7aa548e7
BA
2main
3 .row
4 .col-sm-12.col-md-10.col-md-offset-1.col-lg-8.col-lg-offset-2
5 .button-group
9ddaf8da 6 button(@click="clickReadRules()") {{ st.tr["Rules"] }}
910d631b
BA
7 button(
8 v-show="!gameInProgress"
9 @click="startGame('auto')"
10 )
f44fd3bf 11 | {{ st.tr["Example game"] }}
910d631b
BA
12 button(
13 v-show="!gameInProgress"
14 @click="startGame('versus')"
15 )
602d6bef 16 | {{ st.tr["Practice"] }}
910d631b
BA
17 button(
18 v-show="gameInProgress"
19 @click="stopGame()"
20 )
602d6bef 21 | {{ st.tr["Stop game"] }}
910d631b 22 button(
20620465 23 v-if="showAnalyzeBtn"
910d631b
BA
24 @click="gotoAnalyze()"
25 )
8055eabd 26 | {{ st.tr["Analysis mode"] }}
9bd6786b
BA
27 .row
28 .col-sm-12.col-md-8.col-md-offset-2.col-lg-6.col-lg-offset-3
eaa5ad3e 29 h4#variantName(v-show="display=='rules'") {{ getVariantDisplay }}
910d631b
BA
30 div(
31 v-show="display=='rules'"
32 v-html="content"
33 )
34 ComputerGame(
866842c3 35 ref="compgame"
910d631b
BA
36 v-show="display=='computer'"
37 :game-info="gameInfo"
910d631b
BA
38 @game-stopped="gameStopped"
39 )
cf2343ce
BA
40</template>
41
42<script>
a6088c90 43import ComputerGame from "@/components/ComputerGame.vue";
cf2343ce 44import { store } from "@/store";
07052665 45import { replaceByDiag } from "@/utils/printDiagram";
98b94cc3 46import { CompgameStorage } from "@/utils/compgameStorage";
70c9745d 47import afterRawLoad from "@/utils/afterRawLoad";
cf2343ce 48export default {
6808d7a1 49 name: "my-rules",
24340cae 50 components: {
6808d7a1 51 ComputerGame
24340cae 52 },
cf2343ce
BA
53 data: function() {
54 return {
55 st: store.state,
cf2343ce 56 display: "rules",
cf2343ce 57 gameInProgress: false,
6dd02928 58 // variables passed to ComputerGame:
834c202a 59 gameInfo: {
fcbc92c2 60 vname: "",
834c202a 61 mode: "versus",
b9139251
BA
62 },
63 V: null,
cf2343ce
BA
64 };
65 },
42eb4eaf 66 watch: {
6808d7a1 67 $route: function(newRoute) {
fcbc92c2 68 this.re_setVariant(newRoute.params["vname"]);
6808d7a1 69 }
42eb4eaf 70 },
6f093ada 71 created: function() {
e2732923 72 // NOTE: variant cannot be set before store is initialized
fcbc92c2
BA
73 this.re_setVariant(this.$route.params["vname"]);
74 },
75 computed: {
20620465 76 showAnalyzeBtn: function() {
84fc0f02 77 return !!this.V && this.V.CanAnalyze;
20620465 78 },
eaa5ad3e
BA
79 getVariantDisplay: function() {
80 if (!this.gameInfo.vname) return ""; //variant not set yet
81 return this.st.variants.find(v => v.name == this.gameInfo.vname).display;
82 },
fcbc92c2 83 content: function() {
6808d7a1 84 if (!this.gameInfo.vname) return ""; //variant not set yet
6808d7a1 85 return (
70c9745d
BA
86 afterRawLoad(
87 require(
88 "raw-loader!@/translations/rules/" +
89 this.gameInfo.vname + "/" + this.st.lang + ".pug"
8b3b2151 90 ).default
70c9745d 91 ).replace(/(fen:)([^:]*):/g, replaceByDiag)
6808d7a1
BA
92 );
93 }
cf2343ce
BA
94 },
95 methods: {
41cb9b94 96 clickReadRules: function() {
6808d7a1
BA
97 if (this.display != "rules") this.display = "rules";
98 else if (this.gameInProgress) this.display = "computer";
41cb9b94 99 },
fcbc92c2 100 re_setVariant: async function(vname) {
ba65b8de
BA
101 const key = "rr_" + vname;
102 if (!localStorage.getItem(key))
103 // Mark rules as "read"
104 localStorage.setItem(key, '1');
a97bdbda
BA
105 await import("@/variants/" + vname + ".js")
106 .then((vModule) => {
32f6285e 107 this.V = window.V = vModule[vname + "Rules"];
a97bdbda
BA
108 this.gameInfo.vname = vname;
109 })
110 .catch((err) => {
111 // Soon after component creation, st.tr might be uninitialized.
112 // Set a timeout to let a chance for the message to show translated.
113 const text = "Mispelled variant name";
114 setTimeout(() => {
115 alert(this.st.tr[text] || text);
116 this.$router.replace("/variants");
117 }, 500);
118 });
cf2343ce 119 },
834c202a 120 startGame: function(mode) {
6808d7a1 121 if (this.gameInProgress) return;
cf2343ce 122 this.gameInProgress = true;
cf2343ce 123 this.display = "computer";
834c202a 124 this.gameInfo.mode = mode;
98b94cc3
BA
125 if (this.gameInfo.mode == "versus") {
126 CompgameStorage.get(this.gameInfo.vname, (game) => {
996bdaa4
BA
127 // NOTE: game might be null (if none stored yet)
128 if (!!game && !V.IsGoodFen(game.fen)) {
129 // Some issues with stored game: delete
130 CompgameStorage.remove(game.vname);
131 game = null;
132 }
98b94cc3
BA
133 this.$refs["compgame"].launchGame(game);
134 });
98b94cc3 135 }
aa32568e 136 else this.$refs["compgame"].launchGame();
cf2343ce 137 },
d2edca6d 138 // The user wants to stop the game:
866842c3
BA
139 stopGame: function() {
140 this.$refs["compgame"].gameOver("?", "Undetermined result");
cf2343ce 141 },
41cb9b94
BA
142 // The game is effectively stopped:
143 gameStopped: function() {
144 this.gameInProgress = false;
98b94cc3
BA
145 if (this.gameInfo.mode == "versus")
146 CompgameStorage.remove(this.gameInfo.vname);
41cb9b94 147 },
5157ce0b 148 gotoAnalyze: function() {
6808d7a1 149 this.$router.push(
6b7b2cf7
BA
150 "/analyse/" + this.gameInfo.vname +
151 "/?fen=" + V.GenRandInitFen(this.st.settings.randomness)
6808d7a1
BA
152 );
153 }
154 }
cf2343ce
BA
155};
156</script>
50aed5a1 157
26d8a01a 158<!-- NOTE: not scoped here, because HTML is injected -->
5bcc9b31 159<style lang="sass">
26d8a01a
BA
160@import "@/styles/_board_squares_img.sass"
161@import "@/styles/_rules.sass"
162</style>
c7550017 163
26d8a01a
BA
164<style lang="sass" scoped>
165h4#variantName
50aed5a1 166 text-align: center
9a3049f3 167 font-weight: bold
50aed5a1 168</style>