aboutsummaryrefslogtreecommitdiffstats
path: root/inc
diff options
context:
space:
mode:
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.inc15
-rw-r--r--inc/guild.inc32
-rw-r--r--inc/messages.inc17
-rw-r--r--inc/mine.inc6
-rw-r--r--inc/mine.inc~20
-rw-r--r--inc/shop.inc59
9 files changed, 187 insertions, 38 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..6f398bb
--- /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..33a28d7
--- /dev/null
+++ b/inc/craftmine.inc
@@ -0,0 +1,15 @@
+<?php
+
+require_once("mine.inc");
+require_once("shop.inc");
+
+function sendCraftMine() {
+ $data = array("gold" => sendMine(),
+ "shop" => sendShop(),
+ "inventory" => Inventory::sendContent(),
+ "miners" => sendMiners()
+ );
+ echo json_encode($data);
+}
+
+?>
diff --git a/inc/guild.inc b/inc/guild.inc
index 5ede1f6..c0e8264 100644
--- a/inc/guild.inc
+++ b/inc/guild.inc
@@ -1,29 +1,31 @@
<?php
+define("GUILD_COST",10);
+define("MINER_COST",5);
+
function createGuild(){
- if($_SESSION["mine"]["gold"] >= 150){
- $_SESSION["guild"]= array("miners" => 1);
- $_SESSION["mine"]["gold"] -= 150;
- echo "Guild has been successfully created";
+ if(!empty($_SESSION["guild"])) {
+ sendError("guild_already_built");
}
- else{
- echo "Not enough money";
+ elseif(debitAccount(GUILD_COST)) {
+ $_SESSION["guild"] = true;
+ echo json_encode(array("cost" => GUILD_COST));
}
}
function hireMiner(){
if(!isset($_SESSION["guild"])){
- echo "<p>you need to create a guild first";
+ sendError("guild_not_yet_created");
}
- if($_SESSION["mine"]["gold"] >= 40){
- $_SESSION["guild"]["miners"]++;
- $_SESSION["mine"]["gold"] -= 40;
- $_SESSION["mine"]["miners"] = $_SESSION["guild"]["miners"];
- $mine = $_SESSION["mine"];
- $guild = $_SESSION["guild"];
- echo $mine["gold"];
- echo $mine["miners"];
+ elseif(debitAccount(MINER_COST)){
+ $_SESSION["mine"]["miners"]++;
+ echo json_encode(array("cost" => MINER_COST , "miners" => $_SESSION["mine"]["miners"]));
}
}
+function sendMiners(){
+ return $_SESSION["mine"]["miners"];
+}
+
+
?>
diff --git a/inc/messages.inc b/inc/messages.inc
new file mode 100644
index 0000000..d6ea87e
--- /dev/null
+++ b/inc/messages.inc
@@ -0,0 +1,17 @@
+<?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.",
+ "guild_not_yet_created" => "You need to create a guild first.",
+ "guild_already_built" => "You have aready built a guild."
+);
+
+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 aea04ad..94a2c66 100644
--- a/inc/mine.inc
+++ b/inc/mine.inc
@@ -1,20 +1,20 @@
<?php
function initCraftMine() {
- $_SESSION["mine"] = array("mine" => 0, "gold" => 0, "miners" => 1);
+ $_SESSION["mine"] = array("mine" => 0, "gold" => 0, "miners" => 0);
}
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/mine.inc~ b/inc/mine.inc~
deleted file mode 100644
index a01d768..0000000
--- a/inc/mine.inc~
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-
-function initCraftMine() {
- $_SESSION["mine"] = array("mine" => 0, "or" => 0, "miners" => 1);
-}
-
-function withdrawMine() {
- $mine = $_SESSION["mine"];
- $_SESSION["mine"]["gold"] += $mine["mine"];
- $_SESSION["mine"]["mine"] = 0;
-}
-
-function sendMine() {
- if(empty($_SESSION["mine"])) initMine();
- $mine = $_SESSION["mine"];
- echo $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);
+ }
+}
+
+
+?>