|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Stu\Module\Admin\Action; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use Override; |
| 9 | +use request; |
| 10 | +use Stu\Module\Admin\View\Scripts\ShowScripts; |
| 11 | +use Stu\Module\Control\ActionControllerInterface; |
| 12 | +use Stu\Module\Control\GameControllerInterface; |
| 13 | +use Stu\Module\Crew\Lib\CrewCreatorInterface; |
| 14 | +use Stu\Orm\Repository\ColonyRepositoryInterface; |
| 15 | +use Stu\Orm\Repository\SpacecraftRepositoryInterface; |
| 16 | + |
| 17 | +final class CreateCrew implements ActionControllerInterface |
| 18 | +{ |
| 19 | + public const string ACTION_IDENTIFIER = 'B_CREATE_CREW'; |
| 20 | + |
| 21 | + public function __construct( |
| 22 | + private CrewCreatorInterface $crewCreator, |
| 23 | + private SpacecraftRepositoryInterface $spacecraftRepository, |
| 24 | + private ColonyRepositoryInterface $colonyRepository |
| 25 | + ) {} |
| 26 | + |
| 27 | + #[Override] |
| 28 | + public function handle(GameControllerInterface $game): void |
| 29 | + { |
| 30 | + $game->setView(ShowScripts::VIEW_IDENTIFIER); |
| 31 | + |
| 32 | + if (!$game->isAdmin()) { |
| 33 | + $game->getInfo()->addInformation(_('[b][color=#ff2626]Aktion nicht möglich, Spieler ist kein Admin![/color][/b]')); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + $spacecraftId = request::postInt('spacecraft_id'); |
| 38 | + $colonyId = request::postInt('colony_id'); |
| 39 | + $amount = request::postIntFatal('crew_amount'); |
| 40 | + |
| 41 | + if ($amount <= 0) { |
| 42 | + $game->getInfo()->addInformation('Anzahl muss größer als 0 sein'); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + if ($spacecraftId > 0 && $colonyId > 0) { |
| 47 | + $game->getInfo()->addInformation('Nur Spacecraft-ID ODER Kolonie-ID angeben'); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if ($spacecraftId === 0 && $colonyId === 0) { |
| 52 | + $game->getInfo()->addInformation('Spacecraft-ID oder Kolonie-ID angeben'); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if ($spacecraftId > 0) { |
| 57 | + $this->createCrewForSpacecraft($spacecraftId, $amount, $game); |
| 58 | + } else { |
| 59 | + $this->createCrewForColony($colonyId, $amount, $game); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private function createCrewForSpacecraft(int $spacecraftId, int $amount, GameControllerInterface $game): void |
| 64 | + { |
| 65 | + $spacecraft = $this->spacecraftRepository->find($spacecraftId); |
| 66 | + if ($spacecraft === null) { |
| 67 | + throw new InvalidArgumentException(sprintf('spacecraftId %d does not exist', $spacecraftId)); |
| 68 | + } |
| 69 | + |
| 70 | + $user = $spacecraft->getUser(); |
| 71 | + |
| 72 | + for ($i = 0; $i < $amount; $i++) { |
| 73 | + $crewAssignment = $this->crewCreator->create($user->getId()); |
| 74 | + |
| 75 | + $crewAssignment->setSpacecraft($spacecraft); |
| 76 | + $crewAssignment->setColony(null); |
| 77 | + $crewAssignment->setTradepost(null); |
| 78 | + |
| 79 | + $spacecraft->getCrewAssignments()->add($crewAssignment); |
| 80 | + } |
| 81 | + |
| 82 | + $this->spacecraftRepository->save($spacecraft); |
| 83 | + |
| 84 | + $game->getInfo()->addInformation(sprintf( |
| 85 | + '%d Crew wurde für das Raumschiff %s (ID: %d) von Spieler %s erstellt', |
| 86 | + $amount, |
| 87 | + $spacecraft->getName(), |
| 88 | + $spacecraftId, |
| 89 | + $user->getName() |
| 90 | + )); |
| 91 | + } |
| 92 | + |
| 93 | + private function createCrewForColony(int $colonyId, int $amount, GameControllerInterface $game): void |
| 94 | + { |
| 95 | + $colony = $this->colonyRepository->find($colonyId); |
| 96 | + if ($colony === null) { |
| 97 | + throw new InvalidArgumentException(sprintf('colonyId %d does not exist', $colonyId)); |
| 98 | + } |
| 99 | + |
| 100 | + $user = $colony->getUser(); |
| 101 | + |
| 102 | + for ($i = 0; $i < $amount; $i++) { |
| 103 | + $this->crewCreator->create($user->getId(), $colony); |
| 104 | + } |
| 105 | + |
| 106 | + $game->getInfo()->addInformation(sprintf( |
| 107 | + '%d Crew wurde für die Kolonie %s (ID: %d) von Spieler %s erstellt', |
| 108 | + $amount, |
| 109 | + $colony->getName(), |
| 110 | + $colonyId, |
| 111 | + $user->getName() |
| 112 | + )); |
| 113 | + } |
| 114 | + |
| 115 | + #[Override] |
| 116 | + public function performSessionCheck(): bool |
| 117 | + { |
| 118 | + return true; |
| 119 | + } |
| 120 | +} |
0 commit comments