|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Module\NPC\Action; |
|
6
|
|
|
|
|
7
|
|
|
use Override; |
|
8
|
|
|
use request; |
|
9
|
|
|
use Stu\Exception\AccessViolationException; |
|
10
|
|
|
use Stu\Lib\CleanTextUtils; |
|
11
|
|
|
use Stu\Module\Control\ActionControllerInterface; |
|
12
|
|
|
use Stu\Module\Control\GameControllerInterface; |
|
13
|
|
|
use Stu\Orm\Repository\SpacecraftBuildplanRepositoryInterface; |
|
14
|
|
|
use Stu\Orm\Repository\NPCLogRepositoryInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class RenameBuildplan implements ActionControllerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
public const string ACTION_IDENTIFIER = 'B_CHANGE_BUILDPLAN_NAME'; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
1 |
|
public function __construct( |
|
21
|
|
|
private SpacecraftBuildplanRepositoryInterface $spacecraftBuildplanRepository, |
|
22
|
|
|
private NPCLogRepositoryInterface $npcLogRepository |
|
23
|
1 |
|
) {} |
|
24
|
|
|
|
|
25
|
|
|
#[Override] |
|
26
|
|
|
public function handle(GameControllerInterface $game): void |
|
27
|
|
|
{ |
|
28
|
|
|
$userId = $game->getUser()->getId(); |
|
29
|
|
|
|
|
30
|
|
|
if (!$game->isAdmin() && !$game->isNpc()) { |
|
31
|
|
|
$game->addInformation(_('[b][color=#ff2626]Aktion nicht möglich, Spieler ist kein Admin/NPC![/color][/b]')); |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$buildplanId = request::postIntFatal('planid'); |
|
36
|
|
|
$newName = CleanTextUtils::clearEmojis(request::postStringFatal('newName')); |
|
37
|
|
|
|
|
38
|
|
|
if (mb_strlen($newName) === 0) { |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$nameWithoutUnicode = CleanTextUtils::clearUnicode($newName); |
|
43
|
|
|
if ($newName !== $nameWithoutUnicode) { |
|
44
|
|
|
$game->addInformation(_('Der Name enthält ungültigen Unicode')); |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (mb_strlen($newName) > 255) { |
|
49
|
|
|
$game->addInformation(_('Der Name ist zu lang (Maximum: 255 Zeichen)')); |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$plan = $this->spacecraftBuildplanRepository->find($buildplanId); |
|
54
|
|
|
if ($plan === null || $plan->getUserId() !== $userId) { |
|
55
|
|
|
throw new AccessViolationException(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$oldName = $plan->getName(); |
|
59
|
|
|
$plan->setName($newName); |
|
60
|
|
|
|
|
61
|
|
|
$this->spacecraftBuildplanRepository->save($plan); |
|
62
|
|
|
|
|
63
|
|
|
if ($game->getUser()->isNpc()) { |
|
64
|
|
|
$this->createLogEntry($oldName, $newName, $userId, $game->getUser()->getName(), $plan->getUser()->getName()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$game->addInformation(_('Der Name des Bauplans wurde geändert')); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function createLogEntry(string $oldName, string $newName, int $userId, string $userName, string $planuser): void |
|
71
|
|
|
{ |
|
72
|
|
|
$logText = sprintf( |
|
73
|
|
|
'%s hat den Bauplan %s des Spielers %s zu %s umbenannt.', |
|
74
|
|
|
$userName, |
|
75
|
|
|
$oldName, |
|
76
|
|
|
$planuser, |
|
77
|
|
|
$newName |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$entry = $this->npcLogRepository->prototype(); |
|
81
|
|
|
$entry->setText($logText); |
|
82
|
|
|
$entry->setSourceUserId($userId); |
|
83
|
|
|
$entry->setDate(time()); |
|
84
|
|
|
|
|
85
|
|
|
$this->npcLogRepository->save($entry); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
#[Override] |
|
89
|
|
|
public function performSessionCheck(): bool |
|
90
|
|
|
{ |
|
91
|
|
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|