function displayShop(ret) { var tmphtml = "

Select an item to buy it:

"; for(var key in ret.items){ if(ret.items.hasOwnProperty(key)){ var category = ret.items[key]; tmphtml += "

"+key+"

"; 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); showInfo("Your shop has been successfully created"); }); } function addItem(ret) { var itemhtml = ""; var itemtag = document.querySelector("[data-name=\""+ret[0].name+"\"]"); if(!itemtag){ //si c'est la première itération de l'objet itemhtml += "
  • "; itemhtml += ""; itemhtml += "
  • "; } else{ // si c'est une n-ième itération itemtag.innerHTML=ret[1]; } 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("The "+ ret[0].name + " has been successfully purchased"); } function buyItem(name) { sendRequest("craftmine.php", "op=buyItem&item="+name, function(ret) { addItem(ret); debitAccount(ret[0].cost); }); } function useItem(name) { sendRequest("craftmine.php", "op=useItem&item="+name, function(ret) { if(parseInt(ret[1])>=0){ var nb = parseInt(document.getElementById("nbItem").innerHTML); if(nb>0){ nb--; switch(ret[0].name){ case "Life Bottle": data.hp = parseInt(data.hp) + 3; break; case "Strength Bottle" : break; // to do case "Wooden Sword" : break; //to do case "Metal Sword" : break; //to do } } document.getElementById("nbItem").innerHTML = nb; sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&xp="+data.xp+"&lv="+data.level, function(){ displayPerso(data.hp,data.xp,data.level); }); } }); }