Skip to content

Commit

Permalink
Integrating FleetJourney component with other components
Browse files Browse the repository at this point in the history
  • Loading branch information
senghe committed Nov 24, 2023
1 parent 662ec3c commit b6c719e
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function __invoke(StartJourneyCommand $command): void

$this->eventBus->dispatch(
new FleetHasStartedJourneyEvent(
$command->getPlanetId(),
$fleetTakingJourney->getId()->getUuid(),
$startGalaxyPoint->format(),
$command->getTargetGalaxyPoint(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public function __invoke(TargetJourneysCommand $command): void
$userId = new UserId($command->getUserId());
$fleets = $this->fleetRepository->findInJourneyForUser($userId);
foreach ($fleets as $fleet) {
$mission =$fleet->getJourneyMissionType();
$fleet->reachJourneyTargetPoint();
if ($fleet->didReachJourneyTargetPoint() === false) {
continue;
}

$this->eventBus->dispatch(
new FleetHasReachedJourneyTargetPointEvent(
$mission->value,
$fleet->getId()->getUuid(),
$fleet->getJourneyTargetPoint()->format(),
$fleet->getResourcesLoad(),
Expand Down
28 changes: 28 additions & 0 deletions src/Application/Component/FleetJourney/Domain/Entity/Fleet.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use TheGame\Application\Component\FleetJourney\Domain\Exception\NotEnoughFleetLoadCapacityException;
use TheGame\Application\Component\FleetJourney\Domain\Exception\NotEnoughShipsException;
use TheGame\Application\Component\FleetJourney\Domain\FleetIdInterface;
use TheGame\Application\Component\FleetJourney\Domain\MissionType;
use TheGame\Application\Component\FleetJourney\Domain\ShipsGroupInterface;
use TheGame\Application\SharedKernel\Domain\GalaxyPointInterface;
use TheGame\Application\SharedKernel\Domain\ResourcesInterface;
Expand Down Expand Up @@ -38,6 +39,24 @@ public function getStationingGalaxyPoint(): GalaxyPointInterface
return $this->stationingPoint;
}

public function landOnPlanet(GalaxyPointInterface $newStationingPoint): void
{
$this->stationingPoint = $newStationingPoint;
}

public function merge(Fleet $fleet): void
{
foreach ($fleet->ships as $shipsGroupToMerge) {
foreach ($this->ships as $currentShipsGroup) {
if ($currentShipsGroup->hasType($shipsGroupToMerge->getType())) {
$currentShipsGroup->merge($shipsGroupToMerge);

continue 2;
}
}
}
}

/** @var array<ShipsGroupInterface> $ships */
public function addShips(array $ships): void
{
Expand Down Expand Up @@ -167,6 +186,15 @@ public function startJourney(Journey $journey): void
$this->currentJourney = $journey;
}

public function getJourneyMissionType(): MissionType
{
if ($this->isDuringJourney() === false) {
throw new FleetNotInJourneyYetException($this->fleetId);
}

return $this->currentJourney->getMissionType();
}

public function getJourneyStartPoint(): GalaxyPointInterface
{
if ($this->isDuringJourney() === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public function getId(): JourneyIdInterface
return $this->journeyId;
}

public function getMissionType(): MissionType
{
return $this->missionType;
}

public function getStartPoint(): GalaxyPointInterface
{
return $this->startPoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ final class FleetHasReachedJourneyTargetPointEvent implements EventInterface
{
/** @var array<string, int> $resourcesLoad */
public function __construct(
private readonly string $mission,
private readonly string $fleetId,
private readonly string $targetGalaxyPoint,
private readonly array $resourcesLoad,
) {
}

public function getMission(): string
{
return $this->mission;
}

public function getFleetId(): string
{
return $this->fleetId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class FleetHasStartedJourneyEvent implements EventInterface
{
/** @var array<string, int> $fuelRequirements */
public function __construct(
private readonly string $planetId,
private readonly string $fleetId,
private readonly string $fromGalaxyPoint,
private readonly string $targetGalaxyPoint,
Expand All @@ -18,6 +19,11 @@ public function __construct(
) {
}

public function getPlanetId(): string
{
return $this->planetId;
}

public function getFleetId(): string
{
return $this->fleetId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,46 @@

namespace TheGame\Application\Component\FleetJourney\EventListener;

use TheGame\Application\Component\Balance\Bridge\FleetJourneyContextInterface;
use TheGame\Application\Component\FleetJourney\Domain\Factory\FleetFactoryInterface;
use TheGame\Application\Component\FleetJourney\Domain\ShipsGroup;
use TheGame\Application\Component\FleetJourney\FleetRepositoryInterface;
use TheGame\Application\Component\Galaxy\Bridge\NavigatorInterface;
use TheGame\Application\Component\Shipyard\Domain\Event\NewShipsHaveBeenConstructedEvent;
use TheGame\Application\SharedKernel\Domain\PlanetId;
use TheGame\Application\SharedKernel\Domain\Resources;

final class AddNewlyConstructedShipsToFleetEventListener
{
public function __construct(
private readonly FleetRepositoryInterface $fleetRepository,
private readonly FleetFactoryInterface $fleetFactory,
private readonly NavigatorInterface $navigator,
private readonly FleetJourneyContextInterface $fleetJourneyContext,
) {

}

public function __invoke(NewShipsHaveBeenConstructedEvent $event): void
{
$planetId = new PlanetId($event->getPlanetId());
$fleetCurrentlyStationingOnPlanet = $this->fleetRepository->findStationingOnPlanet($planetId);
if ($fleetCurrentlyStationingOnPlanet === null) {
$fleetCurrentlyStationingOnPlanet = $this->fleetFactory->create(
[],
$this->navigator->getPlanetPoint($planetId),
new Resources(),
);
}

$shipType = $event->getType();
$fleetCurrentlyStationingOnPlanet->addShips([
new ShipsGroup(
$shipType,
$event->getQuantity(),
$this->fleetJourneyContext->getShipBaseSpeed($shipType),
$this->fleetJourneyContext->getShipLoadCapacity($shipType),
),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace TheGame\Application\Component\FleetJourney\EventListener;

use TheGame\Application\Component\FleetJourney\Domain\Event\FleetHasReachedJourneyTargetPointEvent;
use TheGame\Application\Component\FleetJourney\Domain\FleetId;
use TheGame\Application\Component\FleetJourney\Domain\MissionType;
use TheGame\Application\Component\FleetJourney\FleetRepositoryInterface;
use TheGame\Application\Component\Galaxy\Bridge\NavigatorInterface;
use TheGame\Application\SharedKernel\Domain\GalaxyPoint;
use TheGame\Application\SharedKernel\Exception\InconsistentModelException;

final class StationFleetOnReachingTargetPointEventListener
{
public function __construct(
private readonly FleetRepositoryInterface $fleetRepository,
private readonly NavigatorInterface $navigator,
) {

}

public function __invoke(FleetHasReachedJourneyTargetPointEvent $event): void
{
$mission = MissionType::from($event->getMission());
if ($mission !== MissionType::Stationing) {
return;
}

$joiningFleetId = new FleetId($event->getFleetId());
$fleetJoiningPlanet = $this->fleetRepository->find($joiningFleetId);
if ($fleetJoiningPlanet === null) {
throw new InconsistentModelException(sprintf('Fleet %s doesn\'t exist', $event->getFleetId()));
}

$stationingPoint = GalaxyPoint::fromString($event->getTargetGalaxyPoint());
$planetId = $this->navigator->getPlanetId($stationingPoint);
$fleetCurrentlyStationingOnPlanet = $this->fleetRepository->findStationingOnPlanet($planetId);
if ($fleetCurrentlyStationingOnPlanet === null) {
$fleetJoiningPlanet->landOnPlanet($stationingPoint);

return;
}

$fleetCurrentlyStationingOnPlanet->merge($fleetJoiningPlanet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

interface NavigatorInterface
{
public function getPlanetId(GalaxyPointInterface $galaxyPoint): ?PlanetIdInterface;

public function getPlanetPoint(PlanetIdInterface $planetId): GalaxyPointInterface;

public function isWithinBoundaries(GalaxyPointInterface $galaxyPoint): bool;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace TheGame\Application\Component\ResourceStorage\EventListener;

use TheGame\Application\Component\FleetJourney\Domain\Event\FleetHasStartedJourneyEvent;
use TheGame\Application\Component\ResourceStorage\Command\UseResourceCommand;
use TheGame\Application\SharedKernel\CommandBusInterface;

final class TakeResourcesOnStartingJourneyEventListener
{
public function __construct(
private readonly CommandBusInterface $commandBus,
) {
}

public function __invoke(FleetHasStartedJourneyEvent $event): void
{
foreach ($event->getResourcesLoad() as $resourceId => $amount) {
$command = new UseResourceCommand(
$event->getPlanetId(),
$resourceId,
$amount,
);
$this->commandBus->dispatch($command);
}

foreach ($event->getFuelRequirements() as $resourceId => $amount) {
$command = new UseResourceCommand(
$event->getPlanetId(),
$resourceId,
$amount,
);
$this->commandBus->dispatch($command);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace TheGame\Application\Component\ResourceStorage\EventListener;

use TheGame\Application\Component\FleetJourney\Domain\Event\FleetHasReachedJourneyReturnPointEvent;
use TheGame\Application\Component\Galaxy\Bridge\NavigatorInterface;
use TheGame\Application\Component\ResourceStorage\Command\DispatchResourcesCommand;
use TheGame\Application\SharedKernel\CommandBusInterface;
use TheGame\Application\SharedKernel\Domain\GalaxyPoint;

final class UnloadResourcesAfterReachingJourneyReturnPointEventListener
{
public function __construct(
private readonly NavigatorInterface $navigator,
private readonly CommandBusInterface $commandBus,
) {
}

public function __invoke(FleetHasReachedJourneyReturnPointEvent $event): void
{
$returnGalaxyPoint = GalaxyPoint::fromString($event->getStartGalaxyPoint());
$planetId = $this->navigator->getPlanetId($returnGalaxyPoint);

foreach ($event->getResourcesLoad() as $resourceId => $amount) {
$command = new DispatchResourcesCommand(
$planetId->getUuid(),
$resourceId,
$amount,
);
$this->commandBus->dispatch($command);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace TheGame\Application\Component\ResourceStorage\EventListener;

use TheGame\Application\Component\FleetJourney\Domain\Event\FleetHasReachedJourneyTargetPointEvent;
use TheGame\Application\Component\Galaxy\Bridge\NavigatorInterface;
use TheGame\Application\Component\ResourceStorage\Command\DispatchResourcesCommand;
use TheGame\Application\SharedKernel\CommandBusInterface;
use TheGame\Application\SharedKernel\Domain\GalaxyPoint;

final class UnloadResourcesAfterReachingJourneyTargetPointEventListener
{
public function __construct(
private readonly NavigatorInterface $navigator,
private readonly CommandBusInterface $commandBus,
) {
}

public function __invoke(FleetHasReachedJourneyTargetPointEvent $event): void
{
$targetGalaxyPoint = GalaxyPoint::fromString($event->getTargetGalaxyPoint());
$planetId = $this->navigator->getPlanetId($targetGalaxyPoint);

foreach ($event->getResourcesLoad() as $resourceId => $amount) {
$command = new DispatchResourcesCommand(
$planetId->getUuid(),
$resourceId,
$amount,
);
$this->commandBus->dispatch($command);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __invoke(FinishJobsCommand $command): void

$summary = $shipyard->finishJobs();
foreach ($summary->getSummary() as $entry) {
$event = $this->finishedConstructionEventFactory->createEvent($entry);
$event = $this->finishedConstructionEventFactory->createEvent($entry, $shipyard->getPlanetId());
$this->eventBus->dispatch($event);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,35 @@
use TheGame\Application\Component\Shipyard\Domain\Event\NewShipsHaveBeenConstructedEvent;
use TheGame\Application\Component\Shipyard\Domain\Event\NewUnitsHaveBeenConstructedEvent;
use TheGame\Application\Component\Shipyard\Domain\FinishedJobsSummaryEntryInterface;
use TheGame\Application\SharedKernel\Domain\PlanetIdInterface;
use TheGame\Application\SharedKernel\EventInterface;

final class FinishedConstructionEventFactory implements FinishedConstructionEventFactoryInterface
{
public function createEvent(FinishedJobsSummaryEntryInterface $summaryEntry): EventInterface
public function createEvent(
FinishedJobsSummaryEntryInterface $summaryEntry,
PlanetIdInterface $planetId,
): EventInterface
{
switch ($summaryEntry->getUnit()) {
case ConstructibleUnit::Cannon: {
return new NewCannonsHaveBeenConstructedEvent(
$planetId->getUuid(),
$summaryEntry->getType(),
$summaryEntry->getQuantity(),
);
}
case ConstructibleUnit::Ship: {
return new NewShipsHaveBeenConstructedEvent(
$planetId->getUuid(),
$summaryEntry->getType(),
$summaryEntry->getQuantity(),
);
}
}

return new NewUnitsHaveBeenConstructedEvent(
$planetId->getUuid(),
$summaryEntry->getUnit()->value,
$summaryEntry->getType(),
$summaryEntry->getQuantity(),
Expand Down
Loading

0 comments on commit b6c719e

Please sign in to comment.