aboutsummaryrefslogtreecommitdiffstats
path: root/inc/perso.inc
blob: e38fb4c1cbcdc162be6b54fba07bc99bb705e97a (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
124
<?php
/**
 * Manages player's stats: life, experience, level.
 *
 * @package inc\perso.inc
 * @author     Alexandre Renoux
 * @author     Pierre-Emmanuel Novac
 */

/**
 * Returns all player's stats after initializing them if needed.
 *
 * @return int[]  arrays containing player's stats
 */
function sendPerso() {
	if(empty($_SESSION["perso"]))
		initPerso();
	return $_SESSION["perso"];
}

/**
 * Increases a player's stat.
 *
 * @param int  $prop  stat to increase
 * @return void
 */
function increasePerso($prop, $num) {
	if(empty($_SESSION["perso"]))
		initPerso();
	$_SESSION["perso"][$prop] += $num;
	if($_SESSION["perso"]["hp"] > $_SESSION["perso"]["maxHP"]){//if you want to heal even if you have less than 3 hp to heal, heal until the max is attained
		$diff = $_SESSION["perso"]["hp"] - $_SESSION["perso"]["maxHP"];
		$_SESSION["perso"]["hp"] -= $diff;
	} 
}

/**
  *traite le fait que wooden sword n'est pas cumulable
  *metal sword non plus
  *life bottle cumulable 3 fois
  *si on clique sur wooden sword alors que on avait une metal sword, le bonusPower passe de +3 à +1 
  */
function limitUse($item){
	$n = $item->name;
	if(empty($item->feat["limit"])) return true;
	if(empty($_SESSION["usedItem"])){
		initUsedItem();
		$_SESSION["usedItem"][$n]["nbUse"]=1;
		return true;
	}
	else{
		if($_SESSION["usedItem"][$n]["nbUse"] >= $item->feat["limit"])return false; 
		else{
			$_SESSION["usedItem"][$n]["nbUse"]++;
			return true;
		}
	}
}

function initUsedItem(){ 
	$_SESSION["usedItem"]["Strength Bottle"]["longevity"] = 0;//if longevity equals 2, it means that the item was used during 2 battles
	$_SESSION["usedItem"]["Wooden Sword"]["longevity"] = 0;
	$_SESSION["usedItem"]["Metal Sword"]["longevity"] = 0;
	$_SESSION["usedItem"]["Strength Bottle"]["nbUse"] = 0;
	$_SESSION["usedItem"]["Wooden Sword"]["nbUse"] = 0;
	$_SESSION["usedItem"]["Metal Sword"]["nbUse"] = 0;
}

function reusable(){
	if(empty($_SESSION["usedItem"])){
		initUsedItem();
	}
	$_SESSION["usedItem"]["Strength Bottle"]["nbUse"]=0;
	if($_SESSION["usedItem"]["Wooden Sword"]["nbUse"]>=1){
		$_SESSION["usedItem"]["Wooden Sword"]["longevity"]++;
		if($_SESSION["usedItem"]["Wooden Sword"]["longevity"]>=2){
			$_SESSION["perso"]["powerBonus"] -= 1;
			$_SESSION["usedItem"]["Wooden Sword"]["nbUse"]=0;
		}
	}
	else if($_SESSION["usedItem"]["Metal Sword"]["nbUse"]>=1){
		$_SESSION["usedItem"]["Metal Sword"]["longevity"]++;
		if($_SESSION["usedItem"]["Metal Sword"]["longevity"]>=2){
			$_SESSION["perso"]["powerBonus"] -= 3;
			$_SESSION["usedItem"]["Metal Sword"]["nbUse"]=0;
		}
	}
}

/**
 * Copies stats given in POST request to session.
 *
 * @return void
 */
function updatePerso(){
	$hp = $_POST["hp"];
	$maxHP = $_POST["maxHP"];
	$xp = $_POST["xp"];
	$lv = $_POST["lv"];
	$power = $_POST["power"];
	$bonusPower = $_POST["bonusPower"];
	$_SESSION["perso"]["hp"] = +$hp;
	$_SESSION["perso"]["maxHP"] = +$maxHP;
	$_SESSION["perso"]["xp"] = +$xp;
	$_SESSION["perso"]["lv"] = +$lv;
	$_SESSION["perso"]["power"] = +$power;
	$_SESSION["perso"]["bonusPower"] = +$bonusPower;
}

/**
 * Initializes the player's stats.
 *
 * @return void
 */
function initPerso(){
	$_SESSION["perso"]["hp"] = 5;
	$_SESSION["perso"]["maxHP"] = 5;
	$_SESSION["perso"]["xp"] = 0;
	$_SESSION["perso"]["lv"] = 3;
	$_SESSION["perso"]["power"] = 3;
	$_SESSION["perso"]["bonusPower"] = 0;
}

?>