diff options
-rw-r--r-- | craftmine.php | 4 | ||||
-rw-r--r-- | css/craftmine.css | 6 | ||||
-rw-r--r-- | data/items.xml | 41 | ||||
-rw-r--r-- | data/monsters.xml | 63 | ||||
-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 | ||||
-rw-r--r-- | index.xhtml | 2 | ||||
-rw-r--r-- | js/craftmine.js | 14 | ||||
-rw-r--r-- | js/dungeon.js | 42 | ||||
-rw-r--r-- | js/shop.js | 42 |
15 files changed, 312 insertions, 37 deletions
diff --git a/craftmine.php b/craftmine.php index 9a5d3d7..242e1fd 100644 --- a/craftmine.php +++ b/craftmine.php @@ -4,6 +4,7 @@ require_once("inc/mine.inc"); require_once("inc/guild.inc"); require_once("inc/shop.inc"); require_once("inc/craftmine.inc"); +require_once("inc/dungeon.inc"); require_once("inc/savegame.inc"); session_start(); // Must be placed *BEFORE* $_SESSION is actually used and *AFTER* all classes are imported @@ -47,6 +48,9 @@ switch($op) { case "getCraftMine": sendCraftMine(); break; case "buildShop": buildShop(); break; case "buyItem": buyItem(); break; + case "buildDungeon" : buildDungeon(); break; + case "launchDungeon" : launchDungeon(); break; + case "exitDungeon" : exitDungeon(); break; case "saveGame": saveGame(); break; case "downSave": downSave(); break; case "listSaves": listSaves(); break; diff --git a/css/craftmine.css b/css/craftmine.css index a3ab87d..c459baa 100644 --- a/css/craftmine.css +++ b/css/craftmine.css @@ -9,7 +9,13 @@ .tab-pane:target { display: block; } +.list-inline > li { + padding-top: 5px; + padding-bottom: 5px; +} .item-icon { + display:inline-block; + width: 1em; font-size: 3em; font-family: "Symbola"; } diff --git a/data/items.xml b/data/items.xml new file mode 100644 index 0000000..8888796 --- /dev/null +++ b/data/items.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="utf-8"?> +<items cost="3"> + <category name="Swords"> + <item> + <name>Wooden Sword</name> + <cost>10</cost> + <features> + <power>10</power> + </features> + <icon>⚔</icon> + <description>A sword that beginners need to use to improve their skills</description> + </item> + <item> + <name>Metal Sword</name> + <cost>15</cost> + <features> + <power>30</power> + </features> + <icon>⚔</icon> + <description>A sword which are often use buy skilled knight. The material is very good and the sword is very powerful</description> + </item> + </category> + <category name="Potions"> + <item> + <name>Life Bottle</name> + <cost>5</cost> + <features></features> + <icon>💧</icon> + <description>A bottle which can heal you by regaining hp</description> + </item> + <item> + <name>Strength Bottle</name> + <cost>10</cost> + <features> + <power>20</power> + </features> + <icon>💧</icon> + <description>If used, you have 20% more chance to hit your enemy during the battle</description> + </item> + </category> +</items> diff --git a/data/monsters.xml b/data/monsters.xml new file mode 100644 index 0000000..86cf422 --- /dev/null +++ b/data/monsters.xml @@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="utf-8"?> +<dungeon cost="10"> + <floor name="floor1"> + <monster> + <name>monster1</name> + <level>1</level> + <hp>2</hp> + <icon>m</icon> + </monster> + <monster > + <name>monster2</name> + <level>2</level> + <hp>3</hp> + <icon>m</icon> + </monster> + <monster> + <name>monster3</name> + <level>3</level> + <hp>4</hp> + <icon>m</icon> + </monster> + </floor> + <floor name="floor2"> + <monster> + <name>monster4</name> + <level>4</level> + <hp>5</hp> + <icon>m</icon> + </monster> + <monster> + <name>monster5</name> + <level>5</level> + <hp>6</hp> + <icon>m</icon> + </monster> + <monster> + <name>monster5</name> + <level>6</level> + <hp>7</hp> + <icon>m</icon> + </monster> + </floor> + <floor name="floor3"> + <monster> + <name>monster6</name> + <level>8</level> + <hp>9</hp> + <icon>m</icon> + </monster> + <monster> + <name>monster7</name> + <level>9</level> + <hp>10</hp> + <icon>m</icon> + </monster> + <monster> + <name>Boss</name> + <level>10</level> + <hp>12</hp> + <icon>m</icon> + </monster> + </floor> +</dungeon> 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 } } diff --git a/index.xhtml b/index.xhtml index 5e0752a..9260134 100644 --- a/index.xhtml +++ b/index.xhtml @@ -15,6 +15,7 @@ <script type="text/javascript" charset="utf-8" src="js/guild.js"></script> <script type="text/javascript" charset="utf-8" src="js/shop.js"></script> <script type="text/javascript" charset="utf-8" src="js/gui.js"></script> + <script type="text/javascript" charset="utf-8" src="js/dungeon.js"></script> <script type="text/javascript" charset="utf-8" src="js/savegame.js"></script> </head> <body onload="init()" onhashchange="changeTab()"> @@ -71,6 +72,7 @@ <ul class="list-inline"> <li><button class="btn btn-default" type="button" onclick="createGuild()">Miner's guild</button></li> <li><button class="btn btn-default" type="button" onclick="buildShop()">Shop</button></li> + <li><button class="btn btn-default" type="button" onclick="buildDungeon()">Dungeon</button></li> </ul> </div> <div class="tab-pane" id="tab2"> diff --git a/js/craftmine.js b/js/craftmine.js index 4e6490f..61abbf7 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -1,8 +1,11 @@ data = { + name: "You", gold: 0, mine: 0, miners: 0, - level: 1 + level: 1, + hp: 1, + icon : "H" } function sendRequest(url, params, callback, isFile) { @@ -11,7 +14,10 @@ function sendRequest(url, params, callback, isFile) { if(!isFile) xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhr.onreadystatechange = function() { if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == "200") { - var data = JSON.parse(xhr.responseText); + var data = ""; + if(xhr.responseText) data = JSON.parse(xhr.responseText); + if(data.info) + showInfo(data.info); if(data.error) { showError(data.error); return; @@ -47,10 +53,12 @@ function withdrawMine() { function initCraftMine() { sendRequest("craftmine.php", "op=getCraftMine", function(ret) { data.gold = parseInt(ret.gold); // Server's response is a string + data.mine = 0; // Reset mine if(ret.shop) displayShop(ret.shop); displayInventory(ret.inventory); + if(ret.dungeon) displayDungeon(); data.miners = parseInt(ret.miners); - updateData("gold","miners"); + updateData("gold", "mine", "miners"); }) } diff --git a/js/dungeon.js b/js/dungeon.js new file mode 100644 index 0000000..d20a3cf --- /dev/null +++ b/js/dungeon.js @@ -0,0 +1,42 @@ +function buildDungeon(){ + sendRequest("craftmine.php", "op=buildDungeon", function(ret) { + displayDungeon(); + debitAccount(ret.cost); + showInfo("You can acces the dungeon now. Good Luck."); + }); +} + +function displayDungeon(){ + var tmphtml = "<br/>"; + tmphtml += "<button id=\"launch\" type=\"button\" class=\"btn btn-primary\" onclick=\"launchDungeon()\">Launch</button>"; + document.getElementById("tab4").innerHTML = tmphtml; +} + +function launchDungeon(){ + sendRequest("craftmine.php", "op=launchDungeon", function(ret) { + document.getElementById("launch").style.display = "none"; + displayBattle(ret); + }); +} + +function displayBattle(ret){ + var tmphtml = "<div class=\"row\">"; + tmphtml += "<h4>Battle</h4>"; + tmphtml += "<ul class=\"list-inline\">"; + tmphtml += "<li>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" style=\"margin-left:30px;\"><span class=\"item-icon\">" + data.icon + "</span><br />" + data.name + "<br/> lv: " + data.level + " <span id=\"lifeMob\">hp: " + data.hp + "</span></button>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" style=\"margin-left:30px;\"><span class=\"item-icon\">" + ret[0].icon + "</span><br />" + ret[0].name + "<br/> lv: " + ret[0].level + " <span id=\"lifeMob\">hp: " + ret[0].hp + "</span></button>"; + tmphtml += "</li>"; + tmphtml += "</ul>"; + tmphtml += "</div>"; + tmphtml += "<br/><br/>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"exitDungeon()\">Exit</button>"; + document.getElementById("tab4").innerHTML = tmphtml; +} + +function exitDungeon(){ + sendRequest("craftmine.php", "op=exitDungeon", function() { + document.getElementById("tab4").innerHTML = "<h4>Look at how poor you are! You can't access the dungeon, it is only for the elite.</h4>"; + showInfo("You have left the dungeon"); + }); +} @@ -1,12 +1,20 @@ function displayShop(ret) { - var tmphtml = "<h4> Select an item to buy it:</h4>"; - tmphtml += "<ul class=\"list-inline\">"; - for(var i=0; i < ret.items.length; i++) { - tmphtml += "<li>"; - tmphtml += "<button type=\"submit\" class=\"btn btn-primary\" onclick=\"buyItem('" + ret.items[i].name + "')\"><span class=\"item-icon\">" + ret.items[i].icon + "</span><br />" + ret.items[i].name + "</button>"; - tmphtml += "</li>"; + var tmphtml = "<h3> Select an item to buy it:</h3>"; + for(var key in ret.items){ + if(ret.items.hasOwnProperty(key)){ + var category = ret.items[key]; + tmphtml += "<div class=\"row\">"; + tmphtml += "<h4>"+key+"</h4>"; + tmphtml += "<ul class=\"list-inline\">"; + for(var i=0; i < category.length; i++) { + tmphtml += "<li>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"buyItem('" + category[i].name + "')\"><span class=\"item-icon\">" + category[i].icon + "</span><br />" + category[i].name + "</button>"; + tmphtml += "</li>"; + } + tmphtml += "</ul>"; + tmphtml += "</div>"; + } } - tmphtml += "</ul>" document.getElementById("tab2").innerHTML = tmphtml; } @@ -20,14 +28,22 @@ function buildShop() { sendRequest("craftmine.php", "op=buildShop", function(ret) { displayShop(ret); debitAccount(ret.cost); + showInfo("Your shop has been successfully created"); }); } function addItem(ret) { - var itemhtml = "<li>"; - itemhtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"useItem('" + ret.name + "')\"><span class=\"item-icon\">" + ret.icon + "</span><br />" + ret.name + "</button>"; - itemhtml += "</li>"; - + var itemhtml = ""; + var itemtag = document.querySelector("[data-name=\""+ret[0].name+"\"]"); + if(!itemtag){ //si c'est la première itération de l'objet + itemhtml += "<li>"; + itemhtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"useItem('" + ret[0].name + "')\"><span class=\"item-icon\">" + ret[0].icon + "</span><br />" + ret[0].name + "(<span data-name=\""+ ret[0].name + "\">"+ret[1]+"</span>)</button>"; + itemhtml += "</li>"; + } + else{ // si c'est une n-ième itération + itemtag.innerHTML=ret[1]; + } + var invcontent = document.getElementById("tab3"); if(invcontent.children.length <= 1) @@ -40,13 +56,13 @@ function addItem(ret) { } else invcontent.getElementsByTagName('ul')[0].innerHTML += itemhtml; - showInfo(ret.desc); + showInfo("The "+ ret[0].name + " has been successfully purchased"); } function buyItem(name) { sendRequest("craftmine.php", "op=buyItem&item="+name, function(ret) { addItem(ret); - debitAccount(ret.cost); + debitAccount(ret[0].cost); }); } |