From fd2ef569b36f98c00ee7e9953f0041d6dff30316 Mon Sep 17 00:00:00 2001 From: Kevin Ramharak Date: Wed, 3 Jan 2018 05:57:00 +0100 Subject: [PATCH 1/5] Implement lazySave Saves on change with a delay off `3000` ms. Resets the timer when another change fires withing those `3000` ms --- mar/editor.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/mar/editor.js b/mar/editor.js index 9c5b0c9..2011927 100644 --- a/mar/editor.js +++ b/mar/editor.js @@ -404,4 +404,34 @@ function editorClick() { document.getElementById("gameBtns").setAttribute("style", "display: none"); } +/* + im using a closure because i don't like putting everything in global scope + feel free to disagree +*/ +var lazySave = (function() { + if(typeof window.localStorage === 'undefined') { + return function() {}; // if browser does not support local storage make it a no op (no idea what browsers) + } + + // store a reference to the timeout + var DELAY = 3000; + var timeout = null; + + // Basicly on every change set a timeout that will wait DELAY milliseconds before storing the code. + // This will prevent unnececary saving on every change + return function(event) { + // if another timeout was waiting clear that one + if(timeout === null) { + clearTimeout(timeout); + } + // and set a new one + timeout = setTimeout(function() { + window.localStorage.setItem('editorCodeContents', ace.edit("editor").getValue()); + timeout = null; // clear the timeout cached variable after its function has run + }, DELAY); + }; +})(); + editor.on("change", parse); +// You can add multiple handlers on events +editor.on("change", lazySave); From dae48c0ded3d35ccc4e802bd22fdb278e76a574a Mon Sep 17 00:00:00 2001 From: Kevin Ramharak Date: Wed, 3 Jan 2018 06:00:17 +0100 Subject: [PATCH 2/5] implements #20 with #fd2ef569b36f98c00ee7e9953f0041d6dff30316 fixes #20 --- mar/phaser/mar.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mar/phaser/mar.js b/mar/phaser/mar.js index fc453ea..406ff2e 100644 --- a/mar/phaser/mar.js +++ b/mar/phaser/mar.js @@ -803,16 +803,16 @@ function codeListener(message) { * Listens for authentications responses from the server */ function authListener(message) { - - if (message.t === "auth") { - - if (message.m === "ok") { - console.log("Auth successful"); - mar.client.requestUserInfo(); - - } else { - alert("Authentication failed. Please make sure you are logged in and reload the page."); + if (message.t === "code") { + var code = message.code; + if(typeof window.localStorage !== 'undefined') { // localStorage is supported + var value = window.localStorage.getItem("editorCodeContents"); + // if item does not exist null is returned + if(value !== null) { + code = value; + } } + ace.edit("editor").setValue(code); } } From dac61bf051adb47999cecf1cb78adb54c53af22b Mon Sep 17 00:00:00 2001 From: Kevin Ramharak Date: Wed, 3 Jan 2018 06:04:50 +0100 Subject: [PATCH 3/5] Fixed wrong equality check `===` changed to `!==` I should take a break. --- mar/editor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mar/editor.js b/mar/editor.js index 2011927..2ef473b 100644 --- a/mar/editor.js +++ b/mar/editor.js @@ -421,7 +421,7 @@ var lazySave = (function() { // This will prevent unnececary saving on every change return function(event) { // if another timeout was waiting clear that one - if(timeout === null) { + if(timeout !== null) { clearTimeout(timeout); } // and set a new one From 2a0c3992f4cefb3f88ceaf5584b5127e18e6a4fc Mon Sep 17 00:00:00 2001 From: Kevin Ramharak Date: Wed, 3 Jan 2018 06:13:31 +0100 Subject: [PATCH 4/5] reverted authListener changed the correct function Ok. actually fixes #20 without breaking other stuff. --- mar/phaser/mar.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/mar/phaser/mar.js b/mar/phaser/mar.js index 406ff2e..2eeb37c 100644 --- a/mar/phaser/mar.js +++ b/mar/phaser/mar.js @@ -791,18 +791,6 @@ function manhanttanDistance(x1, y1, x2, y2) { } function codeListener(message) { - - if (message.t === "code") { - - ace.edit("editor").setValue(message.code); - - } -} - -/** - * Listens for authentications responses from the server - */ -function authListener(message) { if (message.t === "code") { var code = message.code; if(typeof window.localStorage !== 'undefined') { // localStorage is supported @@ -816,6 +804,22 @@ function authListener(message) { } } +/** + * Listens for authentications responses from the server + */ +function authListener(message) { + if (message.t === "auth") { + + if (message.m === "ok") { + console.log("Auth successful"); + mar.client.requestUserInfo(); + + } else { + alert("Authentication failed. Please make sure you are logged in and reload the page."); + } + } +} + function codeResponseListener(message) { if (message.t === "codeResponse") { From 85ad98e7f5043a92de813f9c2f2830ba599a00e3 Mon Sep 17 00:00:00 2001 From: KevinRamharak Date: Wed, 3 Jan 2018 13:45:13 +0100 Subject: [PATCH 5/5] Changed to load local code only once on startup. If it fails it falls back to current behaviour --- mar/phaser/mar.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/mar/phaser/mar.js b/mar/phaser/mar.js index 2eeb37c..974e23a 100644 --- a/mar/phaser/mar.js +++ b/mar/phaser/mar.js @@ -792,15 +792,7 @@ function manhanttanDistance(x1, y1, x2, y2) { function codeListener(message) { if (message.t === "code") { - var code = message.code; - if(typeof window.localStorage !== 'undefined') { // localStorage is supported - var value = window.localStorage.getItem("editorCodeContents"); - // if item does not exist null is returned - if(value !== null) { - code = value; - } - } - ace.edit("editor").setValue(code); + ace.edit("editor").setValue(message.code); } } @@ -808,6 +800,7 @@ function codeListener(message) { * Listens for authentications responses from the server */ function authListener(message) { + if (message.t === "auth") { if (message.m === "ok") { @@ -1039,7 +1032,15 @@ var GameClient = function (callback) { }; - self.reloadCode(); + // check if we can load code locally first + try { + var value = window.localStorage.getItem("editorCodeContents"); + if(value === null) throw new TypeError("no code stored locally under key: 'editorCodeContents'") + ace.edit("editor").setValue(value); + + } catch(e) { + self.reloadCode(); + } if (callback !== undefined) { @@ -1156,11 +1157,11 @@ BasicGame.Boot.prototype = { game.camera.x = 280; game.camera.y = 90; game.stage.disableVisibilityChange = true; - + this.scale.scaleMode = Phaser.ScaleManager.RESIZE; this.scale.pageAlignHorizontally = true; this.scale.pageAlignVertically = true; - + }, create: function () { @@ -1183,7 +1184,7 @@ BasicGame.Boot.prototype = { }, update: function () { game.scale.setShowAll(); - game.scale.refresh(); + game.scale.refresh(); // Update the cursor position. // It's important to understand that screen-to-isometric projection means you have to specify a z position manually, as this cannot be easily // determined from the 2D pointer position without extra trickery. By default, the z position is 0 if not set.