Passed
Push — master ( 9c6704...e16eca )
by Anthony
02:58
created

Bataille::getGoupeUnite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
	namespace modules\bataille\app\controller;
3
	use core\App;
4
	use core\database\Database;
5
6
	class Bataille {
7
		private static $ressource;
8
		private static $base;
9
		private static $batiment;
10
		private static $unite;
11
		private static $groupe_unite;
12
		private static $centre_recherche;
13
		private static $missions_aleatoire;
14
		private static $database;
15
		private static $nation;
16
17
		private static $id_base;
18
19
		public static $values = [];
20
21
		
22
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
23
		public function __construct() {
24
25
		}
26
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
27
		
28
		
29
		
30
		//-------------------------- GETTER ----------------------------------------------------------------------------//
31
		/**
32
		 * @return array
33
		 * get array of all values wich will be used in the page
34
		 */
35
		public static function getValues() {
36
			return ["bataille" => self::$values];
37
		}
38
39
		//initilisation of all classes of battle
40
		//initialisation of Ressource class
41
		public static function getRessource() {
42
			if (self::$ressource == null) {
43
				self::$ressource = new Ressource();
44
			}
45
46
			return self::$ressource;
47
		}
48
49
		//initialisation of Base class
50
		public static function getBase() {
51
			if (self::$base == null) {
52
				self::$base = new Base();
53
			}
54
55
			return self::$base;
56
		}
57
58
		//initialisation of Batiment class
59
		public static function getBatiment() {
60
			if (self::$batiment == null) {
61
				self::$batiment = new Batiment();
62
			}
63
64
			return self::$batiment;
65
		}
66
67
		//initialisation of Unite class
68
		public static function getUnite() {
69
			if (self::$unite == null) {
70
				self::$unite = new Unite();
71
			}
72
73
			return self::$unite;
74
		}
75
		
76
		//initialisation of GroupeUnite class
77
		public static function getGoupeUnite() {
78
			if (self::$groupe_unite == null) {
79
				self::$groupe_unite = new GroupeUnite();
80
			}
81
			
82
			return self::$groupe_unite;
83
		}
84
85
		//initialisation of CentreRecherche class
86
		public static function getCentreRecherche() {
87
			if (self::$centre_recherche == null) {
88
				self::$centre_recherche = new CentreRecherche();
89
			}
90
91
			return self::$centre_recherche;
92
		}
93
		
94
		//initialisation of MissionsAleatoire class
95
		public static function getMissionsAleatoire() {
96
			if (self::$missions_aleatoire == null) {
97
				self::$missions_aleatoire = new MissionsAleatoire();
98
			}
99
			
100
			return self::$missions_aleatoire;
101
		}
102
103
		//initialisation of Database Core connexion
104
		public static function getDb() {
105
			require_once("config.config.php");
106
			
107
			if (self::$database == null) {
108
				self::$database = new Database($type_co, $dbname, $dbuser, $dbpass, $dbhost);
0 ignored issues
show
Bug introduced by
The variable $type_co does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $dbname does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $dbuser does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $dbpass does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $dbhost does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
109
			}
110
			return self::$database;
111
		}
112
113
		/**
114
		 * @return mixe
115
		 * récupère l'ID_identité du joueur
116
		 */
117
		public static function getIdIdentite() {
0 ignored issues
show
Coding Style introduced by
getIdIdentite uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
118
			return $_SESSION['idlogin'.CLEF_SITE];
119
		}
120
121
		/**
122
		 * @return mixed
123
		 * renvoi l'id_base du joueur
124
		 */
125
		public static function getIdBase() {
0 ignored issues
show
Coding Style introduced by
getIdBase uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
126
			if (self::$id_base == null) {
127
				self::$id_base = $_SESSION['id_base'];
128
129
				return self::$id_base;
130
			}
131
132
			return self::$id_base;
133
		}
134
135
		/**
136
		 * @return mixed
137
		 * renvoi le premier ID_base du joueur (première base et base princ du joueur)
138
		 */
139
		public static function getFirstBase() {
140
			$dbc = App::getDb();
141
142
			$query = $dbc->select("ID_base")->from("_bataille_base")
143
				->where("ID_identite", "=", self::getIdIdentite())
144
				->orderBy("ID_base")
145
				->limit(0, 1)
146
				->get();
147
148
			if ((is_array($query)) && (count($query) == 1)) {
149
				foreach ($query as $obj) return $obj->ID_base;
150
			}
151
		}
152
153
		/**
154
		 * @param $id_base
155
		 * @return array
156
		 * fonction qui renvoi les posisitons en x et y d'une base
157
		 */
158
		private static function getPosistionBase($id_base) {
159
			$dbc = App::getDb();
160
			
161
			$posx = 0;
162
			$posy = 0;
163
164
			$query = $dbc->select("posx")
165
				->select("posy")
166
				->from("_bataille_base")
167
				->where("ID_base", "=", $id_base)
168
				->get();
169
170
			foreach ($query as $obj) {
171
				$posx = $obj->posx;
172
				$posy = $obj->posy;
173
			}
174
175
			return ["posx" => $posx, "posy" => $posy];
176
		}
177
178
		/**
179
		 * @return int
180
		 * return now timestamp
181
		 */
182
		public static function getToday() {
183
			$today = new \DateTime();
184
			return $today->getTimestamp();
185
		}
186
187
		/**
188
		 * @param string $nom_ressource
189
		 * @param $ressource
190
		 * @return array
191
		 * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment
192
		 * ou pour creer une unité...
193
		 */
194
		public static function getTestAssezRessourceBase($nom_ressource, $ressource) {
195
			$f = "get".ucfirst($nom_ressource);
196
197
			if ($ressource > Bataille::getRessource()->$f()) {
198
				return [
199
					"ressource" => $ressource,
200
					"class" => "rouge"
201
				];
202
			}
203
204
			return [
205
				"ressource" => $ressource,
206
				"class" => ""
207
			];
208
		}
209
		
210
		/**
211
		 * @param $id_base
212
		 * @param integer $vitesse = vitesse de l'unité en question
213
		 * @return number
214
		 * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes
215
		 */
216
		public static function getDureeTrajet($id_base, $vitesse = 1) {
0 ignored issues
show
Coding Style introduced by
getDureeTrajet uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
217
			//récupération de la posisiotn de la base du joueur + la base sur laquelle on a cliqué
218
			$base_joueur = self::getPosistionBase($_SESSION['id_base']);
219
			$base_autre = self::getPosistionBase($id_base);
220
			
221
			//calcul des distances séparant les deux bases en x et y
222
			//cette dstance sera multipliée par 15 sur x et y puis ajoutée pour avoir le temps du trajte en seconde
223
			$calc_x = abs($base_joueur['posx']-$base_autre['posx']);
224
			$calc_y = abs($base_joueur['posy']-$base_autre['posy']);
225
			
226
			$temps_voyage = (($calc_x*70)+($calc_y*70))/$vitesse;
227
			
228
			return $temps_voyage;
229
		}
230
231
		/**
232
		 * @param null $id_identite
233
		 * get nation of a player
234
		 */
235
		public static function getNation($id_identite = null) {
236
			$dbc = App::getDb();
237
238
			if (($id_identite === null) && (self::$nation == null)) {
239
				$id_identite = Bataille::getIdIdentite();
240
			}
241
242
			$query = $dbc->select("nation")
243
				->from("identite")
244
				->from("_bataille_nation")
245
				->where("identite.ID_identite", "=", $id_identite, "AND")
246
				->where("identite.ID_identite", "=", "_bataille_nation.ID_identite", "", true)
247
				->get();
248
249
			if ((is_array($query)) && (count($query) > 0)) {
250
				foreach ($query as $obj) {
251
					self::setValues(["nation" => $obj->nation]);
252
				}
253
			}
254
		}
255
256
		/**
257
		 * @param $posx
258
		 * @param $posy
259
		 * @return int
260
		 * fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante
261
		 */
262
		public static function getBaseExistPosition($posx, $posy) {
263
			$dbc = App::getDb();
264
265
			$query = $dbc->select("ID_base")->from("_bataille_base")
266
				->where("posx", "=", $posx, "AND")
267
				->where("posy", "=", $posy)
268
				->get();
269
270
			if ((is_array($query)) && (count($query) == 1)) {
271
				foreach ($query as $obj) return $obj->ID_base;
272
			}
273
274
			return 0;
275
		}
276
277
		/**
278
		 * @param string $param
279
		 * @return mixed
280
		 * fonction qui sert à récupérer un parametre spécifique pour un batiment
281
		 * par exemple la vitesse d'un marchand ou  le nombred'emplacment de la base
282
		 */
283
		public static function getParam($param) {
284
			$dbc = self::getDb();
285
286
			$query = $dbc->select($param)->from("configuration")->where("ID_configuration", "=", 1)->get();
287
288
			if ((is_array($query)) && (count($query) == 1)) {
289
				foreach ($query as $obj) return $obj->$param;
290
			}
291
		}
292
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
293
		
294
		
295
		
296
		//-------------------------- SETTER ----------------------------------------------------------------------------//
297
		/**
298
		 * @param $values
299
		 * can set values while keep older infos
300
		 */
301
		public static function setValues($values) {
302
			Bataille::$values = array_merge(Bataille::$values, $values);
303
		}
304
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
305
	}