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_base.points") |
16
|
|
|
->select("_bataille_base.ID_identite") |
17
|
|
|
->from("_bataille_base") |
18
|
|
|
->from("identite") |
19
|
|
|
->where("_bataille_base.ID_identite", "=", "identite.ID_identite", "", true) |
20
|
|
|
->orderBy("_bataille_base.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
|
|
|
return $points; |
104
|
|
|
} |
105
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
106
|
|
|
|
107
|
|
|
} |
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.