Experimental symmetric randomness + deterministic option
[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
BA
10 BaseGame(
11 :game="game"
8055eabd 12 @fenchange="setFen"
910d631b 13 )
7f3484bd
BA
14</template>
15
16<script>
17import BaseGame from "@/components/BaseGame.vue";
7f3484bd 18import { store } from "@/store";
7f3484bd 19export default {
6808d7a1 20 name: "my-analyse",
7f3484bd 21 components: {
6808d7a1 22 BaseGame
7f3484bd
BA
23 },
24 // gameRef: to find the game in (potentially remote) storage
25 data: function() {
26 return {
27 st: store.state,
6808d7a1
BA
28 gameRef: {
29 //given in URL (rid = remote ID)
652f40de
BA
30 vname: "",
31 fen: ""
32 },
33 game: {
6808d7a1
BA
34 players: [{ name: "Analyse" }, { name: "Analyse" }],
35 mode: "analyze"
7f3484bd 36 },
6808d7a1 37 curFen: ""
7f3484bd
BA
38 };
39 },
8055eabd
BA
40 // NOTE: no watcher for $route change, because if fenStart doesn't change
41 // then it doesn't trigger BaseGame.re_init() and the result is weird.
7f3484bd 42 created: function() {
652f40de 43 this.gameRef.vname = this.$route.params["vname"];
a97bdbda
BA
44 const routeFen = this.$route.query["fen"];
45 if (!routeFen) this.alertAndQuit("Missing FEN");
46 else {
47 this.gameRef.fen = routeFen.replace(/_/g, " ");
7ba4a5bc
BA
48 // orientation is optional: taken from FEN if missing
49 const orientation = this.$route.query["side"];
50 this.initialize(orientation);
a97bdbda 51 }
7f3484bd
BA
52 },
53 methods: {
a97bdbda
BA
54 alertAndQuit: function(text, wrongVname) {
55 // Soon after component creation, st.tr might be uninitialized.
56 // Set a timeout to let a chance for the message to show translated.
57 const newUrl = "/variants" + (wrongVname ? "" : "/" + this.gameRef.vname);
58 setTimeout(() => {
59 alert(this.st.tr[text] || text);
60 this.$router.replace(newUrl);
61 }, 500);
62 },
7ba4a5bc 63 initialize: async function(orientation) {
5157ce0b 64 // Obtain VariantRules object
a97bdbda
BA
65 await import("@/variants/" + this.gameRef.vname + ".js")
66 .then((vModule) => {
67 window.V = vModule.VariantRules;
68 if (!V.CanAnalyze)
69 // Late check, in case the user tried to enter URL by hand
70 this.alertAndQuit("Analysis disabled for this variant");
7ba4a5bc 71 else this.loadGame(orientation);
a97bdbda
BA
72 })
73 .catch((err) => { this.alertAndQuit("Mispelled variant name", true); });
5157ce0b 74 },
7ba4a5bc 75 loadGame: function(orientation) {
0e16cb26 76 // NOTE: no need to set score (~unused)
652f40de
BA
77 this.game.vname = this.gameRef.vname;
78 this.game.fen = this.gameRef.fen;
5157ce0b
BA
79 this.curFen = this.game.fen;
80 this.adjustFenSize();
7ba4a5bc 81 this.game.mycolor = orientation || V.ParseFen(this.gameRef.fen).turn;
77c50966 82 this.$set(this.game, "fenStart", this.gameRef.fen);
7f3484bd 83 },
8055eabd
BA
84 // Triggered by "fenchange" emitted in BaseGame:
85 setFen: function(fen) {
86 this.curFen = fen;
87 this.adjustFenSize();
88 },
5157ce0b
BA
89 adjustFenSize: function() {
90 let fenInput = document.getElementById("fen");
91 fenInput.style.width = this.curFen.length + "ch";
92 },
725da57f
BA
93 tryGotoFen: function() {
94 if (V.IsGoodFen(this.curFen))
95 {
96 this.gameRef.fen = this.curFen;
97 this.loadGame();
98 }
6808d7a1
BA
99 }
100 }
7f3484bd
BA
101};
102</script>