From 79cfbb29042fd60dfbc76a6810f75cce21d3ffe0 Mon Sep 17 00:00:00 2001 From: alexichi Date: Sat, 7 May 2016 12:08:36 +0200 Subject: add the use of the life bottle --- craftmine.php | 1 + inc/Inventory.inc | 17 +++++++++++++++++ inc/perso.inc | 2 +- inc/shop.inc | 6 ++++++ js/craftmine.js | 2 +- js/dungeon.js | 3 ++- js/shop.js | 18 +++++++++++++++++- 7 files changed, 45 insertions(+), 4 deletions(-) diff --git a/craftmine.php b/craftmine.php index d47989c..9a8806b 100644 --- a/craftmine.php +++ b/craftmine.php @@ -49,6 +49,7 @@ switch($op) { case "getCraftMine": sendCraftMine(); break; case "buildShop": buildShop(); break; case "buyItem": buyItem(); break; + case "useItem": useItem(); break; case "buildDungeon" : buildDungeon(); break; case "launchDungeon" : launchDungeon(); break; case "exitDungeon" : exitDungeon(); break; diff --git a/inc/Inventory.inc b/inc/Inventory.inc index 024a4fe..747c4db 100644 --- a/inc/Inventory.inc +++ b/inc/Inventory.inc @@ -46,6 +46,23 @@ class Inventory { $inv->_removeItem($item); } + private function _useItem($item) { + foreach($this->items as $k => $object){ + if($object[0] == $item){ + if($this->items[$k][1]>0)$this->items[$k][1]--; + //if($this->items[$k][1] == 0) _removeItem($item); + return $this->items[$k]; + } + } + return false; + } + + public static function useItem($item) { + $inv = self::get(); + $it = $inv->_useItem($item); + return $it; + } + public function addToXML($root) { foreach($this->items as $item) $item->addToXML($root); diff --git a/inc/perso.inc b/inc/perso.inc index 4fb05ef..0362d8c 100644 --- a/inc/perso.inc +++ b/inc/perso.inc @@ -10,7 +10,7 @@ function updatePerso(){ $xp = $_POST["xp"]; $lv = $_POST["lv"]; $_SESSION["perso"]["hp"] = $hp; - $_SESSION["perso"]["xp"] = $xp; + $_SESSION["perso"]["xp"] = intval($xp); $_SESSION["perso"]["lv"] = $lv; } diff --git a/inc/shop.inc b/inc/shop.inc index 49ee20b..32ecea2 100644 --- a/inc/shop.inc +++ b/inc/shop.inc @@ -60,5 +60,11 @@ function buyItem() { } } +function useItem(){ + $item = getItem($_POST["item"]); + $it = Inventory::useItem($item); + echo json_encode($it); +} + ?> diff --git a/js/craftmine.js b/js/craftmine.js index a8619df..70becec 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -63,7 +63,7 @@ function initCraftMine() { displayPerso(ret.perso.hp,ret.perso.xp,ret.perso.lv); } if(ret.shop) displayShop(ret.shop); - displayInventory(ret.inventory); + if(ret.inventory) displayInventory(ret.inventory); if(ret.dungeon == false){}//if we have left the donjon else if(typeof ret.dungeon.mob == "undefined") displayDungeon(0,1,true); //if we have reload just after buying the ticket else displayDungeon(ret.dungeon.mob,ret.dungeon.flat,true);//if we have reload in the middle of the dungeon diff --git a/js/dungeon.js b/js/dungeon.js index 30808ff..982dcf6 100644 --- a/js/dungeon.js +++ b/js/dungeon.js @@ -55,6 +55,7 @@ function strike(ret,nb, f){ return; } else if(persoLife == 0){ + data.hp = 1; endBattle("mob",nb,f,ret); return; } @@ -100,7 +101,7 @@ function endBattle(v,nb, f, ret){ sendRequest("craftmine.php", "op=sendDungeonProgress&floor="+f+"&mob="+nb); } sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&xp="+data.xp+"&lv="+data.level, function(){ - displayPerso(data.hp,data.xp,data.level); + displayPerso(data.hp,parseInt(data.xp),data.level); }); } diff --git a/js/shop.js b/js/shop.js index bda3885..efb347a 100644 --- a/js/shop.js +++ b/js/shop.js @@ -37,7 +37,7 @@ function addItem(ret) { 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 += ""; itemhtml += "
  • "; } else{ // si c'est une n-ième itération @@ -68,6 +68,22 @@ function buyItem(name) { 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); + }); + } }); } -- cgit v1.2.3-54-g00ecf