aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--inc/Inventory.inc14
-rw-r--r--inc/shop.inc4
-rw-r--r--js/shop.js19
3 files changed, 27 insertions, 10 deletions
diff --git a/inc/Inventory.inc b/inc/Inventory.inc
index efe54f2..3396eb9 100644
--- a/inc/Inventory.inc
+++ b/inc/Inventory.inc
@@ -20,12 +20,21 @@ class Inventory {
}
private function _addItem($item) {
- $this->items[] = $item;
+ $counter = 0;
+ foreach($this->items as $object){
+ if($object[0] == $item){
+ $counter++;
+ }
+ }
+ $tab = array($item,$counter+1);
+ $this->items[] = $tab;
+ return $tab;
}
public static function addItem($item) {
$inv = self::get();
- $inv->_addItem($item);
+ $tab = $inv->_addItem($item);
+ return $tab;
}
private function _removeItem($item) {
@@ -36,6 +45,7 @@ class Inventory {
$inv = self::get();
$inv->_removeItem($item);
}
+
}
?>
diff --git a/inc/shop.inc b/inc/shop.inc
index 6e30923..83b08b0 100644
--- a/inc/shop.inc
+++ b/inc/shop.inc
@@ -56,8 +56,8 @@ function buildShop() {
function buyItem() {
$item = getItem($_POST["item"]);
if($item && debitAccount($item->cost)) {
- Inventory::addItem($item);
- echo json_encode($item);
+ $tab = Inventory::addItem($item);
+ echo json_encode($tab); //renvoyer un tableau avec comme première entrée $item et comme deuxième entrée le nombre
}
}
diff --git a/js/shop.js b/js/shop.js
index efd6ac5..00f13af 100644
--- a/js/shop.js
+++ b/js/shop.js
@@ -28,14 +28,21 @@ 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 = "";
+ if(ret[1]==1){ //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=\"nbObjet\">"+ret[1]+"</span>)</button>";
+ itemhtml += "</li>";
+ }
+ else if(ret[1]>1){ // si c'est une n-ième itération
+ document.getElementById("nbObjet").innerHTML=ret[1];
+ }
+
var invcontent = document.getElementById("tab3");
if(invcontent.children.length <= 1)
@@ -48,13 +55,13 @@ function addItem(ret) {
} else
invcontent.getElementsByTagName('ul')[0].innerHTML += itemhtml;
- showInfo("The "+ ret.name + " has been successfully purchased");
+ 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);
});
}