Passed
Push — master ( e105a7...39babf )
by Anthony
02:38
created

Batiment::getBatimentBase()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 3
nop 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) {
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...
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) {
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...
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
						if (in_array($pour_construire[0][1], $batiment_construit)) {
352
							if ($pour_construire[0][2] <= $this->getNiveauBatiment($pour_construire[0][1])) {
353
								$ressource = $this->getRessourceConstruireBatiment($this->all_batiment[$i], 0);
354
355
								$batiment_construire[] = [
356
									"nom_batiment_sql" => $this->all_batiment[$i],
357
									"nom_batiment" => $this->all_batiment_nom[$i],
358
									"ressource" => $ressource,
359
									"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...
360
									"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...
361
									"height" => $taille_batiment[1]
362
								];
363
							}
364
						}
365
					}
366
					else if (count($pour_construire) > 1) {
367
						$ok_construction = false;
368
						//test si tous les batiments sont construits et on le niveau nécéssaire
369
						$count = count($pour_construire);
370
						for ($j = 0 ; $j < $count ; $j++) {
371
							if (in_array($pour_construire[$j][1], $batiment_construit)) {
372
								if ($pour_construire[$j][2] <= $this->getNiveauBatiment($pour_construire[$j][1])) {
373
									$ok_construction = true;
374
								}
375
								else {
376
									$ok_construction = false;
377
								}
378
							}
379
							else {
380
								$ok_construction = false;
381
							}
382
						}
383
384
						//si ok on affiche le batiment
385 View Code Duplication
						if ($ok_construction === true) {
386
							$ressource = $this->getRessourceConstruireBatiment($this->all_batiment[$i], 0);
387
388
							$batiment_construire[] = [
389
								"nom_batiment_sql" => $this->all_batiment[$i],
390
								"nom_batiment" => $this->all_batiment_nom[$i],
391
								"ressource" => $ressource,
392
								"temps_construction" => $temps_construction,
393
								"width" => $taille_batiment[0],
394
								"height" => $taille_batiment[1]
395
							];
396
						}
397
					}
398 View Code Duplication
					else {
399
						$ressource = $this->getRessourceConstruireBatiment($this->all_batiment[$i], 0);
400
401
						$batiment_construire[] = [
402
							"nom_batiment_sql" => $this->all_batiment[$i],
403
							"nom_batiment" => $this->all_batiment_nom[$i],
404
							"ressource" => $ressource,
405
							"temps_construction" => $temps_construction,
406
							"width" => $taille_batiment[0],
407
							"height" => $taille_batiment[1]
408
						];
409
					}
410
				}
411
			}
412
			Bataille::setValues(["batiments_construire" => $batiment_construire]);
413
		}
414
		
415
		/**
416
		 * @param $case_depart
417
		 * @param $nom_batiment
418
		 * @param $nom_batiment_sql
419
		 * @return false|null
420
		 * 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
421
		 * et en fonction de sa taille et du positionnement des autres batiments
422
		 */
423
		public function getEmplacementConstructionLibre($case_depart, $nom_batiment, $nom_batiment_sql) {
424
			$dbc = App::getDb();
425
426
			//récupération de la taille du batiment
427
			$taille_batiment = $this->getTailleBatiment($nom_batiment_sql);
428
			$width_batiment = $taille_batiment[0];
429
			$height_batiment = $taille_batiment[1];
430
431
			//récupération des coordonnées de la sae de départ du batiment
432
			$case_depart = explode(",", $case_depart);
433
			$posx = $case_depart[0];
434
			$posy = $case_depart[1];
435
			$finx = $width_batiment+$posx;
436
			$finy = $height_batiment+$posy;
437
438
			//récupération de tous les batiments
439
			$query = $dbc->select("posx")
440
				->select("posy")
441
				->select("nom_batiment_sql")
442
				->from("_bataille_batiment")
443
				->where("ID_base", "=", Bataille::getIdBase())
444
				->get();
445
			
446
			if ((is_array($query)) && (count($query) > 0)) {
447
				foreach ($query as $obj) {
448
					$taille_batiment = $this->getTailleBatiment($obj->nom_batiment_sql);
449
					$posx_batiment = $obj->posx;
450
					$posy_batiment = $obj->posy;
451
452
					$finx_batiment = $taille_batiment[0]+$posx_batiment;
453
					$finy_batiment = $taille_batiment[1]+$posy_batiment;
454
					
455
					for ($i = $posy ; $i < $finy ; $i++) {
456
						for ($j = $posx ; $j < $finx ; $j++) {
457
							if ((($posx++ >= $posx_batiment) && ($posx++ <= $finx_batiment)) && (($posy++ >= $posy_batiment) && ($posy++ <= $finy_batiment))) {
458
								FlashMessage::setFlash("Un batiment est déjà présent à cet emplacement, merci d'en choisir un autre");
459
								return false;
460
							}
461
						}
462
					}
463
					
464
					$posx = $case_depart[0];
465
					$posy = $case_depart[1];
466
				}
467
468
				//si tout est ok on commence la construction
469
				if ($this->setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql, $posx, $posy) === false) {
470
					return false;
471
				}
472
			}
473
		}
474
475
		/**
476
		 * @param $nom_batiment_sql
477
		 * @return array
478
		 * fonction qui renvoi un tableau contenant la taille et hauteur d'un batiment en
479
		 * fonction de son nom
480
		 */
481
		public function getTailleBatiment($nom_batiment_sql) {
482
			$dbc1 = Bataille::getDb();
483
484
			$query = $dbc1->select("width")
485
				->select("height")
486
				->from("liste_batiment")
487
				->where("nom_table", "=", $nom_batiment_sql)
488
				->get();
489
490
			if ((is_array($query)) && (count($query) > 0)) {
491
				foreach ($query as $obj) {
492
					return [$obj->width, $obj->height];
493
				}
494
			}
495
		}
496
497
		/**
498
		 * @param $nom_batiment_sql
499
		 * @param integer $niveau
500
		 * @return array
501
		 * recuperation des ressources nécéssaire pour construire le batiment
502
		 */
503
		private function getRessourceConstruireBatiment($nom_batiment_sql, $niveau) {
504
			$dbc1 = Bataille::getDb();
505
506
			$niveau = $niveau+1;
507
508
			$query = $dbc1->select("ressource_construire")->from($nom_batiment_sql)->where("ID_".$nom_batiment_sql, "=", $niveau)->get();
509
510
			if ((is_array($query)) && (count($query) > 0)) {
511
				foreach ($query as $obj) {
512
					$ressource_tmp = $obj->ressource_construire;
513
				}
514
				$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...
515
516
				//on test si assez de ressources dans la base
517
				//fer
518
				$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...
519
				//fuel
520
				$ressource["fuel"] = Bataille::getTestAssezRessourceBase("fuel", $ressource_tmp[3]);
521
				//eau
522
				$ressource["eau"] = Bataille::getTestAssezRessourceBase("eau", $ressource_tmp[0]);
523
				//electricite
524
				$ressource["electricite"] = Bataille::getTestAssezRessourceBase("electricite", $ressource_tmp[1]);
525
526
				return $ressource;
527
			}
528
		}
529
530
		/**
531
		 * fonction qui renvoi un tableau avec le nom du batiment sans (en construction)
532
		 * + true pour dire que le batiment est en construction
533
		 *
534
		 * @param $nom_batiment
535
		 * @return array
536
		 */
537
		private function getTestBatimentConstruction($nom_batiment) {
538
			if (ChaineCaractere::FindInString($nom_batiment, " en construction") == true) {
539
				return [substr($nom_batiment, 0, (0-strlen(" en construction"))), true];
540
			}
541
542
			return [$nom_batiment, false];
543
		}
544
545
		/**
546
		 * fonction qui renvoi les informations pour augmenter le niveau d'un batiment
547
		 * @return int
548
		 */
549
		private function getInfoUpgradeBatiment() {
550
			$dbc1 = Bataille::getDb();
551
552
			//récupération du temps et des ressources pour construire
553
			$query = $dbc1->select()->from($this->nom_batiment_sql)->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)->get();
554
555
			//si on a quelque chose cela veut dire qu'on est pas encore au lvl max du batiment
556
			if ((is_array($query)) && (count($query) > 0)) {
557
				foreach ($query as $obj) {
558
					$this->ressource_construire = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
559
					$this->temps_construction = DateHeure::Secondeenheure(round($obj->temps_construction-($obj->temps_construction*Bataille::getBatiment()->getNiveauBatiment("centre_commandement")/100)));
560
				}
561
562
				//récupération des éléments particulier à un batiment
563
				$xml = simplexml_load_file(MODULEROOT.'bataille/data/batiment.xml');
564
				$nom_batiment_sql = $this->nom_batiment_sql;
565
				$champ = $xml->$nom_batiment_sql->champ;
566
567
				if (!empty($champ)) {
568
					//récupération de la phrase pour le niveau actuel
569
					$query = $dbc1->select($xml->$nom_batiment_sql->champ)
570
						->from($this->nom_batiment_sql)
571
						->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment)
572
						->get();
573
574 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...
575
						foreach ($query as $obj) {
576
							$this->info_batiment = $xml->$nom_batiment_sql->phrase.$obj->$champ.$xml->$nom_batiment_sql->complement;
577
						}
578
					}
579
580
					//récupération de la phrase pour le niveau suivant
581
					$query = $dbc1->select($xml->$nom_batiment_sql->champ)
582
						->from($this->nom_batiment_sql)
583
						->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)
584
						->get();
585
586 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...
587
						foreach ($query as $obj) {
588
							$this->info_batiment_next = $xml->$nom_batiment_sql->phrase_suivant.$obj->$champ.$xml->$nom_batiment_sql->complement;
589
						}
590
					}
591
				}
592
				else {
593
					$this->info_batiment = "";
594
					$this->info_batiment_next = "";
595
				}
596
597
598
				Bataille::setValues([
599
					"ressource" => $this->ressource_construire,
600
					"temps_construction" => $this->temps_construction,
601
					"info_batiment" => $this->info_batiment,
602
					"info_batiment_next" => $this->info_batiment_next,
603
				]);
604
605
				return 1;
606
			}
607
608
			return "max_level";
609
		}
610
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
611
612
613
614
		//-------------------------- SETTER ----------------------------------------------------------------------------//
615
		/**
616
		 * fonction qui initialise la construction d'un batiment
617
		 * @param $nom_batiment
618
		 * @param $nom_batiment_sql
619
		 */
620
		public function setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql, $posx, $posy) {
621
			$dbc = App::getDb();
622
			$dbc1 = Bataille::getDb();
623
			
624
			if ($this->getConstruction() == 1) {
625
				FlashMessage::setFlash("Un batiment est déjà en construction, vous ne pouvez pas en construire un autre !");
626
				return false;
627
			}
628
			
629
			
630
			$un_batiment = $this->getUnBatiment($nom_batiment);
631
			
632
			//on test si assez de ressrouce pour construire le batiment
633
			if ($un_batiment == 0) {
634
				$ressource = $this->getRessourceConstruireBatiment($nom_batiment_sql, 0);
635
				$this->nom_batiment_sql = $nom_batiment_sql;
636
				$this->niveau_batiment = 0;
637
			}
638
			else {
639
				$ressource = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
640
			}
641
			
642
			//si pas assez de ressource
643
			if (($ressource["eau"]["class"] || $ressource["electricite"]["class"] ||$ressource["fer"]["class"] || $ressource["fuel"]["class"]) == "rouge") {
644
				FlashMessage::setFlash("Pas assez de ressources pour construire ce batiment");
645
				return false;
646
			}
647
			
648
			//recuperation du temps de construction
649
			$query = $dbc1->select("ressource_construire")
650
				->select("temps_construction")
651
				->from($this->nom_batiment_sql)
652
				->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)
653
				->get();
654
			
655
			foreach ($query as $obj) {
656
				$temps_construction = round($obj->temps_construction-($obj->temps_construction*Bataille::getBatiment()->getNiveauBatiment("centre_commandement")/100));
657
				$ressource_construction = explode(", ", $obj->ressource_construire);
658
			}
659
			
660
			//on insere la construction dans la table batiment si new batiment
661
			if ($un_batiment == 0) {
662
				$dbc->insert("niveau", $this->niveau_batiment+1)
663
					->insert("nom_batiment", $nom_batiment)
664
					->insert("nom_batiment_sql", $this->nom_batiment_sql)
665
					->insert("posx", intval($posx))
666
					->insert("posy", intval($posy))
667
					->insert("construction", 1)
668
					->insert("ID_base", Bataille::getIdBase())
669
					->into("_bataille_batiment")
670
					->set();
671
				
672
				$this->id_batiment = $dbc->lastInsertId();
673
			}
674
			else {
675
				$dbc->update("niveau", $this->niveau_batiment+1)
676
					->update("construction", 1)
677
					->from("_bataille_batiment")
678
					->where("ID_batiment", "=", $this->id_batiment, "AND")
679
					->where("ID_base", "=", Bataille::getIdBase())
680
					->set();
681
			}
682
			
683
			
684
			//on initialise la construction
685
			//recuperation de la date en seconde
686
			$today = Bataille::getToday();
687
			
688
			//date de la fin de la construction en seconde
689
			$fin_construction = $today+$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...
690
			
691
			$dbc->insert("date_fin", $fin_construction)
692
				->insert("ID_base", Bataille::getIdBase())
693
				->insert("ID_batiment", $this->id_batiment)
694
				->into("_bataille_construction")
695
				->set();
696
			
697
			//on retire les ressources de la base
698
			Bataille::getRessource()->setUpdateRessource($ressource_construction[2], $ressource_construction[3], $ressource_construction[0], $ressource_construction[1], 0, "-");
0 ignored issues
show
Bug introduced by
The variable $ressource_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...
699
			echo("ok");
700
		}
701
702
		/**
703
		 * fonction qui termine la construction d'un batiment
704
		 * @param $id_batiment
705
		 */
706
		private function setTerminerConstruction($id_batiment) {
707
			$dbc = App::getDb();
708
709
			//on le retire de la table construction
710
			$dbc->delete()
711
				->from("_bataille_construction")
712
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
713
				->where("ID_batiment", "=", $id_batiment)
714
				->del();
715
716
			//on termine la construction dans la table batiment
717
			$dbc->update("construction", 0)
718
				->from("_bataille_batiment")
719
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
720
				->where("ID_batiment", "=", $id_batiment)
721
				->set();
722
			
723
			//on ajoute les points à la base
724
			Points::setAjouterPoints(Bataille::getIdBase(), "batiment");
725
		}
726
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
727
	}