1
|
|
|
<?php |
2
|
|
|
namespace modules\bataille\app\controller; |
3
|
|
|
use core\App; |
4
|
|
|
use core\database\Database; |
5
|
|
|
|
6
|
|
|
class Bataille { |
7
|
|
|
private static $ressource; |
8
|
|
|
private static $base; |
9
|
|
|
private static $batiment; |
10
|
|
|
private static $unite; |
11
|
|
|
private static $centre_recherche; |
12
|
|
|
private static $missions_aleatoire; |
13
|
|
|
private static $database; |
14
|
|
|
private static $nation; |
15
|
|
|
|
16
|
|
|
private static $id_base; |
17
|
|
|
|
18
|
|
|
public static $values = []; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
22
|
|
|
public function __construct() { |
23
|
|
|
|
24
|
|
|
} |
25
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
30
|
|
|
/** |
31
|
|
|
* @return array |
32
|
|
|
* get array of all values wich will be used in the page |
33
|
|
|
*/ |
34
|
|
|
public static function getValues() { |
35
|
|
|
return ["bataille" => self::$values]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
//initilisation of all classes of battle |
39
|
|
|
//initialisation of Ressource class |
40
|
|
|
public static function getRessource() { |
41
|
|
|
if (self::$ressource == null) { |
42
|
|
|
self::$ressource = new Ressource(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return self::$ressource; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
//initialisation of Base class |
49
|
|
|
public static function getBase() { |
50
|
|
|
if (self::$base == null) { |
51
|
|
|
self::$base = new Base(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return self::$base; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
//initialisation of Batiment class |
58
|
|
|
public static function getBatiment() { |
59
|
|
|
if (self::$batiment == null) { |
60
|
|
|
self::$batiment = new Batiment(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return self::$batiment; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
//initialisation of Batiment class |
67
|
|
|
public static function getUnite() { |
68
|
|
|
if (self::$unite == null) { |
69
|
|
|
self::$unite = new Unite(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return self::$unite; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
//initialisation of CentreRecherche class |
76
|
|
|
public static function getCentreRecherche() { |
77
|
|
|
if (self::$centre_recherche == null) { |
78
|
|
|
self::$centre_recherche = new CentreRecherche(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return self::$centre_recherche; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
//initialisation of MissionsAleatoire class |
85
|
|
|
public static function getMissionsAleatoire() { |
86
|
|
|
if (self::$missions_aleatoire == null) { |
87
|
|
|
self::$missions_aleatoire = new MissionsAleatoire(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return self::$missions_aleatoire; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
//initialisation of Database Core connexion |
94
|
|
|
public static function getDb() { |
95
|
|
|
require_once("config.config.php"); |
96
|
|
|
|
97
|
|
|
if (self::$database == null) { |
98
|
|
|
self::$database = new Database($type_co, $dbname, $dbuser, $dbpass, $dbhost); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
return self::$database; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return mixe |
105
|
|
|
* récupère l'ID_identité du joueur |
106
|
|
|
*/ |
107
|
|
|
public static function getIdIdentite() { |
|
|
|
|
108
|
|
|
return $_SESSION['idlogin'.CLEF_SITE]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return mixed |
113
|
|
|
* renvoi l'id_base du joueur |
114
|
|
|
*/ |
115
|
|
|
public static function getIdBase() { |
|
|
|
|
116
|
|
|
if (self::$id_base == null) { |
117
|
|
|
self::$id_base = $_SESSION['id_base']; |
118
|
|
|
|
119
|
|
|
return self::$id_base; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return self::$id_base; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return mixed |
127
|
|
|
* renvoi le premier ID_base du joueur (première base et base princ du joueur) |
128
|
|
|
*/ |
129
|
|
|
public static function getFirstBase() { |
130
|
|
|
$dbc = App::getDb(); |
131
|
|
|
|
132
|
|
|
$query = $dbc->select("ID_base")->from("_bataille_base") |
133
|
|
|
->where("ID_identite", "=", self::getIdIdentite()) |
134
|
|
|
->orderBy("ID_base") |
135
|
|
|
->limit(0, 1) |
136
|
|
|
->get(); |
137
|
|
|
|
138
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
139
|
|
|
foreach ($query as $obj) return $obj->ID_base; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param $id_base |
145
|
|
|
* @return array |
146
|
|
|
* fonction qui renvoi les posisitons en x et y d'une base |
147
|
|
|
*/ |
148
|
|
|
private static function getPosistionBase($id_base) { |
149
|
|
|
$dbc = App::getDb(); |
150
|
|
|
|
151
|
|
|
$posx = 0; |
152
|
|
|
$posy = 0; |
153
|
|
|
|
154
|
|
|
$query = $dbc->select("posx") |
155
|
|
|
->select("posy") |
156
|
|
|
->from("_bataille_base") |
157
|
|
|
->where("ID_base", "=", $id_base) |
158
|
|
|
->get(); |
159
|
|
|
|
160
|
|
|
foreach ($query as $obj) { |
161
|
|
|
$posx = $obj->posx; |
162
|
|
|
$posy = $obj->posy; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return ["posx" => $posx, "posy" => $posy]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return int |
170
|
|
|
* return now timestamp |
171
|
|
|
*/ |
172
|
|
|
public static function getToday() { |
173
|
|
|
$today = new \DateTime(); |
174
|
|
|
return $today->getTimestamp(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $nom_ressource |
179
|
|
|
* @param $ressource |
180
|
|
|
* @return array |
181
|
|
|
* fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment |
182
|
|
|
* ou pour creer une unité... |
183
|
|
|
*/ |
184
|
|
|
public static function getTestAssezRessourceBase($nom_ressource, $ressource) { |
185
|
|
|
$f = "get".ucfirst($nom_ressource); |
186
|
|
|
|
187
|
|
|
if ($ressource > Bataille::getRessource()->$f()) { |
188
|
|
|
return [ |
189
|
|
|
"ressource" => $ressource, |
190
|
|
|
"class" => "rouge" |
191
|
|
|
]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return [ |
195
|
|
|
"ressource" => $ressource, |
196
|
|
|
"class" => "" |
197
|
|
|
]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param $id_base |
202
|
|
|
* @param integer $vitesse = vitesse de l'unité en question |
203
|
|
|
* @return number |
204
|
|
|
* fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes |
205
|
|
|
*/ |
206
|
|
|
public static function getDureeTrajet($id_base, $vitesse = 1) { |
|
|
|
|
207
|
|
|
//récupération de la posisiotn de la base du joueur + la base sur laquelle on a cliqué |
208
|
|
|
$base_joueur = self::getPosistionBase($_SESSION['id_base']); |
209
|
|
|
$base_autre = self::getPosistionBase($id_base); |
210
|
|
|
|
211
|
|
|
//calcul des distances séparant les deux bases en x et y |
212
|
|
|
//cette dstance sera multipliée par 15 sur x et y puis ajoutée pour avoir le temps du trajte en seconde |
213
|
|
|
$calc_x = abs($base_joueur['posx']-$base_autre['posx']); |
214
|
|
|
$calc_y = abs($base_joueur['posy']-$base_autre['posy']); |
215
|
|
|
|
216
|
|
|
$temps_voyage = (($calc_x*70)+($calc_y*70))/$vitesse; |
217
|
|
|
|
218
|
|
|
return $temps_voyage; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param null $id_identite |
223
|
|
|
* get nation of a player |
224
|
|
|
*/ |
225
|
|
|
public static function getNation($id_identite = null) { |
226
|
|
|
$dbc = App::getDb(); |
227
|
|
|
|
228
|
|
|
if (($id_identite === null) && (self::$nation == null)) { |
229
|
|
|
$id_identite = Bataille::getIdIdentite(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$query = $dbc->select("nation") |
233
|
|
|
->from("identite") |
234
|
|
|
->from("_bataille_nation") |
235
|
|
|
->where("identite.ID_identite", "=", $id_identite, "AND") |
236
|
|
|
->where("identite.ID_identite", "=", "_bataille_nation.ID_identite", "", true) |
237
|
|
|
->get(); |
238
|
|
|
|
239
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
240
|
|
|
foreach ($query as $obj) { |
241
|
|
|
self::setValues(["nation" => $obj->nation]); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param $posx |
248
|
|
|
* @param $posy |
249
|
|
|
* @return int |
250
|
|
|
* fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante |
251
|
|
|
*/ |
252
|
|
|
public static function getBaseExistPosition($posx, $posy) { |
253
|
|
|
$dbc = App::getDb(); |
254
|
|
|
|
255
|
|
|
$query = $dbc->select("ID_base")->from("_bataille_base") |
256
|
|
|
->where("posx", "=", $posx, "AND") |
257
|
|
|
->where("posy", "=", $posy) |
258
|
|
|
->get(); |
259
|
|
|
|
260
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
261
|
|
|
foreach ($query as $obj) return $obj->ID_base; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return 0; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param string $param |
269
|
|
|
* @return mixed |
270
|
|
|
* fonction qui sert à récupérer un parametre spécifique pour un batiment |
271
|
|
|
* par exemple la vitesse d'un marchand ou le nombred'emplacment de la base |
272
|
|
|
*/ |
273
|
|
|
public static function getParam($param) { |
274
|
|
|
$dbc = self::getDb(); |
275
|
|
|
|
276
|
|
|
$query = $dbc->select($param)->from("configuration")->where("ID_configuration", "=", 1)->get(); |
277
|
|
|
|
278
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
279
|
|
|
foreach ($query as $obj) return $obj->$param; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
283
|
|
|
|
284
|
|
|
|
285
|
|
|
|
286
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
287
|
|
|
/** |
288
|
|
|
* @param $values |
289
|
|
|
* can set values while keep older infos |
290
|
|
|
*/ |
291
|
|
|
public static function setValues($values) { |
292
|
|
|
Bataille::$values = array_merge(Bataille::$values, $values); |
293
|
|
|
} |
294
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
295
|
|
|
} |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.