| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | 	namespace modules\bataille\app\controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | 	use core\App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	class Bataille extends InitialiseClass { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 		private static $nation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 		private static $id_base; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 		public static $values = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 		//-------------------------- GETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		 * get array of all values wich will be used in the page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		public static function getValues() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 			return ["bataille" => self::$values]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		 * @return mixe | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		 * récupère l'ID_identité du joueur | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		public static function getIdIdentite() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			return $_SESSION['idlogin'.CLEF_SITE]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		 * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		 * renvoi l'id_base du joueur | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		public static function getIdBase() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			if (self::$id_base == null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 				self::$id_base = $_SESSION['id_base']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 				return self::$id_base; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			return self::$id_base; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		 * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		 * renvoi le premier ID_base du joueur (première base et base princ du joueur) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  | 		 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 		public static function getFirstBase() { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 			$dbc = App::getDb(); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 			$query = $dbc->select("ID_base")->from("_bataille_base") | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 				->where("ID_identite", "=", self::getIdIdentite()) | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 				->orderBy("ID_base") | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 				->limit(0, 1) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 				->get(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 			if ((is_array($query)) && (count($query) == 1)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 				foreach ($query as $obj) return $obj->ID_base; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		 * @param $id_base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		 * fonction qui renvoi les posisitons en x et y d'une base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		private static function getPosistionBase($id_base) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			$dbc = App::getDb(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			$posx = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			$posy = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			$query = $dbc->select("posx") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 				->select("posy") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 				->from("_bataille_base") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 				->where("ID_base", "=", $id_base) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 				->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			foreach ($query as $obj) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 				$posx = $obj->posx; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 				$posy = $obj->posy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			return ["posx" => $posx, "posy" => $posy]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		 * return now timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		public static function getToday() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			$today = new \DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			return $today->getTimestamp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		 * @param string $nom_ressource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		 * @param $ressource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		 * fonction qui permet de renvyer la couleur rouge si pas assez de ressource pour construire le batiment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		 * ou pour creer une unité... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		public static function getTestAssezRessourceBase($nom_ressource, $ressource) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			$f = "get".ucfirst($nom_ressource); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 			if ($ressource > Bataille::getRessource()->$f()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 				return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 					"ressource" => $ressource, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 					"class" => "rouge" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 				]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 				"ressource" => $ressource, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 				"class" => "" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		 * @param $id_base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		 * @param integer $vitesse = vitesse de l'unité en question | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		 * @return number | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		 * fonction qui renvoi le temps de trajet entre la base du joueur et une autre base en secondes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		public static function getDureeTrajet($id_base, $vitesse = 1) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 			//récupération de la posisiotn de la base du joueur + la base sur laquelle on a cliqué | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			$base_joueur = self::getPosistionBase($_SESSION['id_base']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 			$base_autre = self::getPosistionBase($id_base); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 			//calcul des distances séparant les deux bases en x et y | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 			//cette dstance sera multipliée par 15 sur x et y puis ajoutée pour avoir le temps du trajte en seconde | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			$calc_x = abs($base_joueur['posx']-$base_autre['posx']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			$calc_y = abs($base_joueur['posy']-$base_autre['posy']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			$temps_voyage = (($calc_x*70)+($calc_y*70))/$vitesse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 			 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			return $temps_voyage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		 * @param null $id_identite | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		 * get nation of a player | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		public static function getNation($id_identite = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 			$dbc = App::getDb(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			if (($id_identite === null) && (self::$nation == null)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 				$id_identite = Bataille::getIdIdentite(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 			$query = $dbc->select("nation") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 				->from("identite") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 				->from("_bataille_nation") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 				->where("identite.ID_identite", "=", $id_identite, "AND") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 				->where("identite.ID_identite", "=", "_bataille_nation.ID_identite", "", true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 				->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 			if ((is_array($query)) && (count($query) > 0)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 				foreach ($query as $obj) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 					self::setValues(["nation" => $obj->nation]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 		 * @param $posx | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		 * @param $posy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 		 * fonction qui renvoi un ID_base en fonction de sa posx et posy et 0 si base inexistante | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		public static function getBaseExistPosition($posx, $posy) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 			$dbc = App::getDb(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 			$query = $dbc->select("ID_base")->from("_bataille_base") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 				->where("posx", "=", $posx, "AND") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 				->where("posy", "=", $posy) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 				->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 			if ((is_array($query)) && (count($query) == 1)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 				foreach ($query as $obj) return $obj->ID_base; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 			return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 		 * @param string $param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 		 * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 		 * fonction qui sert à récupérer un parametre spécifique pour un batiment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 		 * par exemple la vitesse d'un marchand ou  le nombred'emplacment de la base | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 		public static function getParam($param) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 			$dbc = self::getDb(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 			$query = $dbc->select($param)->from("configuration")->where("ID_configuration", "=", 1)->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 			if ((is_array($query)) && (count($query) == 1)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 				foreach ($query as $obj) return $obj->$param; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 		//-------------------------- END GETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | 		//-------------------------- SETTER ----------------------------------------------------------------------------// | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | 		 * @param $values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  | 		 * can set values while keep older infos | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | 		public static function setValues($values) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  | 			Bataille::$values = array_merge(Bataille::$values, $values); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | 		} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 214 |  |  | 		//-------------------------- END SETTER ----------------------------------------------------------------------------// | 
            
                                                        
            
                                    
            
            
                | 215 |  |  | 	} | 
            
                        
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: