Computer mode in rules section almost OK
[vchess.git] / public / javascripts / variant.js
index 736d8b3..606c1b9 100644 (file)
@@ -1,19 +1,67 @@
 new Vue({
-       el: "#variantPage",
+       el: "#VueElement",
        data: {
-               display: "room", //default: main hall
-               gameid: "undefined", //...yet
+               display: "undefined", //default to main hall; see "created()" function
+               gameid: undefined, //...yet
+               queryHash: "",
+               conn: null,
+
+               // Settings initialized with values from localStorage
+               settings:       {
+                       bcolor: localStorage["bcolor"] || "lichess",
+                       sound: parseInt(localStorage["sound"]) || 2,
+                       hints: parseInt(localStorage["hints"]) || 1,
+                       coords: !!eval(localStorage["coords"]),
+                       highlight: !!eval(localStorage["highlight"]),
+                       sqSize: parseInt(localStorage["sqSize"]),
+               },
+
+               // TEMPORARY: DEBUG
+               mode: "analyze",
+               orientation: "w",
+               userColor: "w",
+               allowChat: false,
+               allowMovelist: true,
+               fen: V.GenRandInitFen(),
        },
        created: function() {
-               // TODO: navigation becomes a little more complex
-               const url = window.location.href;
-               const hashPos = url.indexOf("#");
-               if (hashPos >= 0)
-                       this.setDisplay(url.substr(hashPos+1));
+               if (!!localStorage["variant"])
+               {
+                       location.hash = "#game?id=" + localStorage["gameId"];
+                       this.display = location.hash.substr(1);
+               }
+               else
+                       this.setDisplay();
+               window.onhashchange = this.setDisplay;
+               this.myid = "abcdefghij";
+//console.log(this.myid + " " + variant);
+                       //myid: localStorage.getItem("myid"), //our ID, always set
+
+               this.conn = new WebSocket(socketUrl + "/?sid=" + this.myid + "&page=" + variant.id);
+               const socketCloseListener = () => {
+                       this.conn = new WebSocket(socketUrl + "/?sid=" + this.myid + "&page=" + variant.id);
+               }
+               this.conn.onclose = socketCloseListener;
+
+               //this.vr = new VariantRules( V.GenRandInitFen() );
        },
        methods: {
-               setDisplay: function(elt) {
-                       this.display = elt;
+               updateSettings: function(event) {
+                       const propName =
+                               event.target.id.substr(3).replace(/^\w/, c => c.toLowerCase())
+                       localStorage[propName] = ["highlight","coords"].includes(propName)
+                               ? event.target.checked
+                               : event.target.value;
+               },
+               setDisplay: function() {
+                       // Prevent set display if there is a running game
+                       if (!!localStorage["variant"])
+                               return;
+                       if (!location.hash)
+                               location.hash = "#room"; //default
+                       const hashParts = location.hash.substr(1).split("?");
+                       this.display = hashParts[0];
+                       this.queryHash = hashParts[1]; //may be empty, undefined...
                        // Close menu on small screens:
                        let menuToggle = document.getElementById("drawer-control");
                        if (!!menuToggle)
@@ -21,6 +69,7 @@ new Vue({
                },
        },
 });
-
-// TODO:
-// si quand on arrive il y a une continuation "humaine" : display="game" et retour à la partie !
+               
+//const continuation = (localStorage.getItem("variant") === variant.name);
+//                     if (continuation) //game VS human has priority
+//                             this.continueGame("human");