Saving current state
[vchess.git] / public / javascripts / variant.js
CommitLineData
f5d3e4f5 1new Vue({
b57dbd12 2 el: "#VueElement",
c794dbb8 3 data: {
baba6070 4 display: "undefined", //default to main hall; see "created()" function
298c42e6 5 gameid: "undefined", //...yet
81bc1102
BA
6
7 conn: null,
8
baba6070 9 // TEMPORARY: DEBUG
baba6070
BA
10 mode: "analyze",
11 orientation: "w",
12 userColor: "w",
c794dbb8 13 },
d449ae46 14 created: function() {
b6487fb9 15 // TODO: navigation becomes a little more complex
d449ae46
BA
16 const url = window.location.href;
17 const hashPos = url.indexOf("#");
baba6070
BA
18 const page = (hashPos >= 0 ? url.substr(hashPos+1) : "room");
19 this.setDisplay(page);
20
81bc1102
BA
21 this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant._id);
22 const socketCloseListener = () => {
23 this.conn = new WebSocket(url + "/?sid=" + this.myid + "&page=" + variant._id);
24 }
25 this.conn.onclose = socketCloseListener;
26
baba6070 27 this.vr = new VariantRules( V.GenRandInitFen() );
d449ae46 28 },
4ecf423b 29 methods: {
e6dcb115
BA
30 setDisplay: function(elt) {
31 this.display = elt;
b6487fb9 32 // Close menu on small screens:
a5d56686
BA
33 let menuToggle = document.getElementById("drawer-control");
34 if (!!menuToggle)
35 menuToggle.checked = false;
e6dcb115 36 },
baba6070
BA
37
38 // TEMPORARY: DEBUG (duplicate code)
39 play: function(move) {
40 // Not programmatic, or animation is over
41 if (!move.notation)
42 move.notation = this.vr.getNotation(move);
43 this.vr.play(move);
44 if (!move.fen)
45 move.fen = this.vr.getFen();
46 if (this.sound == 2)
47 new Audio("/sounds/move.mp3").play().catch(err => {});
48 // Is opponent in check?
49 this.incheck = this.vr.getCheckSquares(this.vr.turn);
50 const score = this.vr.getCurrentScore();
51 },
52 undo: function(move) {
53 this.vr.undo(move);
54 if (this.sound == 2)
55 new Audio("/sounds/undo.mp3").play().catch(err => {});
56 this.incheck = this.vr.getCheckSquares(this.vr.turn);
57 },
4ecf423b 58 },
f5d3e4f5 59});
81da2786 60
8d7e2786
BA
61//const continuation = (localStorage.getItem("variant") === variant.name);
62// if (continuation) //game VS human has priority
63// this.continueGame("human");
b6487fb9
BA
64
65// TODO:
66// si quand on arrive il y a une continuation "humaine" : display="game" et retour à la partie !