diff options
Diffstat (limited to 'inc')
-rw-r--r-- | inc/Inventory.inc | 13 | ||||
-rw-r--r-- | inc/Monster.inc | 19 | ||||
-rw-r--r-- | inc/craftmine.inc | 4 | ||||
-rw-r--r-- | inc/dungeon.inc | 56 | ||||
-rw-r--r-- | inc/messages.inc | 1 | ||||
-rw-r--r-- | inc/mine.inc | 2 | ||||
-rw-r--r-- | inc/shop.inc | 40 |
7 files changed, 114 insertions, 21 deletions
diff --git a/inc/Inventory.inc b/inc/Inventory.inc index 0a93d7f..024a4fe 100644 --- a/inc/Inventory.inc +++ b/inc/Inventory.inc @@ -20,12 +20,21 @@ class Inventory { } private function _addItem($item) { - $this->items[] = $item; + foreach($this->items as $k => $object){ + if($object[0] == $item){ + $this->items[$k][1]++; + return $this->items[$k]; + } + } + $tab = array($item,1); + $this->items[] = $tab; + return $tab; } public static function addItem($item) { $inv = self::get(); - $inv->_addItem($item); + $tab = $inv->_addItem($item); + return $tab; } private function _removeItem($item) { diff --git a/inc/Monster.inc b/inc/Monster.inc new file mode 100644 index 0000000..fbb0fe9 --- /dev/null +++ b/inc/Monster.inc @@ -0,0 +1,19 @@ +<?php + +class Monster { + public $name = ""; + public $icon = ""; + public $desc = ""; + public $hp = 1; + public $level = 1; + + function __construct($name, $level, $hp, $icon) { + $this->name = $name; + $this->level = $level; + $this->hp = $hp; + $this->icon = $icon; + + } +} + +?> diff --git a/inc/craftmine.inc b/inc/craftmine.inc index 33a28d7..0d6a445 100644 --- a/inc/craftmine.inc +++ b/inc/craftmine.inc @@ -2,12 +2,14 @@ require_once("mine.inc"); require_once("shop.inc"); +require_once("dungeon.inc"); function sendCraftMine() { $data = array("gold" => sendMine(), "shop" => sendShop(), "inventory" => Inventory::sendContent(), - "miners" => sendMiners() + "miners" => sendMiners(), + "dungeon" => sendDungeon(), ); echo json_encode($data); } diff --git a/inc/dungeon.inc b/inc/dungeon.inc new file mode 100644 index 0000000..81e2ba7 --- /dev/null +++ b/inc/dungeon.inc @@ -0,0 +1,56 @@ +<?php +require_once("messages.inc"); +require_once("account.inc"); +require_once("Monster.inc"); + + +function generateMonster(){ + $monsters = simplexml_load_file('data/monsters.xml'); + $dungeon = array("cost"=>(string)$monsters["cost"],"monsters"=>array()); + foreach($monsters as $f){ + $floor = (string)$f["name"]; + $dungeon["monsters"][$floor] = array(); + foreach($f as $monster){ + $dungeon["monsters"][$floor][] = new Monster((string)$monster->name, + intval($monster->level), + intval($monster->hp), + (string)$monster->icon); + } + } + return $dungeon; +} + +function initDungeon() { + $_SESSION["dungeon"] = true; +} + +function sendDungeon() { + if(!empty($_SESSION["dungeon"])) + return generateMonster(); + else return false; +} + +function buildDungeon() { + $dungeon=generateMonster(); + if(!empty($_SESSION["dungeon"])) { + sendError("dungeon_already_available"); + } + elseif(debitAccount($dungeon["cost"])) { + initDungeon(); + $_SESSION["mine"]["gold"] -= $dungeon["cost"]; + echo json_encode($dungeon); + } +} + +function launchDungeon(){ + $dungeon=generateMonster(); + $opponent = $dungeon["monsters"]["floor1"]; + echo json_encode($opponent); +} + +function exitDungeon(){ + $_SESSION["dungeon"] = false; + echo json_encode(array("a" => 7));//Cette ligne ne sert qu'à contourner l'erreur créé à cause de sendRequest. A revoir. +} + +?> diff --git a/inc/messages.inc b/inc/messages.inc index d08331a..3a7d9b1 100644 --- a/inc/messages.inc +++ b/inc/messages.inc @@ -6,6 +6,7 @@ $messages = array( "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.", + "dungeon_already_available" => "You can already access the dungeon", "gamesave_ok" => "Game saved.", "gamesave_error" => "An error occured when trying to save the game.", diff --git a/inc/mine.inc b/inc/mine.inc index 94a2c66..752fc69 100644 --- a/inc/mine.inc +++ b/inc/mine.inc @@ -1,7 +1,7 @@ <?php function initCraftMine() { - $_SESSION["mine"] = array("mine" => 0, "gold" => 0, "miners" => 0); + $_SESSION["mine"] = array("gold" => 0, "miners" => 0); } function withdrawMine() { diff --git a/inc/shop.inc b/inc/shop.inc index a0f9a8a..49ee20b 100644 --- a/inc/shop.inc +++ b/inc/shop.inc @@ -5,19 +5,26 @@ 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 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){ + $shop["items"][$category][] = new Item((string)$item->name,intval($item->cost),(string)$item->icon,(string)$item->description); + } + } + 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,28 +36,27 @@ 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(); - 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 } } |