Passed
Push — master ( 3f4c18...7127f7 )
by Anthony
02:45
created

Bataille::getCentreRecherche()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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 $map;
14
		private static $database;
15
		private static $nation;
16
17
		private static $id_base;
18
19
		public static $values = [];
20
21
		
22
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
23
		public function __construct() {
24
25
		}
26
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
27
		
28
		
29
		
30
		//-------------------------- GETTER ----------------------------------------------------------------------------//
31
		/**
32
		 * @return array
33
		 * get array of all values wich will be used in the page
34
		 */
35
		public static function getValues() {
36
		    return ["bataille" => self::$values];
37
		}
38
39
		//initilisation of all classes of battle
40
		//initialisation of Ressource class
41
		public static function getRessource() {
42
			if (self::$ressource == null) {
43
				self::$ressource = new Ressource();
44
			}
45
46
			return self::$ressource;
47
		}
48
49
		//initialisation of Base class
50
		public static function getBase() {
51
			if (self::$base == null) {
52
				self::$base = new Base();
53
			}
54
55
			return self::$base;
56
		}
57
58
		//initialisation of Batiment class
59
		public static function getBatiment() {
60
			if (self::$batiment == null) {
61
				self::$batiment = new Batiment();
62
			}
63
64
			return self::$batiment;
65
		}
66
67
		//initialisation of Batiment class
68
		public static function getPoints() {
69
			if (self::$points == null) {
70
				self::$points = new Points();
71
			}
72
73
			return self::$points;
74
		}
75
76
		//initialisation of Batiment class
77
		public static function getMap() {
78
			if (self::$map == null) {
79
				self::$map = new Map();
80
			}
81
82
			return self::$map;
83
		}
84
85
		//initialisation of Batiment class
86
		public static function getUnite() {
87
			if (self::$unite == null) {
88
				self::$unite = new Unite();
89
			}
90
91
			return self::$unite;
92
		}
93
94
		//initialisation of Batiment class
95
		public static function getCentreRecherche() {
96
			if (self::$centre_recherche == null) {
97
				self::$centre_recherche = new CentreRecherche();
98
			}
99
100
			return self::$centre_recherche;
101
		}
102
103
		//initialisation of Database Core connexion
104
		public static function getDb() {
105
			if (self::$database == null) {
106
				self::$database = new Database("mysql", "bataille_core", "root", "Gerto80", "127.0.0.1");
107
			}
108
			return self::$database;
109
		}
110
111
		/**
112
		 * @return mixe
113
		 * récupère l'ID_identité du joueur
114
		 */
115
		public static function getIdIdentite() {
0 ignored issues
show
Coding Style introduced by
getIdIdentite uses the super-global variable $_SESSION which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
116
			return $_SESSION['idlogin'.CLEF_SITE];
117
		}
118
119
		/**
120
		 * @return mixed
121
		 * renvoi l'id_base du joueur
122
		 */
123
		public static function getIdBase() {
0 ignored issues
show
Coding Style introduced by
getIdBase uses the super-global variable $_SESSION which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
124
			if (self::$id_base == null) {
125
				self::$id_base = $_SESSION['id_base'];
126
127
				return self::$id_base;
128
			}
129
130
			return self::$id_base;
131
		}
132
133
		/**
134
		 * @return mixed
135
		 * renvoi le premier ID_base du joueur (première base et base princ du joueur)
136
		 */
137 View Code Duplication
		public static function getFirstBase() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
			$dbc = App::getDb();
139
140
			$query = $dbc->select("ID_base")->from("_bataille_base")
141
				->where("ID_identite", "=", self::getIdIdentite())
142
				->orderBy("ID_base")
143
				->limit(0, 1)
144
				->get();
145
146
			if ((is_array($query)) && (count($query) == 1)) {
147
				foreach ($query as $obj) return $obj->ID_base;
148
			}
149
		}
150
151
		/**
152
		 * @param $id_base
153
		 * @return array
154
		 * fonction qui renvoi les posisitons en x et y d'une base
155
		 */
156
		private static function getPosistionBase($id_base) {
157
			$dbc = App::getDb();
158
159
			$query = $dbc->select("posx")
160
				->select("posy")
161
				->from("_bataille_base")
162
				->where("ID_base", "=", $id_base)
163
				->get();
164
165
			foreach ($query as $obj) {
166
				$posx = $obj->posx;
167
				$posy = $obj->posy;
168
			}
169
170
			return ["posx" => $posx, "posy" => $posy];
0 ignored issues
show
Bug introduced by
The variable $posx does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $posy does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
171
		}
172
173
		/**
174
		 * @return int
175
		 * return now timestamp
176
		 */
177
		public static function getToday() {
178
			$today = new \DateTime();
179
			return $today->getTimestamp();
180
		}
181
182
		/**
183
		 * @param null $id_base -> sert si definit a recuperer l'id identite de la abse en question
184
		 * @return mixed
185
		 * recupere la date de la derniere connexion
186
		 */
187
		public static function getLastConnexion($id_base = null) {
188
			$dbc = App::getDb();
189
190
			if ($id_base === null) {
191
				$query = $dbc->select()->from("_bataille_last_connexion")->where("ID_identite", "=", self::getIdIdentite())->get();
192
			}
193
			else {
194
				$query = $dbc->select("_bataille_last_connexion.last_connexion")->from("_bataille_base")
195
					->from("_bataille_last_connexion")
196
					->from("identite")
197
					->where("_bataille_base.ID_base", "=", $id_base, "AND")
198
					->where("_bataille_base.ID_identite", "=", "identite.ID_identite", "AND", true)
199
					->where("identite.ID_identite", "=", "_bataille_last_connexion.ID_identite", "", true)
200
					->get();
201
			}
202
203
			if ((is_array($query)) && (count($query) > 0)) {
204
				foreach ($query as $obj) {
205
					return $obj->last_connexion;
206
				}
207
			}
208
		}
209
210
		/**
211
		 * @param $nom_ressource
212
		 * @param $ressource
213
		 * @return array
214
		 * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment
215
		 * ou pour creer une unité...
216
		 */
217
		public static function getTestAssezRessourceBase($nom_ressource, $ressource) {
218
			$f = "get".ucfirst($nom_ressource);
219
220
			if ($ressource >  Bataille::getRessource()->$f()) {
221
				/*echo("$nom_ressource $ressource ".Bataille::getRessource()->getEau()." ---");*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
222
				return [
223
					"ressource" => $ressource,
224
					"class" => "rouge"
225
				];
226
			}
227
228
			return [
229
				"ressource" => $ressource,
230
				"class" => ""
231
			];
232
		}
233
		
234
		/**
235
		 * @param $id_base
236
		 * @param null $vitesse = vitesse de l'unité en question
237
		 * @return number
238
		 * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes
239
		 */
240
		public static function getDureeTrajet($id_base, $vitesse = 1) {
0 ignored issues
show
Coding Style introduced by
getDureeTrajet uses the super-global variable $_SESSION which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
241
			//récupération de la posisiotn de la base du joueur + la base sur laquelle on a cliqué
242
			$base_joueur = self::getPosistionBase($_SESSION['id_base']);
243
			$base_autre = self::getPosistionBase($id_base);
244
			
245
			//calcul des distances séparant les deux bases en x et y
246
			//cette dstance sera multipliée par 15 sur x et y puis ajoutée pour avoir le temps du trajte en seconde
247
			$calc_x = abs($base_joueur['posx']-$base_autre['posx']);
248
			$calc_y = abs($base_joueur['posy']-$base_autre['posy']);
249
			
250
			$temps_voyage = (($calc_x*70)+($calc_y*70))/$vitesse;
251
			
252
			return $temps_voyage;
253
		}
254
255
		/**
256
		 * @param null $id_identite
257
		 * get nation of a player
258
		 */
259
		public static function getNation($id_identite = null) {
260
			$dbc = App::getDb();
261
262
			if (($id_identite === null) && (self::$nation == null)) {
263
				$id_identite = Bataille::getIdIdentite();
264
			}
265
266
			$query = $dbc->select("nation")
267
				->from("identite")
268
				->from("_bataille_nation")
269
				->where("identite.ID_identite", "=", $id_identite, "AND")
270
				->where("identite.ID_identite", "=", "_bataille_nation.ID_identite", "", true)
271
				->get();
272
273
			if ((is_array($query)) && (count($query) > 0)) {
274
				foreach ($query as $obj) {
275
					self::setValues(["nation" => $obj->nation]);
276
				}
277
			}
278
		}
279
280
		/**
281
		 * @param $posx
282
		 * @param $posy
283
		 * @return int
284
		 * fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante
285
		 */
286 View Code Duplication
		public static function getBaseExistPosition($posx, $posy) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
287
			$dbc = App::getDb();
288
289
			$query = $dbc->select("ID_base")->from("_bataille_base")
290
				->where("posx", "=", $posx, "AND")
291
				->where("posy", "=", $posy)
292
				->get();
293
294
			if ((is_array($query)) && (count($query) == 1)) {
295
				foreach ($query as $obj) return $obj->ID_base;
296
			}
297
298
			return 0;
299
		}
300
301
		/**
302
		 * @return int
303
		 * fonction qui permet de récupérer le nombre de joueurs sur le serveur
304
		 */
305
		public static function getNombreJoueur() {
306
			$dbc = App::getDb();
307
308
			$query = $dbc->select("nombre_joueur")->from("_bataille_nombre_joueur")->where("ID_nombre_joueur", "=", 1)->get();
309
310
			if ((is_array($query)) && (count($query) == 1)) {
311
				foreach ($query as $obj) return $obj->nombre_joueur;
312
			}
313
314
			return 0;
315
		}
316
317
		/**
318
		 * @param $param
319
		 * @return mixed
320
		 * fonction qui sert à récupérer un parametre spécifique pour un batiment
321
		 * par exemple la vitesse d'un marchand ou  le nombred'emplacment de la base
322
		 */
323
		public static function getParam($param) {
324
			$dbc = self::getDb();
325
326
			$query = $dbc->select($param)->from("configuration")->where("ID_configuration", "=", 1)->get();
327
328
			if ((is_array($query)) && (count($query) == 1)) {
329
				foreach ($query as $obj) return $obj->$param;
330
			}
331
		}
332
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
333
		
334
		
335
		
336
		//-------------------------- SETTER ----------------------------------------------------------------------------//
337
		/**
338
		 * set la date de derniere connexion a now
339
		 */
340
		public static function setLastConnexion($id_base = null) {
341
			$dbc = App::getDb();
342
343
			if ($id_base === null) {
344
				$id_identite = self::getIdIdentite();
345
			}
346
			else {
347
				$query = $dbc->select("ID_identite")->from("_bataille_base")->where("ID_base", "=", $id_base)->get();
348
349
				foreach ($query as $obj) $id_identite = $obj->ID_identite;
350
			}
351
352
			$dbc->update("last_connexion", date("Y-m-d H:i:s"))
353
				->from("_bataille_last_connexion")
354
				->where("ID_identite", "=", $id_identite)
0 ignored issues
show
Bug introduced by
The variable $id_identite does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
355
				->set();
356
		}
357
358
		/**
359
		 * @param $values
360
		 * can set values while keep older infos
361
		 */
362
		public static function setValues($values) {
363
			Bataille::$values = array_merge(Bataille::$values, $values);
364
		}
365
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
366
	}