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

Ressource::setLastConnexion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 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
		//-------------------------- 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
				$eau = $this->getEau()-$eau;
194
				$electricite = $this->getElectricite()-$electricite;
195
				$fer = $this->getFer()-$fer;
196
				$fuel = $this->getFuel()-$fuel;
197
				$nourriture = $this->getNourriture()-$nourriture;
198
				
199
				if ($eau < 0) $eau = 0;
200
				if ($electricite < 0) $electricite = 0;
201
				if ($fer < 0) $fer = 0;
202
				if ($fuel < 0) $fuel = 0;
203
				if ($nourriture < 0) $nourriture = 0;
204
			}
205
			else {
206
				$eau = $this->getEau()+$eau;
207
				$electricite = $this->getElectricite()+$electricite;
208
				$fer = $this->getFer()+$fer;
209
				$fuel = $this->getFuel()+$fuel;
210
				$nourriture = $this->getNourriture()+$nourriture;
211
212
				$stockage_max = Bataille::getBatiment()->getStockage();
213
				$stockage_max_grenier = Bataille::getBatiment()->getStockage("grenier");
214
215
				if ($eau > $stockage_max) $eau = $stockage_max;
216
				if ($electricite > $stockage_max) $electricite = $stockage_max;
217
				if ($fer > $stockage_max) $fer = $stockage_max;
218
				if ($fuel > $stockage_max) $fuel = $stockage_max;
219
				if ($nourriture > $stockage_max_grenier) $nourriture = $stockage_max_grenier;
220
			}
221
			
222
			Bataille::setValues([
223
				"eau" => $eau,
224
				"electricite" => $electricite,
225
				"fer" => $fer,
226
				"fuel" => $fuel,
227
				"nourriture" => $nourriture,
228
				"max_eau" => $this->getStockageMax("eau"),
229
				"max_electricite" => $this->getStockageMax("electricite"),
230
				"max_fer" => $this->getStockageMax("fer"),
231
				"max_fuel" => $this->getStockageMax("fuel"),
232
				"max_nourriture" => $this->getStockageMax("nourriture")
233
			]);
234
235
236
			$dbc->update("eau", $eau)
237
				->update("electricite", $electricite)
238
				->update("fer", $fer)
239
				->update("fuel", $fuel)
240
				->update("nourriture", $nourriture)
241
				->from("_bataille_base")
242
				->where("ID_base", "=", $this->id_base)
243
				->set();
244
		}
245
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
246
	}