Passed
Push — master ( 3713ac...703043 )
by Anthony
02:58
created

Ressource::setUpdateRessource()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 34
rs 8.8571
cc 2
eloc 25
nc 2
nop 6
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
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
111
112
113
114
		//-------------------------- SETTER ----------------------------------------------------------------------------//
115
		/**
116
		 * set la date de derniere connexion a now
117
		 */
118 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...
119
			$dbc = App::getDb();
120
			
121
			$dbc->update("last_connexion", date("Y-m-d H:i:s"))
122
				->from("_bataille_base")
123
				->where("ID_identite", "=", Bataille::getIdIdentite(), "AND")
124
				->where("ID_base", "=", $this->id_base)
125
				->set();
126
		}
127
		
128
		/**
129
		 * fonction qui au chargement de la base regardera la derniere co du joueur
130
		 * si elle est supérieur à 30sec on recalculera les ressources des bases du joueur
131
		 */
132
		public function setActualiserRessource() {
133
			$last_co = $this->getLastConnexion();
134
135
			$today = Bataille::getToday();
136
137
			$last_co = new \DateTime($last_co);
138
			$last_co = $last_co->getTimestamp();
139
140
			$diff_temps = $today-$last_co;
141
142
			//si la derniere actualisation ou connexion est supérieur à 30 sec
143
			$this->setAddRessource("eau", $this->eau, $diff_temps);
144
			$this->setAddRessource("electricite", $this->electricite, $diff_temps);
145
			$this->setAddRessource("fuel", $this->fuel, $diff_temps);
146
			$this->setAddRessource("fer", $this->fer, $diff_temps);
147
		}
148
149
		/**
150
		 * @param $nom_ressource
151
		 * @param $ressrouce
152
		 * @param $diff_temps
153
		 * fonction qui ajoute les ressources qu'on a eu dans la base et qui reinitialise la last co a now
154
		 */
155
		private function setAddRessource($nom_ressource, $ressrouce, $diff_temps) {
156
			$dbc = App::getDb();
157
158
			$ressource = $ressrouce+(round((Bataille::getBatiment()->getProduction($nom_ressource)/3600)*$diff_temps));
159
			
160
			$stockage_max = Bataille::getBatiment()->getStockage();
161
			if ($nom_ressource == "nourriture") {
162
				$stockage_max = Bataille::getBatiment()->getStockage("grenier");
163
			}
164
165
			if ($ressource > $stockage_max) {
166
				$ressource = $stockage_max;
167
			}
168
169
			$dbc->update($nom_ressource, $ressource)
170
				->from("_bataille_base")
171
				->where("ID_base", "=", $this->id_base)
172
				->set();
173
174
			$this->$nom_ressource = $ressource;
175
176
			$this->setLastConnexion();
177
		}
178
179
		/**
180
		 * @param $eau
181
		 * @param $electricite
182
		 * @param $fer
183
		 * @param $fuel
184
		 * @param $nourriture
185
		 * @param $signe -> contient + ou -
186
		 * fonction qui permet de retirer des ressources pour construire des batiment ou creer unités
187
		 */
188
		public function setUpdateRessource($eau, $electricite, $fer, $fuel, $nourriture, $signe) {
189
			$dbc = App::getDb();
190
191
			//soit on enelve ou on ajoute
192
			if ($signe == "-") {
193
				$calc = $this->getCalcRetirerRessource($eau, $electricite, $fer, $fuel, $nourriture);
194
			}
195
			else {
196
				$calc = $this->getCalcAjoutRessource($eau, $electricite, $fer, $fuel, $nourriture);
197
			}
198
			
199
			Bataille::setValues([
200
				"eau" => $calc["eau"],
201
				"electricite" => $calc["electricite"],
202
				"fer" => $calc["fer"],
203
				"fuel" => $calc["fuel"],
204
				"nourriture" => $calc["nourriture"],
205
				"max_eau" => $this->getStockageMax("eau"),
206
				"max_electricite" => $this->getStockageMax("electricite"),
207
				"max_fer" => $this->getStockageMax("fer"),
208
				"max_fuel" => $this->getStockageMax("fuel"),
209
				"max_nourriture" => $this->getStockageMax("nourriture")
210
			]);
211
212
213
			$dbc->update("eau", $calc["eau"])
214
				->update("electricite", $calc["electricite"])
215
				->update("fer", $calc["fer"])
216
				->update("fuel", $calc["fuel"])
217
				->update("nourriture", $calc["nourriture"])
218
				->from("_bataille_base")
219
				->where("ID_base", "=", $this->id_base)
220
				->set();
221
		}
222
		
223
		/**
224
		 * @param $eau
225
		 * @param $electricite
226
		 * @param $fer
227
		 * @param $fuel
228
		 * @param $nourriture
229
		 * @return array
230
		 * fonction qui gere le calcul d'ajout de setUpdateRessource
231
		 */
232
		private function getCalcAjoutRessource($eau, $electricite, $fer, $fuel, $nourriture) {
233
			$eau = $this->getEau()+$eau;
234
			$electricite = $this->getElectricite()+$electricite;
235
			$fer = $this->getFer()+$fer;
236
			$fuel = $this->getFuel()+$fuel;
237
			$nourriture = $this->getNourriture()+$nourriture;
238
			
239
			$stockage_max = Bataille::getBatiment()->getStockage();
240
			$stockage_max_grenier = Bataille::getBatiment()->getStockage("grenier");
241
			
242
			if ($eau > $stockage_max) $eau = $stockage_max;
243
			if ($electricite > $stockage_max) $electricite = $stockage_max;
244
			if ($fer > $stockage_max) $fer = $stockage_max;
245
			if ($fuel > $stockage_max) $fuel = $stockage_max;
246
			if ($nourriture > $stockage_max_grenier) $nourriture = $stockage_max_grenier;
247
			
248
			return [
249
				"eau" => $eau,
250
				"electricite" => $electricite,
251
				"fer" => $fer,
252
				"fuel" => $fuel,
253
				"nourriture" => $nourriture
254
			];
255
		}
256
		
257
		/**
258
		 * @param $eau
259
		 * @param $electricite
260
		 * @param $fer
261
		 * @param $fuel
262
		 * @param $nourriture
263
		 * @return array
264
		 * fonction qui gere le calcul pour retirer de setUpdateRessource
265
		 */
266
		private function getCalcRetirerRessource($eau, $electricite, $fer, $fuel, $nourriture) {
267
			$eau = $this->eau - $eau;
268
			$electricite = $this->electricite - $electricite;
269
			$fer = $this->fer - $fer;
270
			$fuel = $this->fuel - $fuel;
271
			$nourriture = $this->nourriture - $nourriture;
272
			
273
			if ($eau < 0) $eau = 0;
274
			if ($electricite < 0) $electricite = 0;
275
			if ($fer < 0) $fer = 0;
276
			if ($fuel < 0) $fuel = 0;
277
			if ($nourriture < 0) $nourriture = 0;
278
			
279
			return [
280
				"eau" => $eau,
281
				"electricite" => $electricite,
282
				"fer" => $fer,
283
				"fuel" => $fuel,
284
				"nourriture" => $nourriture
285
			];
286
		}
287
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
288
	}