MissionsAleatoire::setTerminerMissions()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
cc 5
eloc 25
nc 4
nop 0
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 MissionsAleatoire {
10
		private $last_check_mission;
11
		
12
		
13
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
14
		/**
15
		 * MissionsAleatoire constructor.
16
		 * le constructeur s'occupe de vérifier le last_check des missions et au cas ou si il est plus vieux d'un jour
17
		 * appeler la fonction pour recharger les missions
18
		 */
19
		public function __construct() {
20
			$dbc = App::getDb();
21
			
22
			$query = $dbc->select("last_check_mission")->from("_bataille_base")->where("ID_base", "=", Bataille::getIdBase())->get();
23
			
24
			if (is_array($query) && (count($query) == 1)) {
25
				foreach ($query as $obj) {
26
					$this->last_check_mission = $obj->last_check_mission;
27
				}
28
				
29
				if ($this->last_check_mission == "") {
30
					$this->setUpdateLastCheckMissions();
31
					$this->setMissionsAleatoire();
32
				}
33
				else {
34
					$today = Bataille::getToday();
35
					$interval = $today-$this->last_check_mission;
36
					
37
					if ($interval >= 10800) {
38
						$this->setUpdateLastCheckMissions();
39
						$this->setMissionsAleatoire();
40
					}
41
				}
42
			}
43
			
44
			$this->getNbMissions();
45
			Bataille::setValues(["next_check_missions" => ($this->last_check_mission+10800)-Bataille::getToday()]);
46
			
47
			$this->getMissionsCours();
48
		}
49
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
50
		
51
		
52
		
53
		//-------------------------- GETTER ----------------------------------------------------------------------------//
54
		/**
55
		 * fonction qui récupere tous les types de missions et les return dans un array
56
		 */
57
		private function getTypeMission() {
58
			return explode(",", Bataille::getParam("type_missions"));
59
		}
60
		
61
		/**
62
		 * @return int
63
		 * renvoi le nombre de missions encore disponibles dans la base
64
		 */
65
		private function getNbMissions() {
66
			$dbc = App::getDb();
67
			
68
			$query = $dbc->select("ID_mission_aleatoire")->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->get();
69
			
70
			if ((is_array($query)) && (count($query))) {
71
				foreach ($query as $obj) {
72
					$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...
73
				}
74
				
75
				$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...
76
				Bataille::setValues([
77
					"nb_missions" => $count
78
				]);
79
				
80
				return $count;
81
			}
82
		}
83
		
84
		/**
85
		 * @param $id_mission
86
		 * @return mixed
87
		 * fonction qui récupère la durée d'une mission
88
		 */
89
		private function getTempsMission($id_mission) {
90
			$dbc1 = Bataille::getDb();
91
			
92
			$query = $dbc1->select("duree")->from("mission")->where("ID_mission", "=", $id_mission)->get();
93
			
94
			if ((is_array($query)) && (count($query))) {
95
				foreach ($query as $obj) {
96
					return $obj->duree;
97
				}
98
			}
99
		}
100
		
101
		/**
102
		 * récupères les missions encore disponible dans la base
103
		 */
104
		public function getMissions() {
105
			$dbc = App::getDb();
106
			
107
			$query = $dbc->select()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->get();
108
			
109
			if ((is_array($query)) && (count($query))) {
110
				foreach ($query as $obj) {
111
					$missions[] = $this->getInfosMission($obj->ID_mission);
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...
112
				}
113
				
114
				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...
115
			}
116
		}
117
		
118
		/**
119
		 * @param $id_mission
120
		 * @return array
121
		 * pour récupérer les infos d'une mission dans la bdd _core
122
		 */
123
		private function getInfosMission($id_mission) {
124
			$dbc1 = Bataille::getDb();
125
			$query1 = $dbc1->select()->from("mission")->where("ID_mission", "=", $id_mission)->get();
126
			
127
			if ((is_array($query1)) && (count($query1) > 0)) {
128
				foreach ($query1 as $obj) {
129
					return [
130
						"id_mission" => $obj->ID_mission,
131
						"nom_mission" => $obj->nom_mission,
132
						"description" => $obj->description,
133
						"type" => $obj->type,
134
						"ressource_gagnee" => $obj->ressource_gagnee,
135
						"pourcentage_perte" => $obj->pourcentage_perte,
136
						"duree" => DateHeure::Secondeenheure($obj->duree)
137
					];
138
				}
139
			}
140
		}
141
		
142
		/**
143
		 * fonction qui récupère les missions qui sont en cours dans la base
144
		 */
145
		private function getMissionsCours() {
146
			$dbc = App::getDb();
147
			
148
			$query = $dbc->select()->from("_bataille_missions_cours")->where("ID_base", "=", Bataille::getIdBase())->get();
149
			
150
			if ((is_array($query)) && (count($query) > 0)) {
151
				foreach ($query as $obj) {
152
					$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...
153
						"id_missions_cours" => $obj->ID_missions_cours,
154
						"date_fin" => $obj->date_fin-Bataille::getToday(),
155
						"infos" => $this->getInfosMission($obj->ID_mission)
156
					];
157
				}
158
				
159
				Bataille::setValues(["missions_cours" => $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...
160
			}
161
		}
162
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
163
		
164
		
165
		
166
		//-------------------------- SETTER ----------------------------------------------------------------------------//
167
		/**
168
		 * fonction qui met a jour le last_ckeck_missions dans _bataille_base
169
		 * le met à la date du jour
170
		 */
171
		private function setUpdateLastCheckMissions() {
172
			$dbc = App::getDb();
173
			
174
			$dbc->update("last_check_mission", Bataille::getToday())
175
				->from("_bataille_base")
176
				->where("ID_base", "=", Bataille::getIdBase())
177
				->set();
178
			
179
			$this->last_check_mission = Bataille::getToday();
180
		}
181
		
182
		/**
183
		 * @param $id_mission
184
		 * fonction qui retire une mission de la liste des missions aleatoire des qu'on la lance
185
		 */
186
		private function setDeleteMission($id_mission) {
187
			$dbc = App::getDb();
188
			
189
			$dbc->delete()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase(), "AND")
190
				->where("ID_mission", "=", $id_mission)->del();
191
		}
192
		
193
		/**
194
		 * @param $type
195
		 * fonction qui recupere des missions aleatoirement de chaque type et qui les ajoute
196
		 * dans la table _bataille_mission_aleatoire
197
		 */
198
		private function setMissionsAleatoire() {
199
			$dbc = App::getDb();
200
			$dbc1 = Bataille::getDb();
201
			
202
			$dbc->delete()->from("_bataille_mission_aleatoire")->where("ID_base", "=", Bataille::getIdBase())->del();
203
			
204
			$type_missions = $this->getTypeMission();
205
			
206
			foreach ($type_missions as $un_type) {
207
				$query = $dbc1->select()->from("mission")
208
					->where("type", "=", $un_type)
209
					->orderBy("RAND()")
210
					->limit(0, 3)
211
					->get();
212
				
213
				if ((is_array($query)) && (count($query))) {
214
					foreach ($query as $obj) {
215
						$dbc->insert("ID_mission", $obj->ID_mission)
216
							->insert("ID_base", Bataille::getIdBase())
217
							->into("_bataille_mission_aleatoire")
218
							->set();
219
					}
220
				}
221
			}
222
		}
223
		
224
		/**
225
		 * @param $id_mission
226
		 * @param $nombre_unite
227
		 * @param $nom_unite
228
		 * @param $type_unite
229
		 * @param null $id_groupe
230
		 * @return bool
231
		 * fonction sert a lancer une mission
232
		 */
233
		public function setCommencerMission($id_mission, $nombre_unite, $nom_unite, $type_unite, $id_groupe = null) {
234
			$dbc = App::getDb();
235
			
236
			if ((array_sum($nombre_unite) == 0) && (array_sum($id_groupe) == 0)) {
237
				FlashMessage::setFlash("Pas assez d'unité pour effectuer cette missions");
238
				return false;
239
			}
240
			
241
			$dbc->insert("date_fin", $this->getTempsMission($id_mission)+Bataille::getToday())
242
				->insert("ID_base", Bataille::getIdBase())
243
				->insert("ID_mission", $id_mission)
244
				->into("_bataille_missions_cours")
245
				->set();
246
			
247
			$id_missions_cours = $dbc->lastInsertId();
248
			
249
			$count = count($nombre_unite);
250
			
251
			for ($i=0 ; $i<$count ; $i++) {
252
				Bataille::getUnite()->setCommencerExpedition($nombre_unite[$i], $nom_unite[$i], $type_unite[$i], $id_missions_cours);
253
			}
254
			
255
			$count = count($id_groupe);
256
			
257
			for ($i=0 ; $i<$count ; $i++) {
258
				Bataille::getGoupeUnite()->setCommencerExpedition($id_groupe[$i], $id_missions_cours);
259
			}
260
			
261
			$this->setDeleteMission($id_mission);
262
		}
263
		
264
		/**
265
		 * fonctin qui termine les missions en cours et qui ajoutera les ressources + les points
266
		 * et qui au cas ou pourra tuer des inités
267
		 */
268
		public function setTerminerMissions() {
269
			$dbc = App::getDb();
270
			
271
			$query = $dbc->select()->from("_bataille_missions_cours")
272
				->where("date_fin", "<=", Bataille::getToday(), "AND")
273
				->where("ID_base", "=", Bataille::getIdBase())
274
				->get();
275
			
276
			if ((is_array($query)) && (count($query))) {
277
				foreach ($query as $obj) {
278
					$infos_missions = $this->getInfosMission($obj->ID_mission);
279
					
280
					$unites_envoyees = Bataille::getUnite()->getUnitesMission($obj->ID_missions_cours);
281
					$unite_revenu = Bataille::getUnite()->setTerminerExpedition($obj->ID_missions_cours, $infos_missions["pourcentage_perte"]);
282
					
283
					if ($infos_missions["type"] == "nourriture") {
284
						Bataille::getRessource()->setUpdateRessource(0, 0, 0, 0, $infos_missions["ressource_gagnee"]*$unite_revenu, "+");
285
					}
286
					
287
					$dbc->delete()->from("_bataille_missions_cours")
288
						->where("ID_base", "=", Bataille::getIdBase(), "AND")
289
						->where("ID_mission", "=", $obj->ID_mission)
290
						->del();
291
					
292
					Bataille::getGoupeUnite()->setTerminerExpedition($obj->ID_missions_cours);
293
					
294
					//génération du rapport de mission
295
					$titre = "Rapport de la mission ".$infos_missions["nom_mission"];
296
					$infos = [
297
						"mission" => $infos_missions,
298
						"unites_envoyees" => $unites_envoyees,
299
						"unites_revenues" => $unite_revenu,
300
						"ressource_gagnee" => $infos_missions["ressource_gagnee"]*$unite_revenu
301
					];
302
					GenerationRapport::setGenererRapport($titre, $infos, "mission");
303
				}
304
			}
305
		}
306
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
307
		
308
	}