Passed
Push — master ( 7b3593...78d9ba )
by Anthony
02:43
created

Bataille::getBaseExistPosition()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 9
nc 3
nop 2
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 $points;
11
		private static $unite;
12
		private static $centre_recherche;
13
		private static $missions_aleatoire;
14
		private static $nourriture;
0 ignored issues
show
Unused Code introduced by
The property $nourriture is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
		private static $map;
0 ignored issues
show
Unused Code introduced by
The property $map is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
		private static $database;
17
		private static $nation;
18
19
		private static $id_base;
20
21
		public static $values = [];
22
23
		
24
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
25
		public function __construct() {
26
27
		}
28
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
29
		
30
		
31
		
32
		//-------------------------- GETTER ----------------------------------------------------------------------------//
33
		/**
34
		 * @return array
35
		 * get array of all values wich will be used in the page
36
		 */
37
		public static function getValues() {
38
			return ["bataille" => self::$values];
39
		}
40
41
		//initilisation of all classes of battle
42
		//initialisation of Ressource class
43
		public static function getRessource() {
44
			if (self::$ressource == null) {
45
				self::$ressource = new Ressource();
46
			}
47
48
			return self::$ressource;
49
		}
50
51
		//initialisation of Base class
52
		public static function getBase() {
53
			if (self::$base == null) {
54
				self::$base = new Base();
55
			}
56
57
			return self::$base;
58
		}
59
60
		//initialisation of Batiment class
61
		public static function getBatiment() {
62
			if (self::$batiment == null) {
63
				self::$batiment = new Batiment();
64
			}
65
66
			return self::$batiment;
67
		}
68
69
		//initialisation of Points class
70
		public static function getPoints($points = null) {
71
			if (self::$points == null) {
72
				self::$points = new Points($points);
73
			}
74
75
			return self::$points;
76
		}
77
78
		//initialisation of Batiment class
79
		public static function getUnite() {
80
			if (self::$unite == null) {
81
				self::$unite = new Unite();
82
			}
83
84
			return self::$unite;
85
		}
86
87
		//initialisation of CentreRecherche class
88
		public static function getCentreRecherche() {
89
			if (self::$centre_recherche == null) {
90
				self::$centre_recherche = new CentreRecherche();
91
			}
92
93
			return self::$centre_recherche;
94
		}
95
		
96
		//initialisation of MissionsAleatoire class
97
		public static function getMissionsAleatoire() {
98
			if (self::$missions_aleatoire == null) {
99
				self::$missions_aleatoire = new MissionsAleatoire();
100
			}
101
			
102
			return self::$missions_aleatoire;
103
		}
104
105
		//initialisation of Database Core connexion
106
		public static function getDb() {
107
			require_once("config.config.php");
108
			
109
			if (self::$database == null) {
110
				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...
111
			}
112
			return self::$database;
113
		}
114
115
		/**
116
		 * @return mixe
117
		 * récupère l'ID_identité du joueur
118
		 */
119
		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...
120
			return $_SESSION['idlogin'.CLEF_SITE];
121
		}
122
123
		/**
124
		 * @return mixed
125
		 * renvoi l'id_base du joueur
126
		 */
127
		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...
128
			if (self::$id_base == null) {
129
				self::$id_base = $_SESSION['id_base'];
130
131
				return self::$id_base;
132
			}
133
134
			return self::$id_base;
135
		}
136
137
		/**
138
		 * @return mixed
139
		 * renvoi le premier ID_base du joueur (première base et base princ du joueur)
140
		 */
141
		public static function getFirstBase() {
142
			$dbc = App::getDb();
143
144
			$query = $dbc->select("ID_base")->from("_bataille_base")
145
				->where("ID_identite", "=", self::getIdIdentite())
146
				->orderBy("ID_base")
147
				->limit(0, 1)
148
				->get();
149
150
			if ((is_array($query)) && (count($query) == 1)) {
151
				foreach ($query as $obj) return $obj->ID_base;
152
			}
153
		}
154
155
		/**
156
		 * @param $id_base
157
		 * @return array
158
		 * fonction qui renvoi les posisitons en x et y d'une base
159
		 */
160
		private static function getPosistionBase($id_base) {
161
			$dbc = App::getDb();
162
			
163
			$posx = 0;
164
			$posy = 0;
165
166
			$query = $dbc->select("posx")
167
				->select("posy")
168
				->from("_bataille_base")
169
				->where("ID_base", "=", $id_base)
170
				->get();
171
172
			foreach ($query as $obj) {
173
				$posx = $obj->posx;
174
				$posy = $obj->posy;
175
			}
176
177
			return ["posx" => $posx, "posy" => $posy];
178
		}
179
180
		/**
181
		 * @return int
182
		 * return now timestamp
183
		 */
184
		public static function getToday() {
185
			$today = new \DateTime();
186
			return $today->getTimestamp();
187
		}
188
189
		/**
190
		 * @param string $nom_ressource
191
		 * @param $ressource
192
		 * @return array
193
		 * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment
194
		 * ou pour creer une unité...
195
		 */
196
		public static function getTestAssezRessourceBase($nom_ressource, $ressource) {
197
			$f = "get".ucfirst($nom_ressource);
198
199
			if ($ressource > Bataille::getRessource()->$f()) {
200
				return [
201
					"ressource" => $ressource,
202
					"class" => "rouge"
203
				];
204
			}
205
206
			return [
207
				"ressource" => $ressource,
208
				"class" => ""
209
			];
210
		}
211
		
212
		/**
213
		 * @param $id_base
214
		 * @param integer $vitesse = vitesse de l'unité en question
215
		 * @return number
216
		 * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes
217
		 */
218
		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...
219
			//récupération de la posisiotn de la base du joueur + la base sur laquelle on a cliqué
220
			$base_joueur = self::getPosistionBase($_SESSION['id_base']);
221
			$base_autre = self::getPosistionBase($id_base);
222
			
223
			//calcul des distances séparant les deux bases en x et y
224
			//cette dstance sera multipliée par 15 sur x et y puis ajoutée pour avoir le temps du trajte en seconde
225
			$calc_x = abs($base_joueur['posx']-$base_autre['posx']);
226
			$calc_y = abs($base_joueur['posy']-$base_autre['posy']);
227
			
228
			$temps_voyage = (($calc_x*70)+($calc_y*70))/$vitesse;
229
			
230
			return $temps_voyage;
231
		}
232
233
		/**
234
		 * @param null $id_identite
235
		 * get nation of a player
236
		 */
237
		public static function getNation($id_identite = null) {
238
			$dbc = App::getDb();
239
240
			if (($id_identite === null) && (self::$nation == null)) {
241
				$id_identite = Bataille::getIdIdentite();
242
			}
243
244
			$query = $dbc->select("nation")
245
				->from("identite")
246
				->from("_bataille_nation")
247
				->where("identite.ID_identite", "=", $id_identite, "AND")
248
				->where("identite.ID_identite", "=", "_bataille_nation.ID_identite", "", true)
249
				->get();
250
251
			if ((is_array($query)) && (count($query) > 0)) {
252
				foreach ($query as $obj) {
253
					self::setValues(["nation" => $obj->nation]);
254
				}
255
			}
256
		}
257
258
		/**
259
		 * @param $posx
260
		 * @param $posy
261
		 * @return int
262
		 * fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante
263
		 */
264
		public static function getBaseExistPosition($posx, $posy) {
265
			$dbc = App::getDb();
266
267
			$query = $dbc->select("ID_base")->from("_bataille_base")
268
				->where("posx", "=", $posx, "AND")
269
				->where("posy", "=", $posy)
270
				->get();
271
272
			if ((is_array($query)) && (count($query) == 1)) {
273
				foreach ($query as $obj) return $obj->ID_base;
274
			}
275
276
			return 0;
277
		}
278
279
		/**
280
		 * @param string $param
281
		 * @return mixed
282
		 * fonction qui sert à récupérer un parametre spécifique pour un batiment
283
		 * par exemple la vitesse d'un marchand ou  le nombred'emplacment de la base
284
		 */
285
		public static function getParam($param) {
286
			$dbc = self::getDb();
287
288
			$query = $dbc->select($param)->from("configuration")->where("ID_configuration", "=", 1)->get();
289
290
			if ((is_array($query)) && (count($query) == 1)) {
291
				foreach ($query as $obj) return $obj->$param;
292
			}
293
		}
294
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
295
		
296
		
297
		
298
		//-------------------------- SETTER ----------------------------------------------------------------------------//
299
		/**
300
		 * @param $values
301
		 * can set values while keep older infos
302
		 */
303
		public static function setValues($values) {
304
			Bataille::$values = array_merge(Bataille::$values, $values);
305
		}
306
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
307
	}