Passed
Push — master ( c60e25...5c42a2 )
by Anthony
02:55
created

Batiment::getInfoBatiment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
	namespace modules\bataille\app\controller;
3
	use core\App;
4
	use core\functions\ChaineCaractere;
5
	use core\functions\DateHeure;
6
	use core\HTML\flashmessage\FlashMessage;
7
8
	class Batiment {
9
		//pour quand on recup un batiment
10
		private $nom_batiment;
11
		private $nom_batiment_sql;
12
		private $niveau_batiment;
13
		private $temps_construction;
14
		private $ressource_construire;
15
		private $id_batiment;
16
17
		private $info_batiment;
18
		private $info_batiment_next;
19
20
		//pour les constructions
21
		private $nom_batiment_construction;
22
		private $date_fin_construction;
23
		private $niveau_batiment_construction;
24
		
25
		//pour la table dans le core qui contient tous les batiments
26
		private $all_batiment;
27
		private $all_batiment_nom;
28
29
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
30
		public function __construct() {
31
32
		}
33
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
34
35
36
37
		//-------------------------- GETTER ----------------------------------------------------------------------------//
38
		public function getNomBatiment() {
39
			return $this->nom_batiment;
40
		}
41
		public function getNomBatimentSql() {
42
			return $this->nom_batiment_sql;
43
		}
44
		public function getNiveau() {
45
			return $this->niveau_batiment;
46
		}
47
		public function getTempsConstruction() {
48
			return $this->temps_construction;
49
		}
50
		public function getRessourceConstruire() {
51
			return $this->ressource_construire;
52
		}
53
		public function getInfoBatiment() {
54
			return $this->info_batiment;
55
		}
56
		public function getInfoBatimentNext() {
57
			return $this->info_batiment_next;
58
		}
59
60
		public function getNomBatimentConstruction() {
61
			return $this->nom_batiment_construction;
62
		}
63
		public function getDateFinConstruction() {
64
			return $this->date_fin_construction;
65
		}
66
		public function getNiveauBatimentConstruction() {
67
			return $this->niveau_batiment_construction;
68
		}
69
70
		/**
71
		 * @param $nom_batiment
72
		 * @return int
73
		 * pour recuperer le niveau d'un batiment
74
		 */
75
		public function getNiveauBatiment($nom_batiment_sql, $id_base = null) {
76
			$dbc = App::getDb();
77
78
			if ($id_base == null) {
79
				$id_base = Bataille::getIdBase();
80
			}
81
82
			$query = $dbc->select("niveau")
83
				->select("construction")
84
				->from("_bataille_batiment")
85
				->where("nom_batiment_sql", "=", $nom_batiment_sql, "AND")
86
				->where("ID_base", "=", $id_base)
87
				->get();
88
89
			if ((is_array($query)) && (count($query) > 0)) {
90
				foreach ($query as $obj) {
91
					if ($obj->construction == 1) {
92
						return $obj->niveau-1;
93
					}
94
95
					return $obj->niveau;
96
				}
97
			}
98
			else {
99
				return 0;
100
			}
101
		}
102
103
		/**
104
		 * @param $ressource
105
		 * @return int
106
		 * recuperation de la production de chaque ressource en fonction du lvl des batiments
107
		 */
108
		public function getProduction($ressource) {
109
			$dbc1 = Bataille::getDb();
110
			
111
			$nom_batiment = "centrale_eau";
112
			if ($ressource == "electricite") $nom_batiment = "centrale_electrique";
113
			if ($ressource == "fuel") $nom_batiment = "station_pompage_fuel";
114
			if ($ressource == "fer") $nom_batiment = "station_forage";
115
116
			$niveau = $this->getNiveauBatiment($nom_batiment, Bataille::getIdBase());
117
118 View Code Duplication
			if ($niveau > 0) {
119
				$query = $dbc1->select("production")->from("$nom_batiment")->where("ID_".$nom_batiment, "=", $niveau)->get();
120
121
				if ((is_array($query)) && (count($query) > 0)) {
122
					foreach ($query as $obj) {
123
						return $obj->production;
124
					}
125
				}
126
			}
127
			else {
128
				return 20;
129
			}
130
		}
131
132
		/**
133
		 * @return int
134
		 * fonction qui retourne le stockage de l'entrepot
135
		 */
136
		public function getStockage($batiment = "entrepot") {
137
			$dbc1 = Bataille::getDb();
138
139
			$niveau = $this->getNiveauBatiment($batiment, Bataille::getIdBase());
140
141 View Code Duplication
			if ($niveau > 0) {
142
				$query = $dbc1->select("stockage")->from($batiment)->where("ID_".$batiment, "=", $niveau)->get();
143
144
				if ((is_array($query)) && (count($query) > 0)) {
145
					foreach ($query as $obj) {
146
						return $obj->stockage;
147
					}
148
				}
149
			}
150
			else {
151
				return 1000;
152
			}
153
		}
154
155
		/**
156
		 * permet de récupérer toutes les infos d'un batiment dans la popup
157
		 * @param $nom_batiment
158
		 */
159
		public function getUnBatiment($nom_batiment) {
160
			$dbc = App::getDb();
161
			$dbc1 = Bataille::getDb();
162
163
			$construction = $this->getTestBatimentConstruction($nom_batiment);
164
165
			//recuperation des infos du batiment
166
			$query = $dbc->select()
167
				->from("_bataille_batiment")
168
				->where("nom_batiment", "=", $construction[0], "AND")
169
				->where("ID_base", "=", Bataille::getIdBase())
170
				->get();
171
172
			if ((is_array($query)) && (count($query) > 0)) {
173
				foreach ($query as $obj) {
174
					$this->nom_batiment_sql = $obj->nom_batiment_sql;
175
					$this->niveau_batiment = $obj->niveau;
176
					$this->id_batiment = $obj->ID_batiment;
177
				}
178
179
				if (($construction[1] == true) && ($this->niveau_batiment > 1)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
180
					$this->niveau_batiment = $this->niveau_batiment+1;
181
				}
182
183
				$max_level = $this->getInfoUpgradeBatiment();
184
			}
185
			else {
186
				$query = $dbc1->select("nom_table")
187
					->from("liste_batiment")
188
					->where("nom", "=", $nom_batiment)
189
					->get();
190
191
				if ((is_array($query)) && (count($query) > 0)) {
192
					foreach ($query as $obj) {
193
						$this->nom_batiment_sql = $obj->nom_table;
194
					}
195
				}
196
197
				$this->niveau_batiment = 0;
198
				$this->getInfoUpgradeBatiment();
199
				$max_level = 0;
200
			}
201
202
			//permet de savoir si le batiment produit bien des ressoures
203
			$batiment_production = [];
204
205
			$query = $dbc1->select("nom")->from("liste_batiment")->where("type", "=", "ressource")->get();
206
207
			if ((is_array($query)) && (count($query) > 0)) {
208
				foreach ($query as $obj) {
209
					$batiment_production[] = $obj->nom;
210
				}
211
			}
212
213
			Bataille::setValues([
214
				"nom_batiment_sql" => $this->nom_batiment_sql,
215
				"niveau_batiment" => $this->niveau_batiment,
216
				"id_batiment" => $this->niveau_batiment,
217
				"max_level" => $max_level,
218
				"batiment_production" => $batiment_production
219
			]);
220
221
			return $max_level;
222
		}
223
224
		/**
225
		 * pour récupérer la construction en cours dans la base
226
		 */
227
		public function getConstruction() {
228
			$dbc = App::getDb();
229
230
			$today = Bataille::getToday();
231
232
			$query = $dbc->select()
233
				->from("_bataille_construction")
234
				->from("_bataille_batiment")
235
				->where("_bataille_construction.ID_base", "=", Bataille::getIdBase(), "AND")
236
				->where("_bataille_construction.ID_batiment", "=", "_bataille_batiment.ID_batiment", "AND", true)
237
				->where("_bataille_construction.ID_base", "=", "_bataille_batiment.ID_base", "", true)
238
				->get();
239
240
			if ((is_array($query)) && (count($query) > 0)) {
241
				foreach ($query as $obj) {
242
					$this->nom_batiment_construction = $obj->nom_batiment;
243
					$this->date_fin_construction = $obj->date_fin;
244
					$this->niveau_batiment_construction = $obj->niveau;
245
					$id_batiment = $obj->ID_batiment;
246
				}
247
248
				if ($this->date_fin_construction-$today <= 0) {
249
					$this->setTerminerConstruction($id_batiment);
0 ignored issues
show
Bug introduced by
The variable $id_batiment 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...
250
				}
251
				else {
252
					Bataille::setValues([
253
						"date_fin_construction" => $this->date_fin_construction-$today,
254
						"nom_batiment_construction" => $this->nom_batiment_construction
255
					]);
256
				}
257
258
				return 1;
259
			}
260
261
			return 0;
262
		}
263
		
264
		/**
265
		 * @return array
266
		 * fonction qui renvoi tous les nom sql des batiments construit dans la base
267
		 */
268
		private function getBatimentBase() {
269
			$dbc = App::getDb();
270
			
271
			$query = $dbc->select("nom_batiment_sql")
272
				->select("nom_batiment")
273
				->select("niveau")
274
				->from("_bataille_batiment")
275
				->where("ID_base", "=", Bataille::getIdBase())
276
				->get();
277
			
278
			if ((is_array($query)) && (count($query) > 0)) {
279
				foreach ($query as $obj) {
280
					$batiment_construit[] = $obj->nom_batiment_sql;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$batiment_construit was never initialized. Although not strictly required by PHP, it is generally a good practice to add $batiment_construit = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
281
				}
282
				
283
				return $batiment_construit;
0 ignored issues
show
Bug introduced by
The variable $batiment_construit 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...
284
			}
285
			
286
			return [];
287
		}
288
		
289
		/**
290
		 * @return int
291
		 * fonction qui renvoi tous les batiments actifs dans liste_batiment dans la bdd core
292
		 */
293
		private function getAllBatimentGame() {
294
			$dbc1 = Bataille::getDb();
295
			
296
			$query = $dbc1->select("nom_table")->select("nom")->from("liste_batiment")->where("actif", "=", 1)->get();
297
			
298
			if ((is_array($query)) && (count($query) > 0)) {
299
				foreach ($query as $obj) {
300
					$all_batiment[] = $obj->nom_table;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$all_batiment was never initialized. Although not strictly required by PHP, it is generally a good practice to add $all_batiment = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
301
					$all_batiment_nom[] = $obj->nom;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$all_batiment_nom was never initialized. Although not strictly required by PHP, it is generally a good practice to add $all_batiment_nom = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
302
				}
303
				
304
				$this->all_batiment = $all_batiment;
0 ignored issues
show
Bug introduced by
The variable $all_batiment 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...
305
				$this->all_batiment_nom = $all_batiment_nom;
0 ignored issues
show
Bug introduced by
The variable $all_batiment_nom 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...
306
				return count($all_batiment);
307
			}
308
			
309
			return 0;
310
		}
311
312
		/**
313
		 * pour récupérer la liste des batiments qu'il est possible de construire
314
		 */
315
		public function getBatimentAConstruire() {
316
			$dbc1 = Bataille::getDb();
317
			$batiment_construire = [];
318
319
			//recuperation des batiments deja construit dans la base
320
			$batiment_construit = $this->getBatimentBase();
321
322
			//recuperation de la liste complete des batiments
323
			$c_all_batiment  = $this->getAllBatimentGame();
324
325
			//boucle qui recupere en tableau le champ pour_construire d'un batiment
326
			//et compare la liste des batiments qu'il faut pour construire le batiment
327
			//a ceux qui sont deja construit dans la base
328
			//si tous les batments qu'il faut son batis on autorise la construction du batiment
329
			for ($i = 0 ; $i < $c_all_batiment ; $i++) {
330
				if (!in_array($this->all_batiment[$i], $batiment_construit)) {
331
					$query = $dbc1->select("pour_construire")
332
						->select("temps_construction")
333
						->from($this->all_batiment[$i])
334
						->where("ID_".$this->all_batiment[$i], "=", 1)
335
						->get();
336
					
337
					$pour_construire = [];
338
					if ((is_array($query)) && (count($query) > 0)) {
339
						foreach ($query as $obj) {
340
							if ($obj->pour_construire != null) {
341
								$pour_construire = unserialize($obj->pour_construire);
342
							}
343
344
							$temps_construction = gmdate("H:i:s", round($obj->temps_construction-($obj->temps_construction*Bataille::getBatiment()->getNiveauBatiment("centre_commandement")/100)));
345
							$taille_batiment = $this->getTailleBatiment($this->all_batiment[$i]);
346
						}
347
					}
348
349
350
					if (count($pour_construire) >= 1) {
351
						$ok_construction = false;
352
						//test si tous les batiments sont construits et on le niveau nécéssaire
353
						$count = count($pour_construire);
354
						for ($j = 0 ; $j < $count ; $j++) {
355
							if (in_array($pour_construire[$j][1], $batiment_construit)) {
356
								if ($pour_construire[$j][2] <= $this->getNiveauBatiment($pour_construire[$j][1])) {
357
									$ok_construction = true;
358
								}
359
								else {
360
									$ok_construction = false;
361
								}
362
							}
363
							else {
364
								$ok_construction = false;
365
							}
366
						}
367
						
368
						//si ok on affiche le batiment
369 View Code Duplication
						if ($ok_construction === true) {
370
							$ressource = $this->getRessourceConstruireBatiment($this->all_batiment[$i], 0);
371
							
372
							$batiment_construire[] = [
373
								"nom_batiment_sql" => $this->all_batiment[$i],
374
								"nom_batiment" => $this->all_batiment_nom[$i],
375
								"ressource" => $ressource,
376
								"temps_construction" => $temps_construction,
0 ignored issues
show
Bug introduced by
The variable $temps_construction 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...
377
								"width" => $taille_batiment[0],
0 ignored issues
show
Bug introduced by
The variable $taille_batiment 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...
378
								"height" => $taille_batiment[1]
379
							];
380
						}
381
					}
382 View Code Duplication
					else {
383
						$ressource = $this->getRessourceConstruireBatiment($this->all_batiment[$i], 0);
384
385
						$batiment_construire[] = [
386
							"nom_batiment_sql" => $this->all_batiment[$i],
387
							"nom_batiment" => $this->all_batiment_nom[$i],
388
							"ressource" => $ressource,
389
							"temps_construction" => $temps_construction,
390
							"width" => $taille_batiment[0],
391
							"height" => $taille_batiment[1]
392
						];
393
					}
394
				}
395
			}
396
			Bataille::setValues(["batiments_construire" => $batiment_construire]);
397
		}
398
		
399
		/**
400
		 * @param $case_depart
401
		 * @param $nom_batiment
402
		 * @param $nom_batiment_sql
403
		 * @return false|null
404
		 * fonction qui test si un il y a la place de construire un batiment en fonction de sa case ou il a été laché en x et y
405
		 * et en fonction de sa taille et du positionnement des autres batiments
406
		 */
407
		public function getEmplacementConstructionLibre($case_depart, $nom_batiment, $nom_batiment_sql) {
408
			$dbc = App::getDb();
409
410
			//récupération de la taille du batiment
411
			$taille_batiment = $this->getTailleBatiment($nom_batiment_sql);
412
			$width_batiment = $taille_batiment[0];
413
			$height_batiment = $taille_batiment[1];
414
415
			//récupération des coordonnées de la sae de départ du batiment
416
			$case_depart = explode(",", $case_depart);
417
			$posx = $case_depart[0];
418
			$posy = $case_depart[1];
419
			$finx = $width_batiment+$posx;
420
			$finy = $height_batiment+$posy;
421
422
			//récupération de tous les batiments
423
			$query = $dbc->select("posx")
424
				->select("posy")
425
				->select("nom_batiment_sql")
426
				->from("_bataille_batiment")
427
				->where("ID_base", "=", Bataille::getIdBase())
428
				->get();
429
			
430
			if ((is_array($query)) && (count($query) > 0)) {
431
				foreach ($query as $obj) {
432
					$taille_batiment = $this->getTailleBatiment($obj->nom_batiment_sql);
433
					$posx_batiment = $obj->posx;
434
					$posy_batiment = $obj->posy;
435
436
					$finx_batiment = $taille_batiment[0]+$posx_batiment;
437
					$finy_batiment = $taille_batiment[1]+$posy_batiment;
438
					
439
					for ($i = $posy ; $i < $finy ; $i++) {
440
						for ($j = $posx ; $j < $finx ; $j++) {
441
							if ((($posx++ >= $posx_batiment) && ($posx++ <= $finx_batiment)) && (($posy++ >= $posy_batiment) && ($posy++ <= $finy_batiment))) {
442
								FlashMessage::setFlash("Un batiment est déjà présent à cet emplacement, merci d'en choisir un autre");
443
								return false;
444
							}
445
						}
446
					}
447
					
448
					$posx = $case_depart[0];
449
					$posy = $case_depart[1];
450
				}
451
452
				//si tout est ok on commence la construction
453
				if ($this->setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql, $posx, $posy) === false) {
454
					return false;
455
				}
456
			}
457
		}
458
459
		/**
460
		 * @param $nom_batiment_sql
461
		 * @return array
462
		 * fonction qui renvoi un tableau contenant la taille et hauteur d'un batiment en
463
		 * fonction de son nom
464
		 */
465
		public function getTailleBatiment($nom_batiment_sql) {
466
			$dbc1 = Bataille::getDb();
467
468
			$query = $dbc1->select("width")
469
				->select("height")
470
				->from("liste_batiment")
471
				->where("nom_table", "=", $nom_batiment_sql)
472
				->get();
473
474
			if ((is_array($query)) && (count($query) > 0)) {
475
				foreach ($query as $obj) {
476
					return [$obj->width, $obj->height];
477
				}
478
			}
479
		}
480
481
		/**
482
		 * @param $nom_batiment_sql
483
		 * @param integer $niveau
484
		 * @return array
485
		 * recuperation des ressources nécéssaire pour construire le batiment
486
		 */
487
		private function getRessourceConstruireBatiment($nom_batiment_sql, $niveau) {
488
			$dbc1 = Bataille::getDb();
489
490
			$niveau = $niveau+1;
491
492
			$query = $dbc1->select("ressource_construire")->from($nom_batiment_sql)->where("ID_".$nom_batiment_sql, "=", $niveau)->get();
493
494
			if ((is_array($query)) && (count($query) > 0)) {
495
				foreach ($query as $obj) {
496
					$ressource_tmp = $obj->ressource_construire;
497
				}
498
				$ressource_tmp = explode(", ", $ressource_tmp);
0 ignored issues
show
Bug introduced by
The variable $ressource_tmp 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...
499
500
				//on test si assez de ressources dans la base
501
				//fer
502
				$ressource["fer"] = Bataille::getTestAssezRessourceBase("fer", $ressource_tmp[2]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ressource was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ressource = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
503
				//fuel
504
				$ressource["fuel"] = Bataille::getTestAssezRessourceBase("fuel", $ressource_tmp[3]);
505
				//eau
506
				$ressource["eau"] = Bataille::getTestAssezRessourceBase("eau", $ressource_tmp[0]);
507
				//electricite
508
				$ressource["electricite"] = Bataille::getTestAssezRessourceBase("electricite", $ressource_tmp[1]);
509
510
				return $ressource;
511
			}
512
		}
513
514
		/**
515
		 * fonction qui renvoi un tableau avec le nom du batiment sans (en construction)
516
		 * + true pour dire que le batiment est en construction
517
		 *
518
		 * @param $nom_batiment
519
		 * @return array
520
		 */
521
		private function getTestBatimentConstruction($nom_batiment) {
522
			if (ChaineCaractere::FindInString($nom_batiment, " en construction") == true) {
523
				return [substr($nom_batiment, 0, (0-strlen(" en construction"))), true];
524
			}
525
526
			return [$nom_batiment, false];
527
		}
528
529
		/**
530
		 * fonction qui renvoi les informations pour augmenter le niveau d'un batiment
531
		 * @return int
532
		 */
533
		private function getInfoUpgradeBatiment() {
534
			$dbc1 = Bataille::getDb();
535
536
			//récupération du temps et des ressources pour construire
537
			$query = $dbc1->select()->from($this->nom_batiment_sql)->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)->get();
538
			
539
			//si on a quelque chose cela veut dire qu'on est pas encore au lvl max du batiment
540
			if ((is_array($query)) && (count($query) > 0)) {
541
				foreach ($query as $obj) {
542
					$this->ressource_construire = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
543
					$this->temps_construction = round($obj->temps_construction-($obj->temps_construction*Bataille::getBatiment()->getNiveauBatiment("centre_commandement")/100));
544
				}
545
546
				//récupération des éléments particulier à un batiment
547
				$this->getTexteBatiment();
548
				
549
				Bataille::setValues([
550
					"ressource" => $this->ressource_construire,
551
					"temps_construction" => DateHeure::Secondeenheure($this->temps_construction),
552
					"info_batiment" => $this->info_batiment,
553
					"info_batiment_next" => $this->info_batiment_next,
554
				]);
555
556
				return 1;
557
			}
558
559
			return "max_level";
560
		}
561
		
562
		/**
563
		 * fonction qui récupère le contenu du xml en fonction du batiment
564
		 */
565
		private function getTexteBatiment() {
566
			$dbc1 = Bataille::getDb();
567
			
568
			//récupération des éléments particulier à un batiment
569
			$xml = simplexml_load_file(MODULEROOT.'bataille/data/batiment.xml');
570
			$nom_batiment_sql = $this->nom_batiment_sql;
571
			$champ = $xml->$nom_batiment_sql->champ;
572
			
573
			$this->info_batiment = "";
574
			$this->info_batiment_next = "";
575
			
576
			if (!empty($champ)) {
577
				//récupération de la phrase pour le niveau actuel
578
				$query = $dbc1->select($xml->$nom_batiment_sql->champ)
579
					->from($this->nom_batiment_sql)
580
					->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment)
581
					->get();
582
				
583 View Code Duplication
				if ((is_array($query)) && (count($query) > 0)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
584
					foreach ($query as $obj) {
585
						$this->info_batiment = $xml->$nom_batiment_sql->phrase.$obj->$champ.$xml->$nom_batiment_sql->complement;
586
					}
587
				}
588
				
589
				//récupération de la phrase pour le niveau suivant
590
				$query = $dbc1->select($xml->$nom_batiment_sql->champ)
591
					->from($this->nom_batiment_sql)
592
					->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)
593
					->get();
594
				
595 View Code Duplication
				if ((is_array($query)) && (count($query) > 0)){
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
596
					foreach ($query as $obj) {
597
						$this->info_batiment_next = $xml->$nom_batiment_sql->phrase_suivant.$obj->$champ.$xml->$nom_batiment_sql->complement;
598
					}
599
				}
600
			}
601
		}
602
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
603
604
605
606
		//-------------------------- SETTER ----------------------------------------------------------------------------//
607
		/**
608
		 * fonction qui initialise la construction d'un batiment
609
		 * @param $nom_batiment
610
		 * @param $nom_batiment_sql
611
		 */
612
		public function setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql, $posx, $posy) {
613
			$dbc = App::getDb();
614
			
615
			if ($this->getConstruction() == 1) {
616
				FlashMessage::setFlash("Un batiment est déjà en construction, vous ne pouvez pas en construire un autre !");
617
				return false;
618
			}
619
			
620
			$un_batiment = $this->getUnBatiment($nom_batiment);
621
			
622
			//on test si assez de ressrouce pour construire le batiment
623
			if ($un_batiment == 0) {
624
				$this->nom_batiment_sql = $nom_batiment_sql;
625
				$this->niveau_batiment = 0;
626
			}
627
			$ressource = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
628
			
629
			//si pas assez de ressource
630
			if (($ressource["eau"]["class"] || $ressource["electricite"]["class"] ||$ressource["fer"]["class"] || $ressource["fuel"]["class"]) == "rouge") {
631
				FlashMessage::setFlash("Pas assez de ressources pour construire ce batiment");
632
				return false;
633
			}
634
			
635
			//on insere la construction dans la table batiment si new batiment
636
			if ($un_batiment == 0) {
637
				$dbc->insert("niveau", $this->niveau_batiment+1)
638
					->insert("nom_batiment", $nom_batiment)
639
					->insert("nom_batiment_sql", $this->nom_batiment_sql)
640
					->insert("posx", intval($posx))
641
					->insert("posy", intval($posy))
642
					->insert("construction", 1)
643
					->insert("ID_base", Bataille::getIdBase())
644
					->into("_bataille_batiment")
645
					->set();
646
				
647
				$this->id_batiment = $dbc->lastInsertId();
648
			}
649
			else {
650
				$dbc->update("niveau", $this->niveau_batiment+1)
651
					->update("construction", 1)
652
					->from("_bataille_batiment")
653
					->where("ID_batiment", "=", $this->id_batiment, "AND")
654
					->where("ID_base", "=", Bataille::getIdBase())
655
					->set();
656
			}
657
			
658
			//on initialise la construction
659
			//recuperation de la date en seconde
660
			$today = Bataille::getToday();
661
			
662
			//date de la fin de la construction en seconde
663
			$fin_construction = $today+$this->temps_construction;
664
			
665
			$dbc->insert("date_fin", $fin_construction)
666
				->insert("ID_base", Bataille::getIdBase())
667
				->insert("ID_batiment", $this->id_batiment)
668
				->into("_bataille_construction")
669
				->set();
670
			
671
			//on retire les ressources de la base
672
			Bataille::getRessource()->setUpdateRessource($this->ressource_construire["eau"]["ressource"], $this->ressource_construire["electricite"]["ressource"], $this->ressource_construire["fer"]["ressource"], $this->ressource_construire["fuel"]["ressource"], 0, "-");
673
			echo("ok");
674
		}
675
676
		/**
677
		 * fonction qui termine la construction d'un batiment
678
		 * @param $id_batiment
679
		 */
680
		private function setTerminerConstruction($id_batiment) {
681
			$dbc = App::getDb();
682
683
			//on le retire de la table construction
684
			$dbc->delete()
685
				->from("_bataille_construction")
686
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
687
				->where("ID_batiment", "=", $id_batiment)
688
				->del();
689
690
			//on termine la construction dans la table batiment
691
			$dbc->update("construction", 0)
692
				->from("_bataille_batiment")
693
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
694
				->where("ID_batiment", "=", $id_batiment)
695
				->set();
696
			
697
			//on ajoute les points à la base
698
			Points::setAjouterPoints(Bataille::getIdBase(), "batiment");
699
		}
700
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
701
	}