Saving current state
[vchess.git] / public / javascripts / variant.js
1 new Vue({
2 el: "#VueElement",
3 data: {
4 display: "undefined", //default to main hall; see "created()" function
5 gameid: "undefined", //...yet
6
7 conn: null,
8
9 // TEMPORARY: DEBUG
10 mode: "analyze",
11 orientation: "w",
12 userColor: "w",
13 },
14 created: function() {
15 // TODO: navigation becomes a little more complex
16 const url = window.location.href;
17 const hashPos = url.indexOf("#");
18 const page = (hashPos >= 0 ? url.substr(hashPos+1) : "room");
19 this.setDisplay(page);
20
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
27 this.vr = new VariantRules( V.GenRandInitFen() );
28 },
29 methods: {
30 setDisplay: function(elt) {
31 this.display = elt;
32 // Close menu on small screens:
33 let menuToggle = document.getElementById("drawer-control");
34 if (!!menuToggle)
35 menuToggle.checked = false;
36 },
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 },
58 },
59 });
60
61 //const continuation = (localStorage.getItem("variant") === variant.name);
62 // if (continuation) //game VS human has priority
63 // this.continueGame("human");
64
65 // TODO:
66 // si quand on arrive il y a une continuation "humaine" : display="game" et retour à la partie !