Passed
Push — master ( 89b8c6...e67967 )
by Anthony
02:32
created

Batiment::getStockage()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 12
Ratio 66.67 %

Importance

Changes 0
Metric Value
dl 12
loc 18
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 4
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
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
26
		public function __construct() {
27
28
		}
29
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
30
31
32
33
		//-------------------------- GETTER ----------------------------------------------------------------------------//
34
		public function getNomBatiment() {
35
			return $this->nom_batiment;
36
		}
37
		public function getNomBatimentSql() {
38
			return $this->nom_batiment_sql;
39
		}
40
		public function getNiveau() {
41
			return $this->niveau_batiment;
42
		}
43
		public function getTempsConstruction() {
44
			return $this->temps_construction;
45
		}
46
		public function getRessourceConstruire() {
47
			return $this->ressource_construire;
48
		}
49
		public function getInfoBatiment() {
50
			return $this->info_batiment;
51
		}
52
		public function getInfoBatimentNext() {
53
			return $this->info_batiment_next;
54
		}
55
56
		public function getNomBatimentConstruction() {
57
			return $this->nom_batiment_construction;
58
		}
59
		public function getDateFinConstruction() {
60
			return $this->date_fin_construction;
61
		}
62
		public function getNiveauBatimentConstruction() {
63
			return $this->niveau_batiment_construction;
64
		}
65
66
		/**
67
		 * @param $nom_batiment
68
		 * @return int
69
		 * pour recuperer le niveau d'un batiment
70
		 */
71
		public function getNiveauBatiment($nom_batiment_sql, $id_base = null) {
72
			$dbc = App::getDb();
73
74
			if ($id_base == null) {
75
				$id_base = Bataille::getIdBase();
76
			}
77
78
			$query = $dbc->select("niveau")
79
				->select("construction")
80
				->from("_bataille_batiment")
81
				->where("nom_batiment_sql", "=", $nom_batiment_sql, "AND")
82
				->where("ID_base", "=", $id_base)
83
				->get();
84
85
			if ((is_array($query)) && (count($query) > 0)) {
86
				foreach ($query as $obj) {
87
					if ($obj->construction == 1) {
88
						return $obj->niveau-1;
89
					}
90
91
					return $obj->niveau;
92
				}
93
			}
94
			else {
95
				return 0;
96
			}
97
		}
98
99
		/**
100
		 * @param $ressource
101
		 * @return int
102
		 * recuperation de la production de chaque ressource en fonction du lvl des batiments
103
		 */
104
		public function getProduction($ressource) {
105
			$dbc1 = Bataille::getDb();
106
			
107
			$nom_batiment = "centrale_eau";
108
			if ($ressource == "electricite") $nom_batiment = "centrale_electrique";
109
			if ($ressource == "fuel") $nom_batiment = "station_pompage_fuel";
110
			if ($ressource == "fer") $nom_batiment = "station_forage";
111
112
			$niveau = $this->getNiveauBatiment($nom_batiment, Bataille::getIdBase());
113
114 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...
115
				$query = $dbc1->select("production")->from("$nom_batiment")->where("ID_".$nom_batiment, "=", $niveau)->get();
116
117
				if ((is_array($query)) && (count($query) > 0)) {
118
					foreach ($query as $obj) {
119
						return $obj->production;
120
					}
121
				}
122
			}
123
			else {
124
				return 20;
125
			}
126
		}
127
128
		/**
129
		 * @return int
130
		 * fonction qui retourne le stockage de l'entrepot
131
		 */
132
		public function getStockage($batiment = "entrepot") {
133
			$dbc1 = Bataille::getDb();
134
135
			$niveau = $this->getNiveauBatiment($batiment, Bataille::getIdBase());
136
137 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...
138
				$query = $dbc1->select("stockage")->from($batiment)->where("ID_".$batiment, "=", $niveau)->get();
139
140
				if ((is_array($query)) && (count($query) > 0)) {
141
					foreach ($query as $obj) {
142
						return $obj->stockage;
143
					}
144
				}
145
			}
146
			else {
147
				return 1000;
148
			}
149
		}
150
151
		/**
152
		 * permet de récupérer toutes les infos d'un batiment dans la popup
153
		 * @param $nom_batiment
154
		 */
155
		public function getUnBatiment($nom_batiment) {
156
			$dbc = App::getDb();
157
			$dbc1 = Bataille::getDb();
158
159
			$construction = $this->getTestBatimentConstruction($nom_batiment);
160
161
			//recuperation des infos du batiment
162
			$query = $dbc->select()
163
				->from("_bataille_batiment")
164
				->where("nom_batiment", "=", $construction[0], "AND")
165
				->where("ID_base", "=", Bataille::getIdBase())
166
				->get();
167
168
			if ((is_array($query)) && (count($query) > 0)) {
169
				foreach ($query as $obj) {
170
					$this->nom_batiment_sql = $obj->nom_batiment_sql;
171
					$this->niveau_batiment = $obj->niveau;
172
					$this->id_batiment = $obj->ID_batiment;
173
				}
174
175
				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...
176
					$this->niveau_batiment = $this->niveau_batiment+1;
177
				}
178
179
				$max_level = $this->getInfoUpgradeBatiment();
180
			}
181
			else {
182
				$query = $dbc1->select("nom_table")
183
					->from("liste_batiment")
184
					->where("nom", "=", $nom_batiment)
185
					->get();
186
187
				if ((is_array($query)) && (count($query) > 0)) {
188
					foreach ($query as $obj) {
189
						$this->nom_batiment_sql = $obj->nom_table;
190
					}
191
				}
192
193
				$this->niveau_batiment = 0;
194
				$this->getInfoUpgradeBatiment();
195
				$max_level = 0;
196
			}
197
198
			//permet de savoir si le batiment produit bien des ressoures
199
			$batiment_production = [];
200
201
			$query = $dbc1->select("nom")->from("liste_batiment")->where("type", "=", "ressource")->get();
202
203
			if ((is_array($query)) && (count($query) > 0)) {
204
				foreach ($query as $obj) {
205
					$batiment_production[] = $obj->nom;
206
				}
207
			}
208
209
			Bataille::setValues([
210
				"nom_batiment_sql" => $this->nom_batiment_sql,
211
				"niveau_batiment" => $this->niveau_batiment,
212
				"id_batiment" => $this->niveau_batiment,
213
				"max_level" => $max_level,
214
				"batiment_production" => $batiment_production
215
			]);
216
217
			return $max_level;
218
		}
219
220
		/**
221
		 * pour récupérer la construction en cours dans la base
222
		 */
223
		public function getConstruction() {
224
			$dbc = App::getDb();
225
226
			$today = Bataille::getToday();
227
228
			$query = $dbc->select()
229
				->from("_bataille_construction")
230
				->from("_bataille_batiment")
231
				->where("_bataille_construction.ID_base", "=", Bataille::getIdBase(), "AND")
232
				->where("_bataille_construction.ID_batiment", "=", "_bataille_batiment.ID_batiment", "AND", true)
233
				->where("_bataille_construction.ID_base", "=", "_bataille_batiment.ID_base", "", true)
234
				->get();
235
236
			if ((is_array($query)) && (count($query) > 0)) {
237
				foreach ($query as $obj) {
238
					$this->nom_batiment_construction = $obj->nom_batiment;
239
					$this->date_fin_construction = $obj->date_fin;
240
					$this->niveau_batiment_construction = $obj->niveau;
241
					$id_batiment = $obj->ID_batiment;
242
				}
243
244
				if ($this->date_fin_construction-$today <= 0) {
245
					$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...
246
				}
247
				else {
248
					Bataille::setValues([
249
						"date_fin_construction" => $this->date_fin_construction-$today,
250
						"nom_batiment_construction" => $this->nom_batiment_construction
251
					]);
252
				}
253
254
				return 1;
255
			}
256
257
			return 0;
258
		}
259
260
		/**
261
		 * pour récupérer la liste des batiments qu'il est possible de construire
262
		 */
263
		public function getBatimentAConstruire() {
264
			$dbc = App::getDb();
265
			$dbc1 = Bataille::getDb();
266
			$batiment_construit = [];
267
			$batiment_construire = [];
268
269
			//recuperation des batiments deja construit dans la base
270
			$query = $dbc->select("nom_batiment_sql")
271
				->select("nom_batiment")
272
				->select("niveau")
273
				->from("_bataille_batiment")
274
				->where("ID_base", "=", Bataille::getIdBase())
275
				->get();
276
277
			if ((is_array($query)) && (count($query) > 0)) {
278
				foreach ($query as $obj) {
279
					$batiment_construit[] = $obj->nom_batiment_sql;
280
				}
281
			}
282
283
			//recuperation de la liste complete des batiments
284
			$query = $dbc1->select("nom_table")->select("nom")->from("liste_batiment")->where("actif", "=", 1)->get();
285
			if ((is_array($query)) && (count($query) > 0)) {
286
				foreach ($query as $obj) {
287
					$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...
288
					$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...
289
				}
290
291
				$c_all_batiment = count($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...
292
			}
293
294
			//boucle qui recupere en tableau le champ pour_construire d'un batiment
295
			//et compare la liste des batiments qu'il faut pour construire le batiment
296
			//a ceux qui sont deja construit dans la base
297
			//si tous les batments qu'il faut son batis on autorise la construction du batiment
298
			for ($i = 0 ; $i < $c_all_batiment ; $i++) {
0 ignored issues
show
Bug introduced by
The variable $c_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...
299
				if (!in_array($all_batiment[$i], $batiment_construit)) {
300
					$query = $dbc1->select("pour_construire")
301
						->select("temps_construction")
302
						->from($all_batiment[$i])
303
						->where("ID_".$all_batiment[$i], "=", 1)
304
						->get();
305
					
306
					$pour_construire = [];
307
					if ((is_array($query)) && (count($query) > 0)) {
308
						foreach ($query as $obj) {
309
							if ($obj->pour_construire != null) {
310
								$pour_construire = unserialize($obj->pour_construire);
311
							}
312
313
							$temps_construction = gmdate("H:i:s", round($obj->temps_construction-($obj->temps_construction*Bataille::getBatiment()->getNiveauBatiment("centre_commandement")/100)));
314
							$taille_batiment = $this->getTailleBatiment($all_batiment[$i]);
315
						}
316
					}
317
318
319
					if (count($pour_construire) == 1) {
320
						if (in_array($pour_construire[0][1], $batiment_construit)) {
321
							if ($pour_construire[0][2] <= $this->getNiveauBatiment($pour_construire[0][1])) {
322
								$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0);
323
324
								$batiment_construire[] = [
325
									"nom_batiment_sql" => $all_batiment[$i],
326
									"nom_batiment" => $all_batiment_nom[$i],
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...
327
									"ressource" => $ressource,
328
									"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...
329
									"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...
330
									"height" => $taille_batiment[1]
331
								];
332
							}
333
						}
334
					}
335
					else if (count($pour_construire) > 1) {
336
						$ok_construction = false;
337
						//test si tous les batiments sont construits et on le niveau nécéssaire
338
						for ($j = 0 ; $j < count($pour_construire) ; $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

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