aboutsummaryrefslogtreecommitdiffstats
path: root/inc/mine.inc
blob: fb5f612fcb50f2a4a72dad34f149c774cd2c2ddf (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
<?php
/**
 * Manages the mine.
 *
 * @packageĀ inc\mine.inc
 * @author     Alexandre Renoux
 * @author     Pierre-Emmanuel Novac
 */

/**
 * Initializes the gold amount and miners count in the session.
 *
 * @return void
 */
function initMineIfNeeded() {
	if(empty($_SESSION["mine"]))
		$_SESSION["mine"] = array("gold" => 0);
	else if(!array_key_exists("gold", $_SESSION["mine"]))
		$_SESSION["mine"]["gold"] = 0;
}

/**
 * Transfers all gold from the mine to the player's account.
 *
 * @return void
 */
function withdrawMine() {
	$amount = intval($_POST["amount"]);
	if(!empty($_SESSION["dungeon"]) && !empty($_SESSION["dungeon"]["flat"])) { // player in dungeon
		sendError("cant_withdraw_in_dungeon");
		return;
	}
	if($amount == 0) return;
	initMineIfNeeded();
	$_SESSION["mine"]["gold"] += $amount;
	echo json_encode($_SESSION["mine"]["gold"]);
}

/**
 * Returns the amount of gold currently owned by the player.
 *
 * @return int  amount of gold available
 */
function sendMine() {
	initMineIfNeeded();
	$mine = $_SESSION["mine"];
	return $mine["gold"];
}


?>