1 | <?php |
||||
2 | /** |
||||
3 | * Created by Gorlum 18.04.2018 16:30 |
||||
4 | */ |
||||
5 | |||||
6 | namespace Fleet; |
||||
7 | |||||
8 | |||||
9 | use Core\EntityDb; |
||||
10 | use SN; |
||||
11 | |||||
12 | /** |
||||
13 | * Class Fleet |
||||
14 | * @package Fleet |
||||
15 | * |
||||
16 | * @property int|string $id - bigint - |
||||
17 | * @property int|string $ownerId - bigint - Fleet player owner ID |
||||
18 | * @property int $fleet_mission - int - |
||||
19 | * @property int|string $fleet_amount - bigint - |
||||
20 | * @property string $fleet_array - mediumtext - |
||||
21 | * @property int $timeLaunch - int - Fleet launched from source planet (unix) |
||||
22 | * @property int $timeArrive - int - Time fleet arrive to destination (unix) |
||||
23 | * @property int $timeEndStay - int - Time when fleet operation on destination complete (if any) (unix) |
||||
24 | * @property int $timeReturn - int - Time fleet would return to source planet (unix) |
||||
25 | * @property int|string $fleet_start_planet_id - bigint - |
||||
26 | * @property int $fleet_start_galaxy - int - |
||||
27 | * @property int $fleet_start_system - int - |
||||
28 | * @property int $fleet_start_planet - int - |
||||
29 | * @property int $fleet_start_type - int - |
||||
30 | * @property int|string $fleet_end_planet_id - bigint - |
||||
31 | * @property int $fleet_end_galaxy - int - |
||||
32 | * @property int $fleet_end_system - int - |
||||
33 | * @property int $fleet_end_planet - int - |
||||
34 | * @property int $fleet_end_type - int - |
||||
35 | * @property int|string $fleet_resource_metal - decimal - |
||||
36 | * @property int|string $fleet_resource_crystal - decimal - |
||||
37 | * @property int|string $fleet_resource_deuterium - decimal - |
||||
38 | * @property int|string $fleet_target_owner - int - |
||||
39 | * @property int|string $fleet_group - varchar - |
||||
40 | * @property int $status - int - Current fleet status: flying to destination; returning |
||||
41 | * |
||||
42 | * Old fields for direct access |
||||
43 | * @property int $fleet_id |
||||
44 | * @property int $fleet_owner |
||||
45 | * @property int $start_time Time when fleet launched from source |
||||
46 | * @property int $fleet_start_time Time when fleet will arrive to destination point. Wrong name - should be `fleet_dst_arrive` |
||||
47 | * @property int $fleet_end_stay Time when fleet will end its mission on destination point. Should be `fleet_dst_stay_until` |
||||
48 | * @property int $fleet_end_time Time when fleet will return to source point. Should be `fleet_return_to_src` |
||||
49 | * @property int $fleet_mess |
||||
50 | * |
||||
51 | */ |
||||
52 | class Fleet extends EntityDb { |
||||
53 | |||||
54 | /** |
||||
55 | * @var string $_activeClass |
||||
56 | */ |
||||
57 | protected $_activeClass = '\\Fleet\\RecordFleet'; |
||||
58 | |||||
59 | /** |
||||
60 | * @var RecordFleet $_container |
||||
61 | */ |
||||
62 | protected $_container; |
||||
63 | |||||
64 | protected $_containerTranslateNames = [ |
||||
65 | 'ownerId' => 'fleet_owner', |
||||
66 | |||||
67 | 'timeLaunch' => 'start_time', |
||||
68 | 'timeArrive' => 'fleet_start_time', |
||||
69 | 'timeEndStay' => 'fleet_end_stay', |
||||
70 | 'timeReturn' => 'fleet_end_time', |
||||
71 | |||||
72 | 'status' => 'fleet_mess', |
||||
73 | ]; |
||||
74 | |||||
75 | /** |
||||
76 | * Information about ships |
||||
77 | * |
||||
78 | * @var array[] $shipInfo |
||||
79 | */ |
||||
80 | protected static $shipInfo = []; |
||||
81 | |||||
82 | protected $speedPercentTenth = 10; |
||||
83 | |||||
84 | /** |
||||
85 | * @var null|array $ownerRecord |
||||
86 | */ |
||||
87 | protected $ownerRecord = null; |
||||
88 | /** |
||||
89 | * @var null|array $sourcePlanet |
||||
90 | */ |
||||
91 | protected $sourcePlanet = null; |
||||
92 | |||||
93 | |||||
94 | /** |
||||
95 | * Fleet constructor. |
||||
96 | * |
||||
97 | * @throws \Exception |
||||
98 | */ |
||||
99 | public function __construct() { |
||||
100 | parent::__construct(); |
||||
101 | } |
||||
102 | |||||
103 | // Real fleet actions ------------------------------------------------------------------------------------------------ |
||||
104 | |||||
105 | /** |
||||
106 | * Forced return fleet to source planet |
||||
107 | * |
||||
108 | * @param int|string $byPlayerId |
||||
109 | * |
||||
110 | * @return bool |
||||
111 | */ |
||||
112 | public function returnForce($byPlayerId) { |
||||
113 | if ($this->ownerId != $byPlayerId) { |
||||
114 | return false; |
||||
115 | } |
||||
116 | |||||
117 | if ($this->status == FLEET_STATUS_RETURNING) { |
||||
118 | return true; |
||||
119 | } |
||||
120 | |||||
121 | // $ReturnFlyingTime = ($this->timeEndStay != 0 && $this->timeArrive < SN_TIME_NOW ? $this->timeArrive : SN_TIME_NOW) - $this->timeLaunch + SN_TIME_NOW + 1; |
||||
122 | $timeToReturn = SN_TIME_NOW - $this->timeLaunch + 1; |
||||
123 | $ReturnFlyingTime = (!empty($this->timeEndStay) && $this->timeArrive < SN_TIME_NOW ? $this->timeArrive : SN_TIME_NOW) + $timeToReturn; |
||||
124 | |||||
125 | // TODO - Those two lines should be removed - fleet times should be filtered on interface side |
||||
126 | $this->timeArrive = SN_TIME_NOW; |
||||
127 | !empty($this->timeEndStay) ? $this->timeEndStay = SN_TIME_NOW : false; |
||||
128 | |||||
129 | $this->timeReturn = $ReturnFlyingTime; |
||||
130 | $this->status = FLEET_STATUS_RETURNING; |
||||
131 | |||||
132 | return $this->dbUpdate(); |
||||
133 | } |
||||
134 | |||||
135 | // Service functions ----------------------------------------------------------------------------------------------- |
||||
136 | |||||
137 | /** |
||||
138 | * @return RecordFleet |
||||
139 | */ |
||||
140 | public function _getContainer() { |
||||
141 | return $this->_container; |
||||
142 | } |
||||
143 | |||||
144 | /** |
||||
145 | * @param int $shipId |
||||
146 | * |
||||
147 | * @return array |
||||
148 | */ |
||||
149 | protected static function getUnitInfo($shipId) { |
||||
150 | if (!isset(static::$shipInfo[$shipId])) { |
||||
151 | static::$shipInfo[$shipId] = get_unit_param($shipId); |
||||
152 | } |
||||
153 | |||||
154 | return static::$shipInfo[$shipId]; |
||||
155 | } |
||||
156 | |||||
157 | /** |
||||
158 | * @param int $resourceId |
||||
159 | * |
||||
160 | * @return float[] - [(int)$shipId => (float)costInMetal] |
||||
161 | */ |
||||
162 | public function getShipsBasicCosts($resourceId = RES_METAL) { |
||||
163 | $result = []; |
||||
164 | foreach ($this->getShipListArray() as $shipId => $shipAmount) { |
||||
165 | $result[$shipId] = getStackableUnitsCost([$shipId => 1], $resourceId); |
||||
166 | } |
||||
167 | |||||
168 | return $result; |
||||
169 | } |
||||
170 | |||||
171 | /** |
||||
172 | * Get cost of single ship in metal |
||||
173 | * |
||||
174 | * @param int $shipId |
||||
175 | * |
||||
176 | * @return int|float |
||||
177 | */ |
||||
178 | public function getShipCostInMetal($shipId) { |
||||
179 | return getStackableUnitsCost([$shipId => 1], RES_METAL); |
||||
180 | // |
||||
181 | // if(!isset(static::getUnitInfo($shipId)[P_COST_METAL])) { |
||||
182 | // static::$shipInfo[$shipId][P_COST_METAL] = get_unit_cost_in(static::getUnitInfo($shipId)[P_COST], RES_METAL); |
||||
183 | // } |
||||
184 | // |
||||
185 | // return static::getUnitInfo($shipId)[P_COST_METAL]; |
||||
186 | } |
||||
187 | |||||
188 | /** |
||||
189 | * Get fleet cost in metal |
||||
190 | * |
||||
191 | * @return float|int |
||||
192 | */ |
||||
193 | public function getCostInMetal() { |
||||
194 | return getStackableUnitsCost($this->getShipListArray(), RES_METAL); |
||||
195 | // |
||||
196 | // $result = 0; |
||||
197 | // foreach($this->getShipList() as $shipId => $amount) { |
||||
198 | // $result += $amount * $this->getShipCostInMetal($shipId); |
||||
199 | // } |
||||
200 | // |
||||
201 | // return $result; |
||||
202 | } |
||||
203 | |||||
204 | /** |
||||
205 | * Get single ship basic capacity |
||||
206 | * |
||||
207 | * @param int $shipId |
||||
208 | * |
||||
209 | * @return int|mixed |
||||
210 | */ |
||||
211 | public function getShipCapacity($shipId) { |
||||
212 | if (!isset(static::getUnitInfo($shipId)[P_CAPACITY])) { |
||||
213 | static::$shipInfo[$shipId][P_CAPACITY] = 0; |
||||
214 | } |
||||
215 | |||||
216 | return static::getUnitInfo($shipId)[P_CAPACITY]; |
||||
217 | } |
||||
218 | |||||
219 | /** |
||||
220 | * Get current fleet capacity counting loaded resources and fuel |
||||
221 | * |
||||
222 | * @return float|int |
||||
223 | */ |
||||
224 | public function getCapacityActual() { |
||||
225 | $result = 0; |
||||
226 | foreach ($this->getShipListArray() as $shipId => $amount) { |
||||
227 | $result += $amount * $this->getShipCapacity($shipId); |
||||
228 | } |
||||
229 | |||||
230 | $travelData = $this->getTravelData(); |
||||
231 | |||||
232 | $result = max(0, $result - array_sum($this->getResourceList()) - $travelData['consumption']); |
||||
233 | |||||
234 | return $result; |
||||
235 | } |
||||
236 | |||||
237 | public function isEmpty() { |
||||
238 | return $this->getShipCount() < 1; |
||||
239 | } |
||||
240 | |||||
241 | // Using RecordFleet functions --------------------------------------------------------------------------------------- |
||||
242 | |||||
243 | /** |
||||
244 | * @param int $shipSnId |
||||
245 | * @param float $shipCount |
||||
246 | * |
||||
247 | * @throws \Exception |
||||
248 | */ |
||||
249 | public function changeShipCount($shipSnId, $shipCount) { |
||||
250 | $this->_getContainer()->changeShipCount($shipSnId, $shipCount); |
||||
251 | } |
||||
252 | |||||
253 | /** |
||||
254 | * @param int $resourceId |
||||
255 | * @param float $resourceCount |
||||
256 | * |
||||
257 | * @throws \Exception |
||||
258 | */ |
||||
259 | public function changeResource($resourceId, $resourceCount) { |
||||
260 | $this->_getContainer()->changeResource($resourceId, $resourceCount); |
||||
261 | } |
||||
262 | |||||
263 | /** |
||||
264 | * @return float[] - [shipSnId => $shipAmount] |
||||
265 | */ |
||||
266 | public function getShipListArray() { |
||||
267 | return $this->_getContainer()->getShipList(); |
||||
268 | } |
||||
269 | |||||
270 | /** |
||||
271 | * @return float[] - [$resourceSnId => $resourceAmount] |
||||
272 | */ |
||||
273 | public function getResourceList() { |
||||
274 | return $this->_getContainer()->getResourceList(); |
||||
275 | } |
||||
276 | |||||
277 | /** |
||||
278 | * @return float|int |
||||
279 | */ |
||||
280 | public function getShipCount() { |
||||
281 | return array_sum($this->getShipListArray()); |
||||
282 | } |
||||
283 | |||||
284 | /** |
||||
285 | * @param float $multiplier |
||||
286 | * |
||||
287 | * @return int[]|float[] |
||||
288 | */ |
||||
289 | public function calcShipLossByMultiplier($multiplier) { |
||||
290 | $result = []; |
||||
291 | |||||
292 | foreach ($this->getShipListArray() as $unit_id => $unit_amount) { |
||||
293 | $shipsLost = ceil($unit_amount * $multiplier); |
||||
294 | $result[$unit_id] += $shipsLost; |
||||
295 | } |
||||
296 | |||||
297 | return $result; |
||||
298 | } |
||||
299 | |||||
300 | /** |
||||
301 | * @param int $missionId |
||||
302 | * |
||||
303 | * @return Fleet |
||||
304 | */ |
||||
305 | public function setMission($missionId) { |
||||
306 | $this->fleet_mission = $missionId; |
||||
307 | $this->status = FLEET_STATUS_FLYING; |
||||
308 | |||||
309 | return $this; |
||||
310 | } |
||||
311 | |||||
312 | /** |
||||
313 | * @param array $playerRecord |
||||
314 | * |
||||
315 | * @return Fleet |
||||
316 | */ |
||||
317 | public function setFleetOwnerRecord($playerRecord) { |
||||
318 | $this->ownerRecord = $playerRecord; |
||||
319 | !empty($this->ownerRecord['id']) ? $this->ownerId = $this->ownerRecord['id'] : false; |
||||
320 | |||||
321 | return $this; |
||||
322 | } |
||||
323 | |||||
324 | /** |
||||
325 | * @return array|false|null |
||||
326 | */ |
||||
327 | public function getFleetOwnerRecord() { |
||||
328 | if (!isset($this->ownerRecord['id']) && !empty($this->ownerId)) { |
||||
329 | // Trying to get owner record by id |
||||
330 | empty($this->ownerRecord = db_user_by_id($this->ownerId)) ? $this->ownerRecord = null : false; |
||||
0 ignored issues
–
show
Deprecated Code
introduced
by
![]() It seems like
$this->ownerId can also be of type string ; however, parameter $user_id_unsafe of db_user_by_id() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
331 | } |
||||
332 | |||||
333 | return $this->ownerRecord; |
||||
334 | } |
||||
335 | |||||
336 | /** |
||||
337 | * @param array $from |
||||
338 | * |
||||
339 | * @return Fleet |
||||
340 | */ |
||||
341 | public function setSourceFromPlanetRecord($from) { |
||||
342 | empty($this->ownerId) && !empty($from['id_owner']) ? $this->ownerId = $from['id_owner'] : false; |
||||
343 | |||||
344 | $this->fleet_start_planet_id = !empty($from['id']) && intval($from['id']) ? $from['id'] : null; |
||||
345 | |||||
346 | $this->fleet_start_galaxy = $from['galaxy']; |
||||
347 | $this->fleet_start_system = $from['system']; |
||||
348 | $this->fleet_start_planet = $from['planet']; |
||||
349 | $this->fleet_start_type = $from['planet_type']; |
||||
350 | |||||
351 | $this->sourcePlanet = $from; |
||||
352 | |||||
353 | return $this; |
||||
354 | } |
||||
355 | |||||
356 | |||||
357 | /** |
||||
358 | * @param $to |
||||
359 | * |
||||
360 | * @return Fleet |
||||
361 | */ |
||||
362 | public function setDestinationFromPlanetRecord($to) { |
||||
363 | empty($this->fleet_target_owner) ? $this->fleet_target_owner = !empty($to['id_owner']) && intval($to['id_owner']) ? $to['id_owner'] : 0 : false; |
||||
364 | |||||
365 | $this->fleet_end_planet_id = !empty($to['id']) && intval($to['id']) ? $to['id'] : null; |
||||
366 | |||||
367 | $this->fleet_end_galaxy = $to['galaxy']; |
||||
368 | $this->fleet_end_system = $to['system']; |
||||
369 | $this->fleet_end_planet = $to['planet']; |
||||
370 | $this->fleet_end_type = $to['planet_type']; |
||||
371 | |||||
372 | return $this; |
||||
373 | } |
||||
374 | |||||
375 | /** |
||||
376 | * @param array $fleet - list of units [(int)unitId => (float)unitAmount] |
||||
377 | * |
||||
378 | * @return Fleet |
||||
379 | * @throws \Exception |
||||
380 | */ |
||||
381 | public function setUnits($fleet) { |
||||
382 | foreach ($fleet as $unit_id => $amount) { |
||||
383 | if (!$amount || !$unit_id) { |
||||
384 | continue; |
||||
385 | } |
||||
386 | |||||
387 | if (in_array($unit_id, sn_get_groups('fleet'))) { |
||||
388 | /** @noinspection PhpUnhandledExceptionInspection */ |
||||
389 | $this->changeShipCount($unit_id, $amount); |
||||
390 | } elseif (in_array($unit_id, sn_get_groups('resources_loot'))) { |
||||
391 | /** @noinspection PhpUnhandledExceptionInspection */ |
||||
392 | $this->changeResource($unit_id, $amount); |
||||
393 | } |
||||
394 | } |
||||
395 | |||||
396 | return $this; |
||||
397 | } |
||||
398 | |||||
399 | /** |
||||
400 | * @param int $speedPercentTenth - fleet speed percent in 10% from 1..10 - i.e. 1 = 10%, 10 = 100% |
||||
401 | * |
||||
402 | * @return Fleet |
||||
403 | */ |
||||
404 | public function setSpeedPercentInTenth($speedPercentTenth) { |
||||
405 | $this->speedPercentTenth = max(0, min(10, intval($speedPercentTenth))); |
||||
406 | |||||
407 | return $this; |
||||
408 | } |
||||
409 | |||||
410 | /** |
||||
411 | * @param int $launchTime - unix timestamp when fleet leave source planet |
||||
412 | * @param int $stayDuration - seconds how long fleet should stay executing mission task (i.e. HOLD or EXPLORE) |
||||
413 | * |
||||
414 | * @return array |
||||
415 | */ |
||||
416 | public function calcTravelTimes($launchTime = SN_TIME_NOW, $stayDuration = 0) { |
||||
417 | $this->timeLaunch = $launchTime; |
||||
418 | |||||
419 | $travel_data = $this->getTravelData(); |
||||
420 | |||||
421 | $this->timeArrive = $this->timeLaunch + $travel_data['duration']; |
||||
422 | $this->timeEndStay = $this->fleet_mission == MT_EXPLORE || $this->fleet_mission == MT_HOLD ? $this->timeArrive + $stayDuration : 0; |
||||
423 | $this->timeReturn = $this->timeArrive + $stayDuration + $travel_data['duration']; |
||||
424 | |||||
425 | return $travel_data; |
||||
426 | } |
||||
427 | |||||
428 | |||||
429 | public function save() { |
||||
430 | return parent::save(); // TODO: Change the autogenerated stub |
||||
431 | } |
||||
432 | |||||
433 | /** |
||||
434 | * @return array |
||||
435 | */ |
||||
436 | protected function getTravelData() { |
||||
437 | $travel_data = flt_travel_data( |
||||
438 | $this->getFleetOwnerRecord(), |
||||
439 | ['galaxy' => $this->fleet_start_galaxy, 'system' => $this->fleet_start_system, 'planet' => $this->fleet_start_planet,], |
||||
440 | ['galaxy' => $this->fleet_end_galaxy, 'system' => $this->fleet_end_system, 'planet' => $this->fleet_end_planet,], |
||||
441 | $this->getShipListArray(), |
||||
442 | $this->speedPercentTenth |
||||
443 | ); |
||||
444 | |||||
445 | return $travel_data; |
||||
446 | } |
||||
447 | |||||
448 | } |
||||
449 |