Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion grumphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ grumphp:
phpstan:
autoload_file: phpstan.neon
configuration: ~
memory_limit: "512M"
memory_limit: "256M"
phpunit: ~
phpversion:
project: '8.4'
Original file line number Diff line number Diff line change
Expand Up @@ -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] ?? [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -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;
}

Expand All @@ -79,43 +95,51 @@ 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');
}
}

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;
}

Expand All @@ -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);

Expand All @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
32 changes: 32 additions & 0 deletions src/Domain/Model/Shared/ValueObject/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace AdnanMula\Cards\Domain\Model\Shared\ValueObject;

use AdnanMula\Cards\Domain\Model\Keyforge\Deck\ValueObject\KeyforgeDeckType;
use Assert\Assert;

final class Link extends StringValueObject
{
public const string DECK_REGULAR = 'decksofkeyforge.com/decks/';
public const string DECK_ALLIANCE = 'decksofkeyforge.com/alliance-decks/';
public const string DECK_THEORETICAL = 'decksofkeyforge.com/theoretical-decks/';
private const string HTTPS = 'https://';

public static function from(string $value): static
{
Assert::that($value)->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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading