From 5a688c8c35d4a2bcca716aa7da1379c0abd48d3f Mon Sep 17 00:00:00 2001 From: piernov Date: Sun, 24 Apr 2016 22:08:58 +0200 Subject: Add PHP shop requests handlers --- inc/shop.inc | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 inc/shop.inc diff --git a/inc/shop.inc b/inc/shop.inc new file mode 100644 index 0000000..11cf97b --- /dev/null +++ b/inc/shop.inc @@ -0,0 +1,59 @@ + 3, + "items" => array( + new Item("cat", 6, "🐈", "Nyan!"), + new Item("torch", 3, "🔦", "Electric torch"), + ), +); + +function getItem($name) { + global $shop; + foreach($shop["items"] as $item) { + if($name == $item->name) { + return $item; + } + } + sendError("shop_missing_item"); + return false; +} + +function initShop() { + $_SESSION["shop"] = true; +} + +function sendShop() { + global $shop; + if(!empty($_SESSION["shop"])) + return $shop; + else return false; +} + +function buildShop() { + global $shop; + if(!empty($_SESSION["shop"])) { + sendError("shop_already_built"); + } + elseif(debitAccount($shop["cost"])) { + initShop(); + $_SESSION["mine"]["gold"] -= $shop["cost"]; + echo json_encode(sendShop()); + } +} + +function buyItem() { + $item = getItem($_POST["item"]); + if($item && debitAccount($item->cost)) { + Inventory::addItem($item); + echo json_encode($item); + } +} + + +?> -- cgit v1.2.3-54-g00ecf