|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Module\Spacecraft\Lib; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\Collection; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use Stu\Lib\Transfer\Storage\StorageManagerInterface; |
|
10
|
|
|
use Stu\Lib\SpacecraftManagement\Provider\ManagerProviderInterface; |
|
11
|
|
|
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum; |
|
12
|
|
|
use Stu\Module\Message\Lib\PrivateMessageSenderInterface; |
|
13
|
|
|
use Stu\Orm\Entity\Spacecraft; |
|
14
|
|
|
use Stu\Orm\Repository\SpacecraftRepositoryInterface; |
|
15
|
|
|
|
|
16
|
|
|
//TODO create unit test |
|
17
|
|
|
final class ReactorUtil implements ReactorUtilInterface |
|
18
|
|
|
{ |
|
19
|
1 |
|
public function __construct( |
|
20
|
|
|
private StorageManagerInterface $storageManager, |
|
21
|
|
|
private SpacecraftRepositoryInterface $spacecraftRepository, |
|
22
|
|
|
private PrivateMessageSenderInterface $privateMessageSender |
|
23
|
1 |
|
) {} |
|
24
|
|
|
|
|
25
|
|
|
#[\Override] |
|
26
|
|
|
public function storageContainsNeededCommodities(Collection $storages, ReactorWrapperInterface $reactor): bool |
|
27
|
|
|
{ |
|
28
|
|
|
foreach ($reactor->get()->getLoadCost() as $commodityId => $loadCost) { |
|
29
|
|
|
$storage = $storages->get($commodityId); |
|
30
|
|
|
|
|
31
|
|
|
if ($storage === null) { |
|
32
|
|
|
return false; |
|
33
|
|
|
} |
|
34
|
|
|
if ($storage->getAmount() < $loadCost) { |
|
35
|
|
|
return false; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
#[\Override] |
|
43
|
|
|
public function loadReactor( |
|
44
|
|
|
Spacecraft $spacecraft, |
|
45
|
|
|
int $additionalLoad, |
|
46
|
|
|
?ManagerProviderInterface $managerProvider, |
|
47
|
|
|
ReactorWrapperInterface $reactor |
|
48
|
|
|
): ?string { |
|
49
|
|
|
if ($reactor->getLoad() >= $reactor->getCapacity()) { |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$capaPerLoad = $reactor->get()->getLoadUnits(); |
|
54
|
|
|
|
|
55
|
|
|
//check for core limitation |
|
56
|
|
|
$loadUnits = ceil($additionalLoad / $capaPerLoad); |
|
57
|
|
|
if ($loadUnits * $capaPerLoad > $reactor->getCapacity() - $reactor->getLoad()) { |
|
58
|
|
|
$loadUnits = ceil(($reactor->getCapacity() - $reactor->getLoad()) / $capaPerLoad); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$loadUnits = (int) $loadUnits; |
|
62
|
|
|
|
|
63
|
|
|
if ($loadUnits < 1) { |
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$storage = $managerProvider !== null ? $managerProvider->getStorage() : $spacecraft->getStorage(); |
|
68
|
|
|
|
|
69
|
|
|
// check for ressource limitation |
|
70
|
|
|
$costs = $reactor->get()->getLoadCost(); |
|
71
|
|
|
foreach ($costs as $commodityId => $loadUnitsCost) { |
|
72
|
|
|
$storageElement = $storage->get($commodityId); |
|
73
|
|
|
|
|
74
|
|
|
if ($storageElement === null) { |
|
75
|
|
|
throw new RuntimeException('storageContainsNeededCommodities should be called first'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($storageElement->getAmount() < ($loadUnits * $loadUnitsCost)) { |
|
79
|
|
|
$loadUnits = (int) ($storageElement->getAmount() / $loadUnitsCost); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
//consume ressources |
|
84
|
|
|
foreach ($costs as $commodityId => $loadCost) { |
|
85
|
|
|
$storageElement = $storage->get($commodityId); |
|
86
|
|
|
|
|
87
|
|
|
if ($storageElement === null) { |
|
88
|
|
|
throw new RuntimeException('storageContainsNeededCommodities should be called first'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ($managerProvider !== null) { |
|
92
|
|
|
$managerProvider->lowerStorage( |
|
93
|
|
|
$storageElement->getCommodity(), |
|
94
|
|
|
$loadCost * $loadUnits |
|
95
|
|
|
); |
|
96
|
|
|
} else { |
|
97
|
|
|
$this->storageManager->lowerStorage( |
|
98
|
|
|
$spacecraft, |
|
99
|
|
|
$storageElement->getCommodity(), |
|
100
|
|
|
$loadCost * $loadUnits |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
//truncate output |
|
106
|
|
|
if ($reactor->getLoad() + $loadUnits * $capaPerLoad > $reactor->getCapacity()) { |
|
107
|
|
|
$loadUnits = $reactor->getCapacity() - $reactor->getLoad(); |
|
108
|
|
|
} else { |
|
109
|
|
|
$loadUnits *= $capaPerLoad; |
|
110
|
|
|
} |
|
111
|
|
|
$reactor->changeLoad($loadUnits); |
|
112
|
|
|
$this->spacecraftRepository->save($spacecraft); |
|
113
|
|
|
|
|
114
|
|
|
$systemName = $reactor->get()->getSystemType()->getDescription(); |
|
115
|
|
|
|
|
116
|
|
|
if ($managerProvider !== null) { |
|
117
|
|
|
$this->privateMessageSender->send( |
|
118
|
|
|
$managerProvider->getUser()->getId(), |
|
119
|
|
|
$spacecraft->getUser()->getId(), |
|
120
|
|
|
sprintf( |
|
121
|
|
|
_('Die %s hat in Sektor %s den %s der %s um %d Einheiten aufgeladen'), |
|
122
|
|
|
$managerProvider->getName(), |
|
123
|
|
|
$spacecraft->getSectorString(), |
|
124
|
|
|
$systemName, |
|
125
|
|
|
$spacecraft->getName(), |
|
126
|
|
|
$loadUnits |
|
127
|
|
|
), |
|
128
|
|
|
PrivateMessageFolderTypeEnum::SPECIAL_TRADE, |
|
129
|
|
|
$spacecraft |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return sprintf( |
|
134
|
|
|
_('%s: Der %s wurde um %d Einheiten aufgeladen'), |
|
135
|
|
|
$spacecraft->getName(), |
|
136
|
|
|
$systemName, |
|
137
|
|
|
$loadUnits |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|