aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--data/items.xml41
-rw-r--r--inc/shop.inc36
-rw-r--r--js/shop.js24
3 files changed, 78 insertions, 23 deletions
diff --git a/data/items.xml b/data/items.xml
new file mode 100644
index 0000000..528d37a
--- /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>50</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>200</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>100</cost>
+ <features></features>
+ <icon>🍶</icon>
+ <description>A bottle which can heal you by regaining hp</description>
+ </item>
+ <item>
+ <name>Strength Bottle</name>
+ <cost>250</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/inc/shop.inc b/inc/shop.inc
index 11cf97b..6e30923 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,21 +36,20 @@ 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();
$_SESSION["mine"]["gold"] -= $shop["cost"];
- echo json_encode(sendShop());
+ echo json_encode($shop);
}
}
diff --git a/js/shop.js b/js/shop.js
index 22ca85f..efd6ac5 100644
--- a/js/shop.js
+++ b/js/shop.js
@@ -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;
}
@@ -40,7 +48,7 @@ function addItem(ret) {
} else
invcontent.getElementsByTagName('ul')[0].innerHTML += itemhtml;
- showInfo(ret.desc);
+ showInfo("The "+ ret.name + " has been successfully purchased");
}
function buyItem(name) {