aboutsummaryrefslogtreecommitdiffstats
path: root/inc
diff options
context:
space:
mode:
authoralexichi <alexbankai96@gmail.com>2016-04-25 10:43:56 +0200
committeralexichi <alexbankai96@gmail.com>2016-04-25 10:43:56 +0200
commit1ffba62faae83e18597270b68580a90ac3032a31 (patch)
tree603d33ed48a9dd3690aeef379eeb7fd8ce3a257f /inc
parent3ece645cc83e36aaa36c0258afa0f1b36eb13ca2 (diff)
parentdc28807625b9f53e621ec0d7063d99c2527cfa02 (diff)
downloadcandybox-1ffba62faae83e18597270b68580a90ac3032a31.tar.gz
candybox-1ffba62faae83e18597270b68580a90ac3032a31.tar.bz2
candybox-1ffba62faae83e18597270b68580a90ac3032a31.tar.xz
candybox-1ffba62faae83e18597270b68580a90ac3032a31.zip
Merge remote-tracking branch 'origin/piernov' into alexichi
Conflicts: craftmine.php index.xhtml
Diffstat (limited to 'inc')
-rw-r--r--inc/Inventory.inc41
-rw-r--r--inc/Item.inc17
-rw-r--r--inc/account.inc18
-rw-r--r--inc/craftmine.inc14
-rw-r--r--inc/messages.inc15
-rw-r--r--inc/mine.inc4
-rw-r--r--inc/shop.inc59
7 files changed, 166 insertions, 2 deletions
diff --git a/inc/Inventory.inc b/inc/Inventory.inc
new file mode 100644
index 0000000..efe54f2
--- /dev/null
+++ b/inc/Inventory.inc
@@ -0,0 +1,41 @@
+<?php
+
+class Inventory {
+
+ public $items = array();
+
+ public static function created() {
+ return !empty($_SESSION["inventory"]);
+ }
+
+ public static function sendContent() {
+ return self::get()->items;
+ }
+
+ public static function get() {
+ if(!self::created()) {
+ $_SESSION["inventory"] = new Inventory();
+ }
+ return $_SESSION["inventory"];
+ }
+
+ private function _addItem($item) {
+ $this->items[] = $item;
+ }
+
+ public static function addItem($item) {
+ $inv = self::get();
+ $inv->_addItem($item);
+ }
+
+ private function _removeItem($item) {
+ unset($this->items[array_search($item, $this->items)]);
+ }
+
+ public static function removeItem($item) {
+ $inv = self::get();
+ $inv->_removeItem($item);
+ }
+}
+
+?>
diff --git a/inc/Item.inc b/inc/Item.inc
new file mode 100644
index 0000000..bf77818
--- /dev/null
+++ b/inc/Item.inc
@@ -0,0 +1,17 @@
+<?php
+
+class Item {
+ public $name = "";
+ public $cost = 0;
+ public $icon = "";
+ public $desc = "";
+
+ function __construct($name, $cost, $icon, $desc) {
+ $this->name = $name;
+ $this->cost = $cost;
+ $this->icon = $icon;
+ $this->desc = $desc;
+ }
+}
+
+?>
diff --git a/inc/account.inc b/inc/account.inc
new file mode 100644
index 0000000..19f311d
--- /dev/null
+++ b/inc/account.inc
@@ -0,0 +1,18 @@
+<?php
+require_once("messages.inc");
+
+function debitAccount($amount) {
+ if($_SESSION["mine"]["gold"] <= $amount) {
+ sendError("gold_insufficient");
+ return false;
+ }
+ $_SESSION["mine"]["gold"] -= $amount;
+ return true;
+}
+
+function creditAccount($amount) {
+ $_SESSION["mine"]["gold"] += $amount;
+}
+
+
+?>
diff --git a/inc/craftmine.inc b/inc/craftmine.inc
new file mode 100644
index 0000000..a30538c
--- /dev/null
+++ b/inc/craftmine.inc
@@ -0,0 +1,14 @@
+<?php
+
+require_once("mine.inc");
+require_once("shop.inc");
+
+function sendCraftMine() {
+ $data = array("gold" => sendMine(),
+ "shop" => sendShop(),
+ "inventory" => Inventory::sendContent(),
+ );
+ echo json_encode($data);
+}
+
+?>
diff --git a/inc/messages.inc b/inc/messages.inc
new file mode 100644
index 0000000..f1ca8b3
--- /dev/null
+++ b/inc/messages.inc
@@ -0,0 +1,15 @@
+<?php
+
+$messages = array(
+ "shop_already_built" => "You have already built a shop.",
+ "gold_insufficient" => "You don't have enough gold.",
+ "shop_missing_item" => "This item does not exist.",
+);
+
+function sendError($msg) {
+ global $messages;
+ $text = $messages[$msg];
+ echo json_encode(array("error" => $text));
+}
+
+?>
diff --git a/inc/mine.inc b/inc/mine.inc
index a2ecd09..94a2c66 100644
--- a/inc/mine.inc
+++ b/inc/mine.inc
@@ -8,13 +8,13 @@ function withdrawMine() {
$amount = intval($_POST["amount"]);
if($amount == 0) return;
$_SESSION["mine"]["gold"] += $amount;
- echo $_SESSION["mine"]["gold"];
+ echo json_encode($_SESSION["mine"]["gold"]);
}
function sendMine() {
if(empty($_SESSION["mine"])) initCraftMine();
$mine = $_SESSION["mine"];
- echo $mine["gold"];
+ return $mine["gold"];
}
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);
+ }
+}
+
+
+?>