Ressource::getEau()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
	namespace modules\bataille\app\controller;
3
	use core\App;
4
5
	class Ressource {
6
		private $eau;
7
		private $electricite;
8
		private $fuel;
9
		private $fer;
10
		private $nourriture;
11
12
		private $id_base;
13
14
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
15
		public function __construct($id_base = null) {
16
			$dbc = App::getDb();
17
18
			if ($id_base === null) {
19
				$this->id_base = Bataille::getIdBase();
20
			}
21
			else {
22
				$this->id_base = $id_base;
23
			}
24
25
			$query = $dbc->select()->from("_bataille_base")->where("ID_base", "=", $this->id_base)->get();
26
27
			if ((is_array($query)) && (count($query) > 0)) {
28
				foreach ($query as $obj) {
29
					$this->eau = $obj->eau;
30
					$this->electricite = $obj->electricite;
31
					$this->fuel = $obj->fuel;
32
					$this->fer = $obj->fer;
33
					$this->nourriture = $obj->nourriture;
34
				}
35
36
				$this->setActualiserRessource();
37
38
				Bataille::setValues([
39
					"max_eau" => $this->getStockageMax("eau"),
40
					"max_electricite" => $this->getStockageMax("electricite"),
41
					"max_fer" => $this->getStockageMax("fer"),
42
					"max_fuel" => $this->getStockageMax("fuel"),
43
					"max_nourriture" => $this->getStockageMax("nourriture"),
44
					"eau" => $this->eau,
45
					"electricite" => $this->electricite,
46
					"fer" => $this->fer,
47
					"fuel" => $this->fuel,
48
					"nourriture" => $this->nourriture
49
				]);
50
			}
51
		}
52
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
53
54
55
56
		//-------------------------- GETTER ----------------------------------------------------------------------------//
57
		public function getEau() {
58
			return $this->eau;
59
		}
60
		public function getElectricite() {
61
			return $this->electricite;
62
		}
63
		public function getFuel() {
64
			return $this->fuel;
65
		}
66
		public function getFer() {
67
			return $this->fer;
68
		}
69
		public function getNourriture() {
70
			return $this->nourriture;
71
		}
72
		
73
		/**
74
		 * @param $ressource
75
		 * @return string
76
		 * fonction qui sert à tester si on a atteint le stockage maximum pour une ressource en particulier
77
		 */
78
		private function getStockageMax($ressource) {
79
			if ($ressource == "nourriture") {
80
				$stockage_max = Bataille::getBatiment()->getStockage("grenier");
81
			}
82
			else {
83
				$stockage_max = Bataille::getBatiment()->getStockage();
84
			}
85
86
			if ($this->$ressource == $stockage_max) {
87
				return "rouge";
88
			}
89
		}
90
		
91
		/**
92
		 * @param null $id_base -> sert si definit a recuperer l'id identite de la abse en question
0 ignored issues
show
Bug introduced by
There is no parameter named $id_base. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
93
		 * recupere la date de la derniere connexion
94
		 */
95
		private function getLastConnexion() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
			$dbc = App::getDb();
97
			
98
			$query = $dbc->select("last_connexion")->from("_bataille_base")
99
				->where("ID_base", "=", $this->id_base, "AND")
100
				->where("ID_identite", "=", Bataille::getIdIdentite())
101
				->get();
102
			
103
			
104
			if ((is_array($query)) && (count($query) > 0)) {
105
				foreach ($query as $obj) {
106
					return $obj->last_connexion;
107
				}
108
			}
109
		}
110
		
111
		/**
112
		 * @param $eau
113
		 * @param $electricite
114
		 * @param $fer
115
		 * @param $fuel
116
		 * @param $nourriture
117
		 * @return array
118
		 * fonction qui gere le calcul d'ajout de setUpdateRessource
119
		 */
120
		private function getCalcAjoutRessource($eau, $electricite, $fer, $fuel, $nourriture) {
121
			$eau = $this->getEau()+$eau;
122
			$electricite = $this->getElectricite()+$electricite;
123
			$fer = $this->getFer()+$fer;
124
			$fuel = $this->getFuel()+$fuel;
125
			$nourriture = $this->getNourriture()+$nourriture;
126
			
127
			$stockage_max = Bataille::getBatiment()->getStockage();
128
			$stockage_max_grenier = Bataille::getBatiment()->getStockage("grenier");
129
			
130
			if ($eau > $stockage_max) $eau = $stockage_max;
131
			if ($electricite > $stockage_max) $electricite = $stockage_max;
132
			if ($fer > $stockage_max) $fer = $stockage_max;
133
			if ($fuel > $stockage_max) $fuel = $stockage_max;
134
			if ($nourriture > $stockage_max_grenier) $nourriture = $stockage_max_grenier;
135
			
136
			return [
137
				"eau" => $eau,
138
				"electricite" => $electricite,
139
				"fer" => $fer,
140
				"fuel" => $fuel,
141
				"nourriture" => $nourriture
142
			];
143
		}
144
		
145
		/**
146
		 * @param $eau
147
		 * @param $electricite
148
		 * @param $fer
149
		 * @param $fuel
150
		 * @param $nourriture
151
		 * @return array
152
		 * fonction qui gere le calcul pour retirer de setUpdateRessource
153
		 */
154
		private function getCalcRetirerRessource($eau, $electricite, $fer, $fuel, $nourriture) {
155
			$eau = $this->eau - $eau;
156
			$electricite = $this->electricite - $electricite;
157
			$fer = $this->fer - $fer;
158
			$fuel = $this->fuel - $fuel;
159
			$nourriture = $this->nourriture - $nourriture;
160
			
161
			if ($eau < 0) $eau = 0;
162
			if ($electricite < 0) $electricite = 0;
163
			if ($fer < 0) $fer = 0;
164
			if ($fuel < 0) $fuel = 0;
165
			if ($nourriture < 0) $nourriture = 0;
166
			
167
			return [
168
				"eau" => $eau,
169
				"electricite" => $electricite,
170
				"fer" => $fer,
171
				"fuel" => $fuel,
172
				"nourriture" => $nourriture
173
			];
174
		}
175
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
176
177
178
179
		//-------------------------- SETTER ----------------------------------------------------------------------------//
180
		/**
181
		 * set la date de derniere connexion a now
182
		 */
183 View Code Duplication
		private function setLastConnexion() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
			$dbc = App::getDb();
185
			
186
			$dbc->update("last_connexion", date("Y-m-d H:i:s"))
187
				->from("_bataille_base")
188
				->where("ID_identite", "=", Bataille::getIdIdentite(), "AND")
189
				->where("ID_base", "=", $this->id_base)
190
				->set();
191
			
192
			$dbc->update("last_connexion", date("Y-m-d H:i:s"))->from("_bataille_infos_player")->where("ID_identite", "=", Bataille::getIdIdentite())->set();
193
		}
194
		
195
		/**
196
		 * fonction qui au chargement de la base regardera la derniere co du joueur
197
		 * si elle est supérieur à 30sec on recalculera les ressources des bases du joueur
198
		 */
199
		public function setActualiserRessource() {
200
			$last_co = $this->getLastConnexion();
201
202
			$today = Bataille::getToday();
203
204
			$last_co = new \DateTime($last_co);
205
			$last_co = $last_co->getTimestamp();
206
207
			$diff_temps = $today-$last_co;
208
209
			//si la derniere actualisation ou connexion est supérieur à 30 sec
210
			$this->setAddRessource("eau", $this->eau, $diff_temps);
211
			$this->setAddRessource("electricite", $this->electricite, $diff_temps);
212
			$this->setAddRessource("fuel", $this->fuel, $diff_temps);
213
			$this->setAddRessource("fer", $this->fer, $diff_temps);
214
		}
215
216
		/**
217
		 * @param $nom_ressource
218
		 * @param $ressrouce
219
		 * @param $diff_temps
220
		 * fonction qui ajoute les ressources qu'on a eu dans la base et qui reinitialise la last co a now
221
		 */
222
		private function setAddRessource($nom_ressource, $ressrouce, $diff_temps) {
223
			$dbc = App::getDb();
224
225
			$ressource = $ressrouce+(round((Bataille::getBatiment()->getProduction($nom_ressource)/3600)*$diff_temps));
226
			
227
			$stockage_max = Bataille::getBatiment()->getStockage();
228
			if ($nom_ressource == "nourriture") {
229
				$stockage_max = Bataille::getBatiment()->getStockage("grenier");
230
			}
231
232
			if ($ressource > $stockage_max) {
233
				$ressource = $stockage_max;
234
			}
235
236
			$dbc->update($nom_ressource, $ressource)
237
				->from("_bataille_base")
238
				->where("ID_base", "=", $this->id_base)
239
				->set();
240
241
			$this->$nom_ressource = $ressource;
242
243
			$this->setLastConnexion();
244
		}
245
246
		/**
247
		 * @param $eau
248
		 * @param $electricite
249
		 * @param $fer
250
		 * @param $fuel
251
		 * @param $nourriture
252
		 * @param $signe -> contient + ou -
253
		 * fonction qui permet de retirer des ressources pour construire des batiment ou creer unités
254
		 */
255
		public function setUpdateRessource($eau, $electricite, $fer, $fuel, $nourriture, $signe) {
256
			$dbc = App::getDb();
257
258
			//soit on enelve ou on ajoute
259
			if ($signe == "-") {
260
				$calc = $this->getCalcRetirerRessource($eau, $electricite, $fer, $fuel, $nourriture);
261
			}
262
			else {
263
				$calc = $this->getCalcAjoutRessource($eau, $electricite, $fer, $fuel, $nourriture);
264
			}
265
			
266
			Bataille::setValues([
267
				"eau" => $calc["eau"],
268
				"electricite" => $calc["electricite"],
269
				"fer" => $calc["fer"],
270
				"fuel" => $calc["fuel"],
271
				"nourriture" => $calc["nourriture"],
272
				"max_eau" => $this->getStockageMax("eau"),
273
				"max_electricite" => $this->getStockageMax("electricite"),
274
				"max_fer" => $this->getStockageMax("fer"),
275
				"max_fuel" => $this->getStockageMax("fuel"),
276
				"max_nourriture" => $this->getStockageMax("nourriture")
277
			]);
278
279
280
			$dbc->update("eau", $calc["eau"])
281
				->update("electricite", $calc["electricite"])
282
				->update("fer", $calc["fer"])
283
				->update("fuel", $calc["fuel"])
284
				->update("nourriture", $calc["nourriture"])
285
				->from("_bataille_base")
286
				->where("ID_base", "=", $this->id_base)
287
				->set();
288
		}
289
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
290
	}