1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stu\Module\NPC\Action; |
6
|
|
|
|
7
|
|
|
use Override; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use request; |
11
|
|
|
use Stu\Component\Ship\Buildplan\BuildplanSignatureCreationInterface; |
12
|
|
|
use Stu\Component\Ship\Crew\ShipCrewCalculatorInterface; |
13
|
|
|
use Stu\Module\Control\ActionControllerInterface; |
14
|
|
|
use Stu\Module\Control\GameControllerInterface; |
15
|
|
|
use Stu\Orm\Repository\BuildplanModuleRepositoryInterface; |
16
|
|
|
use Stu\Orm\Repository\ModuleRepositoryInterface; |
17
|
|
|
use Stu\Orm\Repository\ShipBuildplanRepositoryInterface; |
18
|
|
|
use Stu\Orm\Repository\ShipRumpModuleLevelRepositoryInterface; |
19
|
|
|
use Stu\Orm\Repository\ShipRumpRepositoryInterface; |
20
|
|
|
use Stu\Orm\Repository\UserRepositoryInterface; |
21
|
|
|
use Stu\Module\ShipModule\ModuleSpecialAbilityEnum; |
22
|
|
|
use Stu\Module\NPC\View\ShowBuildplanCreator\ShowBuildplanCreator; |
23
|
|
|
use Stu\Orm\Repository\NPCLogRepositoryInterface; |
24
|
|
|
|
25
|
|
|
final class CreateBuildplan implements ActionControllerInterface |
26
|
|
|
{ |
27
|
|
|
public const string ACTION_IDENTIFIER = 'B_CREATE_BUILDPLAN'; |
|
|
|
|
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
private EntityManagerInterface $entityManager, |
31
|
|
|
private ShipRumpRepositoryInterface $shipRumpRepository, |
32
|
|
|
private ShipRumpModuleLevelRepositoryInterface $shipRumpModuleLevelRepository, |
33
|
|
|
private ModuleRepositoryInterface $moduleRepository, |
34
|
|
|
private ShipBuildplanRepositoryInterface $buildplanRepository, |
35
|
|
|
private BuildplanModuleRepositoryInterface $buildplanModuleRepository, |
36
|
|
|
private UserRepositoryInterface $userRepository, |
37
|
|
|
private ShipCrewCalculatorInterface $shipCrewCalculator, |
38
|
|
|
private BuildplanSignatureCreationInterface $buildplanSignatureCreation, |
39
|
|
|
private NPCLogRepositoryInterface $npcLogRepository |
40
|
|
|
) {} |
41
|
|
|
|
42
|
|
|
#[Override] |
43
|
|
|
public function handle(GameControllerInterface $game): void |
44
|
|
|
{ |
45
|
|
|
$game->setView(ShowBuildplanCreator::VIEW_IDENTIFIER); |
46
|
|
|
$userId = request::postIntFatal('userId'); |
47
|
|
|
$rumpId = request::postIntFatal('rumpId'); |
48
|
|
|
$moduleList = request::postArray('mod'); |
49
|
|
|
$moduleSpecialList = request::postArray('special_mod'); |
50
|
|
|
|
51
|
|
|
if (!$game->isAdmin() && !$game->isNpc()) { |
52
|
|
|
$game->addInformation(_('[b][color=#ff2626]Aktion nicht möglich, Spieler ist kein Admin/NPC![/color][/b]')); |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$rump = $this->shipRumpRepository->find($rumpId); |
57
|
|
|
if ($rump === null) { |
58
|
|
|
throw new RuntimeException(sprintf('rumpId %d does not exist!', $rumpId)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$mod_level = $this->shipRumpModuleLevelRepository->getByShipRump($rump->getId()); |
62
|
|
|
if ($mod_level === null) { |
63
|
|
|
throw new RuntimeException(sprintf('No module levels found for rump %d', $rump->getId())); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (count($moduleList) < $mod_level->getMandatoryModulesCount()) { |
67
|
|
|
$game->addInformation('Nicht alle benötigten Module wurden ausgewählt'); |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$user = $this->userRepository->find($userId); |
72
|
|
|
if ($user === null) { |
73
|
|
|
throw new RuntimeException(sprintf('userId %d does not exist', $userId)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$moduleIds = array_merge($moduleList, $moduleSpecialList); |
77
|
|
|
$signature = $this->buildplanSignatureCreation->createSignatureByModuleIds( |
78
|
|
|
$moduleIds, |
79
|
|
|
0 |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$plan = $this->buildplanRepository->getByUserShipRumpAndSignature($userId, $rump->getId(), $signature); |
83
|
|
|
|
84
|
|
|
if ($plan === null) { |
85
|
|
|
$planname = sprintf( |
86
|
|
|
'Bauplan %s %s', |
87
|
|
|
$rump->getName(), |
88
|
|
|
date('d.m.Y H:i') |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$plan = $this->buildplanRepository->prototype(); |
92
|
|
|
$plan->setUser($user); |
93
|
|
|
$plan->setRump($rump); |
94
|
|
|
$plan->setName($planname); |
95
|
|
|
$plan->setSignature($signature); |
96
|
|
|
$plan->setBuildtime(0); |
97
|
|
|
|
98
|
|
|
$this->buildplanRepository->save($plan); |
99
|
|
|
$this->entityManager->flush(); |
100
|
|
|
|
101
|
|
|
$modules = []; |
102
|
|
|
|
103
|
|
|
foreach ($moduleList as $moduleId) { |
104
|
|
|
$module = $this->moduleRepository->find($moduleId); |
105
|
|
|
if ($module === null) { |
106
|
|
|
throw new RuntimeException(sprintf('moduleId %d does not exist', $moduleId)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$mod = $this->buildplanModuleRepository->prototype(); |
110
|
|
|
$mod->setModuleType($module->getType()); |
111
|
|
|
$mod->setBuildplan($plan); |
112
|
|
|
$mod->setModule($module); |
113
|
|
|
|
114
|
|
|
$modules[$moduleId] = $module; |
115
|
|
|
|
116
|
|
|
$this->buildplanModuleRepository->save($mod); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
foreach ($moduleSpecialList as $moduleId) { |
120
|
|
|
$module = $this->moduleRepository->find($moduleId); |
121
|
|
|
if ($module === null) { |
122
|
|
|
throw new RuntimeException(sprintf('moduleId %d does not exist', $moduleId)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$mod = $this->buildplanModuleRepository->prototype(); |
126
|
|
|
$mod->setModuleType($module->getType()); |
127
|
|
|
$mod->setBuildplan($plan); |
128
|
|
|
$mod->setModule($module); |
129
|
|
|
$mod->setModuleSpecial(ModuleSpecialAbilityEnum::getHash($module->getSpecials())); |
130
|
|
|
|
131
|
|
|
$modules[$moduleId] = $module; |
132
|
|
|
|
133
|
|
|
$this->buildplanModuleRepository->save($mod); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$crewInput = request::postInt('crew_input'); |
137
|
|
|
|
138
|
|
|
if ($crewInput > 0) { |
139
|
|
|
$plan->setCrew($crewInput); |
140
|
|
|
} else { |
141
|
|
|
$plan->setCrew($this->shipCrewCalculator->getCrewUsage($modules, $rump, $user)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->buildplanRepository->save($plan); |
145
|
|
|
|
146
|
|
|
$this->entityManager->flush(); |
147
|
|
|
|
148
|
|
|
$moduleNames = []; |
149
|
|
|
foreach ($modules as $module) { |
150
|
|
|
$moduleNames[] = $module->getName(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$reason = request::postString('reason'); |
154
|
|
|
|
155
|
|
|
if ($reason === '') { |
156
|
|
|
$game->addInformation("Grund fehlt"); |
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$logText = sprintf( |
161
|
|
|
'%s hat für Spieler %s (%s) einen Bauplan erstellt. Rumpf: %s, Module: %s, Crew: %d, Grund: %s', |
162
|
|
|
$game->getUser()->getName(), |
163
|
|
|
$user->getName(), |
164
|
|
|
$user->getId(), |
165
|
|
|
$rump->getName(), |
166
|
|
|
implode(', ', $moduleNames), |
167
|
|
|
$plan->getCrew(), |
168
|
|
|
$reason |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
$this->createLogEntry($logText, $game->getUser()->getId()); |
172
|
|
|
|
173
|
|
|
$game->addInformation('Bauplan wurde erstellt'); |
174
|
|
|
} else { |
175
|
|
|
$game->addInformation('Bauplan existiert bereits'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function createLogEntry(string $text, int $userId): void |
180
|
|
|
{ |
181
|
|
|
$entry = $this->npcLogRepository->prototype(); |
182
|
|
|
$entry->setText($text); |
183
|
|
|
$entry->setSourceUserId($userId); |
184
|
|
|
$entry->setDate(time()); |
185
|
|
|
|
186
|
|
|
$this->npcLogRepository->save($entry); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
#[Override] |
190
|
|
|
public function performSessionCheck(): bool |
191
|
|
|
{ |
192
|
|
|
return true; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|