c1ba85736dd39ad6b795fa12062f809956e9c01c
[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 allowChat: false,
15 allowMovelist: true,
16 fen: V.GenRandInitFen(),
17 },
18 created: function() {
19 // TODO: navigation becomes a little more complex
20 this.setDisplay();
21 window.onhashchange = this.setDisplay;
22
23 this.myid = "abcdefghij";
24 //console.log(this.myid + " " + variant);
25 //myid: localStorage.getItem("myid"), //our ID, always set
26
27 this.conn = new WebSocket(socketUrl + "/?sid=" + this.myid + "&page=" + variant.id);
28 const socketCloseListener = () => {
29 this.conn = new WebSocket(socketUrl + "/?sid=" + this.myid + "&page=" + variant.id);
30 }
31 this.conn.onclose = socketCloseListener;
32
33 //this.vr = new VariantRules( V.GenRandInitFen() );
34 },
35 methods: {
36 setDisplay: function() {
37
38 //TODO: prevent set display if there is a running game
39
40 if (!location.hash)
41 location.hash = "#room"; //default
42 this.display = location.hash.substr(1);
43 // Close menu on small screens:
44 let menuToggle = document.getElementById("drawer-control");
45 if (!!menuToggle)
46 menuToggle.checked = false;
47 },
48
49 // TEMPORARY: DEBUG (duplicate code)
50 play: function(move) {
51 // Not programmatic, or animation is over
52 if (!move.notation)
53 move.notation = this.vr.getNotation(move);
54 this.vr.play(move);
55 if (!move.fen)
56 move.fen = this.vr.getFen();
57 if (this.sound == 2)
58 new Audio("/sounds/move.mp3").play().catch(err => {});
59 // Is opponent in check?
60 this.incheck = this.vr.getCheckSquares(this.vr.turn);
61 const score = this.vr.getCurrentScore();
62 },
63 undo: function(move) {
64 this.vr.undo(move);
65 if (this.sound == 2)
66 new Audio("/sounds/undo.mp3").play().catch(err => {});
67 this.incheck = this.vr.getCheckSquares(this.vr.turn);
68 },
69 },
70 });
71
72 //const continuation = (localStorage.getItem("variant") === variant.name);
73 // if (continuation) //game VS human has priority
74 // this.continueGame("human");
75
76 // TODO:
77 // si quand on arrive il y a une continuation "humaine" : display="game" et retour à la partie !