aboutsummaryrefslogtreecommitdiffstats
path: root/inc/dungeon.inc
blob: 05dc57dbdd8416bd19e341b98e0fbc2509b434d9 (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
<?php
/**
 * Manages the dungeon.
 *
 * @author     Alexandre Renoux
 * @author     Pierre-Emmanuel Novac
 */

require_once("messages.inc");
require_once("account.inc");
require_once("Monster.inc");

/**
 * Loads all the dungeon's monsters from the XML file data/monsters.xml.
 *
 * @return array  contains all the Monster objects
 */
function generateMonster(){
	$monsters = simplexml_load_file('data/monsters.xml');
	$dungeon = array("cost"=>(string)$monsters["cost"],"monsters"=>array()); //TODO: again, cost is a string?
	foreach($monsters as $f){
		$floor = (string)$f["name"];
		$dungeon["monsters"][$floor] = array();
		foreach($f as $monster){
			$dungeon["monsters"][$floor][] = new Monster((string)$monster->name, 
											intval($monster->level),
											intval($monster->hp),
											intval($monster->xp), 
											(string)$monster->icon);
		}
	}
	return $dungeon;
}

/**
 * Marks the dungeon as accessible in the session.
 *
 * @return void
 */
function initDungeon() {
	$_SESSION["dungeon"]["access"] = true; // TODO: is the $_SESSION["dungeon"] array useful (and created beforehand)?
}

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

/**
 * Allows acces to the dungeon in the session and sends it to the client.
 * Debits the dungeon's ticket cost from the player's gold.
 *
 * @return void
 */
function buildDungeon() {
	$dungeon=generateMonster();
	if(!empty($_SESSION["dungeon"])) {
		sendError("dungeon_already_available");
	}
	elseif(debitAccount($dungeon["cost"])) {
		initDungeon();
		$_SESSION["mine"]["gold"] -= $dungeon["cost"]; // TODO: Account was already debited
		echo json_encode($dungeon);
	}
}

/**
 * Sends monsters for a specific floor as specified by the floor POST parameter to the client.
 *
 * @return void
 */
function launchDungeon(){
	$f= $_POST["floor"];
	$dungeon=generateMonster();
	$opponent = $dungeon["monsters"]["floor".$f];
	echo json_encode($opponent);
}

/**
 * Updates floor and monster number from the POST parameters in the session.
 *
 * @return void
 */
function sendDungeonProgress(){
	$f= $_POST["floor"];
	$nb=$_POST["mob"];
	$_SESSION["dungeon"]["flat"] = $f;
	$_SESSION["dungeon"]["mob"] = $nb;
}

/**
 * Marks the dungeon as not accessible in the session.
 *
 * @return void
 */

function exitDungeon(){
	$_SESSION["dungeon"] = false;
}

?>