1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stu\Module\Spacecraft\Lib\Creation; |
4
|
|
|
|
5
|
|
|
use Override; |
6
|
|
|
use SebastianBergmann\Diff\Differ; |
7
|
|
|
use SebastianBergmann\Diff\Output\DiffOnlyOutputBuilder; |
8
|
|
|
use Stu\Module\Logging\StuLogger; |
9
|
|
|
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum; |
10
|
|
|
use Stu\Module\Message\Lib\PrivateMessageSenderInterface; |
11
|
|
|
use Stu\Module\PlayerSetting\Lib\UserEnum; |
12
|
|
|
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface; |
13
|
|
|
use Stu\Orm\Entity\SpacecraftBuildplanInterface; |
14
|
|
|
use Stu\Orm\Entity\SpacecraftInterface; |
15
|
|
|
use Stu\Orm\Repository\ConstructionProgressRepositoryInterface; |
16
|
|
|
use Stu\Orm\Repository\SpacecraftBuildplanRepositoryInterface; |
17
|
|
|
|
18
|
|
|
class SpacecraftCorrector implements SpacecraftCorrectorInterface |
19
|
|
|
{ |
20
|
|
|
private const string HEADER = "--- Vorher\n+++ Nachher\n"; |
|
|
|
|
21
|
|
|
|
22
|
1 |
|
public function __construct( |
23
|
|
|
private SpacecraftBuildplanRepositoryInterface $spacecraftBuildplanRepository, |
24
|
|
|
private ConstructionProgressRepositoryInterface $constructionProgressRepository, |
25
|
|
|
private SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory, |
26
|
|
|
private PrivateMessageSenderInterface $privateMessageSender |
27
|
1 |
|
) {} |
28
|
|
|
|
29
|
|
|
#[Override] |
30
|
|
|
public function correctAllSpacecrafts(): void |
31
|
|
|
{ |
32
|
|
|
$count = 0; |
33
|
|
|
|
34
|
|
|
$differ = new Differ(new DiffOnlyOutputBuilder(self::HEADER)); |
35
|
|
|
|
36
|
|
|
// correct all ships |
37
|
|
|
foreach ($this->spacecraftBuildplanRepository->getAllNonNpcBuildplans() as $buildplan) { |
38
|
|
|
|
39
|
|
|
$spacecrafts = $buildplan->getSpacecraftList(); |
40
|
|
|
if ($spacecrafts->isEmpty()) { |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($spacecrafts as $spacecraft) { |
45
|
|
|
if ($this->correctSpacecraft($spacecraft, $buildplan, $differ)) { |
46
|
|
|
$count++; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// correct all stations |
52
|
|
|
foreach ($this->constructionProgressRepository->findAll() as $progress) { |
53
|
|
|
|
54
|
|
|
$station = $progress->getStation(); |
55
|
|
|
$buildplan = $station->getBuildplan(); |
56
|
|
|
if ( |
57
|
|
|
$buildplan === null |
58
|
|
|
|| $progress->getRemainingTicks() !== 0 |
59
|
|
|
) { |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($this->correctSpacecraft( |
64
|
|
|
$progress->getStation(), |
65
|
|
|
$buildplan, |
66
|
|
|
$differ |
67
|
|
|
)) { |
68
|
|
|
$count++; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($count > 0) { |
73
|
|
|
StuLogger::logf('corrected %d spacecrafts.', $count); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function correctSpacecraft(SpacecraftInterface $spacecraft, SpacecraftBuildplanInterface $buildplan, Differ $differ): bool |
78
|
|
|
{ |
79
|
|
|
$rump = $buildplan->getRump(); |
80
|
|
|
$wrapper = $this->spacecraftWrapperFactory->wrapSpacecraft($spacecraft); |
81
|
|
|
$toStringBefore = $wrapper->__toString(); |
82
|
|
|
|
83
|
|
|
foreach ($buildplan->getModules() as $buildplanModule) { |
84
|
|
|
$moduleType = $buildplanModule->getModuleType(); |
85
|
|
|
$moduleRumpWrapper = $moduleType->getModuleRumpWrapperCallable()($rump, $buildplan); |
86
|
|
|
$moduleRumpWrapper->apply($wrapper); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$toStringAfter = $wrapper->__toString(); |
90
|
|
|
$diff = $differ->diff($toStringBefore, $toStringAfter); |
91
|
|
|
|
92
|
|
|
if (strlen($diff) > strlen(self::HEADER)) { |
93
|
|
|
StuLogger::logf( |
94
|
|
|
'spacecraftId %d corrected: %s', |
95
|
|
|
$spacecraft->getId(), |
96
|
|
|
$diff |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$this->sendPmToOwner($spacecraft, $diff); |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function sendPmToOwner(SpacecraftInterface $spacecraft, string $diff): void |
108
|
|
|
{ |
109
|
|
|
$this->privateMessageSender |
110
|
|
|
->send( |
111
|
|
|
UserEnum::USER_NOONE, |
112
|
|
|
$spacecraft->getUser()->getId(), |
113
|
|
|
sprintf( |
114
|
|
|
"Die Werte der %s wurden automatisch wie folgt korrigiert:\n\n%s", |
115
|
|
|
$spacecraft->getName(), |
116
|
|
|
$diff |
117
|
|
|
), |
118
|
|
|
PrivateMessageFolderTypeEnum::SPECIAL_SYSTEM, |
119
|
|
|
$spacecraft->getHref() |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|