From 5ff6c7051867ed14ecbbc391709f25106c1bef1f Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:27:33 +0200 Subject: Add client side JS shop functions --- js/craftmine.js | 7 ++++--- js/shop.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 js/shop.js diff --git a/js/craftmine.js b/js/craftmine.js index 912d10c..dc4ee9a 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -44,9 +44,10 @@ function withdrawMine() { } function initCraftMine() { - sendRequest("craftmine.php", "op=getCraftMine", function(xhr) { - var ret = xhr; - data.gold = parseInt(ret); // Server's response is a string + 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); updateData("gold"); }) } diff --git a/js/shop.js b/js/shop.js new file mode 100644 index 0000000..00636e1 --- /dev/null +++ b/js/shop.js @@ -0,0 +1,57 @@ +function displayShop(ret) { + var tmphtml = "

Select an item to buy it:

"; + tmphtml += "" + document.getElementById("tab2").innerHTML = tmphtml; +} + +function displayInventory(items) { + for(var i=0; i < items.length; i++) { + addItem(items[i]); + } +} + +function buildShop() { + sendRequest("craftmine.php", "op=buildShop", function(ret) { + displayShop(ret); + debitAccount(ret.cost); + }); +} + +function addItem(ret) { + var itemhtml = "
  • "; + itemhtml += ""; + itemhtml += "
  • "; + + var invcontent = document.getElementById("tab3"); + + if(invcontent.children.length <= 1) + { + var tmphtml = "

    Your bag contains the following items:

    "; + tmphtml += "" + invcontent.innerHTML = tmphtml; + } else + invcontent.getElementsByTagName('ul')[0].innerHTML += itemhtml; + + showInfo(ret.desc); +} + +function buyItem(name) { + sendRequest("craftmine.php", "op=buyItem&item="+name, function(ret) { + addItem(ret); + debitAccount(ret.cost); + }); +} + +function useItem(name) { + sendRequest("craftmine.php", "op=useItem&item="+name, function(ret) { + }); +} + -- cgit v1.2.3-70-g09d2