diff options
-rw-r--r-- | craftmine.php | 9 | ||||
-rw-r--r-- | css/craftmine.css | 6 | ||||
-rw-r--r-- | data/items.xml | 41 | ||||
-rw-r--r-- | data/monsters.xml | 72 | ||||
-rw-r--r-- | inc/Inventory.inc | 30 | ||||
-rw-r--r-- | inc/Monster.inc | 21 | ||||
-rw-r--r-- | inc/craftmine.inc | 6 | ||||
-rw-r--r-- | inc/dungeon.inc | 65 | ||||
-rw-r--r-- | inc/messages.inc | 2 | ||||
-rw-r--r-- | inc/mine.inc | 2 | ||||
-rw-r--r-- | inc/perso.inc | 23 | ||||
-rw-r--r-- | inc/shop.inc | 46 | ||||
-rw-r--r-- | index.xhtml | 6 | ||||
-rw-r--r-- | js/craftmine.js | 26 | ||||
-rw-r--r-- | js/dungeon.js | 121 | ||||
-rw-r--r-- | js/perso.js | 5 | ||||
-rw-r--r-- | js/shop.js | 58 |
17 files changed, 500 insertions, 39 deletions
diff --git a/craftmine.php b/craftmine.php index 9a5d3d7..9a8806b 100644 --- a/craftmine.php +++ b/craftmine.php @@ -4,7 +4,9 @@ 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"); +require_once("inc/perso.inc"); session_start(); // Must be placed *BEFORE* $_SESSION is actually used and *AFTER* all classes are imported @@ -47,6 +49,13 @@ switch($op) { case "getCraftMine": sendCraftMine(); break; case "buildShop": buildShop(); break; case "buyItem": buyItem(); break; + case "useItem": useItem(); break; + case "buildDungeon" : buildDungeon(); break; + case "launchDungeon" : launchDungeon(); break; + case "exitDungeon" : exitDungeon(); break; + case "sendDungeonProgress" : sendDungeonProgress(); break; + case "updatePerso" : updatePerso(); break; + case "sendPerso" : sendPerso(); 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..6c19b84 --- /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..ab29364 --- /dev/null +++ b/data/monsters.xml @@ -0,0 +1,72 @@ +<?xml version="1.0" encoding="utf-8"?> +<dungeon cost="10"> + <floor name="floor1"> + <monster> + <name>monster1</name> + <level>1</level> + <hp>3</hp> + <xp>1</xp> + <icon>m</icon> + </monster> + <monster > + <name>monster2</name> + <level>2</level> + <hp>3</hp> + <xp>2</xp> + <icon>m</icon> + </monster> + <monster> + <name>monster3</name> + <level>3</level> + <hp>4</hp> + <xp>3</xp> + <icon>m</icon> + </monster> + </floor> + <floor name="floor2"> + <monster> + <name>monster4</name> + <level>4</level> + <hp>5</hp> + <xp>4</xp> + <icon>m</icon> + </monster> + <monster> + <name>monster5</name> + <level>5</level> + <hp>6</hp> + <xp>5</xp> + <icon>m</icon> + </monster> + <monster> + <name>monster5</name> + <level>6</level> + <hp>7</hp> + <xp>6</xp> + <icon>m</icon> + </monster> + </floor> + <floor name="floor3"> + <monster> + <name>monster6</name> + <level>8</level> + <hp>9</hp> + <xp>8</xp> + <icon>m</icon> + </monster> + <monster> + <name>monster7</name> + <level>9</level> + <hp>10</hp> + <xp>9</xp> + <icon>m</icon> + </monster> + <monster> + <name>Boss</name> + <level>10</level> + <hp>12</hp> + <xp>10</xp> + <icon>m</icon> + </monster> + </floor> +</dungeon> 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); +} + ?> diff --git a/index.xhtml b/index.xhtml index 5e0752a..5e2f18d 100644 --- a/index.xhtml +++ b/index.xhtml @@ -15,7 +15,9 @@ <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> + <script type="text/javascript" charset="utf-8" src="js/perso.js"></script> </head> <body onload="init()" onhashchange="changeTab()"> <div class="container-fluid"> @@ -23,6 +25,9 @@ <h1>CraftMine</h1> </div> <div class="row"> + <span id="perso">HP : <span id="hp">5</span>/ LV : <span id="lv">1</span>/ exp : <span id="xp">0</span></span> +</div> +<div class="row"> <div class="col-md-4"> <div class="row"> <div class="col-md-10"> @@ -71,6 +76,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..b87f946 100644 --- a/js/craftmine.js +++ b/js/craftmine.js @@ -1,8 +1,12 @@ data = { + name: "You", gold: 0, mine: 0, miners: 0, - level: 1 + level: 3, + hp: 5, + xp: 0, + icon : "H" } function sendRequest(url, params, callback, isFile) { @@ -11,7 +15,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; @@ -46,11 +53,22 @@ function withdrawMine() { function initCraftMine() { sendRequest("craftmine.php", "op=getCraftMine", function(ret) { + console.log(ret.perso); data.gold = parseInt(ret.gold); // Server's response is a string + data.mine = 0; // Reset mine + if(ret.perso){ + data.hp = ret.perso.hp; + data.xp = ret.perso.xp; + data.level = ret.perso.lv; + displayPerso(ret.perso.hp,ret.perso.xp,ret.perso.lv); + } if(ret.shop) displayShop(ret.shop); - displayInventory(ret.inventory); + if(ret.inventory) displayInventory(ret.inventory); + if(ret.dungeon == false){}//if we have left the donjon + else if(typeof ret.dungeon.mob == "undefined") displayDungeon(0,1,true); //if we have reload just after buying the ticket + else displayDungeon(ret.dungeon.mob,ret.dungeon.flat,true);//if we have reload in the middle of the dungeon 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..982dcf6 --- /dev/null +++ b/js/dungeon.js @@ -0,0 +1,121 @@ +var timeout; + +function buildDungeon(){ + sendRequest("craftmine.php", "op=buildDungeon", function(ret) { + displayDungeon(0,1,true);//mob 0 in the floor 1 and I access the dungeon for the first dungeon + debitAccount(ret.cost); + showInfo("You can acces the dungeon now. Good Luck."); + }); +} + +function displayDungeon(nb,f,firstTime){ + var tmphtml = "<br/>"; + tmphtml += "<button id=\"launch\" type=\"button\" class=\"btn btn-primary\" onclick=\"launchDungeon("+nb+","+f+","+firstTime+")\">Launch</button>"; + document.getElementById("tab4").innerHTML = tmphtml; +} + +function launchDungeon(nb,f,firstTime){ + sendRequest("craftmine.php", "op=launchDungeon&floor="+f, function(ret) { + if(nb==0 && firstTime){ + document.getElementById("launch").style.display = "none"; + } + displayBattle(ret,nb,f); + }); +} + +function displayBattle(ret,nb,f){ + var tmphtml = "<div class=\"row\">"; + tmphtml += "<h4>Battle floor "+ f +"</h4>"; + tmphtml += "<ul class=\"list-inline\">"; + tmphtml += "<li>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" id=\"perso\" style=\"margin-left:30px;\"><span class=\"item-icon\">" + data.icon + "</span><br />" + data.name + "<br/> lv: " + data.level + " hp: <span id=\"lifePerso\">" + data.hp + "</span></button>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" id=\"mob\" style=\"margin-left:30px;\">" + +"<span class=\"item-icon\">" + ret[nb].icon + "</span><br />" + ret[nb].name + "<br/>" + +" lv: " + ret[nb].level + " hp: <span id=\"lifeMob\">" + ret[nb].hp + "</span></button>"; + tmphtml += "</li>"; + tmphtml += "</ul>"; + tmphtml += "</div>"; + document.getElementById("tab4").innerHTML = tmphtml; + displayExit(); + battle(ret,nb,f); +} + +function battle(ret,nb,f){ + timeout=window.setTimeout(strike, 1000, ret, nb, f); +} + +function strike(ret,nb, f){ + var lvDiff = data.level-parseInt(ret[0].level); + var hitRate = Math.floor((Math.random() * 100) + 1); + var mobLife = document.getElementById("lifeMob").innerHTML; + var persoLife = document.getElementById("lifePerso").innerHTML; + if(mobLife == 0){ + data.hp = persoLife; + endBattle("perso",nb,f,ret); + return; + } + else if(persoLife == 0){ + data.hp = 1; + endBattle("mob",nb,f,ret); + return; + } + if(hitRate<50+10*lvDiff){ + mobLife--; + document.getElementById("lifeMob").innerHTML = parseInt(mobLife); + } + else{ + persoLife--; + document.getElementById("lifePerso").innerHTML = parseInt(persoLife); + } + battle(ret,nb,f); +} + +function endBattle(v,nb, f, ret){ + window.clearTimeout(timeout); + if(v=="mob")exitDungeon(false);//if you die in the dungeon, you are immediately sent out of the dungeon + //To level up you have to obtain 2 xp to go to lv 3, 3 to go to lv 4, etc + //A mob level 2 , if defeated gives you 2 xp, ...etc + else{ + data.xp += parseInt(ret[nb].xp); + if(data.xp>=data.level){ + data.xp-=data.level; + data.level++; + //need to send the xp to the server + sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&xp="+data.xp+"&lv="+data.level, function(){ + displayPerso(data.hp,data.xp,data.level); + }); + } + nb++;//go to the next mob in the same floor + if(nb>=3){//floor changing + nb=0;//reset the number of the mob + f++;//increment the number of the floor + if(f>=4){ + exitDungeon(true);//true means that you have completed the dungeon and not just die or exit by yourself + return; + } + } + var tmphtml = "Le " + v + " a gagné."; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"launchDungeon("+nb+","+ f +",false)\">Next Battle</button>" + document.getElementById("tab4").innerHTML = tmphtml; + displayExit(); + sendRequest("craftmine.php", "op=sendDungeonProgress&floor="+f+"&mob="+nb); + } + sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&xp="+data.xp+"&lv="+data.level, function(){ + displayPerso(data.hp,parseInt(data.xp),data.level); + }); +} + +function displayExit(){ + var tmphtml = "<br/><br/>"; + tmphtml += "<button type=\"button\" class=\"btn btn-primary\" onclick=\"exitDungeon(false)\">Exit</button>"; + document.getElementById("tab4").innerHTML += tmphtml; +} + +function exitDungeon(boss){ + sendRequest("craftmine.php", "op=exitDungeon", function() { + window.clearTimeout(timeout); + document.getElementById("tab4").innerHTML = "<h4>Not available, you have to buy a ticket in the build section.</h4>"; + if(boss) showInfo("You have beaten the final boss! CONGRATULATIONS!"); + showInfo("You have left the dungeon"); + }); +} diff --git a/js/perso.js b/js/perso.js new file mode 100644 index 0000000..365cf54 --- /dev/null +++ b/js/perso.js @@ -0,0 +1,5 @@ +function displayPerso(hp,xp,lv){ + document.getElementById("hp").innerHTML = hp; + document.getElementById("lv").innerHTML = lv; + document.getElementById("xp").innerHTML = xp; +} @@ -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 id=\"nbItem\" 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,18 +56,34 @@ 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); }); } function useItem(name) { sendRequest("craftmine.php", "op=useItem&item="+name, function(ret) { + if(parseInt(ret[1])>=0){ + var nb = parseInt(document.getElementById("nbItem").innerHTML); + if(nb>0){ + nb--; + switch(ret[0].name){ + case "Life Bottle": data.hp = parseInt(data.hp) + 3; break; + case "Strength Bottle" : break; // to do + case "Wooden Sword" : break; //to do + case "Metal Sword" : break; //to do + } + } + document.getElementById("nbItem").innerHTML = nb; + sendRequest("craftmine.php", "op=updatePerso&hp="+data.hp+"&xp="+data.xp+"&lv="+data.level, function(){ + displayPerso(data.hp,data.xp,data.level); + }); + } }); } |