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 CentreRecherche { |
10
|
|
|
private $coef_centre; |
11
|
|
|
private $recherche; |
12
|
|
|
private $type; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
16
|
|
View Code Duplication |
public function __construct() { |
|
|
|
|
17
|
|
|
$dbc1 = Bataille::getDb(); |
18
|
|
|
|
19
|
|
|
$query = $dbc1->select("coef_centre_recherche")->from("configuration")->where("ID_configuration", "=", 1)->get(); |
20
|
|
|
|
21
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
22
|
|
|
foreach ($query as $obj) $this->coef_centre = $obj->coef_centre_recherche; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
30
|
|
|
/** |
31
|
|
|
* @param $recherche |
32
|
|
|
* @param $type |
33
|
|
|
* @return int |
34
|
|
|
* fonction qui va cehrcher le niveau de la recherche actuelle |
35
|
|
|
* renvoi 0 si elle n'a pas été trouvée |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
private function getNiveauRecherche($recherche, $type) { |
|
|
|
|
38
|
|
|
$dbc = App::getDb(); |
39
|
|
|
|
40
|
|
|
$query = $dbc->select("niveau") |
41
|
|
|
->from("_bataille_centre_recherche") |
42
|
|
|
->where("ID_base", "=", Bataille::getIdBase(), "AND") |
43
|
|
|
->where("recherche", "=", $recherche, "AND") |
44
|
|
|
->where("type", "=", $type) |
45
|
|
|
->get(); |
46
|
|
|
|
47
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
48
|
|
|
foreach ($query as $obj) { |
49
|
|
|
return $obj->niveau; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return 0; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $cout |
58
|
|
|
* @param $niveau_recherche |
59
|
|
|
* @return array |
60
|
|
|
* fonction qui renvoi le cout d'une recherche |
61
|
|
|
*/ |
62
|
|
|
private function getCoutRecherche($cout, $niveau_recherche) { |
63
|
|
|
return [ |
64
|
|
|
"eau" => $cout["eau"] * ($this->coef_centre * $niveau_recherche), |
65
|
|
|
"electricite" => $cout["electricite"] * ($this->coef_centre * $niveau_recherche), |
66
|
|
|
"fer" => $cout["fer"] * ($this->coef_centre * $niveau_recherche), |
67
|
|
|
"fuel" => $cout["fuel"] * ($this->coef_centre * $niveau_recherche) |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $temps |
73
|
|
|
* @param int $niveau |
74
|
|
|
* @return floatfonction qui renvoi le temps qu'il faut pour effectuer une recherche |
75
|
|
|
*/ |
76
|
|
|
private function getTempsRecherche($temps, $niveau = 0) { |
77
|
|
|
$pourcent = ($temps*Bataille::getBatiment()->getNiveauBatiment("centre_recherche")/100); |
78
|
|
|
|
79
|
|
|
if ($niveau == 0) { |
80
|
|
|
return round($temps-$pourcent);; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return round(($temps * ($this->coef_centre * $niveau))-$pourcent); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $type |
88
|
|
|
* @return array|int |
89
|
|
|
* permet de renvoyer toutes es recherches déjà effectuées pour notre base en fonction |
90
|
|
|
* d'un type donné |
91
|
|
|
*/ |
92
|
|
|
public function getAllRechercheType($type) { |
93
|
|
|
$dbc = App::getDb(); |
94
|
|
|
|
95
|
|
|
$query = $dbc->select()->from("_bataille_centre_recherche")->where("type", "=", $type)->get(); |
96
|
|
|
|
97
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
98
|
|
|
foreach ($query as $obj) { |
99
|
|
|
$recherche[] = [ |
|
|
|
|
100
|
|
|
"niveau" => $obj->niveau, |
101
|
|
|
"recherche" => $obj->recherche |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $recherche; |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return 0; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* fonction qui renvoi toutes les recherches effectuées ou non dans un tableau |
113
|
|
|
* (ne renvoi que celle que l'on peut faire en fonction du niveau du centre) |
114
|
|
|
*/ |
115
|
|
|
public function getAllRecherche() { |
116
|
|
|
$dbc1 = Bataille::getDb(); |
117
|
|
|
|
118
|
|
|
//avant de récupérer toutes les recherches, on finit au cas celle en court |
119
|
|
|
if ($this->getRecherche() == false) { |
|
|
|
|
120
|
|
|
$query = $dbc1->select()->from("recherche") |
121
|
|
|
->where("niveau_centre", "<=", Bataille::getBatiment()->getNiveauBatiment("centre_recherche")) |
122
|
|
|
->get(); |
123
|
|
|
|
124
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
125
|
|
|
foreach ($query as $obj) { |
126
|
|
|
$niveau = $this->getNiveauRecherche($obj->recherche, $obj->type); |
127
|
|
|
$niveau_recherche = $niveau; |
128
|
|
|
|
129
|
|
|
$cout = unserialize($obj->cout); |
130
|
|
|
$temps_recherche = $this->getTempsRecherche($obj->temps_recherche); |
131
|
|
|
|
132
|
|
|
//si niveau == 0 ca veut dire que la recherche n'a pas encore été effectuée dans la base |
133
|
|
View Code Duplication |
if ($niveau > 0) { |
|
|
|
|
134
|
|
|
$cout = $this->getCoutRecherche($cout, $niveau); |
135
|
|
|
$temps_recherche = $this->getTempsRecherche($temps_recherche, $niveau); |
136
|
|
|
} |
137
|
|
|
else { |
138
|
|
|
$niveau_recherche = 1; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$recherhce[] = [ |
|
|
|
|
142
|
|
|
"recherche" => $obj->recherche, |
143
|
|
|
"type" => $obj->type, |
144
|
|
|
"niveau" => $niveau, |
145
|
|
|
"cout" => $cout, |
146
|
|
|
"temps_recherche" => DateHeure::Secondeenheure($temps_recherche), |
147
|
|
|
"special" => Bataille::getUnite()->getCaracteristiqueUnite($obj->recherche, $niveau_recherche, $obj->type), |
148
|
|
|
"coef_amelioration" => Bataille::getParam("coef_niveau_unite") |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
Bataille::setValues(["centre_recherche" => $recherhce]); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return bool |
159
|
|
|
* fonction qui renvoi un tableau contenant la recherche en cours si celle-ci n'est pas finie |
160
|
|
|
* sinon elle appelle la fonction setTerminerRecherche |
161
|
|
|
*/ |
162
|
|
|
public function getRecherche() { |
163
|
|
|
$dbc = App::getDb(); |
164
|
|
|
|
165
|
|
|
$query = $dbc->select()->from("_bataille_recherche")->where("ID_base", "=", Bataille::getIdBase())->get(); |
166
|
|
|
|
167
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
168
|
|
|
$today = Bataille::getToday(); |
169
|
|
|
|
170
|
|
|
foreach ($query as $obj) { |
171
|
|
|
$this->recherche = $obj->recherche; |
172
|
|
|
$this->type = $obj->type; |
173
|
|
|
|
174
|
|
|
if ($obj->date_fin-$today <= 0) { |
175
|
|
|
$this->setTerminerRecherche($obj->ID_recherche); |
176
|
|
|
|
177
|
|
|
return false; |
178
|
|
|
} |
179
|
|
|
else { |
180
|
|
|
$recherche = [ |
181
|
|
|
"recherche" => $obj->recherche, |
182
|
|
|
"type" => $obj->type, |
183
|
|
|
"date_fin_recherche" => $obj->date_fin-$today, |
184
|
|
|
"id_recherche" => $obj->ID_recherche |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
Bataille::setValues(["recherche" => $recherche]); |
|
|
|
|
190
|
|
|
|
191
|
|
|
return true; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
|
200
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
201
|
|
|
public function setCommencerRecherche($recherche, $type) { |
202
|
|
|
$dbc = App::getDb(); |
203
|
|
|
$dbc1 = Bataille::getDb(); |
204
|
|
|
|
205
|
|
|
//on test si il n'y a pas déjà une recherche en cours |
206
|
|
|
if ($this->getRecherche() == true) { |
|
|
|
|
207
|
|
|
FlashMessage::setFlash("Une recherche est déjà en cours, merci d'attendre la fin de celle-ci"); |
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
//on récupère la recherche dans notre base savoir si on l'a déjà recherchée pour avoir son lvl |
212
|
|
|
$niveau_recherche = $this->getNiveauRecherche($recherche, $type); |
213
|
|
|
|
214
|
|
|
//récupération du cout initial plus temps de recherche initial pour calculer les bon en fonction |
215
|
|
|
//du lvl du centre + du niveau actuel de la recherche |
216
|
|
|
$query = $dbc1->select("cout") |
217
|
|
|
->select("temps_recherche") |
218
|
|
|
->from("recherche") |
219
|
|
|
->where("recherche", "=", $recherche, "AND") |
220
|
|
|
->where("type", "=", $type) |
221
|
|
|
->get(); |
222
|
|
|
|
223
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
224
|
|
|
foreach ($query as $obj) { |
225
|
|
|
$cout = unserialize($obj->cout); |
226
|
|
|
$temps_recherche = $obj->temps_recherche; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
View Code Duplication |
if ($niveau_recherche > 0) { |
|
|
|
|
231
|
|
|
$cout = $this->getCoutRecherche($cout, $niveau_recherche); |
|
|
|
|
232
|
|
|
$temps_recherche = $this->getTempsRecherche($temps_recherche, $niveau_recherche); |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
//on test si assez de ressources pour effectuer la recherche |
236
|
|
|
$eau = Bataille::getTestAssezRessourceBase("eau", $cout["eau"]); |
237
|
|
|
$electricite = Bataille::getTestAssezRessourceBase("electricite", $cout["electricite"]); |
238
|
|
|
$fer = Bataille::getTestAssezRessourceBase("fer", $cout["fer"]); |
239
|
|
|
$fuel = Bataille::getTestAssezRessourceBase("fuel", $cout["fuel"]); |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
if (($eau["class"] || $electricite["class"] || $fer["class"] || $fuel["class"]) == "rouge" ) { |
243
|
|
|
FlashMessage::setFlash("Pas assez de ressources pour effectuer cette recherche"); |
244
|
|
|
return false; |
245
|
|
|
} |
246
|
|
|
else { |
247
|
|
|
//on retire les ressources |
248
|
|
|
Bataille::getRessource()->setUpdateRessource($cout["eau"], $cout["electricite"], $cout["fer"], $cout["fuel"], 0, "-"); |
249
|
|
|
|
250
|
|
|
$date_fin = Bataille::getToday()+$temps_recherche; |
251
|
|
|
|
252
|
|
|
$dbc->insert("recherche", $recherche) |
253
|
|
|
->insert("type", $type) |
254
|
|
|
->insert("date_fin", $date_fin) |
255
|
|
|
->insert("ID_base", Bataille::getIdBase()) |
256
|
|
|
->into("_bataille_recherche") |
257
|
|
|
->set(); |
258
|
|
|
|
259
|
|
|
return true; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
private function setTerminerRecherche($id_recherche) { |
264
|
|
|
$dbc = App::getDb(); |
265
|
|
|
$niveau_recherche = $this->getNiveauRecherche($this->recherche, $this->type); |
266
|
|
|
|
267
|
|
|
if ($niveau_recherche == 0) { |
268
|
|
|
$dbc->insert("recherche", $this->recherche) |
269
|
|
|
->insert("type", $this->type) |
270
|
|
|
->insert("niveau", 1) |
271
|
|
|
->insert("ID_base", Bataille::getIdBase()) |
272
|
|
|
->into("_bataille_centre_recherche") |
273
|
|
|
->set(); |
274
|
|
|
} |
275
|
|
|
else { |
276
|
|
|
$dbc->update("niveau", $niveau_recherche+1) |
277
|
|
|
->from("_bataille_centre_recherche") |
278
|
|
|
->where("recherche", "=", $this->recherche, "AND") |
279
|
|
|
->where("type", "=", $this->type, "AND") |
280
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
281
|
|
|
->set(); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
$dbc->delete()->from("_bataille_recherche")->where("ID_recherche", "=", $id_recherche, "AND") |
285
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
286
|
|
|
->del(); |
287
|
|
|
} |
288
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
289
|
|
|
|
290
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.