|
1
|
|
|
<?php |
|
2
|
|
|
namespace modules\bataille\app\controller; |
|
3
|
|
|
|
|
4
|
|
|
use core\App; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class Points { |
|
8
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
|
9
|
|
|
public function __construct($start = null) { |
|
10
|
|
|
$dbc = App::getDb(); |
|
11
|
|
|
|
|
12
|
|
|
if ($start === null) $start = 0; |
|
13
|
|
|
|
|
14
|
|
|
$query = $dbc->select("identite.pseudo") |
|
15
|
|
|
->select("_bataille_infos_player.points") |
|
16
|
|
|
->select("_bataille_infos_player.ID_identite") |
|
17
|
|
|
->from("_bataille_infos_player") |
|
18
|
|
|
->from("identite") |
|
19
|
|
|
->where("_bataille_infos_player.ID_identite", "=", "identite.ID_identite", "", true) |
|
20
|
|
|
->orderBy("_bataille_infos_player.points", "DESC") |
|
21
|
|
|
->limit($start, 50) |
|
22
|
|
|
->get(); |
|
23
|
|
|
|
|
24
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
|
25
|
|
|
$count = 1; |
|
26
|
|
|
foreach ($query as $obj) { |
|
27
|
|
|
$values[] = [ |
|
|
|
|
|
|
28
|
|
|
"id_identite" => $obj->ID_identite, |
|
29
|
|
|
"pseudo" => $obj->pseudo, |
|
30
|
|
|
"points" => $obj->points, |
|
31
|
|
|
"classement" => $count |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
$count++; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
Bataille::setValues(["classement" => $values]); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
|
45
|
|
|
/** |
|
46
|
|
|
* @param $id_base |
|
47
|
|
|
* @return int |
|
48
|
|
|
* renvoi les points de la base |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function getPointsBase($id_base) { |
|
|
|
|
|
|
51
|
|
|
$dbc = App::getDb(); |
|
52
|
|
|
|
|
53
|
|
|
//on récupère les points de la base en cours |
|
54
|
|
|
$query = $dbc->select("points") |
|
55
|
|
|
->from("_bataille_base") |
|
56
|
|
|
->where("ID_base", "=", $id_base, "AND") |
|
57
|
|
|
->where("ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE]) |
|
58
|
|
|
->get(); |
|
59
|
|
|
|
|
60
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
|
61
|
|
|
foreach ($query as $obj) { |
|
62
|
|
|
return $obj->points; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return 0; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
* fonction qui renvoi le nombre de points à ajouter à la base lorsqu'on update un batiment |
|
72
|
|
|
*/ |
|
73
|
|
|
private static function getPointAjoutBatiment() { |
|
74
|
|
|
return Bataille::getParam("points_batiment"); |
|
75
|
|
|
} |
|
76
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
|
80
|
|
|
/** |
|
81
|
|
|
* @param $id_base |
|
82
|
|
|
* @param null $type |
|
83
|
|
|
* @param null $points |
|
84
|
|
|
* @return int|null |
|
85
|
|
|
* fonction qui ajoute des points à la base en fonction du type |
|
86
|
|
|
* le type peut etre : batiment, attaque, defense, troupe |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function setAjouterPoints($id_base, $type=null, $points=null) { |
|
89
|
|
|
$dbc = App::getDb(); |
|
90
|
|
|
|
|
91
|
|
|
if ($type == "batiment") { |
|
92
|
|
|
$points = self::getPointsBase($id_base)+self::getPointAjoutBatiment(); |
|
93
|
|
|
} |
|
94
|
|
|
else if ($type == "missions") { |
|
95
|
|
|
$points = self::getPointsBase($id_base)+$points; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$dbc->update("points", $points) |
|
99
|
|
|
->from("_bataille_base") |
|
100
|
|
|
->where("ID_base", "=", $id_base) |
|
101
|
|
|
->set(); |
|
102
|
|
|
|
|
103
|
|
|
self::setAjouterPointsTotaux(); |
|
104
|
|
|
|
|
105
|
|
|
return $points; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* fonction qui prend les points de toutes les bases et qui les ajoute sur le joueur en lui même |
|
110
|
|
|
*/ |
|
111
|
|
|
private static function setAjouterPointsTotaux() { |
|
112
|
|
|
$dbc = App::getDb(); |
|
113
|
|
|
|
|
114
|
|
|
$query = $dbc->select("points")->from("_bataille_base")->where("ID_identite", "=", Bataille::getIdIdentite())->get(); |
|
115
|
|
|
|
|
116
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
|
117
|
|
|
$points = 0; |
|
118
|
|
|
|
|
119
|
|
|
foreach ($query as $obj) { |
|
120
|
|
|
$points += $obj->points; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$dbc->update("points", $points)->from("_bataille_infos_player")->where("ID_identite", "=", Bataille::getIdIdentite())->set(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
|
127
|
|
|
|
|
128
|
|
|
} |
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.