aboutsummaryrefslogtreecommitdiffstats
path: root/inc
diff options
context:
space:
mode:
Diffstat (limited to 'inc')
-rw-r--r--inc/Inventory.inc30
-rw-r--r--inc/Monster.inc21
-rw-r--r--inc/craftmine.inc6
-rw-r--r--inc/dungeon.inc65
-rw-r--r--inc/messages.inc2
-rw-r--r--inc/mine.inc2
-rw-r--r--inc/perso.inc23
-rw-r--r--inc/shop.inc46
8 files changed, 173 insertions, 22 deletions
diff --git a/inc/Inventory.inc b/inc/Inventory.inc
index 0a93d7f..747c4db 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) {
@@ -37,6 +46,23 @@ class Inventory {
$inv->_removeItem($item);
}
+ private function _useItem($item) {
+ foreach($this->items as $k => $object){
+ if($object[0] == $item){
+ if($this->items[$k][1]>0)$this->items[$k][1]--;
+ //if($this->items[$k][1] == 0) _removeItem($item);
+ return $this->items[$k];
+ }
+ }
+ return false;
+ }
+
+ public static function useItem($item) {
+ $inv = self::get();
+ $it = $inv->_useItem($item);
+ return $it;
+ }
+
public function addToXML($root) {
foreach($this->items as $item)
$item->addToXML($root);
diff --git a/inc/Monster.inc b/inc/Monster.inc
new file mode 100644
index 0000000..c7063ec
--- /dev/null
+++ b/inc/Monster.inc
@@ -0,0 +1,21 @@
+<?php
+
+class Monster {
+ public $name = "";
+ public $icon = "";
+ public $desc = "";
+ public $hp = 1;
+ public $xp = 0;
+ public $level = 1;
+
+ function __construct($name, $level, $hp, $xp, $icon) {
+ $this->name = $name;
+ $this->level = $level;
+ $this->hp = $hp;
+ $this->xp = $xp;
+ $this->icon = $icon;
+
+ }
+}
+
+?>
diff --git a/inc/craftmine.inc b/inc/craftmine.inc
index 33a28d7..f5dbbb1 100644
--- a/inc/craftmine.inc
+++ b/inc/craftmine.inc
@@ -2,12 +2,16 @@
require_once("mine.inc");
require_once("shop.inc");
+require_once("dungeon.inc");
+require_once("perso.inc");
function sendCraftMine() {
$data = array("gold" => sendMine(),
"shop" => sendShop(),
"inventory" => Inventory::sendContent(),
- "miners" => sendMiners()
+ "miners" => sendMiners(),
+ "dungeon" => sendDungeon(),
+ "perso" => sendPerso(),
);
echo json_encode($data);
}
diff --git a/inc/dungeon.inc b/inc/dungeon.inc
new file mode 100644
index 0000000..c023cf8
--- /dev/null
+++ b/inc/dungeon.inc
@@ -0,0 +1,65 @@
+<?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),
+ intval($monster->xp),
+ (string)$monster->icon);
+ }
+ }
+ return $dungeon;
+}
+
+function initDungeon() {
+ $_SESSION["dungeon"]["access"] = true;
+}
+
+function sendDungeon() {
+ if(!empty($_SESSION["dungeon"]))
+ return $_SESSION["dungeon"];
+ 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(){
+ $f= $_POST["floor"];
+ $dungeon=generateMonster();
+ $opponent = $dungeon["monsters"]["floor".$f];
+ echo json_encode($opponent);
+}
+
+function sendDungeonProgress(){
+ $f= $_POST["floor"];
+ $nb=$_POST["mob"];
+ $_SESSION["dungeon"]["flat"] = $f;
+ $_SESSION["dungeon"]["mob"] = $nb;
+}
+
+
+function exitDungeon(){
+ $_SESSION["dungeon"] = false;
+}
+
+?>
diff --git a/inc/messages.inc b/inc/messages.inc
index d08331a..da36c4e 100644
--- a/inc/messages.inc
+++ b/inc/messages.inc
@@ -6,7 +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.",
"gamesave_not_found" => "Couldn't find the specified save file.",
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/perso.inc b/inc/perso.inc
new file mode 100644
index 0000000..0362d8c
--- /dev/null
+++ b/inc/perso.inc
@@ -0,0 +1,23 @@
+<?php
+function sendPerso() {
+ if(empty($_SESSION["perso"]))
+ initPerso();
+ return $_SESSION["perso"];
+}
+
+function updatePerso(){
+ $hp = $_POST["hp"];
+ $xp = $_POST["xp"];
+ $lv = $_POST["lv"];
+ $_SESSION["perso"]["hp"] = $hp;
+ $_SESSION["perso"]["xp"] = intval($xp);
+ $_SESSION["perso"]["lv"] = $lv;
+}
+
+function initPerso(){
+ $_SESSION["perso"]["hp"] = 5;
+ $_SESSION["perso"]["xp"] = 0;
+ $_SESSION["perso"]["lv"] = 3;
+}
+
+?>
diff --git a/inc/shop.inc b/inc/shop.inc
index a0f9a8a..32ecea2 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,30 +36,35 @@ 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
}
}
+function useItem(){
+ $item = getItem($_POST["item"]);
+ $it = Inventory::useItem($item);
+ echo json_encode($it);
+}
+
?>