Better Ball rules. Buggish but almost OK Synchrone variant
[vchess.git] / client / src / views / Analyse.vue
1 <template lang="pug">
2 main
3 .row
4 .col-sm-12
5 .text-center
6 input#fen(
7 v-model="curFen"
8 @input="adjustFenSize(); tryGotoFen()"
9 )
10 BaseGame(
11 ref="basegame"
12 :game="game"
13 @fenchange="setFen"
14 )
15 </template>
16
17 <script>
18 import BaseGame from "@/components/BaseGame.vue";
19 import { store } from "@/store";
20 export default {
21 name: "my-analyse",
22 components: {
23 BaseGame
24 },
25 // gameRef: to find the game in (potentially remote) storage
26 data: function() {
27 return {
28 st: store.state,
29 gameRef: {
30 //given in URL (rid = remote ID)
31 vname: "",
32 fen: ""
33 },
34 game: {
35 players: [{ name: "Analyse" }, { name: "Analyse" }],
36 mode: "analyze"
37 },
38 curFen: ""
39 };
40 },
41 watch: {
42 $route: function() {
43 this.initFromUrl();
44 }
45 },
46 created: function() {
47 this.initFromUrl();
48 },
49 methods: {
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 },
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 },
70 initialize: async function(orientation) {
71 // Obtain VariantRules object
72 await import("@/variants/" + this.gameRef.vname + ".js")
73 .then((vModule) => {
74 window.V = vModule[this.gameRef.vname + "Rules"];
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");
78 else this.loadGame(orientation);
79 })
80 .catch((err) => { this.alertAndQuit("Mispelled variant name", true); });
81 },
82 loadGame: function(orientation) {
83 this.game.vname = this.gameRef.vname;
84 this.game.fenStart = this.gameRef.fen;
85 this.game.fen = this.gameRef.fen;
86 this.game.score = "*"; //never change
87 this.curFen = this.game.fen;
88 this.adjustFenSize();
89 this.game.mycolor = orientation || V.ParseFen(this.gameRef.fen).turn;
90 this.$refs["basegame"].re_setVariables(this.game);
91 },
92 // Triggered by "fenchange" emitted in BaseGame:
93 setFen: function(fen) {
94 this.curFen = fen;
95 this.adjustFenSize();
96 },
97 adjustFenSize: function() {
98 let fenInput = document.getElementById("fen");
99 fenInput.style.width = (this.curFen.length+3) + "ch";
100 },
101 tryGotoFen: function() {
102 if (V.IsGoodFen(this.curFen)) {
103 this.gameRef.fen = this.curFen;
104 this.loadGame();
105 }
106 }
107 }
108 };
109 </script>
110
111 <style lang="sass" scoped=true>
112 input#fen
113 // Use a Monospace font for input FEN width adjustment
114 font-family: "Fira Code"
115 </style>