Batiment::getProduction()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 6
eloc 12
nc 24
nop 1
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
			if ($niveau > 0) {
119
				$query = $dbc1->select("production")->from("$nom_batiment")->where("ID_".$nom_batiment, "=", $niveau)->get();
120
				
121
				foreach ($query as $obj) {
122
					return $obj->production;
123
				}
124
			}
125
			
126
			return 20;
127
		}
128
129
		/**
130
		 * @return int
131
		 * fonction qui retourne le stockage de l'entrepot
132
		 */
133
		public function getStockage($batiment = "entrepot") {
134
			$dbc1 = Bataille::getDb();
135
136
			$niveau = $this->getNiveauBatiment($batiment, Bataille::getIdBase());
137
138
			if ($niveau > 0) {
139
				$query = $dbc1->select("stockage")->from($batiment)->where("ID_".$batiment, "=", $niveau)->get();
140
141
				if ((is_array($query)) && (count($query) > 0)) {
142
					foreach ($query as $obj) {
143
						return $obj->stockage;
144
					}
145
				}
146
			}
147
			else {
148
				return 1000;
149
			}
150
		}
151
152
		/**
153
		 * permet de récupérer toutes les infos d'un batiment dans la popup
154
		 * @param $nom_batiment
155
		 */
156
		public function getUnBatiment($nom_batiment) {
157
			$dbc = App::getDb();
158
			$dbc1 = Bataille::getDb();
159
160
			$construction = $this->getTestBatimentConstruction($nom_batiment);
161
162
			//recuperation des infos du batiment
163
			$query = $dbc->select()
164
				->from("_bataille_batiment")
165
				->where("nom_batiment", "=", $construction[0], "AND")
166
				->where("ID_base", "=", Bataille::getIdBase())
167
				->get();
168
169
			if ((is_array($query)) && (count($query) > 0)) {
170
				foreach ($query as $obj) {
171
					$this->nom_batiment_sql = $obj->nom_batiment_sql;
172
					$this->niveau_batiment = $obj->niveau;
173
					$this->id_batiment = $obj->ID_batiment;
174
				}
175
176
				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...
177
					$this->niveau_batiment = $this->niveau_batiment+1;
178
				}
179
180
				$max_level = $this->getInfoUpgradeBatiment();
181
			}
182
			else {
183
				$query = $dbc1->select("nom_table")
184
					->from("liste_batiment")
185
					->where("nom", "=", $nom_batiment)
186
					->get();
187
188
				if ((is_array($query)) && (count($query) > 0)) {
189
					foreach ($query as $obj) {
190
						$this->nom_batiment_sql = $obj->nom_table;
191
					}
192
				}
193
194
				$this->niveau_batiment = 0;
195
				$this->getInfoUpgradeBatiment();
196
				$max_level = "pas construit";
197
			}
198
199
			//permet de savoir si le batiment produit bien des ressoures
200
			$batiment_production = $this->getListebatimentProduction();
201
202
			Bataille::setValues([
203
				"nom_batiment_sql" => $this->nom_batiment_sql,
204
				"niveau_batiment" => $this->niveau_batiment,
205
				"id_batiment" => $this->niveau_batiment,
206
				"max_level" => $max_level,
207
				"batiment_production" => $batiment_production
208
			]);
209
210
			return $max_level;
211
		}
212
		
213
		/**
214
		 * fonction qui renvoi tous les batiments de type ressource
215
		 */
216
		private function getListebatimentProduction() {
217
			$dbc1 = Bataille::getDb();
218
			
219
			$batiment_production = [];
220
			
221
			$query = $dbc1->select("nom")->from("liste_batiment")->where("type", "=", "ressource")->get();
222
			
223
			if ((is_array($query)) && (count($query) > 0)) {
224
				foreach ($query as $obj) {
225
					$batiment_production[] = $obj->nom;
226
				}
227
			}
228
			
229
			return $batiment_production;
230
		}
231
232
		/**
233
		 * pour récupérer la construction en cours dans la base
234
		 */
235
		public function getConstruction() {
236
			$dbc = App::getDb();
237
238
			$today = Bataille::getToday();
239
240
			$query = $dbc->select()
241
				->from("_bataille_construction")
242
				->from("_bataille_batiment")
243
				->where("_bataille_construction.ID_base", "=", Bataille::getIdBase(), "AND")
244
				->where("_bataille_construction.ID_batiment", "=", "_bataille_batiment.ID_batiment", "AND", true)
245
				->where("_bataille_construction.ID_base", "=", "_bataille_batiment.ID_base", "", true)
246
				->get();
247
248
			if ((is_array($query)) && (count($query) > 0)) {
249
				foreach ($query as $obj) {
250
					$this->nom_batiment_construction = $obj->nom_batiment;
251
					$this->date_fin_construction = $obj->date_fin;
252
					$this->niveau_batiment_construction = $obj->niveau;
253
					$id_batiment = $obj->ID_batiment;
254
				}
255
256
				if ($this->date_fin_construction-$today <= 0) {
257
					$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...
258
				}
259
				else {
260
					Bataille::setValues([
261
						"date_fin_construction" => $this->date_fin_construction-$today,
262
						"nom_batiment_construction" => $this->nom_batiment_construction
263
					]);
264
				}
265
266
				return 1;
267
			}
268
269
			return 0;
270
		}
271
		
272
		/**
273
		 * @return array
274
		 * fonction qui renvoi tous les nom sql des batiments construit dans la base
275
		 */
276
		private function getBatimentBase() {
277
			$dbc = App::getDb();
278
			
279
			$query = $dbc->select("nom_batiment_sql")
280
				->select("nom_batiment")
281
				->select("niveau")
282
				->from("_bataille_batiment")
283
				->where("ID_base", "=", Bataille::getIdBase())
284
				->get();
285
			
286
			if ((is_array($query)) && (count($query) > 0)) {
287
				foreach ($query as $obj) {
288
					$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...
289
				}
290
				
291
				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...
292
			}
293
			
294
			return [];
295
		}
296
		
297
		/**
298
		 * @return int
299
		 * fonction qui renvoi tous les batiments actifs dans liste_batiment dans la bdd core
300
		 */
301
		private function getAllBatimentGame() {
302
			$dbc1 = Bataille::getDb();
303
			
304
			$query = $dbc1->select("nom_table")->select("nom")->from("liste_batiment")->where("actif", "=", 1)->get();
305
			
306
			if ((is_array($query)) && (count($query) > 0)) {
307
				foreach ($query as $obj) {
308
					$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...
309
					$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...
310
				}
311
				
312
				$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...
313
				$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...
314
				return count($all_batiment);
315
			}
316
			
317
			return 0;
318
		}
319
320
		/**
321
		 * pour récupérer la liste des batiments qu'il est possible de construire
322
		 */
323
		public function getBatimentAConstruire() {
324
			$dbc1 = Bataille::getDb();
325
			$batiment_construire = [];
326
327
			//recuperation des batiments deja construit dans la base
328
			$batiment_construit = $this->getBatimentBase();
329
330
			//recuperation de la liste complete des batiments
331
			$c_all_batiment  = $this->getAllBatimentGame();
332
333
			//boucle qui recupere en tableau le champ pour_construire d'un batiment
334
			//et compare la liste des batiments qu'il faut pour construire le batiment
335
			//a ceux qui sont deja construit dans la base
336
			//si tous les batments qu'il faut son batis on autorise la construction du batiment
337
			for ($i = 0 ; $i < $c_all_batiment ; $i++) {
338
				if (!in_array($this->all_batiment[$i], $batiment_construit)) {
339
					$query = $dbc1->select("pour_construire")
340
						->select("temps_construction")
341
						->from($this->all_batiment[$i])
342
						->where("ID_".$this->all_batiment[$i], "=", 1)
343
						->get();
344
					
345
					$pour_construire = [];
346
					if ((is_array($query)) && (count($query) > 0)) {
347
						foreach ($query as $obj) {
348
							if ($obj->pour_construire != null) {
349
								$pour_construire = unserialize($obj->pour_construire);
350
							}
351
352
							$temps_construction = gmdate("H:i:s", round($obj->temps_construction-($obj->temps_construction*Bataille::getBatiment()->getNiveauBatiment("centre_commandement")/100)));
353
							$taille_batiment = $this->getTailleBatiment($this->all_batiment[$i]);
354
						}
355
					}
356
					
357
					if (count($pour_construire) >= 1) {
358
						$ok_construction = false;
359
						//test si tous les batiments sont construits et on le niveau nécéssaire
360
						$count = count($pour_construire);
361
						for ($j = 0 ; $j < $count ; $j++) {
362
							if (in_array($pour_construire[$j][1], $batiment_construit)) {
363
								if ($pour_construire[$j][2] <= $this->getNiveauBatiment($pour_construire[$j][1])) {
364
									$ok_construction = true;
365
								}
366
								else {
367
									$ok_construction = false;
368
								}
369
							}
370
							else {
371
								$ok_construction = false;
372
							}
373
						}
374
						
375
						//si ok on affiche le batiment
376 View Code Duplication
						if ($ok_construction === true) {
377
							$ressource = $this->getRessourceConstruireBatiment($this->all_batiment[$i], 0);
378
							
379
							$batiment_construire[] = [
380
								"nom_batiment_sql" => $this->all_batiment[$i],
381
								"nom_batiment" => $this->all_batiment_nom[$i],
382
								"ressource" => $ressource,
383
								"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...
384
								"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...
385
								"height" => $taille_batiment[1]
386
							];
387
						}
388
					}
389 View Code Duplication
					else {
390
						$ressource = $this->getRessourceConstruireBatiment($this->all_batiment[$i], 0);
391
392
						$batiment_construire[] = [
393
							"nom_batiment_sql" => $this->all_batiment[$i],
394
							"nom_batiment" => $this->all_batiment_nom[$i],
395
							"ressource" => $ressource,
396
							"temps_construction" => $temps_construction,
397
							"width" => $taille_batiment[0],
398
							"height" => $taille_batiment[1]
399
						];
400
					}
401
				}
402
			}
403
			Bataille::setValues(["batiments_construire" => $batiment_construire]);
404
		}
405
		
406
		/**
407
		 * @param $case_depart
408
		 * @param $nom_batiment
409
		 * @param $nom_batiment_sql
410
		 * @return false|null
411
		 * 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
412
		 * et en fonction de sa taille et du positionnement des autres batiments
413
		 */
414
		public function getEmplacementConstructionLibre($case_depart, $nom_batiment, $nom_batiment_sql) {
415
			$dbc = App::getDb();
416
417
			//récupération de la taille du batiment
418
			$taille_batiment = $this->getTailleBatiment($nom_batiment_sql);
419
			$width_batiment = $taille_batiment[0];
420
			$height_batiment = $taille_batiment[1];
421
422
			//récupération des coordonnées de la sae de départ du batiment
423
			$case_depart = explode(",", $case_depart);
424
			$posx = $case_depart[0];
425
			$posy = $case_depart[1];
426
			$finx = $width_batiment+$posx;
427
			$finy = $height_batiment+$posy;
428
429
			//récupération de tous les batiments
430
			$query = $dbc->select("posx")
431
				->select("posy")
432
				->select("nom_batiment_sql")
433
				->from("_bataille_batiment")
434
				->where("ID_base", "=", Bataille::getIdBase())
435
				->get();
436
			
437
			if ((is_array($query)) && (count($query) > 0)) {
438
				foreach ($query as $obj) {
439
					$taille_batiment = $this->getTailleBatiment($obj->nom_batiment_sql);
440
					$posx_batiment = $obj->posx;
441
					$posy_batiment = $obj->posy;
442
443
					$finx_batiment = $taille_batiment[0]+$posx_batiment;
444
					$finy_batiment = $taille_batiment[1]+$posy_batiment;
445
					
446
					for ($i = $posy ; $i < $finy ; $i++) {
447
						for ($j = $posx ; $j < $finx ; $j++) {
448
							if ((($posx++ >= $posx_batiment) && ($posx++ <= $finx_batiment)) && (($posy++ >= $posy_batiment) && ($posy++ <= $finy_batiment))) {
449
								FlashMessage::setFlash("Un batiment est déjà présent à cet emplacement, merci d'en choisir un autre");
450
								return false;
451
							}
452
						}
453
					}
454
					
455
					$posx = $case_depart[0];
456
					$posy = $case_depart[1];
457
				}
458
459
				//si tout est ok on commence la construction
460
				if ($this->setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql, $posx, $posy) === false) {
461
					return false;
462
				}
463
			}
464
		}
465
466
		/**
467
		 * @param $nom_batiment_sql
468
		 * @return array
469
		 * fonction qui renvoi un tableau contenant la taille et hauteur d'un batiment en
470
		 * fonction de son nom
471
		 */
472
		public function getTailleBatiment($nom_batiment_sql) {
473
			$dbc1 = Bataille::getDb();
474
475
			$query = $dbc1->select("width")
476
				->select("height")
477
				->from("liste_batiment")
478
				->where("nom_table", "=", $nom_batiment_sql)
479
				->get();
480
481
			if ((is_array($query)) && (count($query) > 0)) {
482
				foreach ($query as $obj) {
483
					return [$obj->width, $obj->height];
484
				}
485
			}
486
		}
487
488
		/**
489
		 * @param $nom_batiment_sql
490
		 * @param integer $niveau
491
		 * @return array
492
		 * recuperation des ressources nécéssaire pour construire le batiment
493
		 */
494
		private function getRessourceConstruireBatiment($nom_batiment_sql, $niveau) {
495
			$dbc1 = Bataille::getDb();
496
497
			$niveau = $niveau+1;
498
499
			$query = $dbc1->select("ressource_construire")->from($nom_batiment_sql)->where("ID_".$nom_batiment_sql, "=", $niveau)->get();
500
501
			if ((is_array($query)) && (count($query) > 0)) {
502
				foreach ($query as $obj) {
503
					$ressource_tmp = $obj->ressource_construire;
504
				}
505
				$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...
506
507
				//on test si assez de ressources dans la base
508
				//fer
509
				$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...
510
				//fuel
511
				$ressource["fuel"] = Bataille::getTestAssezRessourceBase("fuel", $ressource_tmp[3]);
512
				//eau
513
				$ressource["eau"] = Bataille::getTestAssezRessourceBase("eau", $ressource_tmp[0]);
514
				//electricite
515
				$ressource["electricite"] = Bataille::getTestAssezRessourceBase("electricite", $ressource_tmp[1]);
516
517
				return $ressource;
518
			}
519
		}
520
521
		/**
522
		 * fonction qui renvoi un tableau avec le nom du batiment sans (en construction)
523
		 * + true pour dire que le batiment est en construction
524
		 *
525
		 * @param $nom_batiment
526
		 * @return array
527
		 */
528
		private function getTestBatimentConstruction($nom_batiment) {
529
			if (ChaineCaractere::FindInString($nom_batiment, " en construction") == true) {
530
				return [substr($nom_batiment, 0, (0-strlen(" en construction"))), true];
531
			}
532
533
			return [$nom_batiment, false];
534
		}
535
536
		/**
537
		 * fonction qui renvoi les informations pour augmenter le niveau d'un batiment
538
		 * @return int
539
		 */
540
		private function getInfoUpgradeBatiment() {
541
			$dbc1 = Bataille::getDb();
542
543
			//récupération du temps et des ressources pour construire
544
			$query = $dbc1->select()->from($this->nom_batiment_sql)->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)->get();
545
			
546
			//si on a quelque chose cela veut dire qu'on est pas encore au lvl max du batiment
547
			if ((is_array($query)) && (count($query) > 0)) {
548
				foreach ($query as $obj) {
549
					$this->ressource_construire = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
550
					$this->temps_construction = round($obj->temps_construction-($obj->temps_construction*Bataille::getBatiment()->getNiveauBatiment("centre_commandement")/100));
551
				}
552
553
				//récupération des éléments particulier à un batiment
554
				$this->getTexteBatiment();
555
				
556
				Bataille::setValues([
557
					"ressource" => $this->ressource_construire,
558
					"temps_construction" => DateHeure::Secondeenheure($this->temps_construction),
559
					"info_batiment" => $this->info_batiment,
560
					"info_batiment_next" => $this->info_batiment_next,
561
				]);
562
563
				return 1;
564
			}
565
566
			return "max_level";
567
		}
568
		
569
		/**
570
		 * fonction qui récupère le contenu du xml en fonction du batiment
571
		 */
572
		private function getTexteBatiment() {
573
			$dbc1 = Bataille::getDb();
574
			
575
			//récupération des éléments particulier à un batiment
576
			$xml = simplexml_load_file(MODULEROOT.'bataille/data/batiment.xml');
577
			$nom_batiment_sql = $this->nom_batiment_sql;
578
			$champ = $xml->$nom_batiment_sql->champ;
579
			
580
			$this->info_batiment = "";
581
			$this->info_batiment_next = "";
582
			
583
			if (empty($champ)) return false;
584
			
585
			//récupération de la phrase pour le niveau actuel
586
			$query = $dbc1->select($xml->$nom_batiment_sql->champ)
587
				->from($this->nom_batiment_sql)
588
				->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment)
589
				->get();
590
			
591
			foreach ($query as $obj) {
592
				$this->info_batiment = $xml->$nom_batiment_sql->phrase.$obj->$champ.$xml->$nom_batiment_sql->complement;
593
			}
594
			
595
			//récupération de la phrase pour le niveau suivant
596
			$query = $dbc1->select($xml->$nom_batiment_sql->champ)
597
				->from($this->nom_batiment_sql)
598
				->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)
599
				->get();
600
			
601
			foreach ($query as $obj) {
602
				$this->info_batiment_next = $xml->$nom_batiment_sql->phrase_suivant.$obj->$champ.$xml->$nom_batiment_sql->complement;
603
			}
604
		}
605
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
606
607
608
609
		//-------------------------- SETTER ----------------------------------------------------------------------------//
610
		/**
611
		 * fonction qui initialise la construction d'un batiment
612
		 * @param $nom_batiment
613
		 * @param $nom_batiment_sql
614
		 */
615
		public function setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql, $posx, $posy) {
616
			$dbc = App::getDb();
617
			
618
			if ($this->getConstruction() == 1) {
619
				FlashMessage::setFlash("Un batiment est déjà en construction, vous ne pouvez pas en construire un autre !");
620
				return false;
621
			}
622
			
623
			$un_batiment = $this->getUnBatiment($nom_batiment);
624
			
625
			//on test si assez de ressrouce pour construire le batiment
626
			if ($un_batiment == 0) {
627
				$this->nom_batiment_sql = $nom_batiment_sql;
628
				$this->niveau_batiment = 0;
629
			}
630
			$ressource = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
631
			
632
			//si pas assez de ressource
633
			if (($ressource["eau"]["class"] || $ressource["electricite"]["class"] ||$ressource["fer"]["class"] || $ressource["fuel"]["class"]) == "rouge") {
634
				FlashMessage::setFlash("Pas assez de ressources pour construire ce batiment");
635
				return false;
636
			}
637
			
638
			//on insere la construction dans la table batiment si new batiment
639
			if ($un_batiment == 0) {
640
				$dbc->insert("niveau", $this->niveau_batiment+1)
641
					->insert("nom_batiment", $nom_batiment)
642
					->insert("nom_batiment_sql", $this->nom_batiment_sql)
643
					->insert("posx", intval($posx))
644
					->insert("posy", intval($posy))
645
					->insert("construction", 1)
646
					->insert("ID_base", Bataille::getIdBase())
647
					->into("_bataille_batiment")
648
					->set();
649
				
650
				$this->id_batiment = $dbc->lastInsertId();
651
			}
652
			else {
653
				$dbc->update("niveau", $this->niveau_batiment+1)
654
					->update("construction", 1)
655
					->from("_bataille_batiment")
656
					->where("ID_batiment", "=", $this->id_batiment, "AND")
657
					->where("ID_base", "=", Bataille::getIdBase())
658
					->set();
659
			}
660
			
661
			//on initialise la construction
662
			//recuperation de la date en seconde
663
			$today = Bataille::getToday();
664
			
665
			//date de la fin de la construction en seconde
666
			$fin_construction = $today+$this->temps_construction;
667
			
668
			$dbc->insert("date_fin", $fin_construction)
669
				->insert("ID_base", Bataille::getIdBase())
670
				->insert("ID_batiment", $this->id_batiment)
671
				->into("_bataille_construction")
672
				->set();
673
			
674
			//on retire les ressources de la base
675
			Bataille::getRessource()->setUpdateRessource($this->ressource_construire["eau"]["ressource"], $this->ressource_construire["electricite"]["ressource"], $this->ressource_construire["fer"]["ressource"], $this->ressource_construire["fuel"]["ressource"], 0, "-");
676
			echo("ok");
677
		}
678
679
		/**
680
		 * fonction qui termine la construction d'un batiment
681
		 * @param $id_batiment
682
		 */
683
		private function setTerminerConstruction($id_batiment) {
684
			$dbc = App::getDb();
685
686
			//on le retire de la table construction
687
			$dbc->delete()
688
				->from("_bataille_construction")
689
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
690
				->where("ID_batiment", "=", $id_batiment)
691
				->del();
692
693
			//on termine la construction dans la table batiment
694
			$dbc->update("construction", 0)
695
				->from("_bataille_batiment")
696
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
697
				->where("ID_batiment", "=", $id_batiment)
698
				->set();
699
			
700
			//on ajoute les points à la base
701
			Points::setAjouterPoints(Bataille::getIdBase(), "batiment");
702
		}
703
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
704
	}