data = { name: "You", gold: 0, mine: 0, miners: 0, level: 4, hp: 5, icon : "H" } function sendRequest(url, params, callback) { var xhr = new XMLHttpRequest(); xhr.open("POST", url); xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") { var data = ""; if(xhr.responseText) data = JSON.parse(xhr.responseText); if(data.info) showInfo(data.info); if(data.error) { showError(data.error); return; } if(callback) callback(data); } } xhr.send(params); } function updateData() { for (var i = 0; i < arguments.length; i++) { document.getElementById(arguments[i]).innerHTML = data[arguments[i]]; } } function debitAccount(amount) { data.gold -= amount; updateData("gold"); } function withdrawMine() { sendRequest("craftmine.php", "op=withdrawMine&amount="+data.mine, function(xhr) { var gold = parseInt(xhr); // Server's response is a string if(isNaN(gold)) return; data.gold = gold; data.mine = 0; updateData("gold", "mine"); }) } 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); if(typeof ret.dungeon.mob == "undefined") displayDungeon(0,1,true); else displayDungeon(ret.dungeon.mob,ret.dungeon.flat,true); data.miners = parseInt(ret.miners); updateData("gold", "mine", "miners"); }) } function updateMine() { data.mine += (data.miners+1); updateData("mine"); } function init() { initCraftMine(); changeTab(); window.setInterval(updateMine, 1000); }