1
|
|
|
<?php |
2
|
|
|
namespace modules\bataille\app\controller; |
3
|
|
|
|
4
|
|
|
use core\App; |
5
|
|
|
use core\HTML\flashmessage\FlashMessage; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class Points { |
9
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
10
|
|
|
public function __construct($start = null) { |
11
|
|
|
$dbc = App::getDb(); |
12
|
|
|
|
13
|
|
|
if ($start === null) $start = 0; |
14
|
|
|
|
15
|
|
|
$query = $dbc->select("identite.pseudo") |
16
|
|
|
->select("_bataille_infos_player.points") |
17
|
|
|
->select("_bataille_infos_player.ID_identite") |
18
|
|
|
->from("_bataille_infos_player") |
19
|
|
|
->from("identite") |
20
|
|
|
->where("abandon", "!=", 1, "AND") |
21
|
|
|
->where("_bataille_infos_player.ID_identite", "=", "identite.ID_identite", "", true) |
22
|
|
|
->orderBy("_bataille_infos_player.points", "DESC") |
23
|
|
|
->limit($start, 50) |
24
|
|
|
->get(); |
25
|
|
|
|
26
|
|
View Code Duplication |
if ((is_array($query)) && (count($query) > 0)) { |
27
|
|
|
$count = 1; |
28
|
|
|
foreach ($query as $obj) { |
29
|
|
|
$values[] = [ |
|
|
|
|
30
|
|
|
"id_identite" => $obj->ID_identite, |
31
|
|
|
"pseudo" => $obj->pseudo, |
32
|
|
|
"points" => $obj->points, |
33
|
|
|
"classement" => $count |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
$count++; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
Bataille::setValues(["classement" => $values]); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
} |
43
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
47
|
|
|
/** |
48
|
|
|
* @param $id_base |
49
|
|
|
* @return int |
50
|
|
|
* renvoi les points de la base |
51
|
|
|
*/ |
52
|
|
|
public static function getPointsBase($id_base) { |
|
|
|
|
53
|
|
|
$dbc = App::getDb(); |
54
|
|
|
|
55
|
|
|
//on récupère les points de la base en cours |
56
|
|
|
$query = $dbc->select("points") |
57
|
|
|
->from("_bataille_base") |
58
|
|
|
->where("ID_base", "=", $id_base, "AND") |
59
|
|
|
->where("ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE]) |
60
|
|
|
->get(); |
61
|
|
|
|
62
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
63
|
|
|
foreach ($query as $obj) { |
64
|
|
|
return $obj->points; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return 0; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param null $id_identite |
73
|
|
|
* @return int |
74
|
|
|
* renvoi les points totaux d'un joueur |
75
|
|
|
*/ |
76
|
|
|
public static function getPointsJoueur($id_identite=null) { |
77
|
|
|
$dbc = App::getDb(); |
78
|
|
|
|
79
|
|
|
if ($id_identite === null) { |
80
|
|
|
$id_identite = Bataille::getIdIdentite(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$query = $dbc->select("points")->from("_bataille_infos_player")->where("ID_identite", "=", $id_identite)->get(); |
84
|
|
|
|
85
|
|
|
if (count($query) == 1) { |
86
|
|
|
foreach ($query as $obj) { |
87
|
|
|
return $obj->points; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return mixed |
96
|
|
|
* fonction qui renvoi le nombre de points à ajouter à la base lorsqu'on update un batiment |
97
|
|
|
*/ |
98
|
|
|
private static function getPointAjoutBatiment() { |
99
|
|
|
return Bataille::getParam("points_batiment"); |
100
|
|
|
} |
101
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
105
|
|
|
/** |
106
|
|
|
* @param $id_base |
107
|
|
|
* @param null $type |
108
|
|
|
* @param null $points |
109
|
|
|
* @return int|null |
110
|
|
|
* fonction qui ajoute des points à la base en fonction du type |
111
|
|
|
* le type peut etre : batiment, attaque, defense, troupe |
112
|
|
|
*/ |
113
|
|
|
public static function setAjouterPoints($id_base, $type=null, $points=null) { |
114
|
|
|
$dbc = App::getDb(); |
115
|
|
|
|
116
|
|
|
if ($type == "batiment") { |
117
|
|
|
$points_faction = self::getPointAjoutBatiment(); |
118
|
|
|
$points = self::getPointsBase($id_base)+self::getPointAjoutBatiment(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$dbc->update("points", $points) |
122
|
|
|
->from("_bataille_base") |
123
|
|
|
->where("ID_base", "=", $id_base) |
124
|
|
|
->set(); |
125
|
|
|
|
126
|
|
|
self::setAjouterPointsTotaux(); |
127
|
|
|
self::setAjouterPointsFaction($points_faction); |
|
|
|
|
128
|
|
|
|
129
|
|
|
return $points; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* fonction qui prend les points de toutes les bases et qui les ajoute sur le joueur en lui même |
134
|
|
|
*/ |
135
|
|
|
private static function setAjouterPointsTotaux() { |
136
|
|
|
$dbc = App::getDb(); |
137
|
|
|
|
138
|
|
|
$query = $dbc->select("points")->from("_bataille_base")->where("ID_identite", "=", Bataille::getIdIdentite())->get(); |
139
|
|
|
|
140
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
141
|
|
|
$points = 0; |
142
|
|
|
|
143
|
|
|
foreach ($query as $obj) { |
144
|
|
|
$points += $obj->points; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$dbc->update("points", $points)->from("_bataille_infos_player")->where("ID_identite", "=", Bataille::getIdIdentite())->set(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* fonction qui permet d'ajouter tous les points du joueur aux points de la faction |
153
|
|
|
*/ |
154
|
|
|
public static function setRejoindreQuitterFaction($del = null) { |
155
|
|
|
$dbc = App::getDb(); |
156
|
|
|
|
157
|
|
|
$query = $dbc->select("_bataille_faction.points_faction, _bataille_faction.ID_faction") |
158
|
|
|
->from("_bataille_faction,_bataille_infos_player") |
159
|
|
|
->where("_bataille_infos_player.ID_identite", "=", Bataille::getIdIdentite(), "AND") |
160
|
|
|
->where("_bataille_faction.ID_faction", "=", "_bataille_infos_player.ID_faction", "", true) |
161
|
|
|
->get(); |
162
|
|
|
|
163
|
|
|
if (count($query) > 0) { |
164
|
|
|
foreach ($query as $obj) { |
165
|
|
|
$point_joueur = Points::getPointsJoueur(); |
166
|
|
|
$calc = $obj->points_faction - $point_joueur; |
167
|
|
|
|
168
|
|
|
if ($del === null) { |
169
|
|
|
$calc = $point_joueur+$obj->points_faction; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$dbc->update("points_faction", $calc) |
173
|
|
|
->from("_bataille_faction") |
174
|
|
|
->where("ID_faction", "=", $obj->ID_faction) |
175
|
|
|
->set(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param $points |
182
|
|
|
* permet d'ajouter des points à la faction |
183
|
|
|
*/ |
184
|
|
|
public static function setAjouterPointsFaction($points) { |
185
|
|
|
$dbc = App::getDb(); |
186
|
|
|
|
187
|
|
|
$query = $dbc->select("_bataille_faction.points_faction, _bataille_faction.ID_faction") |
188
|
|
|
->from("_bataille_faction,_bataille_infos_player") |
189
|
|
|
->where("_bataille_infos_player.ID_identite", "=", Bataille::getIdIdentite(), "AND") |
190
|
|
|
->where("_bataille_faction.ID_faction", "=", "_bataille_infos_player.ID_faction", "", true) |
191
|
|
|
->get(); |
192
|
|
|
|
193
|
|
|
if (count($query) > 0) { |
194
|
|
|
foreach ($query as $obj) { |
195
|
|
|
$dbc->update("points_faction", $points+$obj->points_faction) |
196
|
|
|
->from("_bataille_faction") |
197
|
|
|
->where("ID_faction", "=", $obj->ID_faction) |
198
|
|
|
->set(); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
203
|
|
|
|
204
|
|
|
} |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.