|
1
|
|
|
<?php |
|
2
|
|
|
namespace modules\bataille\app\controller; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use core\App; |
|
6
|
|
|
use core\HTML\flashmessage\FlashMessage; |
|
7
|
|
|
|
|
8
|
|
|
class Marche { |
|
9
|
|
|
private $id_base_dest; |
|
10
|
|
|
private $aller; |
|
11
|
|
|
private $ressources; |
|
12
|
|
|
private $date_arrivee; |
|
13
|
|
|
private $nom_base; |
|
14
|
|
|
private $id_marche_transport; |
|
15
|
|
|
private $duree_restante_trajet; |
|
16
|
|
|
|
|
17
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
|
18
|
|
|
public function __construct() { |
|
19
|
|
|
$dbc = App::getDb(); |
|
20
|
|
|
|
|
21
|
|
|
//récupération des trajets en cours |
|
22
|
|
|
$query = $dbc->select()->from("_bataille_marche_transport") |
|
23
|
|
|
->from("_bataille_base") |
|
24
|
|
|
->where("_bataille_marche_transport.ID_base", "=", Bataille::getIdBase(), "AND") |
|
25
|
|
|
->where("_bataille_marche_transport.ID_base_dest", "=", "_bataille_base.ID_base", "", true) |
|
26
|
|
|
->get(); |
|
27
|
|
|
|
|
28
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
|
29
|
|
|
foreach ($query as $obj) { |
|
30
|
|
|
$this->id_base_dest = $obj->ID_base_dest; |
|
31
|
|
|
$this->aller = $obj->aller; |
|
32
|
|
|
$this->ressources = $obj->ressources; |
|
33
|
|
|
$this->nom_base = $obj->nom_base; |
|
34
|
|
|
$this->date_arrivee = $obj->date_arrivee; |
|
35
|
|
|
$this->id_marche_transport = $obj->ID_marche_transport; |
|
36
|
|
|
|
|
37
|
|
|
$marche = $this->getTransportArrive(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
Bataille::setValues(["marche" => $marche]); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
|
48
|
|
|
/** |
|
49
|
|
|
* fonction qui permet de savoir si un transport est arrivé à la base de destination |
|
50
|
|
|
* +si arrivé appel la fonction pour ajouter les ressources et passe le trajet sur le retour |
|
51
|
|
|
* ou en trajet fini suivant le temps de la date d'aujourd'hui et la date à laquelle le |
|
52
|
|
|
* trajet aurait du revenir |
|
53
|
|
|
*/ |
|
54
|
|
|
private function getTransportArrive() { |
|
55
|
|
|
$today = Bataille::getToday(); |
|
56
|
|
|
|
|
57
|
|
|
echo "today : ".$today." ++ date arrivee : "; |
|
58
|
|
|
echo $this->date_arrivee."<br>"; |
|
59
|
|
|
|
|
60
|
|
|
//on test si déja arrivé à destination |
|
61
|
|
|
if (($this->aller == 1) && (($this->date_arrivee-$today) <= 0)) { |
|
62
|
|
|
$this->setLivrerRessource(); |
|
63
|
|
|
|
|
64
|
|
|
//on calcul la date d'arrivée du retour |
|
65
|
|
|
$date_retour = Bataille::getDureeTrajet($this->id_base_dest)+$this->date_arrivee; |
|
66
|
|
|
|
|
67
|
|
|
//si le retour du trajet est également arrivé on finit le transport sinon on le place sur le retour |
|
68
|
|
|
if ($date_retour < $today) { |
|
69
|
|
|
$this->setTerminerTransport(); |
|
70
|
|
|
} |
|
71
|
|
|
else { |
|
72
|
|
|
$this->setTrajetRetour($date_retour); |
|
73
|
|
|
$this->duree_restante_trajet = $date_retour-$today; |
|
74
|
|
|
$set_array = true; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
else if (($this->aller == 0) && (($this->date_arrivee-$today) <= 0)) { |
|
78
|
|
|
$this->setTerminerTransport(); |
|
79
|
|
|
} |
|
80
|
|
|
else { |
|
81
|
|
|
$this->duree_restante_trajet = $this->date_arrivee-$today; |
|
82
|
|
|
$set_array = true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($set_array === true) { |
|
|
|
|
|
|
86
|
|
|
if ($this->aller == 1) { |
|
87
|
|
|
$marche["aller"][] = [ |
|
|
|
|
|
|
88
|
|
|
"id_marche_transport" => $this->id_marche_transport, |
|
89
|
|
|
"date_arrivee" => $this->duree_restante_trajet, |
|
90
|
|
|
"nom_base_dest" => $this->nom_base |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
else { |
|
94
|
|
|
$marche["retour"][] = [ |
|
|
|
|
|
|
95
|
|
|
"id_marche_transport" => $this->id_marche_transport, |
|
96
|
|
|
"date_arrivee" => $this->duree_restante_trajet, |
|
97
|
|
|
"nom_base_dest" => $this->nom_base |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $marche; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param $all_ressource |
|
107
|
|
|
* @return bool |
|
108
|
|
|
* fonction qui renvoi true si on a assez de marchand pour ce trajet dans la base |
|
109
|
|
|
* sinon on renvoi false |
|
110
|
|
|
*/ |
|
111
|
|
|
private function getAssezMarchand($all_ressource) { |
|
112
|
|
|
$dbc = App::getDb(); |
|
113
|
|
|
|
|
114
|
|
|
//récupération du nombre max du marchand dispo dans la base |
|
115
|
|
|
$nombre_max_marchand = Bataille::getBatiment()->getNiveauBatiment("marche"); |
|
116
|
|
|
|
|
117
|
|
|
//on récupère tous les marchands qui sont en transport |
|
118
|
|
|
$query = $dbc->select("nb_marchand")->from("_bataille_marche_transport") |
|
119
|
|
|
->where("ID_base", "=", Bataille::getIdBase(), "OR") |
|
120
|
|
|
->where("ID_base_dest", "=", Bataille::getIdBase()) |
|
121
|
|
|
->get(); |
|
122
|
|
|
|
|
123
|
|
|
$marchand_transport = 0; |
|
124
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
|
125
|
|
|
foreach ($query as $obj) { |
|
126
|
|
|
$marchand_transport += $obj->nb_marchand; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
//on a le nombre de marchand dispo dans la base |
|
131
|
|
|
$nombre_marchand_dispo = $nombre_max_marchand-$marchand_transport; |
|
132
|
|
|
|
|
133
|
|
|
//on calcul savoir si on en a assez pour transport toutes les ressoures |
|
134
|
|
|
//il faut 1 marchand pour 1000 ressource |
|
135
|
|
|
$nombre_marchand_trajet = ceil($all_ressource/1000); |
|
136
|
|
|
|
|
137
|
|
|
//si on a assez de marchand on revoi true sinon false |
|
138
|
|
|
if ($nombre_marchand_dispo >= $nombre_marchand_trajet) { |
|
139
|
|
|
return $nombre_marchand_trajet; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
|
149
|
|
|
/** |
|
150
|
|
|
* fonction qui permet d'ajouter les ressources à la base destinatrice du transport |
|
151
|
|
|
*/ |
|
152
|
|
|
private function setLivrerRessource() { |
|
153
|
|
|
$ressource = new Ressource($this->id_base_dest); |
|
154
|
|
|
|
|
155
|
|
|
$ressource_transport = unserialize($this->ressources); |
|
156
|
|
|
|
|
157
|
|
|
$ressource->setUpdateRessource($ressource_transport['eau'], $ressource_transport['electricite'], $ressource_transport['fer'], $ressource_transport['fuel'], $ressource_transport['nourriture'], "+"); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* fonction qui place le trajet en retour |
|
162
|
|
|
*/ |
|
163
|
|
|
private function setTrajetRetour($date_retour) { |
|
164
|
|
|
$dbc = App::getDb(); |
|
165
|
|
|
|
|
166
|
|
|
$dbc->update("ressources", 0) |
|
167
|
|
|
->update("aller", 0) |
|
168
|
|
|
->update("date_arrivee", $date_retour) |
|
169
|
|
|
->from("_bataille_marche_transport") |
|
170
|
|
|
->where("ID_marche_transport", "=", $this->id_marche_transport) |
|
171
|
|
|
->set(); |
|
172
|
|
|
|
|
173
|
|
|
$this->aller = 0; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* permet de terminer totallement un transport |
|
178
|
|
|
*/ |
|
179
|
|
|
private function setTerminerTransport() { |
|
180
|
|
|
$dbc = App::getDb(); |
|
181
|
|
|
|
|
182
|
|
|
$dbc->delete()->from("_bataille_marche_transport")->where("ID_marche_transport", "=", $this->id_marche_transport)->del(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param $eau |
|
187
|
|
|
* @param $electricite |
|
188
|
|
|
* @param $fer |
|
189
|
|
|
* @param $fuel |
|
190
|
|
|
* @param $nourriture |
|
191
|
|
|
* @param $posx |
|
192
|
|
|
* @param $posy |
|
193
|
|
|
* @return bool |
|
194
|
|
|
* Fonction qui permet d'initialiser un transport de ressources d'une base à une autre |
|
195
|
|
|
*/ |
|
196
|
|
|
public function setCommencerTransport($eau, $electricite, $fer, $fuel, $nourriture, $posx, $posy) { |
|
197
|
|
|
$dbc = App::getDb(); |
|
198
|
|
|
$id_base_dest = Bataille::getBaseExistPosition($posx, $posy); |
|
199
|
|
|
|
|
200
|
|
|
if ($id_base_dest != 0) { |
|
201
|
|
|
$ressource["eau"] = Bataille::getTestAssezRessourceBase("eau", $eau); |
|
|
|
|
|
|
202
|
|
|
$ressource["electricite"] = Bataille::getTestAssezRessourceBase("electricite", $electricite); |
|
203
|
|
|
$ressource["fer"] = Bataille::getTestAssezRessourceBase("fer", $fer); |
|
204
|
|
|
$ressource["fuel"] = Bataille::getTestAssezRessourceBase("fuel", $fuel); |
|
205
|
|
|
$ressource["nourriture"] = Bataille::getTestAssezRessourceBase("nourriture", $nourriture); |
|
206
|
|
|
|
|
207
|
|
|
//si pas assez de ressources dispo dans la base pour l'envoi on renvoi erreur |
|
208
|
|
|
foreach ($ressource as $tab) { |
|
209
|
|
|
if (in_array("rouge", $tab)) { |
|
210
|
|
|
FlashMessage::setFlash("Vous n'avez pas autant de ressources disponibles à l'envoi"); |
|
211
|
|
|
return false; |
|
212
|
|
|
}; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
//on check si assez marchand dans la base, si pas assez on return false |
|
216
|
|
|
$nb_marchand = $this->getAssezMarchand($eau+$electricite+$fer+$fuel+$nourriture); |
|
217
|
|
|
|
|
218
|
|
|
if ($nb_marchand === false) { |
|
219
|
|
|
FlashMessage::setFlash("Vous n'avez pas assez de marchans disponibles pour effectuer ce trajet"); |
|
220
|
|
|
return false; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
//sinon initialise le transport |
|
224
|
|
|
//on recup la date d'arrivee dans la base de destintation |
|
225
|
|
|
$date_arrivee = Bataille::getDureeTrajet($id_base_dest)+Bataille::getToday(); |
|
226
|
|
|
|
|
227
|
|
|
$ressource = [ |
|
228
|
|
|
"eau" => $eau, |
|
229
|
|
|
"electricite" => $electricite, |
|
230
|
|
|
"fer" => $fer, |
|
231
|
|
|
"fuel" => $fuel, |
|
232
|
|
|
"nourriture" => $nourriture, |
|
233
|
|
|
]; |
|
234
|
|
|
|
|
235
|
|
|
//on insert le transport dans la table |
|
236
|
|
|
$dbc->insert("date_arrivee", $date_arrivee) |
|
237
|
|
|
->insert("ressources", serialize($ressource)) |
|
238
|
|
|
->insert("aller", 1) |
|
239
|
|
|
->insert("nb_marchand", $nb_marchand) |
|
240
|
|
|
->insert("ID_base_dest", $id_base_dest) |
|
241
|
|
|
->insert("ID_base", Bataille::getIdBase()) |
|
242
|
|
|
->into("_bataille_marche_transport") |
|
243
|
|
|
->set(); |
|
244
|
|
|
|
|
245
|
|
|
//on retire les ressources de la base |
|
246
|
|
|
Bataille::getRessource()->setUpdateRessource($eau, $electricite, $fer, $fuel, $nourriture, "-"); |
|
247
|
|
|
|
|
248
|
|
|
FlashMessage::setFlash("Votre transport vient de partir !", "info"); |
|
249
|
|
|
return true; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
FlashMessage::setFlash("Aucune base présente aux coordonnées données"); |
|
253
|
|
|
return false; |
|
254
|
|
|
} |
|
255
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
|
256
|
|
|
} |
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: