1
|
|
|
<?php |
2
|
|
|
namespace modules\bataille\app\controller; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use core\App; |
6
|
|
|
use core\functions\DateHeure; |
7
|
|
|
use core\HTML\flashmessage\FlashMessage; |
8
|
|
|
|
9
|
|
|
class MissionsAleatoire { |
10
|
|
|
private $last_check_mission; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
14
|
|
|
/** |
15
|
|
|
* MissionsAleatoire constructor. |
16
|
|
|
* le constructeur s'occupe de vérifier le last_check des missions et au cas ou si il est plus vieux d'un jour |
17
|
|
|
* appeler la fonction pour recharger les missions |
18
|
|
|
*/ |
19
|
|
|
public function __construct() { |
20
|
|
|
$dbc = App::getDb(); |
21
|
|
|
|
22
|
|
|
$query = $dbc->select("last_check_mission")->from("_bataille_base")->where("ID_base", "=", Bataille::getIdBase())->get(); |
23
|
|
|
|
24
|
|
|
if (is_array($query) && (count($query) == 1)) { |
25
|
|
|
foreach ($query as $obj) { |
26
|
|
|
$this->last_check_mission = $obj->last_check_mission; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if ($this->last_check_mission == "") { |
30
|
|
|
$this->setUpdateLastCheckMissions(); |
31
|
|
|
$this->setMissionsAleatoire(); |
32
|
|
|
} |
33
|
|
|
else { |
34
|
|
|
$today = Bataille::getToday(); |
35
|
|
|
$interval = $today-$this->last_check_mission; |
36
|
|
|
|
37
|
|
|
if ($interval >= 10800) { |
38
|
|
|
$this->setUpdateLastCheckMissions(); |
39
|
|
|
$this->setMissionsAleatoire(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->getNbMissions(); |
45
|
|
|
Bataille::setValues(["next_check_missions" => ($this->last_check_mission+10800)-Bataille::getToday()]); |
46
|
|
|
|
47
|
|
|
$this->getMissionsCours(); |
48
|
|
|
} |
49
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
54
|
|
|
/** |
55
|
|
|
* fonction qui récupere tous les types de missions et les return dans un array |
56
|
|
|
*/ |
57
|
|
|
private function getTypeMission() { |
58
|
|
|
return explode(",", Bataille::getParam("type_missions")); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return int |
63
|
|
|
* renvoi le nombre de missions encore disponibles dans la base |
64
|
|
|
*/ |
65
|
|
|
private function getNbMissions() { |
66
|
|
|
$dbc = App::getDb(); |
67
|
|
|
|
68
|
|
|
$query = $dbc->select("ID_mission_aleatoire")->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->get(); |
69
|
|
|
|
70
|
|
|
if ((is_array($query)) && (count($query))) { |
71
|
|
|
foreach ($query as $obj) { |
72
|
|
|
$id[] = $obj->ID_base; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$count = count($id); |
|
|
|
|
76
|
|
|
Bataille::setValues([ |
77
|
|
|
"nb_missions" => $count |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
return $count; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $id_mission |
86
|
|
|
* @return mixed |
87
|
|
|
* fonction qui récupère la durée d'une mission |
88
|
|
|
*/ |
89
|
|
|
private function getTempsMission($id_mission) { |
90
|
|
|
$dbc1 = Bataille::getDb(); |
91
|
|
|
|
92
|
|
|
$query = $dbc1->select("duree")->from("mission")->where("ID_mission", "=", $id_mission)->get(); |
93
|
|
|
|
94
|
|
|
if ((is_array($query)) && (count($query))) { |
95
|
|
|
foreach ($query as $obj) { |
96
|
|
|
return $obj->duree; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* récupères les missions encore disponible dans la base |
103
|
|
|
*/ |
104
|
|
|
public function getMissions() { |
105
|
|
|
$dbc = App::getDb(); |
106
|
|
|
|
107
|
|
|
$query = $dbc->select()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->get(); |
108
|
|
|
|
109
|
|
|
if ((is_array($query)) && (count($query))) { |
110
|
|
|
foreach ($query as $obj) { |
111
|
|
|
$missions[] = $this->getInfosMission($obj->ID_mission); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
Bataille::setValues(["missions" => $missions]); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $id_mission |
120
|
|
|
* @return array |
121
|
|
|
* pour récupérer les infos d'une mission dans la bdd _core |
122
|
|
|
*/ |
123
|
|
|
private function getInfosMission($id_mission) { |
124
|
|
|
$dbc1 = Bataille::getDb(); |
125
|
|
|
$query1 = $dbc1->select()->from("mission")->where("ID_mission", "=", $id_mission)->get(); |
126
|
|
|
|
127
|
|
|
if ((is_array($query1)) && (count($query1) > 0)) { |
128
|
|
|
foreach ($query1 as $obj) { |
129
|
|
|
return [ |
130
|
|
|
"id_mission" => $obj->ID_mission, |
131
|
|
|
"nom_mission" => $obj->nom_mission, |
132
|
|
|
"description" => $obj->description, |
133
|
|
|
"type" => $obj->type, |
134
|
|
|
"ressource_gagnee" => $obj->ressource_gagnee, |
135
|
|
|
"pourcentage_perte" => $obj->pourcentage_perte, |
136
|
|
|
"duree" => DateHeure::Secondeenheure($obj->duree) |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* fonction qui récupère les missions qui sont en cours dans la base |
144
|
|
|
*/ |
145
|
|
|
private function getMissionsCours() { |
146
|
|
|
$dbc = App::getDb(); |
147
|
|
|
|
148
|
|
|
$query = $dbc->select()->from("_bataille_missions_cours")->where("ID_base", "=", Bataille::getIdBase())->get(); |
149
|
|
|
|
150
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
151
|
|
|
foreach ($query as $obj) { |
152
|
|
|
$missions[] =[ |
|
|
|
|
153
|
|
|
"id_missions_cours" => $obj->ID_missions_cours, |
154
|
|
|
"date_fin" => $obj->date_fin-Bataille::getToday(), |
155
|
|
|
"infos" => $this->getInfosMission($obj->ID_mission) |
156
|
|
|
]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
Bataille::setValues(["missions_cours" => $missions]); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
|
166
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
167
|
|
|
/** |
168
|
|
|
* fonction qui met a jour le last_ckeck_missions dans _bataille_base |
169
|
|
|
* le met à la date du jour |
170
|
|
|
*/ |
171
|
|
|
private function setUpdateLastCheckMissions() { |
172
|
|
|
$dbc = App::getDb(); |
173
|
|
|
|
174
|
|
|
$dbc->update("last_check_mission", Bataille::getToday()) |
175
|
|
|
->from("_bataille_base") |
176
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
177
|
|
|
->set(); |
178
|
|
|
|
179
|
|
|
$this->last_check_mission = Bataille::getToday(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param $id_mission |
184
|
|
|
* fonction qui retire une mission de la liste des missions aleatoire des qu'on la lance |
185
|
|
|
*/ |
186
|
|
|
private function setDeleteMission($id_mission) { |
187
|
|
|
$dbc = App::getDb(); |
188
|
|
|
|
189
|
|
|
$dbc->delete()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase(), "AND") |
190
|
|
|
->where("ID_mission", "=", $id_mission)->del(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param $type |
195
|
|
|
* fonction qui recupere des missions aleatoirement de chaque type et qui les ajoute |
196
|
|
|
* dans la table _bataille_mission_aleatoire |
197
|
|
|
*/ |
198
|
|
|
private function setMissionsAleatoire() { |
199
|
|
|
$dbc = App::getDb(); |
200
|
|
|
$dbc1 = Bataille::getDb(); |
201
|
|
|
|
202
|
|
|
$dbc->delete()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->del(); |
203
|
|
|
|
204
|
|
|
$type_missions = $this->getTypeMission(); |
205
|
|
|
|
206
|
|
|
foreach ($type_missions as $un_type) { |
207
|
|
|
$query = $dbc1->select()->from("mission") |
208
|
|
|
->where("type", "=", $un_type) |
209
|
|
|
->orderBy("RAND()") |
210
|
|
|
->limit(0, 3) |
211
|
|
|
->get(); |
212
|
|
|
|
213
|
|
|
if ((is_array($query)) && (count($query))) { |
214
|
|
|
foreach ($query as $obj) { |
215
|
|
|
$dbc->insert("ID_mission", $obj->ID_mission) |
216
|
|
|
->insert("ID_base", Bataille::getIdBase()) |
217
|
|
|
->into("_bataille_mission_aleatoire") |
218
|
|
|
->set(); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param $id_mission |
226
|
|
|
* @param $nombre_unite |
227
|
|
|
* @param $nom_unite |
228
|
|
|
* @param $type_unite |
229
|
|
|
* @param null $id_groupe |
230
|
|
|
* @return bool |
231
|
|
|
* fonction sert a lancer une mission |
232
|
|
|
*/ |
233
|
|
|
public function setCommencerMission($id_mission, $nombre_unite, $nom_unite, $type_unite, $id_groupe = null) { |
234
|
|
|
$dbc = App::getDb(); |
235
|
|
|
|
236
|
|
|
if ((array_sum($nombre_unite) == 0) && (array_sum($id_groupe) == 0)) { |
237
|
|
|
FlashMessage::setFlash("Pas assez d'unité pour effectuer cette missions"); |
238
|
|
|
return false; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$dbc->insert("date_fin", $this->getTempsMission($id_mission)+Bataille::getToday()) |
242
|
|
|
->insert("ID_base", Bataille::getIdBase()) |
243
|
|
|
->insert("ID_mission", $id_mission) |
244
|
|
|
->into("_bataille_missions_cours") |
245
|
|
|
->set(); |
246
|
|
|
|
247
|
|
|
$id_missions_cours = $dbc->lastInsertId(); |
248
|
|
|
|
249
|
|
|
$count = count($nombre_unite); |
250
|
|
|
|
251
|
|
|
for ($i=0 ; $i<$count ; $i++) { |
252
|
|
|
Bataille::getUnite()->setCommencerExpedition($nombre_unite[$i], $nom_unite[$i], $type_unite[$i], $id_missions_cours); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$count = count($id_groupe); |
256
|
|
|
|
257
|
|
|
for ($i=0 ; $i<$count ; $i++) { |
258
|
|
|
Bataille::getGoupeUnite()->setCommencerExpedition($id_groupe[$i], $id_missions_cours); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$this->setDeleteMission($id_mission); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* fonctin qui termine les missions en cours et qui ajoutera les ressources + les points |
266
|
|
|
* et qui au cas ou pourra tuer des inités |
267
|
|
|
*/ |
268
|
|
|
public function setTerminerMissions() { |
269
|
|
|
$dbc = App::getDb(); |
270
|
|
|
|
271
|
|
|
$query = $dbc->select()->from("_bataille_missions_cours") |
272
|
|
|
->where("date_fin", "<=", Bataille::getToday(), "AND") |
273
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
274
|
|
|
->get(); |
275
|
|
|
|
276
|
|
|
if ((is_array($query)) && (count($query))) { |
277
|
|
|
foreach ($query as $obj) { |
278
|
|
|
$infos_missions = $this->getInfosMission($obj->ID_mission); |
279
|
|
|
|
280
|
|
|
$unites_envoyees = Bataille::getUnite()->getUnitesMission($obj->ID_missions_cours); |
281
|
|
|
$unite_revenu = Bataille::getUnite()->setTerminerExpedition($obj->ID_missions_cours, $infos_missions["pourcentage_perte"]); |
282
|
|
|
|
283
|
|
|
if ($infos_missions["type"] == "nourriture") { |
284
|
|
|
Bataille::getRessource()->setUpdateRessource(0, 0, 0, 0, $infos_missions["ressource_gagnee"]*$unite_revenu, "+"); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$dbc->delete()->from("_bataille_missions_cours") |
288
|
|
|
->where("ID_base", "=", Bataille::getIdBase(), "AND") |
289
|
|
|
->where("ID_mission", "=", $obj->ID_mission) |
290
|
|
|
->del(); |
291
|
|
|
|
292
|
|
|
Bataille::getGoupeUnite()->setTerminerExpedition($obj->ID_missions_cours); |
293
|
|
|
|
294
|
|
|
//génération du rapport de mission |
295
|
|
|
$titre = "Rapport de la mission ".$infos_missions["nom_mission"]; |
296
|
|
|
$infos = [ |
297
|
|
|
"mission" => $infos_missions, |
298
|
|
|
"unites_envoyees" => $unites_envoyees, |
299
|
|
|
"unites_revenues" => $unite_revenu, |
300
|
|
|
"ressource_gagnee" => $infos_missions["ressource_gagnee"]*$unite_revenu |
301
|
|
|
]; |
302
|
|
|
GenerationRapport::setGenererRapport($titre, $infos, "mission"); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
307
|
|
|
|
308
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.