Passed
Push — master ( da4356...c64d93 )
by Anthony
02:38
created

MissionsAleatoire::setCommencerMission()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 17
nc 2
nop 4
1
<?php
2
	namespace modules\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	use core\functions\DateHeure;
7
	
8
	class MissionsAleatoire {
9
		private $last_check_mission;
10
		
11
		
12
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
13
		/**
14
		 * MissionsAleatoire constructor.
15
		 * le constructeur s'occupe de vérifier le last_check des missions et au cas ou si il est plus vieux d'un jour
16
		 * appeler la fonction pour recharger les missions
17
		 */
18
		public function __construct() {
19
			$dbc = App::getDb();
20
			
21
			$query = $dbc->select("last_check_mission")->from("_bataille_base")->where("ID_base", "=", Bataille::getIdBase())->get();
22
			
23
			if (is_array($query) && (count($query) == 1)) {
24
				foreach ($query as $obj) {
25
					$this->last_check_mission = $obj->last_check_mission;
26
				}
27
				
28
				if ($this->last_check_mission == "") {
29
					$this->setUpdateLastCheckMissions();
30
					$this->setMissionsAleatoire();
31
				}
32
				else {
33
					$today = Bataille::getToday();
34
					$interval = $today-$this->last_check_mission;
35
					
36
					if ($interval >= 10800) {
37
						$this->setUpdateLastCheckMissions();
38
						$this->setMissionsAleatoire();
39
					}
40
				}
41
			}
42
			
43
			$this->getNbMissions();
44
			Bataille::setValues(["next_check_missions" => ($this->last_check_mission+10800)-Bataille::getToday()]);
45
		}
46
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
47
		
48
		
49
		
50
		//-------------------------- GETTER ----------------------------------------------------------------------------//
51
		/**
52
		 * fonction qui récupere tous les types de missions et les return dans un array
53
		 */
54
		private function getTypeMission() {
55
			return explode(",", Bataille::getParam("type_missions"));
56
		}
57
		
58
		/**
59
		 * @return int
60
		 * renvoi le nombre de missions encore disponibles dans la base
61
		 */
62
		private function getNbMissions() {
63
			$dbc = App::getDb();
64
			
65
			$query = $dbc->select("ID_mission_aleatoire")->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->get();
66
			
67
			if ((is_array($query)) && (count($query))) {
68
				foreach ($query as $obj) {
69
					$id[] = $obj->ID_base;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$id was never initialized. Although not strictly required by PHP, it is generally a good practice to add $id = 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...
70
				}
71
				
72
				$count = count($id);
0 ignored issues
show
Bug introduced by
The variable $id 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...
73
				Bataille::setValues([
74
					"nb_missions" => $count
75
				]);
76
				
77
				return $count;
78
			}
79
		}
80
		
81
		private function getTempsMission($id_mission) {
82
			$dbc1 = Bataille::getDb();
83
			
84
			$query = $dbc1->select("duree")->from("mission")->where("ID_mission", "=", $id_mission)->get();
85
			
86
			if ((is_array($query)) && (count($query))) {
87
				foreach ($query as $obj) {
88
					return $obj->duree;
89
				}
90
			}
91
		}
92
		
93
		/**
94
		 * récupères les missions encore disponible dans la base
95
		 */
96
		public function getMissions() {
97
			$dbc = App::getDb();
98
			$dbc1 = Bataille::getDb();
99
			
100
			$query = $dbc->select()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->get();
101
			
102
			if ((is_array($query)) && (count($query))) {
103
				foreach ($query as $obj) {
104
					$query1 = $dbc1->select()->from("mission")->where("ID_mission", "=", $obj->ID_mission)->get();
105
					
106
					if ((is_array($query1)) && (count($query1))) {
107
						foreach ($query1 as $obj) {
108
							$missions[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$missions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $missions = 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...
109
								"id_mission" => $obj->ID_mission,
110
								"nom_mission" => $obj->nom_mission,
111
								"description" => $obj->description,
112
								"points_gagne" => $obj->points_gagne,
113
								"type" => $obj->type,
114
								"ressource_gagnee" => $obj->ressource_gagnee,
115
								"pourcentage_perte" => $obj->pourcentage_perte,
116
								"duree" => DateHeure::Secondeenheure($obj->duree)
117
							];
118
						}
119
					}
120
				}
121
				
122
				Bataille::setValues(["missions" => $missions]);
0 ignored issues
show
Bug introduced by
The variable $missions 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...
123
			}
124
		}
125
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
126
		
127
		
128
		
129
		//-------------------------- SETTER ----------------------------------------------------------------------------//
130
		/**
131
		 * fonction qui met a jour le last_ckeck_missions dans _bataille_base
132
		 * le met à la date du jour
133
		 */
134
		private function setUpdateLastCheckMissions() {
135
			$dbc = App::getDb();
136
			
137
			$dbc->update("last_check_mission", Bataille::getToday())
138
				->from("_bataille_base")
139
				->where("ID_base", "=", Bataille::getIdBase())
140
				->set();
141
			
142
			$this->last_check_mission = Bataille::getToday();
143
		}
144
		
145
		/**
146
		 * @param $type
147
		 * fonction qui recupere des missions aleatoirement de chaque type et qui les ajoute
148
		 * dans la table _bataille_mission_aleatoire
149
		 */
150
		private function setMissionsAleatoire() {
151
			$dbc = App::getDb();
152
			$dbc1 = Bataille::getDb();
153
			
154
			$dbc->delete()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->del();
155
			
156
			$type_missions = $this->getTypeMission();
157
			
158
			foreach ($type_missions as $un_type) {
159
				$query = $dbc1->select()->from("mission")
160
					->where("type", "=", $un_type)
161
					->orderBy("RAND()")
162
					->limit(0, 3)
163
					->get();
164
				
165
				if ((is_array($query)) && (count($query))) {
166
					foreach ($query as $obj) {
167
						$dbc->insert("ID_mission", $obj->ID_mission)
168
							->insert("ID_base", Bataille::getIdBase())
169
							->into("_bataille_mission_aleatoire")
170
							->set();
171
					}
172
				}
173
			}
174
		}
175
		
176
		/**
177
		 * @param $id_mission
178
		 * @param $nombre_unite
179
		 * @param $nom_unite
180
		 * @param $type_unite
181
		 * fonction sert a lancer une mission
182
		 */
183
		public function setCommencerMission($id_mission, $nombre_unite, $nom_unite, $type_unite) {
184
			$dbc = App::getDb();
185
			
186
			$dbc->insert("date_fin", $this->getTempsMission($id_mission)+Bataille::getToday())
187
				->insert("ID_base", Bataille::getIdBase())
188
				->insert("ID_mission", $id_mission)
189
				->into("_bataille_missions_cours")
190
				->set();
191
			
192
			$id_missions_cours = $dbc->lastInsertId();
0 ignored issues
show
Unused Code introduced by
$id_missions_cours is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
193
			
194
			$count = count($nombre_unite);
195
			for ($i=0 ; $i<$count ; $i++) {
196
				$new_tab[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$new_tab was never initialized. Although not strictly required by PHP, it is generally a good practice to add $new_tab = 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...
197
					"nombre_unite" =>  $nombre_unite[$i],
198
					"nom_unite" =>  $nom_unite[$i],
199
					"type_unite" =>  $type_unite[$i]
200
				];
201
			}
202
			
203
			echo("<pre>");
204
			print_r($new_tab);
0 ignored issues
show
Bug introduced by
The variable $new_tab 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...
205
			echo("</pre>");
206
		}
207
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
208
		
209
	}