aboutsummaryrefslogtreecommitdiffstats
path: root/inc/guild.inc
blob: 537b87abeffd7941f9078d533b10c38d00a82c75 (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
/**
 * Manages miners guild.
 *
 * @package inc\guild.inc
 * @author     Alexandre Renoux
 * @author     Pierre-Emmanuel Novac
 */

/**
 * Amount of gold required to build the miners guild.
 */
define("GUILD_COST",10);
/**
 * Amount of gold required to hire a miner.
 */
define("MINER_COST",5);

function initMinersIfNeeded() {
	if(empty($_SESSION["mine"]))
		$_SESSION["mine"] = array("miners" => 0);
	else if(!array_key_exists("miners", $_SESSION["mine"]))
		$_SESSION["mine"]["miners"] = 0;
}

/**
 * Create the miners guild in the session.
 * Debits GUILD_COST from the player's gold.
 *
 * @return void
 */
function createGuild(){
	if(!empty($_SESSION["guild"])) {
		sendError("guild_already_built");
	}
	elseif(debitAccount(GUILD_COST)) {
		$_SESSION["guild"] = true;
		echo json_encode(array("cost" => GUILD_COST));
	}
}

/**
 * Hire one miner.
 * Debits MINER_COST from the player's gold.
 *
 * @return void
 */
function hireMiner(){
	if(!isset($_SESSION["guild"])){
		sendError("guild_not_yet_created");
	}
	elseif(debitAccount(MINER_COST)){
		initMinersIfNeeded();
		$_SESSION["mine"]["miners"]++;
		echo json_encode(array("cost" => MINER_COST , "miners" => $_SESSION["mine"]["miners"]));	
	}
}

/**
 * Returns the number of miners currently in the guild.
 *
 * @return int  number of miners in the guild
 */
function sendMiners(){
	initMinersIfNeeded();
	return $_SESSION["mine"]["miners"];
}
	

?>