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

Map::__construct()   D

Complexity

Conditions 13
Paths 101

Size

Total Lines 100
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 100
rs 4.9737
cc 13
eloc 76
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
					];
116
				}
117
				
118
				Bataille::setValues(["map" => $map]);
119
			}
120
		}
121
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
122
123
124
125
		//-------------------------- GETTER ----------------------------------------------------------------------------//
126
		/**
127
		 * @return integer
128
		 * fonction qui permet de récupérer le nombre de joueurs sur le serveur
129
		 */
130
		private function getNombreJoueur() {
131
			$dbc = App::getDb();
132
			
133
			$query = $dbc->select("nombre_joueur")->from("_bataille_nombre_joueur")->where("ID_nombre_joueur", "=", 1)->get();
134
			
135
			if ((is_array($query)) && (count($query) == 1)) {
136
				foreach ($query as $obj) return $obj->nombre_joueur;
137
			}
138
			
139
			return 0;
140
		}
141
		
142
		/**
143
		 * fonction qui sert à récupérer les parametres de la map
144
		 */
145
		private function getParametres() {
146
			$dbc = Bataille::getDb();
147
148
			$query = $dbc->select()->from("map")->where("ID_map", "=", 1)->get();
149
150
			foreach ($query as $obj) {
151
				Bataille::setValues([
152
					"largeur_map" => $obj->largeur,
153
					"hauteur_map" => $obj->hauteur
154
				]);
155
156
				$this->largeur = $obj->largeur;
157
				$this->hauteur = $obj->hauteur;
158
			}
159
		}
160
161
		/**
162
		 * @return array
163
		 * fonction utilisée lors de la création d'un compte
164
		 * renvoi les positions en x et y non occupées
165
		 */
166
		public function getPositionNewBase() {
167
			$this->getParametres();
168
169
			if ($this->getNombreJoueur() <= 150) {
170
				$posx = rand(0, 79);
171
				$posy = rand(0, 75);
172
			}
173
			else {
174
				$posx = rand(0, $this->largeur);
175
				$posy = rand(0, $this->hauteur);
176
			}
177
178
			//on test si il y a une base sur ces positions
179
			if (Bataille::getBaseExistPosition($posx, $posy)) {
180
				$this->getPositionNewBase();
181
			}
182
			else {
183
				//on a une position de base inexistante donc on la return
184
				return ["posx" => $posx, "posy" => $posy];
185
			}
186
		}
187
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
188
189
190
191
		//-------------------------- SETTER ----------------------------------------------------------------------------//
192
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
193
	}