1
|
|
|
<?php |
2
|
|
|
namespace modules\bataille\app\controller; |
3
|
|
|
use core\App; |
4
|
|
|
use core\functions\ChaineCaractere; |
5
|
|
|
use core\functions\DateHeure; |
6
|
|
|
use core\HTML\flashmessage\FlashMessage; |
7
|
|
|
use Nette\Utils\DateTime; |
8
|
|
|
|
9
|
|
|
class Batiment { |
10
|
|
|
//pour quand on recup un batiment |
11
|
|
|
private $nom_batiment; |
12
|
|
|
private $nom_batiment_sql; |
13
|
|
|
private $niveau_batiment; |
14
|
|
|
private $temps_construction; |
15
|
|
|
private $ressource_construire; |
16
|
|
|
private $id_batiment; |
17
|
|
|
|
18
|
|
|
private $info_batiment; |
19
|
|
|
private $info_batiment_next; |
20
|
|
|
|
21
|
|
|
//pour les constructions |
22
|
|
|
private $nom_batiment_construction; |
23
|
|
|
private $date_fin_construction; |
24
|
|
|
private $niveau_batiment_construction; |
25
|
|
|
|
26
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
27
|
|
|
public function __construct() { |
28
|
|
|
|
29
|
|
|
} |
30
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
35
|
|
|
public function getNomBatiment() { |
36
|
|
|
return $this->nom_batiment; |
37
|
|
|
} |
38
|
|
|
public function getNomBatimentSql() { |
39
|
|
|
return $this->nom_batiment_sql; |
40
|
|
|
} |
41
|
|
|
public function getNiveau() { |
42
|
|
|
return $this->niveau_batiment; |
43
|
|
|
} |
44
|
|
|
public function getTempsConstruction() { |
45
|
|
|
return $this->temps_construction; |
46
|
|
|
} |
47
|
|
|
public function getRessourceConstruire() { |
48
|
|
|
return $this->ressource_construire; |
49
|
|
|
} |
50
|
|
|
public function getInfoBatiment(){ |
51
|
|
|
return $this->info_batiment; |
52
|
|
|
} |
53
|
|
|
public function getInfoBatimentNext(){ |
54
|
|
|
return $this->info_batiment_next; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getNomBatimentConstruction() { |
58
|
|
|
return $this->nom_batiment_construction; |
59
|
|
|
} |
60
|
|
|
public function getDateFinConstruction() { |
61
|
|
|
return $this->date_fin_construction; |
62
|
|
|
} |
63
|
|
|
public function getNiveauBatimentConstruction() { |
64
|
|
|
return $this->niveau_batiment_construction; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $nom_batiment |
69
|
|
|
* @return int |
70
|
|
|
* pour recuperer le niveau d'un batiment |
71
|
|
|
*/ |
72
|
|
|
public function getNiveauBatiment($nom_batiment_sql, $id_base = null) { |
73
|
|
|
$dbc = App::getDb(); |
74
|
|
|
|
75
|
|
|
if ($id_base == null) { |
76
|
|
|
$id_base = Bataille::getIdBase(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$query = $dbc->select("niveau") |
80
|
|
|
->select("construction") |
81
|
|
|
->from("_bataille_batiment") |
82
|
|
|
->where("nom_batiment_sql", "=", $nom_batiment_sql, "AND") |
83
|
|
|
->where("ID_base", "=", $id_base) |
84
|
|
|
->get(); |
85
|
|
|
|
86
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
87
|
|
|
foreach ($query as $obj) { |
88
|
|
|
if ($obj->construction == 1) { |
89
|
|
|
return $obj->niveau-1; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $obj->niveau; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
else { |
96
|
|
|
return 0; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $ressource |
102
|
|
|
* @return int |
103
|
|
|
* recuperation de la production de chaque ressource en fonction du lvl des batiments |
104
|
|
|
*/ |
105
|
|
|
public function getProduction($ressource, $id_base = null) { |
106
|
|
|
$dbc1 = Bataille::getDb(); |
107
|
|
|
|
108
|
|
|
if ($id_base == null) { |
109
|
|
|
$id_base = Bataille::getIdBase(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($ressource == "eau") $nom_batiment = "centrale_eau"; |
113
|
|
|
if ($ressource == "electricite") $nom_batiment = "centrale_electrique"; |
114
|
|
|
if ($ressource == "fuel") $nom_batiment = "station_pompage_fuel"; |
115
|
|
|
if ($ressource == "fer") $nom_batiment = "station_forage"; |
116
|
|
|
|
117
|
|
|
$niveau = $this->getNiveauBatiment($nom_batiment, $id_base); |
|
|
|
|
118
|
|
|
|
119
|
|
|
if ($niveau > 0) { |
120
|
|
|
$query = $dbc1->select("production")->from("$nom_batiment")->where("ID_".$nom_batiment, "=", $niveau)->get(); |
121
|
|
|
|
122
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
123
|
|
|
foreach ($query as $obj) { |
124
|
|
|
$prod = $obj->production; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $prod; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
else { |
131
|
|
|
return 20; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return int |
137
|
|
|
* fonction qui retourne le stockage de l'entrepot |
138
|
|
|
*/ |
139
|
|
|
public function getStockageEntrepot($id_base = null) { |
140
|
|
|
$dbc1 = Bataille::getDb(); |
141
|
|
|
|
142
|
|
|
if ($id_base == null) { |
143
|
|
|
$id_base = Bataille::getIdBase(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$niveau = $this->getNiveauBatiment("entrepot", $id_base); |
147
|
|
|
|
148
|
|
|
if ($niveau > 0) { |
149
|
|
|
$query = $dbc1->select("stockage")->from("entrepot")->where("ID_entrepot", "=", $niveau)->get(); |
150
|
|
|
|
151
|
|
|
if ((is_array($query)) && (count($query) > 0)){ |
152
|
|
|
foreach ($query as $obj) { |
153
|
|
|
return $obj->stockage; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
else { |
158
|
|
|
return 1000; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* permet de récupérer toutes les infos d'un batiment dans la popup |
164
|
|
|
* @param $nom_batiment |
165
|
|
|
*/ |
166
|
|
|
public function getUnBatiment($nom_batiment) { |
167
|
|
|
$dbc = App::getDb(); |
168
|
|
|
$dbc1 = Bataille::getDb(); |
169
|
|
|
|
170
|
|
|
$construction = $this->getTestBatimentConstruction($nom_batiment); |
171
|
|
|
|
172
|
|
|
//recuperation des infos du batiment |
173
|
|
|
$query = $dbc->select() |
174
|
|
|
->from("_bataille_batiment") |
175
|
|
|
->where("nom_batiment", "=", $construction[0], "AND") |
176
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
177
|
|
|
->get(); |
178
|
|
|
|
179
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
180
|
|
|
foreach ($query as $obj) { |
181
|
|
|
$this->nom_batiment_sql = $obj->nom_batiment_sql; |
182
|
|
|
$this->niveau_batiment = $obj->niveau; |
183
|
|
|
$this->id_batiment = $obj->ID_batiment; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (($construction[1] == true) && ($this->niveau_batiment > 1)) { |
|
|
|
|
187
|
|
|
$this->niveau_batiment = $this->niveau_batiment + 1; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$max_level = $this->getInfoUpgradeBatiment(); |
191
|
|
|
} |
192
|
|
|
else { |
193
|
|
|
$query = $dbc1->select("nom_table") |
194
|
|
|
->from("liste_batiment") |
195
|
|
|
->where("nom", "=", $nom_batiment) |
196
|
|
|
->get(); |
197
|
|
|
|
198
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
199
|
|
|
foreach ($query as $obj) { |
200
|
|
|
$this->nom_batiment_sql = $obj->nom_table; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$this->niveau_batiment = 0; |
205
|
|
|
$this->getInfoUpgradeBatiment(); |
206
|
|
|
$max_level = 0; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
//permet de savoir si le batiment produit bien des ressoures |
210
|
|
|
$batiment_production = []; |
211
|
|
|
|
212
|
|
|
$query = $dbc1->select("nom")->from("liste_batiment")->where("type", "=", "ressource")->get(); |
213
|
|
|
|
214
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
215
|
|
|
foreach ($query as $obj) { |
216
|
|
|
$batiment_production[] = $obj->nom; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
Bataille::setValues([ |
221
|
|
|
"nom_batiment_sql" => $this->nom_batiment_sql, |
222
|
|
|
"niveau_batiment" => $this->niveau_batiment, |
223
|
|
|
"id_batiment" => $this->niveau_batiment, |
224
|
|
|
"max_level" => $max_level, |
225
|
|
|
"batiment_production" => $batiment_production |
226
|
|
|
]); |
227
|
|
|
|
228
|
|
|
return $max_level; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* pour récupérer la construction en cours dans la base |
233
|
|
|
*/ |
234
|
|
|
public function getConstruction() { |
235
|
|
|
$dbc = App::getDb(); |
236
|
|
|
|
237
|
|
|
$today = Bataille::getToday(); |
238
|
|
|
|
239
|
|
|
$query = $dbc->select() |
240
|
|
|
->from("_bataille_construction") |
241
|
|
|
->from("_bataille_batiment") |
242
|
|
|
->where("_bataille_construction.ID_base", "=", Bataille::getIdBase(), "AND") |
243
|
|
|
->where("_bataille_construction.ID_batiment", "=", "_bataille_batiment.ID_batiment", "AND", true) |
244
|
|
|
->where("_bataille_construction.ID_base", "=", "_bataille_batiment.ID_base", "", true) |
245
|
|
|
->get(); |
246
|
|
|
|
247
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
248
|
|
|
foreach ($query as $obj) { |
249
|
|
|
$this->nom_batiment_construction = $obj->nom_batiment; |
250
|
|
|
$this->date_fin_construction = $obj->date_fin; |
251
|
|
|
$this->niveau_batiment_construction = $obj->niveau; |
252
|
|
|
$id_batiment = $obj->ID_batiment; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
if ($this->date_fin_construction-$today <= 0) { |
256
|
|
|
$this->setTerminerConstruction($id_batiment); |
|
|
|
|
257
|
|
|
} |
258
|
|
|
else { |
259
|
|
|
Bataille::setValues([ |
260
|
|
|
"date_fin_construction" => $this->date_fin_construction-$today, |
261
|
|
|
"nom_batiment_construction" => $this->nom_batiment_construction |
262
|
|
|
]); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return 1; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return 0; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* pour récupérer la liste des batiments qu'il est possible de construire |
273
|
|
|
*/ |
274
|
|
|
public function getBatimentAConstruire() { |
275
|
|
|
$dbc = App::getDb(); |
276
|
|
|
$dbc1 = Bataille::getDb(); |
277
|
|
|
$batiment_construit = []; |
278
|
|
|
$batiment_construire = []; |
279
|
|
|
|
280
|
|
|
//recuperation des batiments deja construit dans la base |
281
|
|
|
$query = $dbc->select("nom_batiment_sql") |
282
|
|
|
->select("nom_batiment") |
283
|
|
|
->select("niveau") |
284
|
|
|
->from("_bataille_batiment") |
285
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
286
|
|
|
->get(); |
287
|
|
|
|
288
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
289
|
|
|
foreach ($query as $obj) { |
290
|
|
|
$batiment_construit[] = $obj->nom_batiment_sql; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
//recuperation de la liste complete des batiments |
295
|
|
|
$query = $dbc1->select("nom_table")->select("nom")->from("liste_batiment")->where("actif", "=", 1)->get(); |
296
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
297
|
|
|
foreach ($query as $obj) { |
298
|
|
|
$all_batiment[] = $obj->nom_table; |
|
|
|
|
299
|
|
|
$all_batiment_nom[] = $obj->nom; |
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$c_all_batiment = count($all_batiment); |
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
//boucle qui recupere en tableau le champ pour_construire d'un batiment |
306
|
|
|
//et compare la liste des batiments qu'il faut pour construire le batiment |
307
|
|
|
//a ceux qui sont deja construit dans la base |
308
|
|
|
//si tous les batments qu'il faut son batis on autorise la construction du batiment |
309
|
|
|
for ($i=0 ; $i<$c_all_batiment ; $i++) { |
|
|
|
|
310
|
|
|
if (!in_array($all_batiment[$i], $batiment_construit)) { |
311
|
|
|
$query = $dbc1->select("pour_construire") |
312
|
|
|
->select("temps_construction") |
313
|
|
|
->from($all_batiment[$i]) |
314
|
|
|
->where("ID_".$all_batiment[$i], "=", 1) |
315
|
|
|
->get(); |
316
|
|
|
|
317
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
318
|
|
|
foreach ($query as $obj) { |
319
|
|
|
if ($obj->pour_construire != null) { |
320
|
|
|
$pour_construire = unserialize($obj->pour_construire); |
321
|
|
|
} |
322
|
|
|
else { |
323
|
|
|
$pour_construire = []; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
$temps_construction = gmdate("H:i:s", round($obj->temps_construction-($obj->temps_construction*Bataille::getBatiment()->getNiveauBatiment("centre_commandement")/100))); |
327
|
|
|
$taille_batiment = $this->getTailleBatiment($all_batiment[$i]); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
|
332
|
|
|
if (count($pour_construire) == 1) { |
333
|
|
|
if (in_array($pour_construire[0][1], $batiment_construit)) { |
334
|
|
|
if ($pour_construire[0][2] <= $this->getNiveauBatiment($pour_construire[0][1])) { |
|
|
|
|
335
|
|
|
$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0); |
336
|
|
|
|
337
|
|
|
$batiment_construire[] = [ |
338
|
|
|
"nom_batiment_sql" => $all_batiment[$i], |
339
|
|
|
"nom_batiment" => $all_batiment_nom[$i], |
|
|
|
|
340
|
|
|
"ressource" => $ressource, |
341
|
|
|
"temps_construction" => $temps_construction, |
|
|
|
|
342
|
|
|
"width" => $taille_batiment[0], |
|
|
|
|
343
|
|
|
"height" => $taille_batiment[1] |
344
|
|
|
]; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
else if (count($pour_construire) > 1) { |
349
|
|
|
$ok_construction = false; |
350
|
|
|
//test si tous les batiments sont construits et on le niveau nécéssaire |
351
|
|
|
for ($j=0 ; $j<count($pour_construire) ; $j++) { |
|
|
|
|
352
|
|
|
if (in_array($pour_construire[$j][1], $batiment_construit)) { |
353
|
|
|
if ($pour_construire[$j][2] <= $this->getNiveauBatiment($pour_construire[$j][1])) { |
354
|
|
|
$ok_construction = true; |
355
|
|
|
} |
356
|
|
|
else { |
357
|
|
|
$ok_construction = false; |
358
|
|
|
break; |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
else { |
362
|
|
|
$ok_construction = false; |
363
|
|
|
break; |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
//si ok on affiche le batiment |
368
|
|
View Code Duplication |
if ($ok_construction === true) { |
|
|
|
|
369
|
|
|
$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0); |
370
|
|
|
|
371
|
|
|
$batiment_construire[] = [ |
372
|
|
|
"nom_batiment_sql" => $all_batiment[$i], |
373
|
|
|
"nom_batiment" => $all_batiment_nom[$i], |
374
|
|
|
"ressource" => $ressource, |
375
|
|
|
"temps_construction" => $temps_construction, |
376
|
|
|
"width" => $taille_batiment[0], |
377
|
|
|
"height" => $taille_batiment[1] |
378
|
|
|
]; |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
View Code Duplication |
else { |
|
|
|
|
382
|
|
|
$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0); |
383
|
|
|
|
384
|
|
|
$batiment_construire[] = [ |
385
|
|
|
"nom_batiment_sql" => $all_batiment[$i], |
386
|
|
|
"nom_batiment" => $all_batiment_nom[$i], |
387
|
|
|
"ressource" => $ressource, |
388
|
|
|
"temps_construction" => $temps_construction, |
389
|
|
|
"width" => $taille_batiment[0], |
390
|
|
|
"height" => $taille_batiment[1] |
391
|
|
|
]; |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
Bataille::setValues(["batiments_construire" => $batiment_construire]); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @param $case_depart |
400
|
|
|
* @param $nom_batiment |
401
|
|
|
* @param $nom_batiment_sql |
402
|
|
|
* @return bool |
403
|
|
|
* fonction qui test si un il y a la place de construire un batiment en fonction de sa case ou il a été laché en x et y |
404
|
|
|
* et en fonction de sa taille et du positionnement des autres batiments |
405
|
|
|
*/ |
406
|
|
|
public function getEmplacementConstructionLibre($case_depart, $nom_batiment, $nom_batiment_sql) { |
407
|
|
|
$dbc = App::getDb(); |
408
|
|
|
|
409
|
|
|
//récupération de la taille du batiment |
410
|
|
|
$taille_batiment = $this->getTailleBatiment($nom_batiment_sql); |
411
|
|
|
$width_batiment = $taille_batiment[0]; |
412
|
|
|
$height_batiment = $taille_batiment[1]; |
413
|
|
|
|
414
|
|
|
//récupération des coordonnées de la sae de départ du batiment |
415
|
|
|
$case_depart = explode(",", $case_depart); |
416
|
|
|
$posx = $case_depart[0]; |
417
|
|
|
$posy = $case_depart[1]; |
418
|
|
|
$finx = $width_batiment+$posx; |
419
|
|
|
$finy = $height_batiment+$posy; |
420
|
|
|
|
421
|
|
|
//récupération de tous les batiments |
422
|
|
|
$query = $dbc->select("posx") |
423
|
|
|
->select("posy") |
424
|
|
|
->select("nom_batiment_sql") |
425
|
|
|
->from("_bataille_batiment") |
426
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
427
|
|
|
->get(); |
428
|
|
|
|
429
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
430
|
|
|
foreach ($query as $obj) { |
431
|
|
|
$taille_batiment = $this->getTailleBatiment($obj->nom_batiment_sql); |
432
|
|
|
$posx_batiment = $obj->posx; |
433
|
|
|
$posy_batiment = $obj->posy; |
434
|
|
|
|
435
|
|
|
$finx_batiment = $taille_batiment[0]+$posx_batiment; |
436
|
|
|
$finy_batiment = $taille_batiment[1]+$posy_batiment; |
437
|
|
|
|
438
|
|
|
for ($i=$posy ; $i<$finy ; $i++) { |
439
|
|
|
for ($j=$posx ; $j<$finx ; $j++) { |
440
|
|
|
if ((($posx++ >= $posx_batiment) && ($posx++ <= $finx_batiment)) && (($posy++ >= $posy_batiment) && ($posy++ <= $finy_batiment))) { |
441
|
|
|
FlashMessage::setFlash("Un batiment est déjà présent à cet emplacement, merci d'en choisir un autre"); |
442
|
|
|
return false; |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
$posx = $case_depart[0]; |
448
|
|
|
$posy = $case_depart[1]; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
//si tout est ok on commence la construction |
452
|
|
|
if ($this->setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql, $posx, $posy) === false) { |
453
|
|
|
return false; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
echo("ok"); |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* @param $nom_batiment_sql |
462
|
|
|
* @return array |
463
|
|
|
* fonction qui renvoi un tableau contenant la taille et hauteur d'un batiment en |
464
|
|
|
* fonction de son nom |
465
|
|
|
*/ |
466
|
|
View Code Duplication |
public function getTailleBatiment($nom_batiment_sql) { |
|
|
|
|
467
|
|
|
$dbc1 = Bataille::getDb(); |
468
|
|
|
|
469
|
|
|
$query = $dbc1->select("width") |
470
|
|
|
->select("height") |
471
|
|
|
->from("liste_batiment") |
472
|
|
|
->where("nom_table", "=", $nom_batiment_sql) |
473
|
|
|
->get(); |
474
|
|
|
|
475
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
476
|
|
|
foreach ($query as $obj) { |
477
|
|
|
return [$obj->width, $obj->height]; |
478
|
|
|
} |
479
|
|
|
} |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @param $nom_batiment_sql |
484
|
|
|
* @param $niveau |
485
|
|
|
* @return array |
486
|
|
|
* recuperation des ressources nécéssaire pour construire le batiment |
487
|
|
|
*/ |
488
|
|
|
private function getRessourceConstruireBatiment($nom_batiment_sql, $niveau) { |
489
|
|
|
$dbc1 = Bataille::getDb(); |
490
|
|
|
|
491
|
|
|
$niveau = $niveau+1; |
492
|
|
|
|
493
|
|
|
$query = $dbc1->select("ressource_construire")->from($nom_batiment_sql)->where("ID_".$nom_batiment_sql, "=", $niveau)->get(); |
494
|
|
|
|
495
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
496
|
|
|
foreach ($query as $obj) { |
497
|
|
|
$ressource_tmp = $obj->ressource_construire; |
498
|
|
|
} |
499
|
|
|
$ressource_tmp = explode(", ", $ressource_tmp); |
|
|
|
|
500
|
|
|
|
501
|
|
|
//on test si assez de ressources dans la base |
502
|
|
|
//fer |
503
|
|
|
$ressource["fer"] = Bataille::getTestAssezRessourceBase("fer", $ressource_tmp[2]); |
|
|
|
|
504
|
|
|
//fuel |
505
|
|
|
$ressource["fuel"] = Bataille::getTestAssezRessourceBase("fuel", $ressource_tmp[3]); |
506
|
|
|
//eau |
507
|
|
|
$ressource["eau"] = Bataille::getTestAssezRessourceBase("eau", $ressource_tmp[0]); |
508
|
|
|
//electricite |
509
|
|
|
$ressource["electricite"] = Bataille::getTestAssezRessourceBase("electricite", $ressource_tmp[1]); |
510
|
|
|
|
511
|
|
|
return $ressource; |
512
|
|
|
} |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* fonction qui renvoi un tableau avec le nom du batiment sans (en construction) |
517
|
|
|
* + true pour dire que le batiment est en construction |
518
|
|
|
* |
519
|
|
|
* @param $nom_batiment |
520
|
|
|
* @return array |
521
|
|
|
*/ |
522
|
|
|
private function getTestBatimentConstruction($nom_batiment) { |
523
|
|
|
if (ChaineCaractere::FindInString($nom_batiment, " en construction") == true) { |
524
|
|
|
return [substr($nom_batiment, 0, (0-strlen(" en construction"))), true]; |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
return [$nom_batiment, false]; |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* fonction qui renvoi les informations pour augmenter le niveau d'un batiment |
532
|
|
|
* @return int |
533
|
|
|
*/ |
534
|
|
|
private function getInfoUpgradeBatiment() { |
535
|
|
|
$dbc1 = Bataille::getDb(); |
536
|
|
|
|
537
|
|
|
//récupération du temps et des ressources pour construire |
538
|
|
|
$query = $dbc1->select()->from($this->nom_batiment_sql)->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)->get(); |
539
|
|
|
|
540
|
|
|
//si on a quelque chose cela veut dire qu'on est pas encore au lvl max du batiment |
541
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
542
|
|
|
foreach ($query as $obj) { |
543
|
|
|
$this->ressource_construire = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment); |
544
|
|
|
$this->temps_construction = DateHeure::Secondeenheure(round($obj->temps_construction-($obj->temps_construction*Bataille::getBatiment()->getNiveauBatiment("centre_commandement")/100))); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
//récupération des éléments particulier à un batiment |
548
|
|
|
$xml = simplexml_load_file(MODULEROOT.'bataille/data/batiment.xml'); |
549
|
|
|
$nom_batiment_sql = $this->nom_batiment_sql; |
550
|
|
|
$champ = $xml->$nom_batiment_sql->champ; |
551
|
|
|
|
552
|
|
|
if (!empty($champ)) { |
553
|
|
|
//récupération de la phrase pour le niveau actuel |
554
|
|
|
$query = $dbc1->select($xml->$nom_batiment_sql->champ) |
555
|
|
|
->from($this->nom_batiment_sql) |
556
|
|
|
->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment) |
557
|
|
|
->get(); |
558
|
|
|
|
559
|
|
View Code Duplication |
if ((is_array($query)) && (count($query) > 0)){ |
|
|
|
|
560
|
|
|
foreach ($query as $obj) { |
561
|
|
|
$this->info_batiment = $xml->$nom_batiment_sql->phrase.$obj->$champ.$xml->$nom_batiment_sql->complement; |
562
|
|
|
} |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
//récupération de la phrase pour le niveau suivant |
566
|
|
|
$query = $dbc1->select($xml->$nom_batiment_sql->champ) |
567
|
|
|
->from($this->nom_batiment_sql) |
568
|
|
|
->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1) |
569
|
|
|
->get(); |
570
|
|
|
|
571
|
|
View Code Duplication |
if ((is_array($query)) && (count($query) > 0)){ |
|
|
|
|
572
|
|
|
foreach ($query as $obj) { |
573
|
|
|
$this->info_batiment_next = $xml->$nom_batiment_sql->phrase_suivant.$obj->$champ.$xml->$nom_batiment_sql->complement; |
574
|
|
|
} |
575
|
|
|
} |
576
|
|
|
} |
577
|
|
|
else { |
578
|
|
|
$this->info_batiment = ""; |
579
|
|
|
$this->info_batiment_next = ""; |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
|
583
|
|
|
Bataille::setValues([ |
584
|
|
|
"ressource" => $this->ressource_construire, |
585
|
|
|
"temps_construction" => $this->temps_construction, |
586
|
|
|
"info_batiment" => $this->info_batiment, |
587
|
|
|
"info_batiment_next" => $this->info_batiment_next, |
588
|
|
|
]); |
589
|
|
|
|
590
|
|
|
return 1; |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
return "max_level"; |
594
|
|
|
} |
595
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
596
|
|
|
|
597
|
|
|
|
598
|
|
|
|
599
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
600
|
|
|
/** |
601
|
|
|
* fonction qui initialise la construction d'un batiment |
602
|
|
|
* @param $nom_batiment |
603
|
|
|
* @param $nom_batiment_sql |
604
|
|
|
*/ |
605
|
|
|
public function setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql, $posx, $posy) { |
606
|
|
|
$dbc = App::getDb(); |
607
|
|
|
$dbc1 = Bataille::getDb(); |
608
|
|
|
|
609
|
|
|
if ($this->getConstruction() == 0) { |
610
|
|
|
$un_batiment = $this->getUnBatiment($nom_batiment); |
611
|
|
|
|
612
|
|
|
//on test si assez de ressrouce pour construire le batiment |
613
|
|
|
if ($un_batiment == 0) { |
614
|
|
|
$ressource = $this->getRessourceConstruireBatiment($nom_batiment_sql, 0); |
615
|
|
|
$this->nom_batiment_sql = $nom_batiment_sql; |
616
|
|
|
$this->niveau_batiment = 0; |
617
|
|
|
} |
618
|
|
|
else { |
619
|
|
|
$ressource = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment); |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
//si pas assez de ressource |
623
|
|
|
if (($ressource["eau"]["class"] || $ressource["electricite"]["class"] ||$ressource["fer"]["class"] ||$ressource["fuel"]["class"]) == "rouge") { |
624
|
|
|
FlashMessage::setFlash("Pas assez de ressources pour construire ce batiment"); |
625
|
|
|
return false; |
626
|
|
|
} |
627
|
|
|
else { |
628
|
|
|
//recuperation du temps de construction |
629
|
|
|
$query = $dbc1->select("ressource_construire") |
630
|
|
|
->select("temps_construction") |
631
|
|
|
->from($this->nom_batiment_sql) |
632
|
|
|
->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1) |
633
|
|
|
->get(); |
634
|
|
|
|
635
|
|
|
foreach ($query as $obj) { |
636
|
|
|
$temps_construction = round($obj->temps_construction-($obj->temps_construction*Bataille::getBatiment()->getNiveauBatiment("centre_commandement")/100)); |
637
|
|
|
$ressource_construction = explode(", ", $obj->ressource_construire); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
//on insere la construction dans la table batiment si new batiment |
641
|
|
|
if ($un_batiment == 0) { |
642
|
|
|
$dbc->insert("niveau", $this->niveau_batiment+1) |
643
|
|
|
->insert("nom_batiment", $nom_batiment) |
644
|
|
|
->insert("nom_batiment_sql", $this->nom_batiment_sql) |
645
|
|
|
->insert("posx", intval($posx)) |
646
|
|
|
->insert("posy", intval($posy)) |
647
|
|
|
->insert("construction", 1) |
648
|
|
|
->insert("ID_base", Bataille::getIdBase()) |
649
|
|
|
->into("_bataille_batiment") |
650
|
|
|
->set(); |
651
|
|
|
|
652
|
|
|
$this->id_batiment = $dbc->lastInsertId(); |
653
|
|
|
} |
654
|
|
|
else { |
655
|
|
|
$dbc->update("niveau", $this->niveau_batiment+1) |
656
|
|
|
->update("construction", 1) |
657
|
|
|
->from("_bataille_batiment") |
658
|
|
|
->where("ID_batiment", "=", $this->id_batiment, "AND") |
659
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
660
|
|
|
->set(); |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
|
664
|
|
|
//on initialise la construction |
665
|
|
|
//recuperation de la date en seconde |
666
|
|
|
$today = Bataille::getToday(); |
667
|
|
|
|
668
|
|
|
//date de la fin de la construction en seconde |
669
|
|
|
$fin_construction = $today+$temps_construction; |
|
|
|
|
670
|
|
|
|
671
|
|
|
$dbc->insert("date_fin", $fin_construction) |
672
|
|
|
->insert("ID_base", Bataille::getIdBase()) |
673
|
|
|
->insert("ID_batiment", $this->id_batiment) |
674
|
|
|
->into("_bataille_construction") |
675
|
|
|
->set(); |
676
|
|
|
|
677
|
|
|
//on retire les ressources de la base |
678
|
|
|
Bataille::getRessource()->setUpdateRessource($ressource_construction[2], $ressource_construction[3], $ressource_construction[0], $ressource_construction[1], 0, "-"); |
|
|
|
|
679
|
|
|
} |
680
|
|
|
} |
681
|
|
|
else { |
682
|
|
|
FlashMessage::setFlash("Un batiment est déjà en construction, vous ne pouvez pas en construire un autre !"); |
683
|
|
|
return false; |
684
|
|
|
} |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* fonction qui termine la construction d'un batiment |
689
|
|
|
* @param $id_batiment |
690
|
|
|
*/ |
691
|
|
|
private function setTerminerConstruction($id_batiment) { |
692
|
|
|
$dbc = App::getDb(); |
693
|
|
|
|
694
|
|
|
//on le retire de la table construction |
695
|
|
|
$dbc->delete() |
696
|
|
|
->from("_bataille_construction") |
697
|
|
|
->where("ID_base", "=", Bataille::getIdBase(), "AND") |
698
|
|
|
->where("ID_batiment", "=", $id_batiment) |
699
|
|
|
->del(); |
700
|
|
|
|
701
|
|
|
//on termine la construction dans la table batiment |
702
|
|
|
$dbc->update("construction", 0) |
703
|
|
|
->from("_bataille_batiment") |
704
|
|
|
->where("ID_base", "=", Bataille::getIdBase(), "AND") |
705
|
|
|
->where("ID_batiment", "=", $id_batiment) |
706
|
|
|
->set(); |
707
|
|
|
|
708
|
|
|
//on ajoute les points à la base |
709
|
|
|
Points::setAjouterPoints(Bataille::getIdBase(), "batiment"); |
710
|
|
|
} |
711
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
712
|
|
|
} |
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: