From 2b0a84a41eeb90094d42c3c516bf99837d08a745 Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 27 Apr 2016 19:47:34 +0200 Subject: Automatically show received info --- js/craftmine.js | 2 ++ 1 file changed, 2 insertions(+) (limited to 'js') diff --git a/js/craftmine.js b/js/craftmine.js index 92fa45a..28583dd 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -12,6 +12,8 @@ function sendRequest(url, params, callback) { xhr.onreadystatechange = function() { if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") { var data = JSON.parse(xhr.responseText); + if(data.info) + showInfo(data.info); if(data.error) { showError(data.error); return; -- cgit v1.2.3-70-g09d2 From 0133251e523b1c984b2b5cdb60c7392c18e6bd73 Mon Sep 17 00:00:00 2001 From: piernov Date: Wed, 27 Apr 2016 19:51:32 +0200 Subject: Add button for saving on server/downloading the game --- index.xhtml | 2 ++ js/craftmine.js | 8 ++++++++ 2 files changed, 10 insertions(+) (limited to 'js') diff --git a/index.xhtml b/index.xhtml index aa29615..d12585f 100644 --- a/index.xhtml +++ b/index.xhtml @@ -32,6 +32,8 @@
+ +
diff --git a/js/craftmine.js b/js/craftmine.js index 92fa45a..13dd0a2 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -60,6 +60,14 @@ function updateMine() { updateData("mine"); } +function saveGame() { + sendRequest("craftmine.php", "op=saveGame"); +} + +function downGame() { + window.open("craftmine.php?op=downGame", "_blank"); +} + function init() { initCraftMine(); changeTab(); -- cgit v1.2.3-70-g09d2 From b60d56327a1b8531d15348034889e67f8b49444d Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 15:44:28 +0200 Subject: Allow empty data from server --- js/craftmine.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'js') diff --git a/js/craftmine.js b/js/craftmine.js index 8af8296..608efb7 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -11,7 +11,8 @@ function sendRequest(url, params, callback) { xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") { - var data = JSON.parse(xhr.responseText); + var data = ""; + if(xhr.responseText) data = JSON.parse(xhr.responseText); if(data.info) showInfo(data.info); if(data.error) { -- cgit v1.2.3-70-g09d2 From 6bff76321ab05d6193bc1d35327cb44128f9ed66 Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 15:45:44 +0200 Subject: Force reset mine on game reload --- js/craftmine.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'js') diff --git a/js/craftmine.js b/js/craftmine.js index 608efb7..d649305 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -50,10 +50,11 @@ function withdrawMine() { function initCraftMine() { sendRequest("craftmine.php", "op=getCraftMine", function(ret) { data.gold = parseInt(ret.gold); // Server's response is a string + data.mine = 0; // Reset mine if(ret.shop) displayShop(ret.shop); displayInventory(ret.inventory); data.miners = parseInt(ret.miners); - updateData("gold","miners"); + updateData("gold", "mine", "miners"); }) } -- cgit v1.2.3-70-g09d2 From f53549e4c2668ca268c8e31ef4c7cf3718fe08b5 Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 15:50:14 +0200 Subject: Move saveGame()/downGame() to its own JS file --- index.xhtml | 1 + js/craftmine.js | 8 -------- js/savegame.js | 7 +++++++ 3 files changed, 8 insertions(+), 8 deletions(-) create mode 100644 js/savegame.js (limited to 'js') diff --git a/index.xhtml b/index.xhtml index d12585f..e879cf1 100644 --- a/index.xhtml +++ b/index.xhtml @@ -15,6 +15,7 @@ +
diff --git a/js/craftmine.js b/js/craftmine.js index 13dd0a2..92fa45a 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -60,14 +60,6 @@ function updateMine() { updateData("mine"); } -function saveGame() { - sendRequest("craftmine.php", "op=saveGame"); -} - -function downGame() { - window.open("craftmine.php?op=downGame", "_blank"); -} - function init() { initCraftMine(); changeTab(); diff --git a/js/savegame.js b/js/savegame.js new file mode 100644 index 0000000..2a83f77 --- /dev/null +++ b/js/savegame.js @@ -0,0 +1,7 @@ +function saveGame() { + sendRequest("craftmine.php", "op=saveGame"); +} + +function downGame() { + window.open("craftmine.php?op=downSave", "_blank"); +} -- cgit v1.2.3-70-g09d2 From 8c0851a7d4db8be1bac99e034c745a45c64cc3ba Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 15:51:08 +0200 Subject: Add various JS functions to work with game save --- js/savegame.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'js') diff --git a/js/savegame.js b/js/savegame.js index 2a83f77..438be0a 100644 --- a/js/savegame.js +++ b/js/savegame.js @@ -1,3 +1,35 @@ +function listSaves() { + sendRequest("craftmine.php", "op=listSaves", function(ret) { + var tmphtml="" + for(var i=0; i" + ret[i] + "\n"; + } + console.log(tmphtml); + document.getElementById("listsaves").innerHTML = tmphtml; + }); +} + +function getCheckedSave() { + var radios = document.getElementsByName('saveRadio'); + for (var i = 0, length = radios.length; i < length; i++) { + if (radios[i].checked) return radios[i].parentNode.textContent; + } + return -1; +} + +function loadSave() { + sendRequest("craftmine.php", "op=loadSave&filename="+getCheckedSave(), function(ret) { + initCraftMine(); + }); +} + +function downloadSave() { + window.open("craftmine.php?op=downSave&filename="+getCheckedSave(), "_blank"); +} + +function deleteSave() { +} + function saveGame() { sendRequest("craftmine.php", "op=saveGame"); } -- cgit v1.2.3-70-g09d2 From 0b72114561a685b247922a93c576edabd4d3b09e Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 15:59:16 +0200 Subject: Add ability to delete a save Doesn't work because idk which method to use and I don't have the PHP doc right now --- craftmine.php | 1 + inc/messages.inc | 2 ++ inc/savegame.inc | 7 +++++++ js/savegame.js | 3 +++ 4 files changed, 13 insertions(+) (limited to 'js') diff --git a/craftmine.php b/craftmine.php index 28bb49d..9a5d3d7 100644 --- a/craftmine.php +++ b/craftmine.php @@ -51,6 +51,7 @@ switch($op) { case "downSave": downSave(); break; case "listSaves": listSaves(); break; case "loadSave": loadSave(); break; + case "deleteSave": deleteSave(); break; default: reportBadRequest(); } diff --git a/inc/messages.inc b/inc/messages.inc index 955c0a0..5574bcf 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -10,6 +10,8 @@ $messages = array( "gamesave_ok" => "Game saved.", "gamesave_error" => "An error occured when trying to save the game.", "gamesave_not_found" => "Couldn't find the specified save file.", + "gamesave_delete_error" => "Couldn't delete the specified save file.", + "gamesave_delete_success" => "Game save successfully removed from server", ); function sendMessage($type, $msg) { diff --git a/inc/savegame.inc b/inc/savegame.inc index 79b1dae..eb4134f 100644 --- a/inc/savegame.inc +++ b/inc/savegame.inc @@ -71,6 +71,13 @@ function parseSave($xml, &$table) { // Passing $table by reference } } +function deleteSave() { + if(empty($_POST["filename"])) return; + $filename = $_POST["filename"]; + if(rm_file(SAVEDIR + "/" + $filename)) sendError("gamesave_delete_failed"); // TODO: find the correct method for removing a fileā€¦ + else sendInfo("gamesave_delete_success"); +} + function loadSave() { if(empty($_POST["filename"])) return; $xml = simplexml_load_file(SAVEDIR . "/" . $_POST["filename"]); diff --git a/js/savegame.js b/js/savegame.js index 438be0a..4a08f91 100644 --- a/js/savegame.js +++ b/js/savegame.js @@ -28,6 +28,9 @@ function downloadSave() { } function deleteSave() { + sendRequest("craftmine.php", "op=deleteSave&filename="+getCheckedSave(), function(ret) { + listSaves(); + }); } function saveGame() { -- cgit v1.2.3-70-g09d2 From cbfbedcaceccda890a7f15cb806561ede72e8044 Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 2 May 2016 20:14:17 +0200 Subject: Allow multiple classes on tabs --- js/gui.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'js') diff --git a/js/gui.js b/js/gui.js index bdf1710..89fa0fa 100644 --- a/js/gui.js +++ b/js/gui.js @@ -6,10 +6,8 @@ function changeTab() { var id = window.location.hash.substr(4); var tabs = document.querySelectorAll("#tabs-panel > ul > li"); for(var i=0; i < tabs.length; i++) { - if(i == id-1) - tabs[i].className = "active"; - else - tabs[i].className = ""; + if(i == id-1) tabs[i].classList.add("active"); // Doesn't work with IE < 10 (and Opera Mini), but who cares? + else tabs[i].classList.remove("active"); } } -- cgit v1.2.3-70-g09d2