aboutsummaryrefslogtreecommitdiffstats
path: root/inc/shop.inc
blob: 32ecea28fbd312001b2faf8c172fbe9691fdfa50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

require_once("messages.inc");
require_once("account.inc");
require_once("Item.inc");
require_once("Inventory.inc");

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) {
	$shop=loadShop();
	foreach($shop["items"] as $cat) {
		foreach($cat as $item){
			if($name == $item->name) {
				return $item;
			}
		}
	}
	sendError("shop_missing_item");
	return false;
}

function initShop() {
	$_SESSION["shop"] = true;
}

function sendShop() {
	if(!empty($_SESSION["shop"]))
		return loadShop();
	else return false;
}

function buildShop() {
	$shop=loadShop();
	if(!empty($_SESSION["shop"])) {
		sendError("shop_already_built");
	}
	elseif(debitAccount($shop["cost"])) {
		initShop();
		echo json_encode($shop);
	}
}

function buyItem() {
	$item = getItem($_POST["item"]);
	if($item && debitAccount($item->cost)) {
		$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);
}


?>