Step toward a one-page application
[vchess.git] / public / javascripts / variant.js
index 606c1b9..5920c0c 100644 (file)
@@ -1,11 +1,13 @@
 new Vue({
        el: "#VueElement",
        data: {
-               display: "undefined", //default to main hall; see "created()" function
-               gameid: undefined, //...yet
-               queryHash: "",
+               display: "", //default to main hall; see "created()" function
+               gameRef: undefined, //...for now
+               probId: undefined,
                conn: null,
-
+               mode: "analyze",
+               allowChat: false,
+               allowMovelist: true,
                // Settings initialized with values from localStorage
                settings:       {
                        bcolor: localStorage["bcolor"] || "lichess",
@@ -15,35 +17,20 @@ new Vue({
                        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() {
+               window.onhashchange = this.setDisplay;
                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
-
+               // Our ID, always set (DB id if registered, collision-free random string otherwise)
+               this.myid = user.id || localStorage["myid"] || "anon-" + getRandString();
                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: {
                updateSettings: function(event) {
@@ -53,6 +40,11 @@ new Vue({
                                ? event.target.checked
                                : event.target.value;
                },
+               // Game is over, clear storage and put it in indexedDB
+               archiveGame: function() {
+                       // TODO: ...
+                       //clearStorage();
+               },
                setDisplay: function() {
                        // Prevent set display if there is a running game
                        if (!!localStorage["variant"])
@@ -61,7 +53,21 @@ new Vue({
                                location.hash = "#room"; //default
                        const hashParts = location.hash.substr(1).split("?");
                        this.display = hashParts[0];
-                       this.queryHash = hashParts[1]; //may be empty, undefined...
+                       if (hashParts[0] == "problems" && !!hashParts[1])
+                       {
+                               // Show a specific problem
+                               this.probId = hashParts[1].split("=")[1];
+                       }
+                       else if (hashParts[0] == "game" && !!hashParts[1])
+                       {
+                               // Show a specific game, maybe with a user ID
+                               const params = hashParts[1].split("&").filter(h => h.split("=")[1]);
+                               // TODO: Vue.set(...) probably required here
+                               this.gameRef = {
+                                       id: params[0],
+                                       uid: params[1], //may be undefined
+                               };
+                       }
                        // Close menu on small screens:
                        let menuToggle = document.getElementById("drawer-control");
                        if (!!menuToggle)
@@ -69,7 +75,3 @@ new Vue({
                },
        },
 });
-               
-//const continuation = (localStorage.getItem("variant") === variant.name);
-//                     if (continuation) //game VS human has priority
-//                             this.continueGame("human");