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