Passed
Push — master ( b9668a...d3086a )
by Anthony
04:34
created

CentreRecherche   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 313
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 37
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 313
rs 8.6

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getNiveauRecherche() 0 18 4
A getCoutRecherche() 0 20 1
A getTempsRecherche() 0 9 2
B getAllRechercheType() 0 22 4
C getAllRecherche() 0 48 7
B getRecherche() 0 34 5
C setCommencerRecherche() 0 65 11
B setTerminerRecherche() 0 25 2
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	use core\functions\DateHeure;
7
	use core\HTML\flashmessage\FlashMessage;
8
9
	class CentreRecherche {
10
		private $coef_centre;
11
		private $recherche;
12
		private $type;
13
		
14
		
15
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
16
		public function __construct() {
17
			$this->coef_centre = Bataille::getParam("coef_centre_recherche");
18
		}
19
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
20
		
21
		
22
		
23
		//-------------------------- GETTER ----------------------------------------------------------------------------//
24
		/**
25
		 * @param $recherche
26
		 * @param $type
27
		 * @return int
28
		 * fonction qui va cehrcher le niveau de la recherche actuelle
29
		 * renvoi 0 si elle n'a pas été trouvée
30
		 */
31
		private function getNiveauRecherche($recherche, $type) {
32
			$dbc = App::getDb();
33
34
			$query = $dbc->select("niveau")
35
				->from("_bataille_centre_recherche")
36
				->where("ID_base", "=", Bataille::getIdBase(), "AND")
37
				->where("recherche", "=", $recherche, "AND")
38
				->where("type", "=", $type)
39
				->get();
40
41
			if ((is_array($query)) && (count($query) > 0)) {
42
				foreach ($query as $obj) {
43
					return $obj->niveau;
44
				}
45
			}
46
47
			return 0;
48
		}
49
50
		/**
51
		 * @param $cout
52
		 * @param integer $niveau_recherche
53
		 * @return array
54
		 * fonction qui renvoi le cout d'une recherche
55
		 */
56
		private function getCoutRecherche($cout, $niveau_recherche) {
57
			$cout_eau = $cout["eau"] * ($this->coef_centre * $niveau_recherche);
58
			$cout_eau = Bataille::getTestAssezRessourceBase("eau", $cout_eau);
59
			
60
			$cout_electricite = $cout["electricite"] * ($this->coef_centre * $niveau_recherche);
61
			$cout_electricite = Bataille::getTestAssezRessourceBase("electricite", $cout_electricite);
62
			
63
			$cout_fer = $cout["fer"] * ($this->coef_centre * $niveau_recherche);
64
			$cout_fer = Bataille::getTestAssezRessourceBase("fer", $cout_fer);
65
			
66
			$cout_fuel = $cout["fuel"] * ($this->coef_centre * $niveau_recherche);
67
			$cout_fuel = Bataille::getTestAssezRessourceBase("fuel", $cout_fuel);
68
			
69
			return [
70
				"eau" => $cout_eau,
71
				"electricite" => $cout_electricite,
72
				"fer" => $cout_fer,
73
				"fuel" => $cout_fuel
74
			];
75
		}
76
77
		/**
78
		 * @param $temps
79
		 * @param int $niveau
80
		 * @return double fonction qui renvoi le temps qu'il faut pour effectuer une recherche
81
		 */
82
		private function getTempsRecherche($temps, $niveau = 0) {
83
			$pourcent = ($temps*Bataille::getBatiment()->getNiveauBatiment("centre_recherche")/100);
84
85
			if ($niveau == 0) {
86
				return round($temps-$pourcent);;
87
			}
88
89
			return round(($temps * ($this->coef_centre * $niveau))-$pourcent);
90
		}
91
92
		/**
93
		 * @param $type
94
		 * @return array|int
95
		 * permet de renvoyer toutes es recherches déjà effectuées pour notre base en fonction
96
		 * d'un type donné
97
		 */
98
		public function getAllRechercheType($type) {
99
			$dbc = App::getDb();
100
			$recherche = [];
101
102
			$query = $dbc->select()->from("_bataille_centre_recherche")
103
				->where("type", "=", $type, "AND")
104
				->where("ID_base", "=", Bataille::getIdBase())
105
				->get();
106
107
			if ((is_array($query)) && (count($query) > 0)) {
108
				foreach ($query as $obj) {
109
					$recherche[] = [
110
						"niveau" => $obj->niveau,
111
						"recherche" => $obj->recherche
112
					];
113
				}
114
115
				return $recherche;
116
			}
117
118
			return 0;
119
		}
120
121
		/**
122
		 * fonction qui renvoi toutes les recherches effectuées ou non dans un tableau
123
		 * (ne renvoi que celle que l'on peut faire en fonction du niveau du centre)
124
		 */
125
		public function getAllRecherche() {
126
			$dbc1 = Bataille::getDb();
127
128
			//avant de récupérer toutes les recherches, on finit au cas celle en court
129
			if ($this->getRecherche() == false) {
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...
130
				$query = $dbc1->select()->from("recherche")
131
					->where("niveau_centre", "<=", Bataille::getBatiment()->getNiveauBatiment("centre_recherche"))
132
					->get();
133
134
				if ((is_array($query)) && (count($query) > 0)) {
135
					$recherche = [];
136
					foreach ($query as $obj) {
137
						$niveau = $this->getNiveauRecherche($obj->recherche, $obj->type);
138
						$niveau_recherche = $niveau;
139
140
						$cout = unserialize($obj->cout);
141
						$temps_recherche = $this->getTempsRecherche($obj->temps_recherche);
142
143
						//si niveau == 0 ca veut dire que la recherche n'a pas encore été effectuée dans la base
144
						if ($niveau > 0) {
145
							$temps_recherche = $this->getTempsRecherche($temps_recherche, $niveau);
146
						}
147
						else {
148
							$niveau_recherche = 1;
149
						}
150
						
151
						$cout = $this->getCoutRecherche($cout, $niveau_recherche);
152
						
153
						if ($obj->type == "infanterie") {
154
							$special = Bataille::getUnite()->getCaracteristiqueUnite($obj->recherche, $niveau_recherche, $obj->type);
155
						}
156
						
157
						$recherche[] = [
158
							"recherche" => $obj->recherche,
159
							"type" => $obj->type,
160
							"niveau" => $niveau,
161
							"max_level_recherche" => $obj->max_level,
162
							"cout" => $cout,
163
							"temps_recherche" => DateHeure::Secondeenheure($temps_recherche),
164
							"special" => $special,
0 ignored issues
show
Bug introduced by
The variable $special 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...
165
							"coef_amelioration" => Bataille::getParam("coef_niveau_unite")
166
						];
167
					}
168
				}
169
170
				Bataille::setValues(["centre_recherche" => $recherche]);
0 ignored issues
show
Bug introduced by
The variable $recherche 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...
171
			}
172
		}
173
174
		/**
175
		 * @return bool
176
		 * fonction qui renvoi un tableau contenant la recherche en cours si celle-ci n'est pas finie
177
		 * sinon elle appelle la fonction setTerminerRecherche
178
		 */
179
		public function getRecherche() {
180
			$dbc = App::getDb();
181
182
			$query = $dbc->select()->from("_bataille_recherche")->where("ID_base", "=", Bataille::getIdBase())->get();
183
184
			if ((is_array($query)) && (count($query) > 0)) {
185
				$today = Bataille::getToday();
186
187
				foreach ($query as $obj) {
188
					$this->recherche = $obj->recherche;
189
					$this->type = $obj->type;
190
191
					if ($obj->date_fin-$today <= 0) {
192
						$this->setTerminerRecherche($obj->ID_recherche);
193
194
						return false;
195
					}
196
					else {
197
						$recherche = [
198
							"recherche" => $obj->recherche,
199
							"type" => $obj->type,
200
							"date_fin_recherche" => $obj->date_fin-$today,
201
							"id_recherche" => $obj->ID_recherche
202
						];
203
					}
204
				}
205
206
				Bataille::setValues(["recherche" => $recherche]);
0 ignored issues
show
Bug introduced by
The variable $recherche 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...
207
208
				return true;
209
			}
210
211
			return false;
212
		}
213
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
214
		
215
		
216
		
217
		//-------------------------- SETTER ----------------------------------------------------------------------------//
218
		/**
219
		 * @param $recherche
220
		 * @param $type
221
		 * @return bool
222
		 * fonction qui va initialiser la recherche en question
223
		 */
224
		public function setCommencerRecherche($recherche, $type) {
225
			$dbc = App::getDb();
226
			$dbc1 = Bataille::getDb();
227
228
			//on test si il n'y a pas déjà une recherche en cours
229
			if ($this->getRecherche() == 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...
230
				FlashMessage::setFlash("Une recherche est déjà en cours, merci d'attendre la fin de celle-ci");
231
				return false;
232
			}
233
234
			//on récupère la recherche dans notre base savoir si on l'a déjà recherchée pour avoir son lvl
235
			$niveau_recherche = $this->getNiveauRecherche($recherche, $type);
236
237
			//récupération du cout initial plus temps de recherche initial pour calculer les bon en fonction
238
			//du lvl du centre + du niveau actuel de la recherche
239
			$query = $dbc1->select()
240
				->from("recherche")
241
				->where("recherche", "=", $recherche, "AND")
242
				->where("type", "=", $type)
243
				->get();
244
245
			if ((is_array($query)) && (count($query) == 1)) {
246
				foreach ($query as $obj) {
247
					$cout = unserialize($obj->cout);
248
					$temps_recherche = $obj->temps_recherche;
249
					$max_level_recherche = $obj->max_level;
250
				}
251
			}
252
			
253
			if ($niveau_recherche >= $max_level_recherche) {
0 ignored issues
show
Bug introduced by
The variable $max_level_recherche 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...
254
				FlashMessage::setFlash("Cette unité est déjà au niveau maxium");
255
				return false;
256
			}
257
258
			if ($niveau_recherche > 0) {
259
				$cout = $this->getCoutRecherche($cout, $niveau_recherche);
0 ignored issues
show
Bug introduced by
The variable $cout 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...
260
				$temps_recherche = $this->getTempsRecherche($temps_recherche, $niveau_recherche);
0 ignored issues
show
Bug introduced by
The variable $temps_recherche 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...
261
			}
262
263
			//on test si assez de ressources pour effectuer la recherche
264
			$eau = Bataille::getTestAssezRessourceBase("eau", $cout["eau"]["ressource"]);
265
			$electricite = Bataille::getTestAssezRessourceBase("electricite", $cout["electricite"]["ressource"]);
266
			$fer = Bataille::getTestAssezRessourceBase("fer", $cout["fer"]["ressource"]);
267
			$fuel = Bataille::getTestAssezRessourceBase("fuel", $cout["fuel"]["ressource"]);
268
269
270
			if (($eau["class"] || $electricite["class"] || $fer["class"] || $fuel["class"]) == "rouge" ) {
271
				FlashMessage::setFlash("Pas assez de ressources pour effectuer cette recherche");
272
				return false;
273
			}
274
			
275
			//on retire les ressources
276
			Bataille::getRessource()->setUpdateRessource($cout["eau"]["ressource"], $cout["electricite"]["ressource"], $cout["fer"]["ressource"], $cout["fuel"]["ressource"], 0, "-");
277
			
278
			$date_fin = Bataille::getToday()+$temps_recherche;
279
			
280
			$dbc->insert("recherche", $recherche)
281
				->insert("type", $type)
282
				->insert("date_fin", $date_fin)
283
				->insert("ID_base", Bataille::getIdBase())
284
				->into("_bataille_recherche")
285
				->set();
286
			
287
			return true;
288
		}
289
290
		/**
291
		 * @param $id_recherche
292
		 * fonction qui va terminer une recherche en fonction de son ID
293
		 */
294
		private function setTerminerRecherche($id_recherche) {
295
			$dbc = App::getDb();
296
			$niveau_recherche = $this->getNiveauRecherche($this->recherche, $this->type);
297
298
			if ($niveau_recherche == 0) {
299
				$dbc->insert("recherche", $this->recherche)
300
					->insert("type", $this->type)
301
					->insert("niveau", 1)
302
					->insert("ID_base", Bataille::getIdBase())
303
					->into("_bataille_centre_recherche")
304
					->set();
305
			}
306
			else {
307
				$dbc->update("niveau", $niveau_recherche+1)
308
					->from("_bataille_centre_recherche")
309
					->where("recherche", "=", $this->recherche, "AND")
310
					->where("type", "=", $this->type, "AND")
311
					->where("ID_base", "=", Bataille::getIdBase())
312
					->set();
313
			}
314
315
			$dbc->delete()->from("_bataille_recherche")->where("ID_recherche", "=", $id_recherche, "AND")
316
				->where("ID_base", "=", Bataille::getIdBase())
317
				->del();
318
		}
319
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
320
		
321
	}