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