|
1
|
|
|
<?php |
|
2
|
|
|
namespace modules\bataille\app\controller; |
|
3
|
|
|
use core\App; |
|
4
|
|
|
|
|
5
|
|
|
class Base { |
|
6
|
|
|
private $batiments; |
|
7
|
|
|
|
|
8
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
|
9
|
|
|
public function __construct() { |
|
10
|
|
|
|
|
11
|
|
|
} |
|
12
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
|
17
|
|
|
public function getBatiments() { |
|
18
|
|
|
return $this->batiments; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* fonction qui va initaliser la base |
|
23
|
|
|
*/ |
|
24
|
|
|
public function getMaBase() { |
|
25
|
|
|
$this->getInfoBase(); |
|
26
|
|
|
|
|
27
|
|
|
$this->getBasesJoueur(); |
|
28
|
|
|
|
|
29
|
|
|
Bataille::getRessource(); |
|
30
|
|
|
|
|
31
|
|
|
Bataille::setValues([ |
|
32
|
|
|
"production_eau" => Bataille::getBatiment()->getProduction("eau"), |
|
33
|
|
|
"production_electricite" => Bataille::getBatiment()->getProduction("electricite"), |
|
34
|
|
|
"production_fer" => Bataille::getBatiment()->getProduction("fer"), |
|
35
|
|
|
"production_fuel" => Bataille::getBatiment()->getProduction("fuel"), |
|
36
|
|
|
"stockage_entrepot" => Bataille::getBatiment()->getStockage(), |
|
37
|
|
|
"stockage_grenier" => Bataille::getBatiment()->getStockage("grenier") |
|
38
|
|
|
]); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param null $id_base |
|
43
|
|
|
* fonction qui va récupérer les infos de la base |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getInfoBase($id_base = null) { |
|
46
|
|
|
$dbc = App::getDb(); |
|
47
|
|
|
|
|
48
|
|
|
if ($id_base == null) { |
|
49
|
|
|
$id_base = Bataille::getIdBase(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$query = $dbc->select("nom_base")->select("points")->from("_bataille_base")->where("ID_base", "=", $id_base)->get(); |
|
53
|
|
|
|
|
54
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
|
55
|
|
|
foreach ($query as $obj) { |
|
56
|
|
|
Bataille::setValues([ |
|
57
|
|
|
"nom_base" => $obj->nom_base, |
|
58
|
|
|
"points" => $obj->points |
|
59
|
|
|
]); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* fonction qui recupere tous les batiments de la base |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getBatimentsBase() { |
|
68
|
|
|
$dbc = App::getDb(); |
|
69
|
|
|
|
|
70
|
|
|
$query = $dbc->select()->from("_bataille_batiment")->where("ID_base", "=", Bataille::getIdBase())->get(); |
|
71
|
|
|
|
|
72
|
|
|
if (count($query) > 0) { |
|
73
|
|
|
$batiments = []; |
|
74
|
|
|
|
|
75
|
|
|
foreach ($query as $obj) { |
|
76
|
|
|
$taille_batiment = Bataille::getBatiment()->getTailleBatiment($obj->nom_batiment_sql); |
|
77
|
|
|
$construction = ""; |
|
78
|
|
|
|
|
79
|
|
|
if ($obj->construction) { |
|
80
|
|
|
$construction = "construction"; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$batiments[] = [ |
|
84
|
|
|
"nom_batiment" => $obj->nom_batiment, |
|
85
|
|
|
"nom_batiment_sql" => $obj->nom_batiment_sql, |
|
86
|
|
|
"niveau" => $obj->niveau, |
|
87
|
|
|
"posx" => $obj->posx, |
|
88
|
|
|
"posy" => $obj->posy, |
|
89
|
|
|
"width" => $taille_batiment[0], |
|
90
|
|
|
"height" => $taille_batiment[1], |
|
91
|
|
|
"construction" => $construction |
|
92
|
|
|
]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
Bataille::setValues(["batiments" => $batiments]); |
|
96
|
|
|
|
|
97
|
|
|
$this->setBatimentsBase($batiments); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param null $id_identite |
|
103
|
|
|
* fonction qui récupère les bases d'un joueur |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getBasesJoueur($id_identite = null) { |
|
106
|
|
|
$dbc = App::getDb(); |
|
107
|
|
|
$exclude = 0; |
|
108
|
|
|
|
|
109
|
|
|
if ($id_identite === null) { |
|
110
|
|
|
$id_identite = Bataille::getIdIdentite(); |
|
111
|
|
|
$exclude = Bataille::getIdBase(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$query = $dbc->select("pseudo")->from("identite")->where("ID_identite", "=", $id_identite)->get(); |
|
115
|
|
|
|
|
116
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
|
117
|
|
|
foreach ($query as $obj) { |
|
118
|
|
|
Bataille::setValues(["pseudo" => $obj->pseudo]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$query = $dbc->select() |
|
122
|
|
|
->from("_bataille_base") |
|
123
|
|
|
->where("ID_identite", "=", $id_identite, "AND") |
|
124
|
|
|
->where("ID_base", "!=", $exclude) |
|
125
|
|
|
->orderBy("ID_base", "DESC") |
|
126
|
|
|
->get(); |
|
127
|
|
|
|
|
128
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
|
129
|
|
|
foreach ($query as $obj) { |
|
130
|
|
|
$une_base[] = [ |
|
|
|
|
|
|
131
|
|
|
"id_base" => $obj->ID_base, |
|
132
|
|
|
"nom_base" => $obj->nom_base, |
|
133
|
|
|
"points_base" => $obj->points, |
|
134
|
|
|
"posx" => $obj->posx, |
|
135
|
|
|
"posy" => $obj->posy |
|
136
|
|
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (count($une_base) > 0) { |
|
141
|
|
|
Bataille::setValues(["base_joueur" => $une_base]); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
|
150
|
|
|
private function setBatimentsBase($batiments) { |
|
151
|
|
|
$this->batiments = $batiments; |
|
152
|
|
|
} |
|
153
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
|
154
|
|
|
} |
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.