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