Passed
Push — master ( 287d79...d7bad9 )
by Anthony
02:38
created

Bataille::getNombreEmplacementBase()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 0
dl 11
loc 11
rs 9.2
c 0
b 0
f 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 $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() {
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...
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() {
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...
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() {
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...
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];
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...
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()." ---");*/
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...
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) {
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...
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) {
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...
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)
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...
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
	}