Map::__construct()   D
last analyzed

Complexity

Conditions 13
Paths 101

Size

Total Lines 101
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 101
rs 4.9737
cc 13
eloc 77
nc 101
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
	namespace modules\bataille\app\controller;
4
5
	use core\App;
6
	use core\functions\DateHeure;
7
8
	class Map {
9
		private $largeur;
10
		private $hauteur;
11
12
13
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
14
		/**
15
		 * Map constructor.
16
		 * @param null $id_base
17
		 * @param null $install_base
18
		 * si id_base == null et install_base != null on renvoi true car on est sur l'install d'une base
19
		 * donc inutile de tout cahrger la map
20
		 */
21
		public function __construct($id_base = null, $install_base = null) {
22
			$dbc = App::getDb();
23
			$temps_trajet = "";
24
			$map = [];
25
26
			if ($install_base != null) {
27
				return true;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
28
			}
29
			
30
			if ($id_base == null) {
31
				$this->getParametres();
32
				
33
				$query = $dbc->select("_bataille_base.nom_base")
34
					->select("_bataille_base.points")
35
					->select("_bataille_base.posx")
36
					->select("_bataille_base.posy")
37
					->select("_bataille_base.ID_base")
38
					->select("identite.pseudo")
39
					->select("identite.ID_identite")
40
					->from("identite")
41
					->from("_bataille_base")
42
					->where("_bataille_base.ID_identite", "=", "identite.ID_identite", "", true)
43
					->get();
44
			}
45
			else {
46
				$query = $dbc->select("_bataille_base.nom_base")
47
					->select("_bataille_base.points")
48
					->select("_bataille_base.posx")
49
					->select("_bataille_base.posy")
50
					->select("_bataille_base.ID_base")
51
					->select("identite.ID_identite")
52
					->select("identite.pseudo")
53
					->from("identite")
54
					->from("_bataille_base")
55
					->where("_bataille_base.ID_base", "=", $id_base, "AND")
56
					->where("_bataille_base.ID_identite", "=", "identite.ID_identite", "", true)
57
					->get();
58
				
59
				$temps_trajet = DateHeure::Secondeenheure(Bataille::getDureeTrajet($id_base));
60
			}
61
			
62
			if ((is_array($query)) && (count($query) > 0)) {
63
				$faction = Bataille::getRelationFaction();
64
				$faction->getFactionPlayer();
65
				$id_faction = $faction->getIdFaction();
66
				$faction_allie = $faction->getIdFactionRelation("allié");
67
				$faction_non_agression = $faction->getIdFactionRelation("pacte non agression");
68
				$faction_ennemies = $faction->getIdFactionRelation("ennemi");
69
				
70
				foreach ($query as $obj) {
71
					$ma_base = "";
72
					$mes_bases = "";
73
					$ma_faction = "";
74
					$allie = "";
75
					$pacte_non_agression = "";
76
					$ennemi = "";
77
					
78
					if ($obj->ID_base == Bataille::getIdBase()) {
79
						$ma_base = "ma-base";
80
					}
81
					else if ($obj->ID_identite == Bataille::getIdIdentite()) {
82
						$mes_bases = "mes-bases";
83
					}
84
					
85
					$faction->getFactionPlayer($obj->ID_identite);
86
					if (($id_faction == $faction->getIdFaction()) && ($obj->ID_identite != Bataille::getIdIdentite())) {
87
						$ma_faction = "ma-faction";
88
					}
89
					
90
					if (in_array($faction->getIdFaction(), $faction_allie)) {
91
						$allie = "faction-allie";
92
					}
93
					if (in_array($faction->getIdFaction(), $faction_non_agression)) {
94
						$pacte_non_agression = "faction-non-agression";
95
					}
96
					if (in_array($faction->getIdFaction(), $faction_ennemies)) {
97
						$ennemi = "faction-ennemi";
98
					}
99
					
100
					$map[] = [
101
						"nom_base" => $obj->nom_base,
102
						"points" => $obj->points,
103
						"posx" => $obj->posx,
104
						"posy" => $obj->posy,
105
						"id_base" => $obj->ID_base,
106
						"id_identite" => $obj->ID_identite,
107
						"pseudo" => $obj->pseudo,
108
						"ma_base" => $ma_base,
109
						"mes_bases" => $mes_bases,
110
						"ma_faction" => $ma_faction,
111
						"faction_allie" => $allie,
112
						"faction_pacte_non_agression" => $pacte_non_agression,
113
						"faction_ennemies" => $ennemi,
114
						"temps_trajet" => $temps_trajet,
115
						"mode_vacances" => Profil::getTestVacancesBase($obj->ID_base)
116
					];
117
				}
118
				
119
				Bataille::setValues(["map" => $map]);
120
			}
121
		}
122
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
123
124
125
126
		//-------------------------- GETTER ----------------------------------------------------------------------------//
127
		/**
128
		 * @return integer
129
		 * fonction qui permet de récupérer le nombre de joueurs sur le serveur
130
		 */
131 View Code Duplication
		private function getNombreJoueur() {
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...
132
			$dbc = App::getDb();
133
			
134
			$query = $dbc->select("nombre_joueur")->from("_bataille_nombre_joueur")->where("ID_nombre_joueur", "=", 1)->get();
135
			
136
			if ((is_array($query)) && (count($query) == 1)) {
137
				foreach ($query as $obj) return $obj->nombre_joueur;
138
			}
139
			
140
			return 0;
141
		}
142
		
143
		/**
144
		 * fonction qui sert à récupérer les parametres de la map
145
		 */
146
		private function getParametres() {
147
			$dbc = Bataille::getDb();
148
149
			$query = $dbc->select()->from("map")->where("ID_map", "=", 1)->get();
150
151
			foreach ($query as $obj) {
152
				Bataille::setValues([
153
					"largeur_map" => $obj->largeur,
154
					"hauteur_map" => $obj->hauteur
155
				]);
156
157
				$this->largeur = $obj->largeur;
158
				$this->hauteur = $obj->hauteur;
159
			}
160
		}
161
162
		/**
163
		 * @return array
164
		 * fonction utilisée lors de la création d'un compte
165
		 * renvoi les positions en x et y non occupées
166
		 */
167
		public function getPositionNewBase() {
168
			$this->getParametres();
169
170
			if ($this->getNombreJoueur() <= 150) {
171
				$posx = rand(0, 79);
172
				$posy = rand(0, 75);
173
			}
174
			else {
175
				$posx = rand(0, $this->largeur);
176
				$posy = rand(0, $this->hauteur);
177
			}
178
179
			//on test si il y a une base sur ces positions
180
			if (Bataille::getBaseExistPosition($posx, $posy)) {
181
				$this->getPositionNewBase();
182
			}
183
			else {
184
				//on a une position de base inexistante donc on la return
185
				return ["posx" => $posx, "posy" => $posy];
186
			}
187
		}
188
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
189
190
191
192
		//-------------------------- SETTER ----------------------------------------------------------------------------//
193
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
194
	}