diff options
author | piernov <piernov@piernov.org> | 2016-05-09 22:06:30 +0200 |
---|---|---|
committer | piernov <piernov@piernov.org> | 2016-05-09 22:06:30 +0200 |
commit | f10c51f07a755d75a583f85316efbcd3bd1e4b6d (patch) | |
tree | 87419a11e12f5b7433459fcb5cb9da5211dcbd9e /inc/shop.inc | |
parent | 54635d17eef27eb2546d69599e4107b242509ced (diff) | |
parent | 2f32bc3153b7f2c2561e4603f912573921e6449f (diff) | |
download | candybox-f10c51f07a755d75a583f85316efbcd3bd1e4b6d.tar.gz candybox-f10c51f07a755d75a583f85316efbcd3bd1e4b6d.tar.bz2 candybox-f10c51f07a755d75a583f85316efbcd3bd1e4b6d.tar.xz candybox-f10c51f07a755d75a583f85316efbcd3bd1e4b6d.zip |
Merge branch 'alexichi' of ssh://piernov.org/srv/git/candybox into alexichi
Diffstat (limited to 'inc/shop.inc')
-rw-r--r-- | inc/shop.inc | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/inc/shop.inc b/inc/shop.inc index 11cf97b..69161e1 100644 --- a/inc/shop.inc +++ b/inc/shop.inc @@ -4,20 +4,36 @@ require_once("messages.inc"); require_once("account.inc"); require_once("Item.inc"); require_once("Inventory.inc"); +require_once("perso.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){ + $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; +} 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,31 +45,35 @@ 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); } } 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 } } +function useItem(){ + $item = getItem($_POST["item"]); + $it = Inventory::useItem($item); + echo json_encode(array("perso" => sendPerso(), "item" => $it)); +} + ?> |