diff --git a/grumphp.yml b/grumphp.yml index 13c9ae95..674d3a44 100644 --- a/grumphp.yml +++ b/grumphp.yml @@ -13,7 +13,7 @@ grumphp: phpstan: autoload_file: phpstan.neon configuration: ~ - memory_limit: "512M" + memory_limit: "256M" phpunit: ~ phpversion: project: '8.4' diff --git a/src/Application/Command/Keyforge/Deck/GenerateAlliances/GenerateDeckAlliancesCommand.php b/src/Application/Command/Keyforge/Deck/GenerateAlliances/GenerateDeckAlliancesCommand.php index 3a0d0d49..495d0f95 100644 --- a/src/Application/Command/Keyforge/Deck/GenerateAlliances/GenerateDeckAlliancesCommand.php +++ b/src/Application/Command/Keyforge/Deck/GenerateAlliances/GenerateDeckAlliancesCommand.php @@ -7,24 +7,47 @@ final readonly class GenerateDeckAlliancesCommand { - private(set) array $deckIds; - private(set) array $deckHouses; + private(set) array $decks; + private(set) ?string $extraCardType; + private(set) ?string $extraCards; + private(set) bool $addToMyDecks; + private(set) bool $addToOwnedDok; - public function __construct($deckIds, $deckHouses) + public function __construct($decks, $extraCardType, $extraCards, $addToMyDecks, $addToOwnedDok) { Assert::lazy() - ->that($deckIds, 'deckIds')->all()->uuid() - ->that($deckHouses, 'deckHouses')->isArray() + ->that($decks, 'decks')->all()->isArray() + ->that($extraCardType, 'extraCardType')->nullOr()->inArray(['Token', 'Prophecies']) + ->that($extraCards, 'extraCards')->nullOr()->string()->notBlank() + ->that($addToMyDecks, 'addToMyDecks')->boolean() + ->that($addToOwnedDok, 'addToOwnedDok')->boolean() ->verifyNow(); - foreach ($deckHouses as $index => $item) { + foreach ($decks as $id => $houses) { Assert::lazy() - ->that($index)->uuid() - ->that($item)->all()->inArray(KeyforgeHouse::values()) + ->that($id)->uuid() + ->that($houses)->all()->inArray(KeyforgeHouse::values()) ->verifyNow(); + + if (0 === count($houses)) { + unset($decks[$id]); + } } - $this->deckIds = $deckIds; - $this->deckHouses = $deckHouses; + $this->decks = $decks; + $this->extraCardType = $extraCardType; + $this->extraCards = $extraCards; + $this->addToMyDecks = $addToMyDecks; + $this->addToOwnedDok = $addToOwnedDok; + } + + public function deckIds(): array + { + return array_keys($this->decks); + } + + public function housesOf(string $deckId): array + { + return $this->decks[$deckId] ?? []; } } diff --git a/src/Application/Command/Keyforge/Deck/GenerateAlliances/GenerateDeckAlliancesCommandHandler.php b/src/Application/Command/Keyforge/Deck/GenerateAlliances/GenerateDeckAlliancesCommandHandler.php index 679e18b7..74323062 100644 --- a/src/Application/Command/Keyforge/Deck/GenerateAlliances/GenerateDeckAlliancesCommandHandler.php +++ b/src/Application/Command/Keyforge/Deck/GenerateAlliances/GenerateDeckAlliancesCommandHandler.php @@ -5,7 +5,9 @@ use AdnanMula\Cards\Domain\Model\Keyforge\Deck\KeyforgeDeck; use AdnanMula\Cards\Domain\Model\Keyforge\Deck\KeyforgeDeckAllianceRepository; use AdnanMula\Cards\Domain\Model\Keyforge\Deck\KeyforgeDeckRepository; +use AdnanMula\Cards\Domain\Model\Keyforge\Deck\ValueObject\KeyforgeDeckType; use AdnanMula\Cards\Domain\Model\Shared\User; +use AdnanMula\Cards\Domain\Model\Shared\ValueObject\Link; use AdnanMula\Cards\Domain\Model\Shared\ValueObject\UserRole; use AdnanMula\Cards\Domain\Model\Shared\ValueObject\Uuid; use AdnanMula\Cards\Domain\Service\Keyforge\ImportDeckAllianceService; @@ -16,6 +18,7 @@ use AdnanMula\Criteria\Filter\FilterType; use AdnanMula\Criteria\FilterField\FilterField; use AdnanMula\Criteria\FilterValue\StringArrayFilterValue; +use AdnanMula\Criteria\FilterValue\StringFilterValue; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\HttpFoundation\Request; use Symfony\Contracts\HttpClient\HttpClientInterface; @@ -42,18 +45,16 @@ public function __invoke(GenerateDeckAlliancesCommand $command): array $decks = $this->deckRepository->search(new Criteria( new Filters( FilterType::AND, - new Filter(new FilterField('id'), new StringArrayFilterValue(...$command->deckIds), FilterOperator::IN), + new Filter(new FilterField('id'), new StringArrayFilterValue(...$command->deckIds()), FilterOperator::IN), ), )); - if (\count($decks) !== \count(array_unique($command->deckIds))) { - throw new \Exception('Missing deck'); - } + $this->validations($command, ...$decks); - $this->validations(...$decks); + $pods = $this->pods($command, ...$decks); + $combinations = $this->combinations($command, $pods, $command->addToOwnedDok); - $pods = $this->pods($command->deckHouses, ...$decks); - $combinations = $this->combinations($pods); + $importedDecks = []; $authToken = $this->login(); @@ -65,9 +66,24 @@ public function __invoke(GenerateDeckAlliancesCommand $command): array $combination['houseTwo'], $combination['houseThreeDeckId'], $combination['houseThree'], + $command->extraCardType, + $command->extraCards, ); - if ($isAlreadyImported) { + if (null !== $isAlreadyImported) { + $deck = $this->deckRepository->searchOne(new Criteria( + new Filters( + FilterType::AND, + new Filter(new FilterField('id'), new StringFilterValue($isAlreadyImported), FilterOperator::EQUAL), + ), + )); + + $importedDecks[] = [ + 'id' => $isAlreadyImported, + 'url' => Link::dokDeckFromId(KeyforgeDeckType::ALLIANCE, Uuid::from($isAlreadyImported)), + 'deck' => $deck, + ]; + continue; } @@ -79,9 +95,16 @@ public function __invoke(GenerateDeckAlliancesCommand $command): array 'json' => $combination, ]); - $this->importDeckAllianceService->execute(Uuid::from(\str_replace('"', '', $response->getContent())), $user->id()); + $importedDeckId = Uuid::from(\str_replace('"', '', $response->getContent())); + $importedDeck = $this->importDeckAllianceService->execute($importedDeckId, $command->addToMyDecks ? $user->id() : null); $this->deckRepository->commit(); $this->deckRepository->beginTransaction(); + + $importedDecks[] = [ + 'id' => $importedDeckId->value(), + 'url' => Link::dokDeckFromId(KeyforgeDeckType::ALLIANCE, $importedDeckId), + 'deck' => $importedDeck, + ]; } catch (\Throwable) { throw new \Exception('Error desconocido'); } @@ -89,33 +112,34 @@ public function __invoke(GenerateDeckAlliancesCommand $command): array return [ 'combinations' => \count($combinations), + 'decks' => $importedDecks, ]; } - private function validations(?KeyforgeDeck ...$decks): void + private function validations(GenerateDeckAlliancesCommand $command, ?KeyforgeDeck ...$decks): void { + if (\count($decks) !== \count($command->deckIds())) { + throw new \Exception('Missing deck'); + } + if (0 === \count($decks)) { throw new \Exception('Deck error'); } foreach ($decks as $deck) { - if (null === $deck) { - throw new \Exception('Deck error'); - } - if ($deck->set() !== $decks[0]->set()) { throw new \Exception('Set error'); } } } - private function pods(array $houses, KeyforgeDeck ...$decks): array + private function pods(GenerateDeckAlliancesCommand $command, KeyforgeDeck ...$decks): array { $pods = []; foreach ($decks as $deck) { foreach ($deck->houses()->value() as $house) { - if (false === \in_array($house->value, $houses[$deck->id()->value()] ?? [], true)) { + if (false === \in_array($house->value, $command->housesOf($deck->id()->value()), true)) { continue; } @@ -129,7 +153,7 @@ private function pods(array $houses, KeyforgeDeck ...$decks): array return $pods; } - private function combinations(array $data): array + private function combinations(GenerateDeckAlliancesCommand $command, array $data, bool $addToOwned): array { $count = \count($data); @@ -153,15 +177,25 @@ private function combinations(array $data): array $isNotSameDeck = $id1 !== $id2 || $id1 !== $id3 || $id2 !== $id3; if ($isNotDuplicate && $isNotSameDeck) { - $combinations[] = [ + $combination = [ 'houseOne' => $data[$i]['house'], 'houseOneDeckId' => $data[$i]['id'], 'houseTwo' => $data[$j]['house'], 'houseTwoDeckId' => $data[$j]['id'], 'houseThree' => $data[$k]['house'], 'houseThreeDeckId' => $data[$k]['id'], - 'owned' => true, + 'owned' => $addToOwned, ]; + + if ('Token' === $command->extraCardType) { + $combination['tokenName'] = $command->extraCards; + } + + if ('Prophecies' === $command->extraCardType) { + $combination['propheciesDeckId'] = $command->extraCards; + } + + $combinations[] = $combination; } } } diff --git a/src/Domain/Model/Keyforge/Deck/KeyforgeDeckAllianceRepository.php b/src/Domain/Model/Keyforge/Deck/KeyforgeDeckAllianceRepository.php index cb022d42..22ad83df 100644 --- a/src/Domain/Model/Keyforge/Deck/KeyforgeDeckAllianceRepository.php +++ b/src/Domain/Model/Keyforge/Deck/KeyforgeDeckAllianceRepository.php @@ -7,5 +7,5 @@ interface KeyforgeDeckAllianceRepository { public function saveComposition(Uuid $id, array $composition): void; - public function isAlreadyImported(string $id1, string $house1, string $id2, string $house2, string $id3, string $house3): bool; + public function isAlreadyImported(string $id1, string $house1, string $id2, string $house2, string $id3, string $house3, string $extraCardType, string $extraCard): ?string; } diff --git a/src/Domain/Model/Shared/ValueObject/Link.php b/src/Domain/Model/Shared/ValueObject/Link.php new file mode 100644 index 00000000..c357a95a --- /dev/null +++ b/src/Domain/Model/Shared/ValueObject/Link.php @@ -0,0 +1,32 @@ +url(); + + return new static($value); + } + + public static function dokDeckFromId(KeyforgeDeckType $type, Uuid $uuid): static + { + $domain = match ($type) { + KeyforgeDeckType::STANDARD => self::DECK_REGULAR, + KeyforgeDeckType::ALLIANCE => self::DECK_ALLIANCE, + KeyforgeDeckType::THEORETICAL => self::DECK_THEORETICAL, + }; + + return self::from(self::HTTPS . $domain . $uuid->value()); + } +} diff --git a/src/Entrypoint/Controller/Keyforge/Deck/Alliance/GenerateAlliancesController.php b/src/Entrypoint/Controller/Keyforge/Deck/Alliance/GenerateAlliancesController.php index 5c459880..45f6cd89 100644 --- a/src/Entrypoint/Controller/Keyforge/Deck/Alliance/GenerateAlliancesController.php +++ b/src/Entrypoint/Controller/Keyforge/Deck/Alliance/GenerateAlliancesController.php @@ -24,9 +24,20 @@ public function __invoke(Request $request): Response try { $payload = Json::decode($request->getContent()); - $this->bus->dispatch(new GenerateDeckAlliancesCommand($payload['decks'], $payload['houses'])); - - return new JsonResponse(['success' => true]); + $result = $this->extractResult( + $this->bus->dispatch(new GenerateDeckAlliancesCommand( + $payload['decks'], + $payload['extraCardType'], + $payload['extraCard'], + $payload['addToMyDecks'], + $payload['addToOwnedDok'], + )), + ); + + return new JsonResponse([ + 'success' => true, + 'result' => $result, + ]); } catch (InvalidUuidStringException) { return new JsonResponse(['error' => 'Invalid uuid']); } catch (\Throwable $e) { diff --git a/src/Entrypoint/Controller/Keyforge/Deck/Alliance/generate_alliances.html.twig b/src/Entrypoint/Controller/Keyforge/Deck/Alliance/generate_alliances.html.twig index 8b9148e9..892519f7 100644 --- a/src/Entrypoint/Controller/Keyforge/Deck/Alliance/generate_alliances.html.twig +++ b/src/Entrypoint/Controller/Keyforge/Deck/Alliance/generate_alliances.html.twig @@ -18,151 +18,326 @@
-