diff options
Diffstat (limited to 'js/shop.js')
-rw-r--r-- | js/shop.js | 58 |
1 files changed, 45 insertions, 13 deletions
@@ -1,12 +1,20 @@ function displayShop(ret) { - var tmphtml = "<h4> Select an item to buy it:</h4>"; - tmphtml += "<ul class=\"list-inline\">"; - for(var i=0; i < ret.items.length; i++) { - tmphtml += "<li>"; - tmphtml += "<button type=\"submit\" class=\"btn btn-primary\" onclick=\"buyItem('" + ret.items[i].name + "')\"><span class=\"item-icon\">" + ret.items[i].icon + "</span><br />" + ret.items[i].name + "</button>"; - tmphtml += "</li>"; + var tmphtml = "<h3> Select an item to buy it:</h3>"; + for(var key in ret.items){ + if(ret.items.hasOwnProperty(key)){ + var category = ret.items[key]; + tmphtml += "<div class=\"row\">"; + tmphtml += "<h4>"+key+"</h4>"; + tmphtml += "<ul class=\"list-inline\">"; + for(var i=0; i < category.length; i++) { + tmphtml += "<li>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"buyItem('" + category[i].name + "')\"><span class=\"item-icon\">" + category[i].icon + "</span><br />" + category[i].name + "</button>"; + tmphtml += "</li>"; + } + tmphtml += "</ul>"; + tmphtml += "</div>"; + } } - tmphtml += "</ul>" document.getElementById("tab2").innerHTML = tmphtml; } @@ -20,14 +28,22 @@ 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 = "<li>"; - itemhtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"useItem('" + ret.name + "')\"><span class=\"item-icon\">" + ret.icon + "</span><br />" + ret.name + "</button>"; - itemhtml += "</li>"; - + 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 += "<li>"; + itemhtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"useItem('" + ret[0].name + "')\"><span class=\"item-icon\">" + ret[0].icon + "</span><br />" + ret[0].name + "(<span id=\"nbItem\" data-name=\""+ ret[0].name + "\">"+ret[1]+"</span>)</button>"; + itemhtml += "</li>"; + } + else{ // si c'est une n-ième itération + itemtag.innerHTML=ret[1]; + } + var invcontent = document.getElementById("tab3"); if(invcontent.children.length <= 1) @@ -40,18 +56,34 @@ function addItem(ret) { } else invcontent.getElementsByTagName('ul')[0].innerHTML += itemhtml; - showInfo(ret.desc); + 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.cost); + 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); + }); + } }); } |