1
|
|
|
<?php |
2
|
|
|
namespace modules\bataille\app\controller; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use core\App; |
6
|
|
|
|
7
|
|
|
class Nourriture { |
8
|
|
|
private $date_last_check; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
12
|
|
|
public function __construct() { |
13
|
|
|
$last_check = $this->getLastCheckNourriture(); |
14
|
|
|
$nb_unite = Bataille::getUnite()->getNombreUniteHumaine(); |
15
|
|
|
|
16
|
|
|
if ($last_check >= 3600) { |
17
|
|
|
$this->setNourritureConsomee(round($last_check/3600), $nb_unite); |
18
|
|
|
$this->setUpdateLastCheckNourriture(); |
19
|
|
|
} |
20
|
|
|
else if (($last_check == 0) && ($nb_unite > 0)) { |
21
|
|
|
$this->setUpdateLastCheckNourriture(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (Bataille::getRessource()->getNourriture() == 0) { |
25
|
|
|
Bataille::setValues(["nourriture_mort_heure" => $this->getNombreUniteMortHeure()]); |
26
|
|
|
} |
27
|
|
|
else { |
28
|
|
|
Bataille::setValues(["nourriture_consom_heure" => $this->getConsommationNourritureHeure()]); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
36
|
|
|
/** |
37
|
|
|
* @return int |
38
|
|
|
* fonction qui renvoi la durée écoulée depuis le dernier check de la nourriture |
39
|
|
|
*/ |
40
|
|
|
private function getLastCheckNourriture() { |
41
|
|
|
$dbc= App::getDb(); |
42
|
|
|
|
43
|
|
|
$query = $dbc->select("last_check_nourriture")->from("_bataille_base") |
44
|
|
|
->where("ID_identite", "=", Bataille::getIdIdentite(), "AND") |
45
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
46
|
|
|
->get(); |
47
|
|
|
|
48
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
49
|
|
|
$today = Bataille::getToday(); |
50
|
|
|
|
51
|
|
|
foreach ($query as $obj) { |
52
|
|
|
$last_check = $obj->last_check_nourriture; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->date_last_check = $last_check; |
|
|
|
|
56
|
|
|
|
57
|
|
|
$last_co = new \DateTime($last_check); |
58
|
|
|
$last_co = $last_co->getTimestamp(); |
59
|
|
|
|
60
|
|
|
return $today-$last_co; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return 0; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return mixed |
68
|
|
|
* renvoi la consommation de nourriture d'une unité par heure |
69
|
|
|
*/ |
70
|
|
|
private function getConsommationNourritureUnite() { |
71
|
|
|
return Bataille::getParam("unite_nourriture_heure"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return number |
76
|
|
|
* renvoi le nombre d'unités qui meurrent à l'aheure quand plus de nourriture dans la base |
77
|
|
|
*/ |
78
|
|
|
private function getNombreUniteMortHeure() { |
79
|
|
|
$nb_unite = Bataille::getUnite()->getNombreUniteHumaine(); |
80
|
|
|
|
81
|
|
|
if ($nb_unite == 0) { |
82
|
|
|
return 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return abs(round((0-($nb_unite*$this->getConsommationNourritureUnite()))/100))+1; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return number |
90
|
|
|
* renvoi le nombre d'unités qui meurrent à l'aheure quand plus de nourriture dans la base |
91
|
|
|
*/ |
92
|
|
|
private function getConsommationNourritureHeure() { |
93
|
|
|
$nb_unite = Bataille::getUnite()->getNombreUniteHumaine(); |
94
|
|
|
|
95
|
|
|
return round(($nb_unite*$this->getConsommationNourritureUnite())); |
96
|
|
|
} |
97
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
|
101
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
102
|
|
|
/** |
103
|
|
|
* remet la date de last_check_nourriture a Y-m-d H:i:s |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
private function setUpdateLastCheckNourriture() { |
|
|
|
|
106
|
|
|
$dbc = App::getDb(); |
107
|
|
|
|
108
|
|
|
$dbc->update("last_check_nourriture", date("Y-m-d H:i:s"))->from("_bataille_base") |
109
|
|
|
->where("ID_identite", "=", Bataille::getIdIdentite(), "AND") |
110
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
111
|
|
|
->set(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $nb_heure |
116
|
|
|
* @param $nb_unite |
117
|
|
|
* fonction qui calcule la nourriture consomee en 1h |
118
|
|
|
* si elle est inférieur à 0 on tue un nombre d'unite defini en fonction de combien le nombre de |
119
|
|
|
* nourriture est en dessous de 0 (valeur divisée par 100) |
120
|
|
|
* puis on retire les ressources du grenier |
121
|
|
|
*/ |
122
|
|
|
private function setNourritureConsomee($nb_heure, $nb_unite) { |
123
|
|
|
$nourriture_base = Bataille::getRessource()->getNourriture(); |
124
|
|
|
$nourriture_consommee = ($nb_unite*$this->getConsommationNourritureUnite())*$nb_heure; |
125
|
|
|
|
126
|
|
|
$nourriture_retirer = $nourriture_base - $nourriture_consommee; |
127
|
|
|
|
128
|
|
|
if ($nourriture_retirer < 0) { |
129
|
|
|
$unite_tuer = (abs(round($nourriture_retirer/100)))+1; |
130
|
|
|
|
131
|
|
|
Bataille::getUnite()->setTuerUnites($unite_tuer); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
Bataille::getRessource()->setUpdateRessource(0, 0, 0, 0, $nourriture_consommee, "-"); |
135
|
|
|
} |
136
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
137
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: