diff options
Diffstat (limited to 'js/craftmine.js')
-rw-r--r-- | js/craftmine.js | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/js/craftmine.js b/js/craftmine.js index bbb1074..92fa45a 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -1,7 +1,7 @@ data = { gold: 0, mine: 0, - miners: 1, + miners: 0, level: 1 } @@ -11,7 +11,12 @@ 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") { - callback(xhr); + var data = JSON.parse(xhr.responseText); + if(data.error) { + showError(data.error); + return; + } + if(callback) callback(data); } } xhr.send(params); @@ -24,9 +29,14 @@ function updateData() { } } +function debitAccount(amount) { + data.gold -= amount; + updateData("gold"); +} + function withdrawMine() { sendRequest("craftmine.php", "op=withdrawMine&amount="+data.mine, function(xhr) { - var gold = parseInt(xhr.responseText); // Server's response is a string + var gold = parseInt(xhr); // Server's response is a string if(isNaN(gold)) return; data.gold = gold; data.mine = 0; @@ -35,10 +45,12 @@ function withdrawMine() { } function initCraftMine() { - sendRequest("craftmine.php", "op=getCraftMine", function(xhr) { - var ret = xhr.responseText; - data.gold = parseInt(ret); // Server's response is a string - updateData("gold"); + sendRequest("craftmine.php", "op=getCraftMine", function(ret) { + data.gold = parseInt(ret.gold); // Server's response is a string + if(ret.shop) displayShop(ret.shop); + displayInventory(ret.inventory); + data.miners = parseInt(ret.miners); + updateData("gold","miners"); }) } @@ -50,5 +62,6 @@ function updateMine() { function init() { initCraftMine(); + changeTab(); window.setInterval(updateMine, 1000); } |