From 1c8629abd2bd8fdcc29fe50d358c176bbfb2e3a3 Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 25 Apr 2016 12:31:00 +0200 Subject: Account already debited --- inc/shop.inc | 1 - 1 file changed, 1 deletion(-) (limited to 'inc/shop.inc') diff --git a/inc/shop.inc b/inc/shop.inc index 11cf97b..a0f9a8a 100644 --- a/inc/shop.inc +++ b/inc/shop.inc @@ -42,7 +42,6 @@ function buildShop() { } elseif(debitAccount($shop["cost"])) { initShop(); - $_SESSION["mine"]["gold"] -= $shop["cost"]; echo json_encode(sendShop()); } } -- cgit v1.2.3-70-g09d2 From 349c6fe7ecf10e5929dc27c2446853f0fe416077 Mon Sep 17 00:00:00 2001 From: alexichi Date: Wed, 27 Apr 2016 12:59:51 +0200 Subject: add loadShop() and categories in the xml file and modify the presentation of the items in the shop --- data/items.xml | 41 +++++++++++++++++++++++++++++++++++++++++ inc/shop.inc | 36 +++++++++++++++++++++--------------- js/shop.js | 24 ++++++++++++++++-------- 3 files changed, 78 insertions(+), 23 deletions(-) create mode 100644 data/items.xml (limited to 'inc/shop.inc') diff --git a/data/items.xml b/data/items.xml new file mode 100644 index 0000000..528d37a --- /dev/null +++ b/data/items.xml @@ -0,0 +1,41 @@ + + + + + Wooden Sword + 50 + + 10 + + + A sword that beginners need to use to improve their skills + + + Metal Sword + 200 + + 30 + + + A sword which are often use buy skilled knight. The material is very good and the sword is very powerful + + + + + Life Bottle + 100 + + 🍶 + A bottle which can heal you by regaining hp + + + Strength Bottle + 250 + + 20 + + 🍶 + If used, you have 20% more chance to hit your enemy during the battle + + + diff --git a/inc/shop.inc b/inc/shop.inc index 11cf97b..6e30923 100644 --- a/inc/shop.inc +++ b/inc/shop.inc @@ -5,19 +5,26 @@ require_once("account.inc"); require_once("Item.inc"); require_once("Inventory.inc"); -$shop = array( - "cost" => 3, - "items" => array( - new Item("cat", 6, "🐈", "Nyan!"), - new Item("torch", 3, "🔦", "Electric torch"), - ), -); +function loadShop(){ + $items = simplexml_load_file('data/items.xml'); + $shop = array("cost"=>(string)$items["cost"],"items"=>array()); + foreach($items as $cat){ + $category = (string)$cat["name"]; + $shop["items"][$category] = array(); + foreach($cat as $item){ + $shop["items"][$category][] = new Item((string)$item->name,intval($item->cost),(string)$item->icon,(string)$item->description); + } + } + return $shop; +} function getItem($name) { - global $shop; - foreach($shop["items"] as $item) { - if($name == $item->name) { - return $item; + $shop=loadShop(); + foreach($shop["items"] as $cat) { + foreach($cat as $item){ + if($name == $item->name) { + return $item; + } } } sendError("shop_missing_item"); @@ -29,21 +36,20 @@ function initShop() { } function sendShop() { - global $shop; if(!empty($_SESSION["shop"])) - return $shop; + return loadShop(); else return false; } function buildShop() { - global $shop; + $shop=loadShop(); if(!empty($_SESSION["shop"])) { sendError("shop_already_built"); } elseif(debitAccount($shop["cost"])) { initShop(); $_SESSION["mine"]["gold"] -= $shop["cost"]; - echo json_encode(sendShop()); + echo json_encode($shop); } } diff --git a/js/shop.js b/js/shop.js index 22ca85f..efd6ac5 100644 --- a/js/shop.js +++ b/js/shop.js @@ -1,12 +1,20 @@ function displayShop(ret) { - var tmphtml = "

Select an item to buy it:

"; - tmphtml += "" document.getElementById("tab2").innerHTML = tmphtml; } @@ -40,7 +48,7 @@ function addItem(ret) { } else invcontent.getElementsByTagName('ul')[0].innerHTML += itemhtml; - showInfo(ret.desc); + showInfo("The "+ ret.name + " has been successfully purchased"); } function buyItem(name) { -- cgit v1.2.3-70-g09d2 From 48ca3e8ee63787d567fd0fe1bcfea7158a6f9596 Mon Sep 17 00:00:00 2001 From: alexichi Date: Fri, 29 Apr 2016 21:06:59 +0200 Subject: add the option to not create 2 objects of the same type, doesn't work well --- inc/Inventory.inc | 14 ++++++++++++-- inc/shop.inc | 4 ++-- js/shop.js | 19 +++++++++++++------ 3 files changed, 27 insertions(+), 10 deletions(-) (limited to 'inc/shop.inc') 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 = "
  • "; - itemhtml += ""; - itemhtml += "
  • "; - + var itemhtml = ""; + if(ret[1]==1){ //si c'est la première itération de l'objet + itemhtml += "
  • "; + itemhtml += ""; + itemhtml += "
  • "; + } + 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); }); } -- cgit v1.2.3-70-g09d2 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(-) (limited to 'inc/shop.inc') 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-70-g09d2 From cdba8c2fd7785b0a6506d1369d790bbd1d8542ad Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 8 May 2016 14:42:30 +0200 Subject: Read features from XML + send updated perso stats on useItem() --- inc/shop.inc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'inc/shop.inc') diff --git a/inc/shop.inc b/inc/shop.inc index 32ecea2..69161e1 100644 --- a/inc/shop.inc +++ b/inc/shop.inc @@ -4,6 +4,7 @@ require_once("messages.inc"); require_once("account.inc"); require_once("Item.inc"); require_once("Inventory.inc"); +require_once("perso.inc"); function loadShop(){ $items = simplexml_load_file('data/items.xml'); @@ -12,7 +13,15 @@ function loadShop(){ $category = (string)$cat["name"]; $shop["items"][$category] = array(); foreach($cat as $item){ - $shop["items"][$category][] = new Item((string)$item->name,intval($item->cost),(string)$item->icon,(string)$item->description); + $feats = array(); + foreach($item->features[0] as $k => $v) + $feats[(string)$k] = (string)$v; + $shop["items"][$category][] = new Item( + (string)$item->name, + intval($item->cost), + (string)$item->icon, + (string)$item->description, + $feats); } } return $shop; @@ -63,7 +72,7 @@ function buyItem() { function useItem(){ $item = getItem($_POST["item"]); $it = Inventory::useItem($item); - echo json_encode($it); + echo json_encode(array("perso" => sendPerso(), "item" => $it)); } -- cgit v1.2.3-70-g09d2