aboutsummaryrefslogtreecommitdiffstats
path: root/inc/shop.inc
blob: a0f9a8ac0b08f87fbd900e05bbe3ea274fd67c28 (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
<?php

require_once("messages.inc");
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 getItem($name) {
	global $shop;
	foreach($shop["items"] as $item) {
		if($name == $item->name) {
			return $item;
		}
	}
	sendError("shop_missing_item");
	return false;
}

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

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

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

function buyItem() {
	$item = getItem($_POST["item"]);
	if($item && debitAccount($item->cost)) {
		Inventory::addItem($item);
		echo json_encode($item);
	}
}


?>