Better fix to remove corrupted games vs computer
[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
f76dd684 29 h4#variantName(v-show="display=='rules'") {{ gameInfo.vname }}
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 },
fcbc92c2 79 content: function() {
6808d7a1 80 if (!this.gameInfo.vname) return ""; //variant not set yet
6808d7a1 81 return (
70c9745d
BA
82 afterRawLoad(
83 require(
84 "raw-loader!@/translations/rules/" +
85 this.gameInfo.vname + "/" + this.st.lang + ".pug"
8b3b2151 86 ).default
70c9745d 87 ).replace(/(fen:)([^:]*):/g, replaceByDiag)
6808d7a1
BA
88 );
89 }
cf2343ce
BA
90 },
91 methods: {
41cb9b94 92 clickReadRules: function() {
6808d7a1
BA
93 if (this.display != "rules") this.display = "rules";
94 else if (this.gameInProgress) this.display = "computer";
41cb9b94 95 },
fcbc92c2 96 re_setVariant: async function(vname) {
ba65b8de
BA
97 const key = "rr_" + vname;
98 if (!localStorage.getItem(key))
99 // Mark rules as "read"
100 localStorage.setItem(key, '1');
a97bdbda
BA
101 await import("@/variants/" + vname + ".js")
102 .then((vModule) => {
32f6285e 103 this.V = window.V = vModule[vname + "Rules"];
a97bdbda
BA
104 this.gameInfo.vname = vname;
105 })
106 .catch((err) => {
107 // Soon after component creation, st.tr might be uninitialized.
108 // Set a timeout to let a chance for the message to show translated.
109 const text = "Mispelled variant name";
110 setTimeout(() => {
111 alert(this.st.tr[text] || text);
112 this.$router.replace("/variants");
113 }, 500);
114 });
cf2343ce 115 },
834c202a 116 startGame: function(mode) {
6808d7a1 117 if (this.gameInProgress) return;
cf2343ce 118 this.gameInProgress = true;
cf2343ce 119 this.display = "computer";
834c202a 120 this.gameInfo.mode = mode;
98b94cc3
BA
121 if (this.gameInfo.mode == "versus") {
122 CompgameStorage.get(this.gameInfo.vname, (game) => {
996bdaa4
BA
123 // NOTE: game might be null (if none stored yet)
124 if (!!game && !V.IsGoodFen(game.fen)) {
125 // Some issues with stored game: delete
126 CompgameStorage.remove(game.vname);
127 game = null;
128 }
98b94cc3
BA
129 this.$refs["compgame"].launchGame(game);
130 });
98b94cc3 131 }
aa32568e 132 else this.$refs["compgame"].launchGame();
cf2343ce 133 },
d2edca6d 134 // The user wants to stop the game:
866842c3
BA
135 stopGame: function() {
136 this.$refs["compgame"].gameOver("?", "Undetermined result");
cf2343ce 137 },
41cb9b94
BA
138 // The game is effectively stopped:
139 gameStopped: function() {
140 this.gameInProgress = false;
98b94cc3
BA
141 if (this.gameInfo.mode == "versus")
142 CompgameStorage.remove(this.gameInfo.vname);
41cb9b94 143 },
5157ce0b 144 gotoAnalyze: function() {
6808d7a1 145 this.$router.push(
6b7b2cf7
BA
146 "/analyse/" + this.gameInfo.vname +
147 "/?fen=" + V.GenRandInitFen(this.st.settings.randomness)
6808d7a1
BA
148 );
149 }
150 }
cf2343ce
BA
151};
152</script>
50aed5a1 153
26d8a01a 154<!-- NOTE: not scoped here, because HTML is injected -->
5bcc9b31 155<style lang="sass">
26d8a01a
BA
156@import "@/styles/_board_squares_img.sass"
157@import "@/styles/_rules.sass"
158</style>
c7550017 159
26d8a01a
BA
160<style lang="sass" scoped>
161h4#variantName
50aed5a1 162 text-align: center
9a3049f3 163 font-weight: bold
50aed5a1 164</style>