aboutsummaryrefslogtreecommitdiffstats
path: root/inc/shop.inc
blob: 2dbd19dc859d867a252395fb0284c583534579ac (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
 * Manages the shop.
 *
 * @package inc\shop.inc
 * @author     Alexandre Renoux
 * @author     Pierre-Emmanuel Novac
 */

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

/**
 * Loads all the shop's items from the XML file data/items.xml.
 *
 * @return array  contains all the Item objects
 */
function loadShop(){
	$items = simplexml_load_file('data/items.xml'); //TODO: handle errors
	$shop = array("cost"=>(string)$items["cost"],"items"=>array()); //TODO: cost is a string?
	foreach($items as $cat){ // Loop over all categories
		$category = (string)$cat["name"];
		$shop["items"][$category] = array();
		foreach($cat as $item){ // Loop over all items inside a category
			$feats = array();
			foreach($item->features[0] as $k => $v) // Reads features
				$feats[(string)$k] = (string)$v;
			$shop["items"][$category][] = new Item(
				(string)$item->name,
				intval($item->cost), //Note: cost is an int here.
				(string)$item->icon,
				(string)$item->description,
				$feats);
		}
	}
	return $shop;
}

/**
 * Gets an Item object from the shop from its name.
 *
 * @params string $name  the name of the object to search for
 * @return Item|false  the Item object or false if the item was not found
 */
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;
}

/**
 * Marks the shop as created in the session.
 *
 * @return void
 */
function initShop() {
	$_SESSION["shop"] = true;
}

/**
 * Returns the shop array if it was created.
 *
 * @return array  shop array as created by loadShop
 */
function sendShop() {
	if(!empty($_SESSION["shop"]))
		return loadShop();
	else return false;
}

/**
 * Creates the shop in the session and sends it to the client.
 * Debits the shop's cost from the player's gold.
 *
 * @return void
 */
function buildShop() {
	$shop=loadShop();
	if(!empty($_SESSION["shop"])) {
		sendError("shop_already_built");
	}
	elseif(debitAccount($shop["cost"])) {
		initShop();
		echo json_encode($shop);
	}
}

/**
 * Debits the item's cost specified as the item POST parameter, adds it to the Inventory and sends it to the client.
 *
 * @return void
 */
function buyItem() {
	$item = getItem($_POST["item"]);
	if($item && debitAccount($item->cost)) {
		$tab = Inventory::addItem($item);
		echo json_encode($tab); // Sends an array with the Item as first member and count as second.
	}
}

/**
 * Invoke useItem on an item passed as the item POST parameter, sends to the client the updated player stats and the item.
 *
 * @return void
 */
function useItem(){
	$item = getItem($_POST["item"]);
	$it = Inventory::useItem($item);
	echo json_encode(array("perso" => sendPerso(), "item" => $it));
}


?>