|
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
|
|
|
"points_gagne" => $obj->points_gagne, |
|
134
|
|
|
"type" => $obj->type, |
|
135
|
|
|
"ressource_gagnee" => $obj->ressource_gagnee, |
|
136
|
|
|
"pourcentage_perte" => $obj->pourcentage_perte, |
|
137
|
|
|
"duree" => DateHeure::Secondeenheure($obj->duree) |
|
138
|
|
|
]; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* fonction qui récupère les missions qui sont en cours dans la base |
|
145
|
|
|
*/ |
|
146
|
|
|
private function getMissionsCours() { |
|
147
|
|
|
$dbc = App::getDb(); |
|
148
|
|
|
|
|
149
|
|
|
$query = $dbc->select()->from("_bataille_missions_cours")->where("ID_base", "=", Bataille::getIdBase())->get(); |
|
150
|
|
|
|
|
151
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
|
152
|
|
|
foreach ($query as $obj) { |
|
153
|
|
|
$missions[] =[ |
|
|
|
|
|
|
154
|
|
|
"id_missions_cours" => $obj->ID_missions_cours, |
|
155
|
|
|
"date_fin" => $obj->date_fin-Bataille::getToday(), |
|
156
|
|
|
"infos" => $this->getInfosMission($obj->ID_mission) |
|
157
|
|
|
]; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
Bataille::setValues(["missions_cours" => $missions]); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
|
168
|
|
|
/** |
|
169
|
|
|
* fonction qui met a jour le last_ckeck_missions dans _bataille_base |
|
170
|
|
|
* le met à la date du jour |
|
171
|
|
|
*/ |
|
172
|
|
|
private function setUpdateLastCheckMissions() { |
|
173
|
|
|
$dbc = App::getDb(); |
|
174
|
|
|
|
|
175
|
|
|
$dbc->update("last_check_mission", Bataille::getToday()) |
|
176
|
|
|
->from("_bataille_base") |
|
177
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
|
178
|
|
|
->set(); |
|
179
|
|
|
|
|
180
|
|
|
$this->last_check_mission = Bataille::getToday(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param $id_mission |
|
185
|
|
|
* fonction qui retire une mission de la liste des missions aleatoire des qu'on la lance |
|
186
|
|
|
*/ |
|
187
|
|
|
private function setDeleteMission($id_mission) { |
|
188
|
|
|
$dbc = App::getDb(); |
|
189
|
|
|
|
|
190
|
|
|
$dbc->delete()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase(), "AND") |
|
191
|
|
|
->where("ID_mission", "=", $id_mission)->del(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @param $type |
|
196
|
|
|
* fonction qui recupere des missions aleatoirement de chaque type et qui les ajoute |
|
197
|
|
|
* dans la table _bataille_mission_aleatoire |
|
198
|
|
|
*/ |
|
199
|
|
|
private function setMissionsAleatoire() { |
|
200
|
|
|
$dbc = App::getDb(); |
|
201
|
|
|
$dbc1 = Bataille::getDb(); |
|
202
|
|
|
|
|
203
|
|
|
$dbc->delete()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->del(); |
|
204
|
|
|
|
|
205
|
|
|
$type_missions = $this->getTypeMission(); |
|
206
|
|
|
|
|
207
|
|
|
foreach ($type_missions as $un_type) { |
|
208
|
|
|
$query = $dbc1->select()->from("mission") |
|
209
|
|
|
->where("type", "=", $un_type) |
|
210
|
|
|
->orderBy("RAND()") |
|
211
|
|
|
->limit(0, 3) |
|
212
|
|
|
->get(); |
|
213
|
|
|
|
|
214
|
|
|
if ((is_array($query)) && (count($query))) { |
|
215
|
|
|
foreach ($query as $obj) { |
|
216
|
|
|
$dbc->insert("ID_mission", $obj->ID_mission) |
|
217
|
|
|
->insert("ID_base", Bataille::getIdBase()) |
|
218
|
|
|
->into("_bataille_mission_aleatoire") |
|
219
|
|
|
->set(); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @param $id_mission |
|
227
|
|
|
* @param $nombre_unite |
|
228
|
|
|
* @param $nom_unite |
|
229
|
|
|
* @param $type_unite |
|
230
|
|
|
* fonction sert a lancer une mission |
|
231
|
|
|
*/ |
|
232
|
|
|
public function setCommencerMission($id_mission, $nombre_unite, $nom_unite, $type_unite) { |
|
233
|
|
|
$dbc = App::getDb(); |
|
234
|
|
|
if ($nombre_unite > 0) { |
|
235
|
|
|
$dbc->insert("date_fin", $this->getTempsMission($id_mission)+Bataille::getToday()) |
|
236
|
|
|
->insert("ID_base", Bataille::getIdBase()) |
|
237
|
|
|
->insert("ID_mission", $id_mission) |
|
238
|
|
|
->into("_bataille_missions_cours") |
|
239
|
|
|
->set(); |
|
240
|
|
|
|
|
241
|
|
|
$id_missions_cours = $dbc->lastInsertId(); |
|
242
|
|
|
|
|
243
|
|
|
$count = count($nombre_unite); |
|
244
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
for ($i=0 ; $i<$count ; $i++) { |
|
247
|
|
|
Bataille::getUnite()->setCommencerExpedition($nombre_unite[$i], $nom_unite[$i], $type_unite[$i], $id_missions_cours); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
$this->setDeleteMission($id_mission); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
FlashMessage::setFlash("Pas assez d'unité pour effectuer cette missions"); |
|
254
|
|
|
return false; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* fonctin qui termine les missions en cours et qui ajoutera les ressources + les points |
|
259
|
|
|
* et qui au cas ou pourra tuer des inités |
|
260
|
|
|
*/ |
|
261
|
|
|
public function setTerminerMissions() { |
|
262
|
|
|
$dbc = App::getDb(); |
|
263
|
|
|
|
|
264
|
|
|
$query = $dbc->select()->from("_bataille_missions_cours") |
|
265
|
|
|
->where("date_fin", "<=", Bataille::getToday(), "AND") |
|
266
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
|
267
|
|
|
->get(); |
|
268
|
|
|
|
|
269
|
|
|
if ((is_array($query)) && (count($query))) { |
|
270
|
|
|
foreach ($query as $obj) { |
|
271
|
|
|
$infos_missions = $this->getInfosMission($obj->ID_mission); |
|
272
|
|
|
|
|
273
|
|
|
$unite_revenu = Bataille::getUnite()->setTerminerExpedition($obj->ID_missions_cours, $infos_missions["pourcentage_perte"]); |
|
274
|
|
|
|
|
275
|
|
|
if ($infos_missions["type"] == "nourriture") { |
|
276
|
|
|
Bataille::getRessource()->setUpdateRessource(0, 0, 0, 0, $infos_missions["ressource_gagnee"]*$unite_revenu, "+"); |
|
277
|
|
|
} |
|
278
|
|
|
else { |
|
|
|
|
|
|
279
|
|
|
//Bataille::getRessource()->setUpdateRessource(0, 0, 0, 0, $obj->ressource_gagnee, "+"); |
|
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
Points::setAjouterPoints(Bataille::getIdBase(), "missions", $infos_missions["points_gange"]); |
|
283
|
|
|
|
|
284
|
|
|
$dbc->delete()->from("_bataille_missions_cours") |
|
285
|
|
|
->where("ID_base", "=", Bataille::getIdBase(), "AND") |
|
286
|
|
|
->where("ID_mission", "=", $obj->ID_mission) |
|
287
|
|
|
->del(); |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
|
292
|
|
|
|
|
293
|
|
|
} |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.