aboutsummaryrefslogtreecommitdiffstats
path: root/inc/shop.inc
diff options
context:
space:
mode:
authorpiernov <piernov@piernov.org>2016-04-24 22:30:43 +0200
committerpiernov <piernov@piernov.org>2016-04-24 22:30:43 +0200
commit64e92bb7b10bf521aa2992d20c243c41573e0ee8 (patch)
tree81eeb7a7502ed3e8e40cd2e434a462d5d13581f1 /inc/shop.inc
parenta7d9febaba01a5f32ff77dade8f4fa1e97870354 (diff)
parentbc8e1117591e22a2ec850ce34358abadf950d87e (diff)
downloadcandybox-64e92bb7b10bf521aa2992d20c243c41573e0ee8.tar.gz
candybox-64e92bb7b10bf521aa2992d20c243c41573e0ee8.tar.bz2
candybox-64e92bb7b10bf521aa2992d20c243c41573e0ee8.tar.xz
candybox-64e92bb7b10bf521aa2992d20c243c41573e0ee8.zip
Merge branch 'feat/shop' into piernov
Diffstat (limited to 'inc/shop.inc')
-rw-r--r--inc/shop.inc59
1 files changed, 59 insertions, 0 deletions
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 @@
+<?php
+
+require_once("messages.inc");
+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 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);
+ }
+}
+
+
+?>