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 $points; |
11
|
|
|
private static $unite; |
12
|
|
|
private static $centre_recherche; |
13
|
|
|
private static $missions_aleatoire; |
14
|
|
|
private static $nourriture; |
15
|
|
|
private static $map; |
16
|
|
|
private static $database; |
17
|
|
|
private static $nation; |
18
|
|
|
|
19
|
|
|
private static $id_base; |
20
|
|
|
|
21
|
|
|
public static $values = []; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
//-------------------------- BUILDER ----------------------------------------------------------------------------// |
25
|
|
|
public function __construct() { |
26
|
|
|
|
27
|
|
|
} |
28
|
|
|
//-------------------------- END BUILDER ----------------------------------------------------------------------------// |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
//-------------------------- GETTER ----------------------------------------------------------------------------// |
33
|
|
|
/** |
34
|
|
|
* @return array |
35
|
|
|
* get array of all values wich will be used in the page |
36
|
|
|
*/ |
37
|
|
|
public static function getValues() { |
38
|
|
|
return ["bataille" => self::$values]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
//initilisation of all classes of battle |
42
|
|
|
//initialisation of Ressource class |
43
|
|
|
public static function getRessource() { |
44
|
|
|
if (self::$ressource == null) { |
45
|
|
|
self::$ressource = new Ressource(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return self::$ressource; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
//initialisation of Base class |
52
|
|
|
public static function getBase() { |
53
|
|
|
if (self::$base == null) { |
54
|
|
|
self::$base = new Base(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return self::$base; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
//initialisation of Batiment class |
61
|
|
|
public static function getBatiment() { |
62
|
|
|
if (self::$batiment == null) { |
63
|
|
|
self::$batiment = new Batiment(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return self::$batiment; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
//initialisation of Points class |
70
|
|
|
public static function getPoints() { |
71
|
|
|
if (self::$points == null) { |
72
|
|
|
self::$points = new Points(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return self::$points; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
//initialisation of Map class |
79
|
|
|
public static function getMap() { |
80
|
|
|
if (self::$map == null) { |
81
|
|
|
self::$map = new Map(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return self::$map; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
//initialisation of Batiment class |
88
|
|
|
public static function getUnite() { |
89
|
|
|
if (self::$unite == null) { |
90
|
|
|
self::$unite = new Unite(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return self::$unite; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
//initialisation of CentreRecherche class |
97
|
|
|
public static function getCentreRecherche() { |
98
|
|
|
if (self::$centre_recherche == null) { |
99
|
|
|
self::$centre_recherche = new CentreRecherche(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return self::$centre_recherche; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
//initialisation of MissionsAleatoire class |
106
|
|
|
public static function getMissionsAleatoire() { |
107
|
|
|
if (self::$missions_aleatoire == null) { |
108
|
|
|
self::$missions_aleatoire = new MissionsAleatoire(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return self::$missions_aleatoire; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
//initialisation of Nourriture class |
115
|
|
|
public static function getNourriture() { |
116
|
|
|
if (self::$nourriture == null) { |
117
|
|
|
self::$nourriture = new Nourriture(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return self::$nourriture; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
//initialisation of Database Core connexion |
124
|
|
|
public static function getDb() { |
125
|
|
|
require_once("config.config.php"); |
126
|
|
|
|
127
|
|
|
if (self::$database == null) { |
128
|
|
|
self::$database = new Database($type_co, $dbname, $dbuser, $dbpass, $dbhost); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
return self::$database; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return mixe |
135
|
|
|
* récupère l'ID_identité du joueur |
136
|
|
|
*/ |
137
|
|
|
public static function getIdIdentite() { |
|
|
|
|
138
|
|
|
return $_SESSION['idlogin'.CLEF_SITE]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return mixed |
143
|
|
|
* renvoi l'id_base du joueur |
144
|
|
|
*/ |
145
|
|
|
public static function getIdBase() { |
|
|
|
|
146
|
|
|
if (self::$id_base == null) { |
147
|
|
|
self::$id_base = $_SESSION['id_base']; |
148
|
|
|
|
149
|
|
|
return self::$id_base; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return self::$id_base; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return mixed |
157
|
|
|
* renvoi le premier ID_base du joueur (première base et base princ du joueur) |
158
|
|
|
*/ |
159
|
|
|
public static function getFirstBase() { |
160
|
|
|
$dbc = App::getDb(); |
161
|
|
|
|
162
|
|
|
$query = $dbc->select("ID_base")->from("_bataille_base") |
163
|
|
|
->where("ID_identite", "=", self::getIdIdentite()) |
164
|
|
|
->orderBy("ID_base") |
165
|
|
|
->limit(0, 1) |
166
|
|
|
->get(); |
167
|
|
|
|
168
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
169
|
|
|
foreach ($query as $obj) return $obj->ID_base; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param $id_base |
175
|
|
|
* @return array |
176
|
|
|
* fonction qui renvoi les posisitons en x et y d'une base |
177
|
|
|
*/ |
178
|
|
|
private static function getPosistionBase($id_base) { |
179
|
|
|
$dbc = App::getDb(); |
180
|
|
|
|
181
|
|
|
$posx = 0; |
182
|
|
|
$posy = 0; |
183
|
|
|
|
184
|
|
|
$query = $dbc->select("posx") |
185
|
|
|
->select("posy") |
186
|
|
|
->from("_bataille_base") |
187
|
|
|
->where("ID_base", "=", $id_base) |
188
|
|
|
->get(); |
189
|
|
|
|
190
|
|
|
foreach ($query as $obj) { |
191
|
|
|
$posx = $obj->posx; |
192
|
|
|
$posy = $obj->posy; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return ["posx" => $posx, "posy" => $posy]; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return int |
200
|
|
|
* return now timestamp |
201
|
|
|
*/ |
202
|
|
|
public static function getToday() { |
203
|
|
|
$today = new \DateTime(); |
204
|
|
|
return $today->getTimestamp(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param null $id_base -> sert si definit a recuperer l'id identite de la abse en question |
209
|
|
|
* @return string|null |
210
|
|
|
* recupere la date de la derniere connexion |
211
|
|
|
*/ |
212
|
|
|
public static function getLastConnexion($id_base = null) { |
213
|
|
|
$dbc = App::getDb(); |
214
|
|
|
|
215
|
|
|
if ($id_base === null) { |
216
|
|
|
$query = $dbc->select()->from("_bataille_last_connexion")->where("ID_identite", "=", self::getIdIdentite())->get(); |
217
|
|
|
} |
218
|
|
|
else { |
219
|
|
|
$query = $dbc->select("_bataille_last_connexion.last_connexion")->from("_bataille_base") |
220
|
|
|
->from("_bataille_last_connexion") |
221
|
|
|
->from("identite") |
222
|
|
|
->where("_bataille_base.ID_base", "=", $id_base, "AND") |
223
|
|
|
->where("_bataille_base.ID_identite", "=", "identite.ID_identite", "AND", true) |
224
|
|
|
->where("identite.ID_identite", "=", "_bataille_last_connexion.ID_identite", "", true) |
225
|
|
|
->get(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
229
|
|
|
foreach ($query as $obj) { |
230
|
|
|
return $obj->last_connexion; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param string $nom_ressource |
237
|
|
|
* @param $ressource |
238
|
|
|
* @return array |
239
|
|
|
* fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment |
240
|
|
|
* ou pour creer une unité... |
241
|
|
|
*/ |
242
|
|
|
public static function getTestAssezRessourceBase($nom_ressource, $ressource) { |
243
|
|
|
$f = "get".ucfirst($nom_ressource); |
244
|
|
|
|
245
|
|
|
if ($ressource > Bataille::getRessource()->$f()) { |
246
|
|
|
return [ |
247
|
|
|
"ressource" => $ressource, |
248
|
|
|
"class" => "rouge" |
249
|
|
|
]; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return [ |
253
|
|
|
"ressource" => $ressource, |
254
|
|
|
"class" => "" |
255
|
|
|
]; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param $id_base |
260
|
|
|
* @param integer $vitesse = vitesse de l'unité en question |
261
|
|
|
* @return number |
262
|
|
|
* fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes |
263
|
|
|
*/ |
264
|
|
|
public static function getDureeTrajet($id_base, $vitesse = 1) { |
|
|
|
|
265
|
|
|
//récupération de la posisiotn de la base du joueur + la base sur laquelle on a cliqué |
266
|
|
|
$base_joueur = self::getPosistionBase($_SESSION['id_base']); |
267
|
|
|
$base_autre = self::getPosistionBase($id_base); |
268
|
|
|
|
269
|
|
|
//calcul des distances séparant les deux bases en x et y |
270
|
|
|
//cette dstance sera multipliée par 15 sur x et y puis ajoutée pour avoir le temps du trajte en seconde |
271
|
|
|
$calc_x = abs($base_joueur['posx']-$base_autre['posx']); |
272
|
|
|
$calc_y = abs($base_joueur['posy']-$base_autre['posy']); |
273
|
|
|
|
274
|
|
|
$temps_voyage = (($calc_x*70)+($calc_y*70))/$vitesse; |
275
|
|
|
|
276
|
|
|
return $temps_voyage; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param null $id_identite |
281
|
|
|
* get nation of a player |
282
|
|
|
*/ |
283
|
|
|
public static function getNation($id_identite = null) { |
284
|
|
|
$dbc = App::getDb(); |
285
|
|
|
|
286
|
|
|
if (($id_identite === null) && (self::$nation == null)) { |
287
|
|
|
$id_identite = Bataille::getIdIdentite(); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$query = $dbc->select("nation") |
291
|
|
|
->from("identite") |
292
|
|
|
->from("_bataille_nation") |
293
|
|
|
->where("identite.ID_identite", "=", $id_identite, "AND") |
294
|
|
|
->where("identite.ID_identite", "=", "_bataille_nation.ID_identite", "", true) |
295
|
|
|
->get(); |
296
|
|
|
|
297
|
|
|
if ((is_array($query)) && (count($query) > 0)) { |
298
|
|
|
foreach ($query as $obj) { |
299
|
|
|
self::setValues(["nation" => $obj->nation]); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param $posx |
306
|
|
|
* @param $posy |
307
|
|
|
* @return int |
308
|
|
|
* fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante |
309
|
|
|
*/ |
310
|
|
|
public static function getBaseExistPosition($posx, $posy) { |
311
|
|
|
$dbc = App::getDb(); |
312
|
|
|
|
313
|
|
|
$query = $dbc->select("ID_base")->from("_bataille_base") |
314
|
|
|
->where("posx", "=", $posx, "AND") |
315
|
|
|
->where("posy", "=", $posy) |
316
|
|
|
->get(); |
317
|
|
|
|
318
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
319
|
|
|
foreach ($query as $obj) return $obj->ID_base; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return 0; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @return integer |
327
|
|
|
* fonction qui permet de récupérer le nombre de joueurs sur le serveur |
328
|
|
|
*/ |
329
|
|
|
public static function getNombreJoueur() { |
330
|
|
|
$dbc = App::getDb(); |
331
|
|
|
|
332
|
|
|
$query = $dbc->select("nombre_joueur")->from("_bataille_nombre_joueur")->where("ID_nombre_joueur", "=", 1)->get(); |
333
|
|
|
|
334
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
335
|
|
|
foreach ($query as $obj) return $obj->nombre_joueur; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
return 0; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @param string $param |
343
|
|
|
* @return mixed |
344
|
|
|
* fonction qui sert à récupérer un parametre spécifique pour un batiment |
345
|
|
|
* par exemple la vitesse d'un marchand ou le nombred'emplacment de la base |
346
|
|
|
*/ |
347
|
|
|
public static function getParam($param) { |
348
|
|
|
$dbc = self::getDb(); |
349
|
|
|
|
350
|
|
|
$query = $dbc->select($param)->from("configuration")->where("ID_configuration", "=", 1)->get(); |
351
|
|
|
|
352
|
|
|
if ((is_array($query)) && (count($query) == 1)) { |
353
|
|
|
foreach ($query as $obj) return $obj->$param; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
//-------------------------- END GETTER ----------------------------------------------------------------------------// |
357
|
|
|
|
358
|
|
|
|
359
|
|
|
|
360
|
|
|
//-------------------------- SETTER ----------------------------------------------------------------------------// |
361
|
|
|
/** |
362
|
|
|
* set la date de derniere connexion a now |
363
|
|
|
*/ |
364
|
|
|
public static function setLastConnexion($id_base = null) { |
365
|
|
|
$dbc = App::getDb(); |
366
|
|
|
|
367
|
|
|
$id_identite = self::getIdIdentite(); |
368
|
|
|
|
369
|
|
|
if ($id_base !== null) { |
370
|
|
|
$query = $dbc->select("ID_identite")->from("_bataille_base")->where("ID_base", "=", $id_base)->get(); |
371
|
|
|
|
372
|
|
|
foreach ($query as $obj) $id_identite = $obj->ID_identite; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
$dbc->update("last_connexion", date("Y-m-d H:i:s")) |
376
|
|
|
->from("_bataille_last_connexion") |
377
|
|
|
->where("ID_identite", "=", $id_identite) |
378
|
|
|
->set(); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @param $values |
383
|
|
|
* can set values while keep older infos |
384
|
|
|
*/ |
385
|
|
|
public static function setValues($values) { |
386
|
|
|
Bataille::$values = array_merge(Bataille::$values, $values); |
387
|
|
|
} |
388
|
|
|
//-------------------------- END SETTER ----------------------------------------------------------------------------// |
389
|
|
|
} |
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.