|
1
|
|
|
<?php |
|
2
|
|
|
namespace modules\bataille\app\controller; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use core\App; |
|
6
|
|
|
use core\functions\DateHeure; |
|
7
|
|
|
|
|
8
|
|
|
class CentreRecherche { |
|
9
|
|
|
private $coef_centre; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
|
13
|
|
|
public function __construct() { |
|
14
|
|
|
$dbc1 = Bataille::getDb(); |
|
15
|
|
|
|
|
16
|
|
|
$query = $dbc1->select("coef_centre_recherche")->from("configuration")->where("ID_configuration", "=", 1)->get(); |
|
17
|
|
|
|
|
18
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
|
19
|
|
|
foreach ($query as $obj) $this->coef_centre = $obj->coef_centre_recherche; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
$query = $dbc1->select()->from("recherche") |
|
23
|
|
|
->where("niveau_centre", "<=", Bataille::getBatiment()->getNiveauBatiment("centre_recherche")) |
|
24
|
|
|
->get(); |
|
25
|
|
|
|
|
26
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
|
27
|
|
|
foreach ($query as $obj) { |
|
28
|
|
|
$niveau = $this->getNiveauRecherche($obj->recherche, $obj->type); |
|
29
|
|
|
$niveau_recherche = $niveau; |
|
30
|
|
|
|
|
31
|
|
|
$cout = unserialize($obj->cout); |
|
32
|
|
|
$temps_recherche = $this->getTempsRecherche($obj->temps_recherche); |
|
33
|
|
|
|
|
34
|
|
|
//si niveau == 0 ca veut dire que la recherche n'a pas encore été effectuée dans la base |
|
35
|
|
View Code Duplication |
if ($niveau > 0) { |
|
|
|
|
|
|
36
|
|
|
$cout = $this->getCoutRecherche($cout, $niveau); |
|
37
|
|
|
$temps_recherche = $this->getTempsRecherche($temps_recherche, $niveau); |
|
38
|
|
|
} |
|
39
|
|
|
else { |
|
40
|
|
|
$niveau_recherche = 1; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$recherhce[] = [ |
|
|
|
|
|
|
44
|
|
|
"recherche" => $obj->recherche, |
|
45
|
|
|
"type" => $obj->type, |
|
46
|
|
|
"niveau" => $niveau, |
|
47
|
|
|
"cout" => $cout, |
|
48
|
|
|
"temps_recherche" => DateHeure::Secondeenheure($temps_recherche), |
|
49
|
|
|
"special" => Bataille::getUnite()->getCaracteristiqueUnite($obj->recherche, $niveau_recherche, $obj->type), |
|
50
|
|
|
"coef_amelioration" => Bataille::getParam("coef_niveau_unite") |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
Bataille::setValues(["centre_recherche" => $recherhce]); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $recherche |
|
60
|
|
|
* @param $type |
|
61
|
|
|
* @return int |
|
62
|
|
|
* fonction qui va cehrcher le niveau de la recherche actuelle |
|
63
|
|
|
* renvoi 0 si elle n'a pas été trouvée |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
private function getNiveauRecherche($recherche, $type) { |
|
|
|
|
|
|
66
|
|
|
$dbc = App::getDb(); |
|
67
|
|
|
|
|
68
|
|
|
$query = $dbc->select("niveau") |
|
69
|
|
|
->from("_bataille_centre_recherche") |
|
70
|
|
|
->where("ID_base", "=", Bataille::getIdBase(), "AND") |
|
71
|
|
|
->where("recherche", "=", $recherche, "AND") |
|
72
|
|
|
->where("type", "=", $type) |
|
73
|
|
|
->get(); |
|
74
|
|
|
|
|
75
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
|
76
|
|
|
foreach ($query as $obj) { |
|
77
|
|
|
return $obj->niveau; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return 0; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function getCoutRecherche($cout, $niveau_recherche) { |
|
85
|
|
|
return [ |
|
86
|
|
|
"eau" => $cout["eau"] * ($this->coef_centre * $niveau_recherche), |
|
87
|
|
|
"electricite" => $cout["electricite"] * ($this->coef_centre * $niveau_recherche), |
|
88
|
|
|
"fer" => $cout["fer"] * ($this->coef_centre * $niveau_recherche), |
|
89
|
|
|
"fuel" => $cout["fuel"] * ($this->coef_centre * $niveau_recherche) |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function getTempsRecherche($temps, $niveau = 0) { |
|
94
|
|
|
$pourcent = ($temps*Bataille::getBatiment()->getNiveauBatiment("centre_recherche")/100); |
|
95
|
|
|
|
|
96
|
|
|
if ($niveau == 0) { |
|
97
|
|
|
return round($temps-$pourcent);; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return round(($temps * ($this->coef_centre * $niveau))-$pourcent); |
|
101
|
|
|
} |
|
102
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
|
107
|
|
|
/** |
|
108
|
|
|
* @param $type |
|
109
|
|
|
* @return array|int |
|
110
|
|
|
* permet de renvoyer toutes es recherches déjà effectuées pour notre base en fonction |
|
111
|
|
|
* d'un type donné |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getAllRechercheType($type) { |
|
114
|
|
|
$dbc = App::getDb(); |
|
115
|
|
|
|
|
116
|
|
|
$query = $dbc->select()->from("_bataille_centre_recherche")->where("type", "=", $type)->get(); |
|
117
|
|
|
|
|
118
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
|
119
|
|
|
foreach ($query as $obj) { |
|
120
|
|
|
$recherche[] = [ |
|
|
|
|
|
|
121
|
|
|
"niveau" => $obj->niveau, |
|
122
|
|
|
"recherche" => $obj->recherche |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $recherche; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return 0; |
|
130
|
|
|
} |
|
131
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
|
136
|
|
|
public function setCommencerRecherche($recherche, $type) { |
|
137
|
|
|
$dbc = App::getDb(); |
|
138
|
|
|
$dbc1 = Bataille::getDb(); |
|
139
|
|
|
$niveau_recherche = 0; |
|
140
|
|
|
|
|
141
|
|
|
//on récupère la recherche dans notre base savoir si on l'a déjà recherchée pour avoir son lvl |
|
142
|
|
|
$query = $dbc->select("niveau")->from("_bataille_centre_recherche") |
|
143
|
|
|
->where("recherche", "=", $recherche, "AND") |
|
144
|
|
|
->where("type", "=", $type, "AND") |
|
145
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
|
146
|
|
|
->get(); |
|
147
|
|
|
|
|
148
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
|
149
|
|
|
foreach ($query as $obj) $niveau_recherche = $obj->niveau; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
//récupération du cout initial plus temps de recherche initial pour calculer les bon en fonction |
|
153
|
|
|
//du lvl du centre + du niveau actuel de la recherche |
|
154
|
|
|
$query = $dbc1->select("cout") |
|
155
|
|
|
->select("temps_recherche") |
|
156
|
|
|
->from("recherche") |
|
157
|
|
|
->where("recherche", "=", $recherche, "AND") |
|
158
|
|
|
->where("type", "=", $type) |
|
159
|
|
|
->get(); |
|
160
|
|
|
|
|
161
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
|
162
|
|
|
foreach ($query as $obj) { |
|
163
|
|
|
$cout = $obj->cout; |
|
164
|
|
|
$temps_recherche = $obj->temps_recherche; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
View Code Duplication |
if ($niveau_recherche > 0) { |
|
|
|
|
|
|
169
|
|
|
$cout = $this->getCoutRecherche($cout, $niveau_recherche); |
|
|
|
|
|
|
170
|
|
|
$temps_recherche = $this->getTempsRecherche($temps_recherche, $niveau_recherche); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$date_fin = Bataille::getToday()+$temps_recherche; |
|
174
|
|
|
|
|
175
|
|
|
$dbc->insert("recherche", $recherche) |
|
176
|
|
|
->insert("type", $type) |
|
177
|
|
|
->insert("date_fin", $date_fin) |
|
178
|
|
|
->insert("ID_base", Bataille::getIdBase()) |
|
179
|
|
|
->into("_bataille_recherche") |
|
180
|
|
|
->set(); |
|
181
|
|
|
} |
|
182
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
|
183
|
|
|
|
|
184
|
|
|
} |
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.