Passed
Push — master ( 1252ce...732591 )
by Anthony
02:44
created

Batiment::getInfoBatimentNext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
	namespace modules\bataille\app\controller;
3
	use core\App;
4
	use core\functions\ChaineCaractere;
5
	use core\HTML\flashmessage\FlashMessage;
6
	use Nette\Utils\DateTime;
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
		private function getNiveauBatiment($nom_batiment_sql) {
72
			$dbc = App::getDb();
73
74
			$query = $dbc->select("niveau")
75
				->select("construction")
76
				->from("_bataille_batiment")
77
				->where("nom_batiment_sql", "=", $nom_batiment_sql, "AND")
78
				->where("ID_base", "=", Bataille::getIdBase())
79
				->get();
80
81
			if ((is_array($query)) && (count($query) > 0)) {
82
				foreach ($query as $obj) {
83
					if ($obj->construction == 1) {
84
						return $obj->niveau-1;
85
					}
86
87
					return $obj->niveau;
88
				}
89
			}
90
			else {
91
				return 0;
92
			}
93
		}
94
95
		/**
96
		 * @param $ressource
97
		 * @return int
98
		 * recuperation de la production de chaque ressource en fonction du lvl des batiments + recup prod addon
99
		 */
100
		public function getProduction($ressource) {
101
			$dbc1 = Bataille::getDb();
102
103
			if ($ressource == "eau") $nom_batiment = "centrale_eau";
104
			if ($ressource == "electricite") $nom_batiment = "centrale_electrique";
105
			if ($ressource == "fuel") $nom_batiment = "station_pompage_fuel";
106
			if ($ressource == "fer") $nom_batiment = "station_forage";
107
108
			$niveau = $this->getNiveauBatiment($nom_batiment);
0 ignored issues
show
Bug introduced by
The variable $nom_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...
109
110
			if ($niveau > 0) {
111
				$query = $dbc1->select("production")->from("$nom_batiment")->where("ID_".$nom_batiment, "=", $niveau)->get();
112
113
				if ((is_array($query)) && (count($query) > 0)) {
114
					foreach ($query as $obj) {
115
						$prod = $obj->production;
116
					}
117
118
					$query = $dbc1->select("production")
119
						->from($nom_batiment."_addon")
120
						->where("ID_".$nom_batiment."_addon", "=", $this->getNiveauBatiment($nom_batiment."_addon"))
121
						->get();
122
123
					if ((is_array($query)) && (count($query) > 0)) {
124
						foreach ($query as $obj) {
125
							$prod_addon = $obj->production;
126
						}
127
					}
128
					else {
129
						$prod_addon = 0;
130
					}
131
132
					return $prod + $prod_addon;
0 ignored issues
show
Bug introduced by
The variable $prod does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $prod_addon 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...
133
				}
134
			}
135
			else {
136
				return 20;
137
			}
138
		}
139
140
		/**
141
		 * @return int
142
		 * fonction qui retourne le stockage de l'entrepot
143
		 */
144
		public function getStockageEntrepot() {
145
			$dbc1 = Bataille::getDb();
146
147
			$niveau = $this->getNiveauBatiment("entrepot");
148
149
			if ($niveau > 0) {
150
				$query = $dbc1->select("stockage")->from("entrepot")->where("ID_entrepot", "=", $niveau)->get();
151
152 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...
153
					foreach ($query as $obj) {
154
						return $obj->stockage;
155
					}
156
				}
157
			}
158
			else {
159
				return 1000;
160
			}
161
		}
162
163
		/**
164
		 * permet de récupérer toutes les infos d'un batiment dans la popup
165
		 * @param $nom_batiment
166
		 * @param $emplacement
167
		 */
168
		public function getUnBatiment($nom_batiment, $emplacement) {
169
			$dbc = App::getDb();
170
			$dbc1 = Bataille::getDb();
171
172
			$construction = $this->getTestBatimentConstruction($nom_batiment);
173
174
			//recuperation des infos du batiment
175
			$query = $dbc->select()
176
				->from("_bataille_batiment")
177
				->where("nom_batiment", "=", $construction[0], "AND")
178
				->where("emplacement", "=", $emplacement, "AND")
179
				->where("ID_base", "=", Bataille::getIdBase())
180
				->get();
181
182
			if ((is_array($query)) && (count($query) > 0)) {
183
				foreach ($query as $obj) {
184
					$this->nom_batiment_sql = $obj->nom_batiment_sql;
185
					$this->niveau_batiment = $obj->niveau;
186
					$this->id_batiment = $obj->ID_batiment;
187
				}
188
189
				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...
190
					$this->niveau_batiment = $this->niveau_batiment + 1;
191
				}
192
				$niveau_batiment_base = $this->niveau_batiment;
193
				$max_level =  $this->getInfoUpgradeBatiment();
194
			}
195
			else {
196
				//on test voir si le bat est au niveau max et si il peut avoir un addon
197
				if (ChaineCaractere::FindInString($nom_batiment, "addon")) {
198
					$query = $dbc1->select("nom_table")->from("liste_batiment")->where("nom", "=", $nom_batiment)->get();
199
200
					if ((is_array($query)) && (count($query) > 0)) {
201
						foreach ($query as $obj) {
202
							$this->nom_batiment_sql = $obj->nom_table;
203
						}
204
205
						$this->niveau_batiment = 0;
206
207
						$max_level = $this->getInfoUpgradeBatiment();
208
						$niveau_batiment_base = 10;
209
					}
210
					else {
211
						$max_level = 0;
212
					}
213
				}
214
				else {
215
					$max_level = 0;
216
				}
217
			}
218
219
			//permet de savoir si le batiment produit bien des ressoures
220
			$batiment_production = [];
221
222
			$query = $dbc1->select("nom")->from("liste_batiment")->where("type", "=", "ressource")->get();
223
224
			if ((is_array($query)) && (count($query) > 0)) {
225
				foreach ($query as $obj) {
226
					$batiment_production[] = $obj->nom;
227
				}
228
			}
229
230
			Bataille::setValues([
231
				"nom_batiment_sql" => $this->nom_batiment_sql,
232
				"niveau_batiment_base" => $niveau_batiment_base,
0 ignored issues
show
Bug introduced by
The variable $niveau_batiment_base 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...
233
				"niveau_batiment" => $this->niveau_batiment,
234
				"id_batiment" => $this->niveau_batiment,
235
				"max_level" => $max_level,
236
				"batiment_production" => $batiment_production
237
			]);
238
239
			return $max_level;
240
		}
241
242
		/**
243
		 * pour récupérer la construction en cours dans la base
244
		 */
245
		public function getConstruction() {
246
			$dbc = App::getDb();
247
248
			$today = new DateTime();
249
			$today = $today->getTimestamp();
250
251
			$query = $dbc->select()
252
				->from("_bataille_construction")
253
				->from("_bataille_batiment")
254
				->where("_bataille_construction.ID_base", "=", Bataille::getIdBase(), "AND")
255
				->where("_bataille_construction.ID_batiment", "=", "_bataille_batiment.ID_batiment", "AND", true)
256
				->where("_bataille_construction.ID_base", "=", "_bataille_batiment.ID_base", "", true)
257
				->get();
258
259
			if ((is_array($query)) && (count($query) > 0)) {
260
				foreach ($query as $obj) {
261
					$this->nom_batiment_construction = $obj->nom_batiment;
262
					$this->date_fin_construction = $obj->date_fin;
263
					$this->niveau_batiment_construction = $obj->niveau;
264
					$id_batiment = $obj->ID_batiment;
265
				}
266
267
				if ($this->date_fin_construction-$today <= 0) {
268
					$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...
269
				}
270
				else {
271
					Bataille::setValues([
272
						"date_fin_construction" => $this->date_fin_construction-$today,
273
						"nom_batiment_construction" => $this->nom_batiment_construction
274
					]);
275
				}
276
277
				return 1;
278
			}
279
280
			return 0;
281
		}
282
283
		/**
284
		 * pour récupérer la liste des batiments qu'il est possible de construire
285
		 */
286
		public function getBatimentAConstruire() {
287
			$dbc = App::getDb();
288
			$dbc1 = Bataille::getDb();
289
			$batiment_construit = [];
290
			$batiment_construire = [];
291
292
			//recuperation des batiments deja construit dans la base
293
			$query = $dbc->select("nom_batiment_sql")
294
				->select("nom_batiment")
295
				->select("niveau")
296
				->from("_bataille_batiment")
297
				->where("ID_base", "=", Bataille::getIdBase())
298
				->get();
299
300
			if ((is_array($query)) && (count($query) > 0)) {
301
				foreach ($query as $obj) {
302
					$batiment_construit[] = $obj->nom_batiment_sql;
303
				}
304
			}
305
306
			//recuperation de la liste complete des batiments
307
			$query = $dbc1->select("nom_table")->select("nom")->from("liste_batiment")->where("actif", "=", 1)->get();
308
			if ((is_array($query)) && (count($query) > 0)) {
309
				foreach ($query as $obj) {
310
					$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...
311
					$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...
312
				}
313
314
				$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...
315
			}
316
317
			//boucle qui recupere en tableau le champ pour_construire d'un batiment
318
			//et compare la liste des batiments qu'il faut pour construire le batiment
319
			//a ceux qui sont deja construit dans la base
320
			//si tous les batments qu'il faut son batis on autorise la construction du batiment
321
			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...
322
				if (!in_array($all_batiment[$i], $batiment_construit)) {
323
					$query = $dbc1->select("pour_construire")
324
						->select("temps_construction")
325
						->from($all_batiment[$i])
326
						->where("ID_".$all_batiment[$i], "=", 1)
327
						->get();
328
329
					if ((is_array($query)) && (count($query) > 0)) {
330
						foreach ($query as $obj) {
331
							if ($obj->pour_construire != null) {
332
								$pour_construire = unserialize($obj->pour_construire);
333
							}
334
							else {
335
								$pour_construire = [];
336
							}
337
338
339
							$temps_construction = gmdate("H:i:s", $obj->temps_construction);
340
						}
341
					}
342
343
344
					if (count($pour_construire) == 1) {
345
						if (in_array($pour_construire[0][1], $batiment_construit)) {
346
							if ($pour_construire[0][2] <= $this->getNiveauBatiment($pour_construire[0][1])) {
0 ignored issues
show
Bug introduced by
The variable $pour_construire 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...
347
								$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0);
348
349
								$batiment_construire[] = [
350
									"nom_batiment_sql" => $all_batiment[$i],
351
									"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...
352
									"ressource" => $ressource,
353
									"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...
354
								];
355
							}
356
						}
357
					}
358
					else if (count($pour_construire) > 1) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
359
						//TODO si besoin de plus d'un seul batiment pour etre construit
360
					}
361
					else {
362
						$ressource = $this->getRessourceConstruireBatiment($all_batiment[$i], 0);
363
364
						$batiment_construire[] = [
365
							"nom_batiment_sql" => $all_batiment[$i],
366
							"nom_batiment" => $all_batiment_nom[$i],
367
							"ressource" => $ressource,
368
							"temps_construction" => $temps_construction
369
						];
370
					}
371
				}
372
			}
373
			Bataille::setValues(["batiments" => $batiment_construire]);
374
		}
375
376
		/**
377
		 * @param $nom_batiment_sql
378
		 * @param $niveau
379
		 * @return array
380
		 * recuperation des ressources nécéssaire pour construire le batiment
381
		 */
382
		private function getRessourceConstruireBatiment($nom_batiment_sql, $niveau) {
383
			$dbc1 = Bataille::getDb();
384
385
			$niveau = $niveau+1;
386
387
			$query = $dbc1->select("ressource_construire")->from($nom_batiment_sql)->where("ID_".$nom_batiment_sql, "=", $niveau)->get();
388
389
			if ((is_array($query)) && (count($query) > 0)) {
390
				foreach ($query as $obj) {
391
					$ressource_tmp = $obj->ressource_construire;
392
				}
393
				$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...
394
395
				//on test si assez de ressources dans la base
396
				//fer
397
				$ressource["fer"] = Bataille::getTestAssezRessourceBase("fer", $ressource_tmp[0]);
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...
398
				//fuel
399
				$ressource["fuel"] = Bataille::getTestAssezRessourceBase("fuel", $ressource_tmp[1]);
400
				//eau
401
				$ressource["eau"] = Bataille::getTestAssezRessourceBase("eau", $ressource_tmp[2]);
402
				//electricite
403
				$ressource["electricite"] = Bataille::getTestAssezRessourceBase("electricite", $ressource_tmp[3]);
404
405
				return $ressource;
406
			}
407
		}
408
409
		/**
410
		 * fonction qui renvoi un tableau avec le nom du batiment sans (en construction)
411
		 * + true pour dire que le batiment est en construction
412
		 *
413
		 * @param $nom_batiment
414
		 * @return array
415
		 */
416
		private function getTestBatimentConstruction($nom_batiment) {
417
			if (ChaineCaractere::FindInString($nom_batiment, " en construction") == true) {
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...
418
				return [substr($nom_batiment, 0, (0-strlen(" en construction"))), true];
419
			}
420
421
			return [$nom_batiment, false];
422
		}
423
424
		/**
425
		 * fonction qui renvoi les informations pour augmenter le niveau d'un batiment
426
		 * ou la création d'un addon sur ce batiment
427
		 * @return int
428
		 */
429
		private function getInfoUpgradeBatiment() {
430
			$dbc1 = Bataille::getDb();
431
432
			//récupération du temps et des ressources pour construire
433
			$query = $dbc1->select()->from($this->nom_batiment_sql)->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)->get();
434
435
			//si on a quelque chose cela veut dire qu'on est pas encore au lvl max du batiment
436
			if ((is_array($query)) && (count($query) > 0)) {
437
				foreach ($query as $obj) {
438
					$this->ressource_construire = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
439
					$this->temps_construction = gmdate("H:i:s", $obj->temps_construction);
440
				}
441
442
				//récupération des éléments particulier à un batiment
443
				if ($this->nom_batiment_sql == "entrepot") {
444
					$query = $dbc1->select("stockage")->from("entrepot")->where("ID_entrepot", "=", $this->niveau_batiment)->get();
445
					
446 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...
447
						foreach ($query as $obj) {
448
							$this->info_batiment = "Capacité de l'entrepôt : ". $obj->stockage;
449
						}
450
					}
451
					
452
					$query = $dbc1->select("stockage")->from("entrepot")->where("ID_entrepot", "=", $this->niveau_batiment+1)->get();
453
454 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...
455
						foreach ($query as $obj) {
456
							$this->info_batiment_next = "Capacité de l'entrepôt au prochain niveau : ". $obj->stockage;
457
						}
458
					}
459
				}
460
461
				Bataille::setValues([
462
					"ressource" => $this->ressource_construire,
463
					"temps_construction" => $this->temps_construction,
464
					"info_batiment" => $this->info_batiment,
465
					"info_batiment_next" => $this->info_batiment_next,
466
				]);
467
468
				return 1;
469
			}
470
471
			return 0;
472
		}
473
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
474
475
476
477
		//-------------------------- SETTER ----------------------------------------------------------------------------//
478
		/**
479
		 * fonction qui initialise la construction d'un batiment
480
		 * @param $nom_batiment
481
		 * @param $nom_batiment_sql
482
		 * @param $emplacement
483
		 */
484
		public function setCommencerConstruireBatiment($nom_batiment, $nom_batiment_sql, $emplacement) {
485
			$dbc = App::getDb();
486
			$dbc1 = Bataille::getDb();
487
488
			if (ChaineCaractere::FindInString($nom_batiment, "addon")) {
489
				$emplacement = 0;
490
			}
491
492
			if ($this->getConstruction() == 0) {
493
				$un_batiment = $this->getUnBatiment($nom_batiment, $emplacement);
494
495
				//on test si assez de ressrouce pour construire le batiment
496
				if ($un_batiment == 0) {
497
					$ressource = $this->getRessourceConstruireBatiment($nom_batiment_sql, 0);
498
					$this->nom_batiment_sql = $nom_batiment_sql;
499
					$this->niveau_batiment = 0;
500
				}
501
				else {
502
					//si c'est le lvl 0 de l'addon
503
					if ($this->niveau_batiment == 0) {
504
						$un_batiment = 0;
505
					}
506
					$ressource = $this->getRessourceConstruireBatiment($this->nom_batiment_sql, $this->niveau_batiment);
507
				}
508
509
				//si pas assez de ressource
510
				if (in_array("rouge", $ressource[0])) {
511
					FlashMessage::setFlash("Pas assez de ressources pour construire ce batiment");
512
				}
513
				else {
514
					//recuperation du temps de construction
515
					$query = $dbc1->select("ressource_construire")
516
						->select("temps_construction")
517
						->from($this->nom_batiment_sql)
518
						->where("ID_".$this->nom_batiment_sql, "=", $this->niveau_batiment+1)
519
						->get();
520
521
					foreach ($query as $obj) {
522
						$temps_construction = $obj->temps_construction;
523
						$ressource_construction = explode(", ", $obj->ressource_construire);
524
					}
525
526
					//on insere la construction dans la table batiment si new batiment
527
					if ($un_batiment == 0) {
528
						$dbc->insert("niveau", $this->niveau_batiment+1)
529
							->insert("emplacement", $emplacement)
530
							->insert("nom_batiment", $nom_batiment)
531
							->insert("nom_batiment_sql", $this->nom_batiment_sql)
532
							->insert("construction", 1)
533
							->insert("ID_base", Bataille::getIdBase())
534
							->into("_bataille_batiment")
535
							->set();
536
537
						$this->id_batiment = $dbc->lastInsertId();
538
					}
539
					else {
540
						$dbc->update("niveau", $this->niveau_batiment+1)
541
							->update("construction", 1)
542
							->from("_bataille_batiment")
543
							->where("ID_batiment", "=", $this->id_batiment, "AND")
544
							->where("ID_base", "=", Bataille::getIdBase())
545
							->set();
546
					}
547
548
549
					//on initialise la construction
550
					//recuperation de la date en seconde
551
					$today = new DateTime();
552
					$today = $today->getTimestamp();
553
554
					//date de la fin de la construction en seconde
555
					$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...
556
557
					$dbc->insert("date_fin", $fin_construction)
558
						->insert("emplacement_construction", $emplacement)
559
						->insert("ID_base", Bataille::getIdBase())
560
						->insert("ID_batiment", $this->id_batiment)
561
						->into("_bataille_construction")
562
						->set();
563
564
					//on retire les ressources de la base
565
					Bataille::getRessource()->setRetirerRessource($ressource_construction[2], $ressource_construction[3], $ressource_construction[0], $ressource_construction[1]);
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...
566
				}
567
			}
568
			else {
569
				FlashMessage::setFlash("Un batiment est déjà en construction, vous ne pouvez pas en construire un autre !");
570
			}
571
		}
572
573
		/**
574
		 * fonction qui termine la construction d'un batiment
575
		 * @param $id_batiment
576
		 */
577
		private function setTerminerConstruction($id_batiment) {
578
			$dbc = App::getDb();
579
580
			//on le retire de la table construction
581
			$dbc->delete()
582
				->from("_bataille_construction")
583
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
584
				->where("ID_batiment", "=", $id_batiment)
585
				->del();
586
587
			//on termine la construction dans la table batiment
588
			$dbc->update("construction", 0)
589
				->from("_bataille_batiment")
590
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
591
				->where("ID_batiment", "=", $id_batiment)
592
				->set();
593
		}
594
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
595
	}