Fix autoplay mode
[vchess.git] / client / src / views / Analyse.vue
CommitLineData
7f3484bd 1<template lang="pug">
7aa548e7 2main
63ca2b89
BA
3 .row
4 .col-sm-12
9a3049f3 5 .text-center
910d631b
BA
6 input#fen(
7 v-model="curFen"
725da57f 8 @input="adjustFenSize(); tryGotoFen()"
910d631b 9 )
910d631b 10 BaseGame(
b1e46b33 11 ref="basegame"
910d631b 12 :game="game"
8055eabd 13 @fenchange="setFen"
910d631b 14 )
7f3484bd
BA
15</template>
16
17<script>
18import BaseGame from "@/components/BaseGame.vue";
7f3484bd 19import { store } from "@/store";
7f3484bd 20export default {
6808d7a1 21 name: "my-analyse",
7f3484bd 22 components: {
6808d7a1 23 BaseGame
7f3484bd
BA
24 },
25 // gameRef: to find the game in (potentially remote) storage
26 data: function() {
27 return {
28 st: store.state,
6808d7a1
BA
29 gameRef: {
30 //given in URL (rid = remote ID)
652f40de
BA
31 vname: "",
32 fen: ""
33 },
34 game: {
6808d7a1
BA
35 players: [{ name: "Analyse" }, { name: "Analyse" }],
36 mode: "analyze"
7f3484bd 37 },
6808d7a1 38 curFen: ""
7f3484bd
BA
39 };
40 },
b1e46b33
BA
41 watch: {
42 $route: function() {
43 this.initFromUrl();
a97bdbda 44 }
7f3484bd 45 },
b1e46b33
BA
46 created: function() {
47 this.initFromUrl();
48 },
7f3484bd 49 methods: {
a97bdbda
BA
50 alertAndQuit: function(text, wrongVname) {
51 // Soon after component creation, st.tr might be uninitialized.
52 // Set a timeout to let a chance for the message to show translated.
53 const newUrl = "/variants" + (wrongVname ? "" : "/" + this.gameRef.vname);
54 setTimeout(() => {
55 alert(this.st.tr[text] || text);
56 this.$router.replace(newUrl);
57 }, 500);
58 },
b1e46b33
BA
59 initFromUrl: function() {
60 this.gameRef.vname = this.$route.params["vname"];
61 const routeFen = this.$route.query["fen"];
62 if (!routeFen) this.alertAndQuit("Missing FEN");
63 else {
64 this.gameRef.fen = routeFen.replace(/_/g, " ");
65 // orientation is optional: taken from FEN if missing
66 const orientation = this.$route.query["side"];
67 this.initialize(orientation);
68 }
69 },
7ba4a5bc 70 initialize: async function(orientation) {
5157ce0b 71 // Obtain VariantRules object
a97bdbda
BA
72 await import("@/variants/" + this.gameRef.vname + ".js")
73 .then((vModule) => {
32f6285e 74 window.V = vModule[this.gameRef.vname + "Rules"];
a97bdbda
BA
75 if (!V.CanAnalyze)
76 // Late check, in case the user tried to enter URL by hand
77 this.alertAndQuit("Analysis disabled for this variant");
7ba4a5bc 78 else this.loadGame(orientation);
a97bdbda
BA
79 })
80 .catch((err) => { this.alertAndQuit("Mispelled variant name", true); });
5157ce0b 81 },
7ba4a5bc 82 loadGame: function(orientation) {
652f40de 83 this.game.vname = this.gameRef.vname;
b1e46b33 84 this.game.fenStart = this.gameRef.fen;
652f40de 85 this.game.fen = this.gameRef.fen;
0234201f 86 this.game.score = "*"; //never change
5157ce0b
BA
87 this.curFen = this.game.fen;
88 this.adjustFenSize();
7ba4a5bc 89 this.game.mycolor = orientation || V.ParseFen(this.gameRef.fen).turn;
b1e46b33 90 this.$refs["basegame"].re_setVariables(this.game);
7f3484bd 91 },
8055eabd
BA
92 // Triggered by "fenchange" emitted in BaseGame:
93 setFen: function(fen) {
94 this.curFen = fen;
95 this.adjustFenSize();
96 },
5157ce0b
BA
97 adjustFenSize: function() {
98 let fenInput = document.getElementById("fen");
3a2a7b5f 99 fenInput.style.width = (this.curFen.length+1) + "ch";
5157ce0b 100 },
725da57f 101 tryGotoFen: function() {
3a2a7b5f 102 if (V.IsGoodFen(this.curFen)) {
725da57f
BA
103 this.gameRef.fen = this.curFen;
104 this.loadGame();
105 }
6808d7a1
BA
106 }
107 }
7f3484bd
BA
108};
109</script>