Passed
Push — master ( 8038cd...1d6c2d )
by Anthony
02:56
created

Unite::getRecrutement()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 11
loc 11
rs 9.2
c 1
b 0
f 0
cc 4
eloc 5
nc 3
nop 0
1
<?php
2
	
3
	namespace modules\bataille\app\controller;
4
	
5
	
6
	use core\App;
7
	use core\functions\DateHeure;
8
	use core\HTML\flashmessage\FlashMessage;
9
10
	class Unite {
11
		private $coef_unite;
12
13
14
		
15
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
16 View Code Duplication
		public function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
17
			$dbc1 = Bataille::getDb();
18
19
			$query = $dbc1->select("coef_niveau_unite")->from("configuration")->where("ID_configuration", "=", 1)->get();
20
21
			if ((is_array($query)) && (count($query) == 1)) {
22
				foreach ($query as $obj) $this->coef_unite = $obj->coef_niveau_unite;
23
			}
24
		}
25
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
26
		
27
		
28
		//-------------------------- GETTER ----------------------------------------------------------------------------//
29
30
		/**
31
		 * @param $unite
32
		 * @param $niveau
33
		 * @param $type
34
		 * @return array
35
		 * récupère les caractéristiques de l'unité en fonction de son niveau
36
		 */
37
		private function getCaracteristiqueUnite($unite, $niveau, $type) {
38
			$dbc1 = Bataille::getDb();
39
40
			$query = $dbc1->select()
41
				->from("unites")
42
				->where("nom", "=", $unite, "AND")
43
				->where("type", "=", $type, "")
44
				->get();
45
46
			if ((is_array($query)) && (count($query) == 1)) {
47
				foreach ($query as $obj) {
48
					$base_carac = unserialize($obj->caracteristique);
49
					$ressource = unserialize($obj->pour_recruter);
50
					$temps_recrutement = DateHeure::Secondeenheure($obj->temps_recrutement);
51
				}
52
53
				$coef = $this->coef_unite*$niveau;
54
				$coef_ameliorer = $this->coef_unite*($niveau+1);
55
56
				if ($niveau == 1) $coef = 1;
57
58
				return [
59
					"caracteristique" => [
60
						"attaque" => round($base_carac["attaque"]*$coef),
0 ignored issues
show
Bug introduced by
The variable $base_carac 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...
61
						"defense" => round($base_carac["defense"]*$coef),
62
						"resistance" => round($base_carac["resistance"]*$coef),
63
						"vitesse" => $base_carac["vitesse"]
64
					],
65
					"cout_recruter" => [
66
						"eau" => $ressource["eau"]*$coef,
0 ignored issues
show
Bug introduced by
The variable $ressource 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...
67
						"electricite" => $ressource["electricite"]*$coef,
68
						"fer" => $ressource["fer"]*$coef,
69
						"fuel" => $ressource["fuel"]*$coef,
70
					],
71
					"cout_ameliorer" => [
72
						"eau" => $ressource["eau"]*$coef_ameliorer,
73
						"electricite" => $ressource["electricite"]*$coef_ameliorer,
74
						"fer" => $ressource["fer"]*$coef_ameliorer,
75
						"fuel" => $ressource["fuel"]*$coef_ameliorer,
76
					],
77
					"temps_recrutement" => $temps_recrutement
0 ignored issues
show
Bug introduced by
The variable $temps_recrutement 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...
78
				];
79
			}
80
			else {
81
				return [];
82
			}
83
		}
84
85
		/**
86
		 * @param $type
87
		 * fonction qui permet de récupérer les unités qu'i est possible de recruter en fonction
88
		 * du type (batiment sur lequel on a cliqué)
89
		 */
90
		public function getUnitePossibleRecruter($type) {
91
			//on recup toutes les unites deja recherchée donc que l'on peut faire
92
			$unites = Bataille::getCentreRecherche()->getAllRechercheType($type);
93
94
			//recupérer les caractéristiques de l'unité en question
95
			for ($i=0 ; $i<count($unites) ; $i++) {
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...
96
				$unites[$i] += $this->getCaracteristiqueUnite($unites[$i]["recherche"], $unites[$i]["niveau"], $type);
97
				$unites[$i] += ["type" => $type];
98
			}
99
100
			//si pas d'unites encore recherchees on renvoit un array juste avec 0 dedans
101
			Bataille::setValues(["unites" => $unites]);
102
		}
103
104
		/**
105
		 * fonction qui renvoi les unité  en cours de recrutement
106
		 */
107 View Code Duplication
		public function getRecrutement() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
108
			$dbc = App::getDb();
109
110
			$query = $dbc->select()->from("_bataille_recrutement")->where("ID_base", "=", Bataille::getIdBase())->get();
111
112
			if ((is_array($query)) && (count($query) > 0)) {
113
				foreach ($query as $obj) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops 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.

Consider removing the loop.

Loading history...
114
115
				}
116
			}
117
		}
118
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
119
		
120
		
121
		//-------------------------- SETTER ----------------------------------------------------------------------------//
122
		/**
123
		 * @param $nom -> nom de l'unité à recruter
124
		 * @param $type -> type de l'unité à recruter
125
		 * @param $nombre -> nombre d'unité à recruter
126
		 * fonction qui permet d'initialiser le début du recrutement d'unités
127
		 */
128
		public function setCommencerRecruter($nom, $type, $nombre) {
129
			$dbc1 = Bataille::getDb();
130
			$dbc = App::getDb();
131
132
			$query = $dbc1->select("temps_recrutement")
133
				->select("pour_recruter")
134
				->from("unites")
135
				->where("nom", "=", $nom, "AND")
136
				->where("type", "=", $type, "")
137
				->get();
138
139
			if ((is_array($query)) && (count($query) == 1)) {
140
				foreach ($query as $obj) {
141
					$pour_recruter = unserialize($obj->pour_recruter);
142
					$temps_recrutement = $obj->temps_recrutement;
143
				}
144
			}
145
146
			//on test si on a assez de ressource pour recruter les unites
147
			//on test si assez de ressources dans la base
148
			$retirer_eau = $pour_recruter["eau"]*$nombre;
0 ignored issues
show
Bug introduced by
The variable $pour_recruter 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...
149
			$retirer_electricite = $pour_recruter["electricite"]*$nombre;
150
			$retirer_fer = $pour_recruter["fer"]*$nombre;
151
			$retirer_fuel = $pour_recruter["fuel"]*$nombre;
152
			$eau = Bataille::getTestAssezRessourceBase("eau", $retirer_eau);
153
			$electricite = Bataille::getTestAssezRessourceBase("electricite", $retirer_electricite);
154
			$fer = Bataille::getTestAssezRessourceBase("fer", $retirer_fer);
155
			$fuel = Bataille::getTestAssezRessourceBase("fuel", $retirer_fuel);
156
157
158
			if (($eau["class"] || $electricite["class"] || $fer["class"] || $fuel["class"]) == "rouge" ) {
159
				FlashMessage::setFlash("Pas assez de ressources pour recruter autant d'unités");
160
				return false;
161
			}
162
			else {
163
				//on retire les ressources
164
				Bataille::getRessource()->setUpdateRessource($retirer_eau, $retirer_electricite, $retirer_fer, $retirer_fuel, 0, "-");
165
166
				$date_fin = Bataille::getToday()+($temps_recrutement*$nombre);
0 ignored issues
show
Bug introduced by
The variable $temps_recrutement 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...
167
168
				$dbc->insert("nom", $nom)
169
					->insert("type", $type)
170
					->insert("nombre", $nombre)
171
					->insert("date_fin", $date_fin)
172
					->insert("ID_base", Bataille::getIdBase())
173
					->into("_bataille_recrutement")
174
					->set();
175
176
				return true;
177
			}
178
		}
179
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
180
	}