|
1
|
|
|
<?php |
|
2
|
|
|
namespace modules\bataille\app\controller; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use core\App; |
|
6
|
|
|
|
|
7
|
|
|
class MissionsAleatoire { |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
|
12
|
|
|
/** |
|
13
|
|
|
* MissionsAleatoire constructor. |
|
14
|
|
|
* le constructeur s'occupe de vérifier le last_check des missions et au cas ou si il est plus vieux d'un jour |
|
15
|
|
|
* appeler la fonction pour recharger les missions |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct() { |
|
18
|
|
|
$dbc = App::getDb(); |
|
19
|
|
|
|
|
20
|
|
|
$query = $dbc->select("last_check_mission")->from("_bataille_base")->where("ID_base", "=", Bataille::getIdBase())->get(); |
|
21
|
|
|
|
|
22
|
|
|
if (is_array($query) && (count($query) == 1)) { |
|
23
|
|
|
foreach ($query as $obj) { |
|
24
|
|
|
$last_check_mission = $obj->last_check_mission; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if ($last_check_mission == "") { |
|
28
|
|
|
$this->setUpdateLastCheckMissions(); |
|
29
|
|
|
$this->setMissionsAleatoire(); |
|
30
|
|
|
} |
|
31
|
|
|
else { |
|
32
|
|
|
$today = new \DateTime(); |
|
33
|
|
|
$last_check_mission = new \DateTime($last_check_mission); |
|
|
|
|
|
|
34
|
|
|
$interval = $last_check_mission->diff($today); |
|
35
|
|
|
|
|
36
|
|
|
$diff_jour = explode("+", $interval->format("%R%a"))[1]; |
|
37
|
|
|
|
|
38
|
|
|
if ($diff_jour >= 1) { |
|
39
|
|
|
$this->setUpdateLastCheckMissions(); |
|
40
|
|
|
$this->setMissionsAleatoire(); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
|
50
|
|
|
/** |
|
51
|
|
|
* fonction qui récupere tous les types de missions et les return dans un array |
|
52
|
|
|
*/ |
|
53
|
|
|
private function getTypeMission() { |
|
54
|
|
|
return explode(",", Bataille::getParam("type_missions")); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return int |
|
59
|
|
|
* renvoi le nombre de missions encore disponibles dans la base |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getNbMissions() { |
|
62
|
|
|
$dbc = App::getDb(); |
|
63
|
|
|
|
|
64
|
|
|
$query = $dbc->select("ID_mission_aleatoire")->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->get(); |
|
65
|
|
|
|
|
66
|
|
|
if ((is_array($query)) && (count($query))) { |
|
67
|
|
|
foreach ($query as $obj) { |
|
68
|
|
|
$id[] = $obj->ID_base; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$count = count($id); |
|
|
|
|
|
|
72
|
|
|
Bataille::setValues([ |
|
73
|
|
|
"nb_mission" => $count |
|
74
|
|
|
]); |
|
75
|
|
|
|
|
76
|
|
|
return $count; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getMissions() { |
|
81
|
|
|
$dbc = App::getDb(); |
|
|
|
|
|
|
82
|
|
|
$dbc1 = Bataille::getDb(); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
|
91
|
|
|
/** |
|
92
|
|
|
* fonction qui met a jour le last_ckeck_missions dans _bataille_base |
|
93
|
|
|
* le met à la date du jour |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setUpdateLastCheckMissions() { |
|
96
|
|
|
$dbc = App::getDb(); |
|
97
|
|
|
|
|
98
|
|
|
$dbc->update("last_check_mission", date("Y-m-d")) |
|
99
|
|
|
->from("_bataille_base") |
|
100
|
|
|
->where("ID_base", "=", Bataille::getIdBase()) |
|
101
|
|
|
->set(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param $type |
|
106
|
|
|
* fonction qui recupere des missions aleatoirement de chaque type et qui les ajoute |
|
107
|
|
|
* dans la table _bataille_mission_aleatoire |
|
108
|
|
|
*/ |
|
109
|
|
|
private function setMissionsAleatoire() { |
|
110
|
|
|
$dbc = App::getDb(); |
|
111
|
|
|
$dbc1 = Bataille::getDb(); |
|
112
|
|
|
|
|
113
|
|
|
$dbc->delete()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->del(); |
|
114
|
|
|
|
|
115
|
|
|
$type_missions = $this->getTypeMission(); |
|
116
|
|
|
|
|
117
|
|
|
foreach ($type_missions as $un_type) { |
|
118
|
|
|
$query = $dbc1->select()->from("mission") |
|
119
|
|
|
->where("type", "=", $un_type) |
|
120
|
|
|
->orderBy("RAND()") |
|
121
|
|
|
->limit(0, 3) |
|
122
|
|
|
->get(); |
|
123
|
|
|
|
|
124
|
|
|
if ((is_array($query)) && (count($query))) { |
|
125
|
|
|
foreach ($query as $obj) { |
|
126
|
|
|
$dbc->insert("ID_mission", $obj->ID_mission) |
|
127
|
|
|
->insert("ID_base", Bataille::getIdBase()) |
|
128
|
|
|
->into("_bataille_mission_aleatoire") |
|
129
|
|
|
->set(); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
|
135
|
|
|
|
|
136
|
|
|
} |
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: