Passed
Push — master ( bf07a0...5869f6 )
by Anthony
05:05
created

Bataille::getNationBase()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 13
nc 6
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Bataille::getDureeTrajet() 0 14 1
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
		 * @return mixed
192
		 * recupere le nombre maximum d'emplacement dans la base
193
		 */
194 View Code Duplication
		public static function getNombreEmplacementBase() {
0 ignored issues
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...
195
			$dbc1 = self::getDb();
196
197
			$query = $dbc1->select("nombre_emplacement")->from("configuration")->where("ID_configuration", "=", 1)->get();
198
199
			if ((is_array($query)) && (count($query) > 0)) {
200
				foreach ($query as $obj) {
201
					return $obj->nombre_emplacement;
202
				}
203
			}
204
		}
205
206
		/**
207
		 * @param $nom_ressource
208
		 * @param $ressource
209
		 * @return array
210
		 * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment
211
		 * ou pour creer une unité...
212
		 */
213
		public static function getTestAssezRessourceBase($nom_ressource, $ressource) {
214
			$f = "get".ucfirst($nom_ressource);
215
216
			if ($ressource >  Bataille::getRessource()->$f()) {
217
				/*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...
218
				return [
219
					"ressource" => $ressource,
220
					"class" => "rouge"
221
				];
222
			}
223
224
			return [
225
				"ressource" => $ressource,
226
				"class" => ""
227
			];
228
		}
229
		
230
		/**
231
		 * @param $id_base
232
		 * @return number
233
		 * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes
234
		 */
235
		public static function getDureeTrajet($id_base) {
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...
236
			//récupération de la posisiotn de la base du joueur + la base sur laquelle on a cliqué
237
			$base_joueur = self::getPosistionBase($_SESSION['id_base']);
238
			$base_autre = self::getPosistionBase($id_base);
239
			
240
			//calcul des distances séparant les deux bases en x et y
241
			//cette dstance sera multipliée par 15 sur x et y puis ajoutée pour avoir le temps du trajte en seconde
242
			$calc_x = abs($base_joueur['posx']-$base_autre['posx']);
243
			$calc_y = abs($base_joueur['posy']-$base_autre['posy']);
244
			
245
			$temps_voyage = ($calc_x*15)+($calc_y*15);
246
			
247
			return $temps_voyage;
248
		}
249
250
		/**
251
		 * @param null $id_identite
252
		 * get nation of a player
253
		 */
254
		public static function getNation($id_identite = null) {
255
			$dbc = App::getDb();
256
257
			if (($id_identite === null) && (self::$nation == null)) {
258
				$id_identite = Bataille::getIdIdentite();
259
			}
260
261
			$query = $dbc->select("nation")
262
				->from("identite")
263
				->from("_bataille_nation")
264
				->where("identite.ID_identite", "=", $id_identite, "AND")
265
				->where("identite.ID_identite", "=", "_bataille_nation.ID_identite", "", true)
266
				->get();
267
268
			if ((is_array($query)) && (count($query) > 0)) {
269
				foreach ($query as $obj) {
270
					self::setValues(["nation" => $obj->nation]);
271
				}
272
			}
273
		}
274
275
		/**
276
		 * @param $posx
277
		 * @param $posy
278
		 * @return int
279
		 * fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante
280
		 */
281 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...
282
			$dbc = App::getDb();
283
284
			$query = $dbc->select("ID_base")->from("_bataille_base")
285
				->where("posx", "=", $posx, "AND")
286
				->where("posy", "=", $posy)
287
				->get();
288
289
			if ((is_array($query)) && (count($query) == 1)) {
290
				foreach ($query as $obj) return $obj->ID_base;
291
			}
292
293
			return 0;
294
		}
295
296
		/**
297
		 * @return int
298
		 * fonction qui permet de récupérer le nombre de joueurs sur le serveur
299
		 */
300
		public static function getNombreJoueur() {
301
			$dbc = App::getDb();
302
303
			$query = $dbc->select("nombre_joueur")->from("_bataille_nombre_joueur")->where("ID_nombre_joueur", "=", 1)->get();
304
305
			if ((is_array($query)) && (count($query) == 1)) {
306
				foreach ($query as $obj) return $obj->nombre_joueur;
307
			}
308
309
			return 0;
310
		}
311
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
312
		
313
		
314
		
315
		//-------------------------- SETTER ----------------------------------------------------------------------------//
316
		/**
317
		 * set la date de derniere connexion a now
318
		 */
319
		public static function setLastConnexion($id_base = null) {
320
			$dbc = App::getDb();
321
322
			if ($id_base === null) {
323
				$id_identite = self::getIdIdentite();
324
			}
325
			else {
326
				$query = $dbc->select("ID_identite")->from("_bataille_base")->where("ID_base", "=", $id_base)->get();
327
328
				foreach ($query as $obj) $id_identite = $obj->ID_identite;
329
			}
330
331
			$dbc->update("last_connexion", date("Y-m-d H:i:s"))
332
				->from("_bataille_last_connexion")
333
				->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...
334
				->set();
335
		}
336
337
		/**
338
		 * @param $values
339
		 * can set values while keep older infos
340
		 */
341
		public static function setValues($values) {
342
			Bataille::$values = array_merge(Bataille::$values, $values);
343
		}
344
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
345
	}