Skip to content

Commit

Permalink
introduce effect handling
Browse files Browse the repository at this point in the history
  • Loading branch information
g5bot committed Feb 1, 2025
1 parent 7f2cdf5 commit 3f1691f
Show file tree
Hide file tree
Showing 21 changed files with 50,869 additions and 50,667 deletions.
4 changes: 2 additions & 2 deletions src/Component/Anomaly/Type/SubspaceEllipseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function handleSpacecraftTick(AnomalyInterface $anomaly): void

$spacecrafts = $location->getSpacecrafts();

$messagesForShips = new MessageCollection();
$messagesForBases = new MessageCollection();
$messagesForShips = $this->messageFactory->createMessageCollection();
$messagesForBases = $this->messageFactory->createMessageCollection();

$intro = $this->messageFactory->createMessage(
UserEnum::USER_NOONE,
Expand Down
50 changes: 50 additions & 0 deletions src/Component/Map/Effects/EffectHandling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Stu\Component\Map\Effects;

use Override;
use RuntimeException;
use Stu\Component\Map\Effects\Type\EffectHandlerInterface;
use Stu\Lib\Information\InformationInterface;
use Stu\Lib\Map\FieldTypeEffectEnum;
use Stu\Module\Spacecraft\Lib\Message\MessageCollectionInterface;
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;

final class EffectHandling implements EffectHandlingInterface
{
/**
* @param array<string, EffectHandlerInterface> $handlerList
*/
public function __construct(
private array $handlerList
) {}

#[Override]
public function handleSpacecraftTick(SpacecraftWrapperInterface $wrapper, InformationInterface $information): void
{
foreach ($wrapper->get()->getLocation()->getFieldType()->getEffects() as $effect) {
$this->getHandler($effect)->handleSpacecraftTick($wrapper, $information);
}
}

#[Override]
public function handleIncomingSpacecraft(SpacecraftWrapperInterface $wrapper, MessageCollectionInterface $messages): void
{
foreach ($wrapper->get()->getLocation()->getFieldType()->getEffects() as $effect) {
$this->getHandler($effect)->handleIncomingSpacecraft($wrapper, $messages);
}
}

private function getHandler(FieldTypeEffectEnum $effect): EffectHandlerInterface
{
if (!array_key_exists($effect->value, $this->handlerList)) {
throw new RuntimeException(sprintf('no handler defined for type: %d', $effect->value));
}

$handler = $this->handlerList[$effect->value];

return $handler;
}
}
16 changes: 16 additions & 0 deletions src/Component/Map/Effects/EffectHandlingInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Stu\Component\Map\Effects;

use Stu\Lib\Information\InformationInterface;
use Stu\Module\Spacecraft\Lib\Message\MessageCollectionInterface;
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;

interface EffectHandlingInterface
{
public function handleSpacecraftTick(SpacecraftWrapperInterface $wrapper, InformationInterface $information): void;

public function handleIncomingSpacecraft(SpacecraftWrapperInterface $wrapper, MessageCollectionInterface $messages): void;
}
37 changes: 37 additions & 0 deletions src/Component/Map/Effects/Type/CloakUnuseableEffectHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Stu\Component\Map\Effects\Type;

use Stu\Component\Spacecraft\System\SpacecraftSystemManagerInterface;
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
use Stu\Lib\Information\InformationInterface;
use Stu\Module\Spacecraft\Lib\Message\MessageCollectionInterface;
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;

class CloakUnuseableEffectHandler implements EffectHandlerInterface
{
public function __construct(
private SpacecraftSystemManagerInterface $spacecraftSystemManager
) {}

public function handleSpacecraftTick(SpacecraftWrapperInterface $wrapper, InformationInterface $information): void
{
// not needed
}

public function handleIncomingSpacecraft(SpacecraftWrapperInterface $wrapper, MessageCollectionInterface $messages): void
{
$spacecraft = $wrapper->get();
if ($spacecraft->isCloaked()) {

$this->spacecraftSystemManager->deactivate($wrapper, SpacecraftSystemTypeEnum::CLOAK, true);

$messages->addInformation(
sprintf("[color=yellow]Tarnung durch %s ausgefallen.[/color]", $spacecraft->getLocation()->getFieldType()->getName()),
$wrapper->get()->getUser()->getId()
);
}
}
}
16 changes: 16 additions & 0 deletions src/Component/Map/Effects/Type/EffectHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Stu\Component\Map\Effects\Type;

use Stu\Lib\Information\InformationInterface;
use Stu\Module\Spacecraft\Lib\Message\MessageCollectionInterface;
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;

interface EffectHandlerInterface
{
public function handleSpacecraftTick(SpacecraftWrapperInterface $wrapper, InformationInterface $information): void;

public function handleIncomingSpacecraft(SpacecraftWrapperInterface $wrapper, MessageCollectionInterface $messages): void;
}
10 changes: 10 additions & 0 deletions src/Component/Map/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@

namespace Stu\Component\Map;

use Stu\Component\Map\Effects\EffectHandling;
use Stu\Component\Map\Effects\EffectHandlingInterface;
use Stu\Component\Map\Effects\Type\CloakUnuseableEffectHandler;
use Stu\Lib\Map\FieldTypeEffectEnum;

use function DI\autowire;

return [
EffectHandlingInterface::class => autowire(EffectHandling::class)
->constructorParameter(
'handlerList',
[FieldTypeEffectEnum::CLOAK_UNUSEABLE->value => autowire(CloakUnuseableEffectHandler::class)]
),
EncodedMapInterface::class => autowire(EncodedMap::class),
];
7 changes: 7 additions & 0 deletions src/Component/Spacecraft/System/Type/CloakShipSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Stu\Component\Spacecraft\System\SpacecraftSystemModeEnum;
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeInterface;
use Stu\Lib\Map\FieldTypeEffectEnum;
use Stu\Module\Spacecraft\Lib\SpacecraftStateChangerInterface;
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
use Stu\Orm\Entity\ShipInterface;
Expand Down Expand Up @@ -40,6 +41,12 @@ public function checkActivationConditions(SpacecraftWrapperInterface $wrapper, s
return false;
}

$fieldType = $wrapper->get()->getLocation()->getFieldType();
if ($fieldType->hasEffect(FieldTypeEffectEnum::CLOAK_UNUSEABLE)) {
$reason = sprintf('"%s" es verhindert', $fieldType->getName());
return false;
}

if ($spacecraft->getSubspaceState()) {
$reason = _('die Subraumfeldsensoren aktiv sind');
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Migrations/Sqlite/Version20250130172650.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public function up(Schema $schema): void
$this->addSql('CREATE INDEX map_bordertype_idx ON stu_map (bordertype_id)');
$this->addSql('CREATE INDEX map_admin_region_idx ON stu_map (admin_region_id)');
$this->addSql('CREATE TABLE stu_map_bordertypes (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, faction_id INTEGER NOT NULL, color VARCHAR(255) NOT NULL, description VARCHAR(255) NOT NULL)');
$this->addSql('CREATE TABLE stu_map_ftypes (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, type INTEGER NOT NULL, is_system BOOLEAN NOT NULL, ecost SMALLINT NOT NULL, name VARCHAR(255) NOT NULL, colonies_classes_id INTEGER DEFAULT NULL, damage SMALLINT NOT NULL, x_damage SMALLINT NOT NULL, x_damage_system SMALLINT DEFAULT NULL, x_damage_type SMALLINT DEFAULT NULL, "view" BOOLEAN NOT NULL, passable BOOLEAN NOT NULL, consequences CLOB DEFAULT NULL, CONSTRAINT FK_3D24A6CE9106126 FOREIGN KEY (colonies_classes_id) REFERENCES stu_colonies_classes (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE TABLE stu_map_ftypes (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, type INTEGER NOT NULL, is_system BOOLEAN NOT NULL, ecost SMALLINT NOT NULL, name VARCHAR(255) NOT NULL, colonies_classes_id INTEGER DEFAULT NULL, damage SMALLINT NOT NULL, x_damage SMALLINT NOT NULL, x_damage_system SMALLINT DEFAULT NULL, x_damage_type SMALLINT DEFAULT NULL, "view" BOOLEAN NOT NULL, passable BOOLEAN NOT NULL, effects CLOB DEFAULT NULL, CONSTRAINT FK_3D24A6CE9106126 FOREIGN KEY (colonies_classes_id) REFERENCES stu_colonies_classes (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_3D24A6CE9106126 ON stu_map_ftypes (colonies_classes_id)');
$this->addSql('CREATE INDEX map_ftypes_type_idx ON stu_map_ftypes (type)');
$this->addSql('CREATE TABLE stu_map_regions (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, description VARCHAR(255) NOT NULL, database_id INTEGER DEFAULT NULL, CONSTRAINT FK_3CCEB5C5F0AA09DB FOREIGN KEY (database_id) REFERENCES stu_database_entrys (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
Expand Down
2 changes: 1 addition & 1 deletion src/Module/Spacecraft/Action/BoardShip/BoardShip.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function handle(GameControllerInterface $game): void
$combatGroupAttacker = $this->closeCombatUtil->getCombatGroup($spacecraft);
$combatGroupDefender = $this->closeCombatUtil->getCombatGroup($target);

$messages = new MessageCollection();
$messages = $this->messageFactory->createMessageCollection();
$message = $this->messageFactory->createMessage($userId, $targetUserId, [sprintf(
'Die %s entsendet ein Enterkommando auf die %s',
$spacecraft->getName(),
Expand Down
20 changes: 17 additions & 3 deletions src/Module/Spacecraft/Lib/Message/MessageCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@

final class MessageCollection implements MessageCollectionInterface
{
/**
* @var MessageInterface[]
*/
/** @var MessageInterface[] */
private array $messages = [];

public function __construct(private MessageFactoryInterface $messageFactory) {}

#[Override]
public function add(MessageInterface $msg): void
{
$this->messages[] = $msg;
}

#[Override]
public function addInformation(string $text, ?int $recipient = null): MessageInterface
{
$message = $this->messageFactory->createMessage(
UserEnum::USER_NOONE,
$recipient,
[$text]
);

$this->add($message);

return $message;
}

#[Override]
public function getRecipientIds(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ interface MessageCollectionInterface
{
public function add(MessageInterface $msg): void;

public function addInformation(
string $text,
?int $recipient = null
): MessageInterface;

/**
* @return array<int>
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Module/Spacecraft/Lib/Message/MessageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public function createMessage(
#[Override]
public function createMessageCollection(): MessageCollectionInterface
{
return new MessageCollection();
return new MessageCollection($this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Stu\Module\Spacecraft\Lib\Movement\Component\Consequence\PostFlight;

use Override;
use Stu\Component\Map\Effects\EffectHandlingInterface;
use Stu\Module\Spacecraft\Lib\Message\MessageCollectionInterface;
use Stu\Module\Spacecraft\Lib\Movement\Component\Consequence\AbstractFlightConsequence;
use Stu\Module\Spacecraft\Lib\Movement\Route\FlightRouteInterface;
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;

class FieldTypeEffectConsequence extends AbstractFlightConsequence implements PostFlightConsequenceInterface
{
public function __construct(private EffectHandlingInterface $effectHandling) {}

#[Override]
protected function skipWhenTractored(): bool
{
return false;
}

#[Override]
protected function triggerSpecific(
SpacecraftWrapperInterface $wrapper,
FlightRouteInterface $flightRoute,
MessageCollectionInterface $messages
): void {

$this->effectHandling->handleIncomingSpacecraft($wrapper, $messages);
}
}
22 changes: 8 additions & 14 deletions src/Module/Spacecraft/Lib/Movement/ShipMover.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Stu\Module\PlayerSetting\Lib\UserEnum;
use Stu\Module\Spacecraft\Lib\Battle\AlertDetection\AlertReactionFacadeInterface;
use Stu\Module\Ship\Lib\Fleet\LeaveFleetInterface;
use Stu\Module\Spacecraft\Lib\Message\MessageCollection;
use Stu\Module\Spacecraft\Lib\Message\MessageCollectionInterface;
use Stu\Module\Spacecraft\Lib\Message\MessageFactoryInterface;
use Stu\Module\Spacecraft\Lib\Movement\Component\PreFlight\ConditionCheckResult;
Expand Down Expand Up @@ -38,7 +37,7 @@ public function checkAndMove(
FlightRouteInterface $flightRoute
): MessageCollectionInterface {

$messages = new MessageCollection();
$messages = $this->messageFactory->createMessageCollection();

$leadSpacecraft = $leadWrapper->get();
$leadSpacecraftName = $leadSpacecraft->getName();
Expand Down Expand Up @@ -111,7 +110,7 @@ private function travelFlightRoute(
// nächstes Feld nicht passierbar
if (!$nextWaypoint->getFieldType()->getPassable()) {
$flightRoute->abortFlight();
$this->addInformation('Das nächste Feld kann nicht passiert werden', $messages);
$messages->addInformation('Das nächste Feld kann nicht passiert werden');
break;
}

Expand All @@ -127,7 +126,7 @@ private function travelFlightRoute(

if (!$conditionCheckResult->isFlightPossible()) {
$flightRoute->abortFlight();
$this->addInformation('Der Weiterflug wurde aus folgenden Gründen abgebrochen:', $messages);
$messages->addInformation('Der Weiterflug wurde aus folgenden Gründen abgebrochen:');
$this->addInformationMerge($conditionCheckResult->getInformations(), $messages);
break;
}
Expand Down Expand Up @@ -155,7 +154,7 @@ private function travelFlightRoute(

if ($this->areAllShipsDestroyed($activeWrappers)) {
$flightRoute->abortFlight();
$this->addInformation('Es wurden alle Schiffe zerstört', $messages);
$messages->addInformation('Es wurden alle Schiffe zerstört');
}
}

Expand Down Expand Up @@ -261,7 +260,7 @@ private function leaveFleetIfNotFleetLeader(SpacecraftInterface $ship, bool $has
{
if ($hasToLeaveFleet && $ship instanceof ShipInterface) {
if ($this->leaveFleet->leaveFleet($ship)) {
$this->addInformation(sprintf('Die %s hat die Flotte verlassen', $ship->getName()), $messages);
$messages->addInformation(sprintf('Die %s hat die Flotte verlassen', $ship->getName()));
}
}
}
Expand Down Expand Up @@ -333,14 +332,14 @@ private function postFlightInformations(

//add info about anomalies
foreach ($leadWrapper->get()->getLocation()->getAnomalies() as $anomaly) {
$this->addInformation(sprintf(
$messages->addInformation(sprintf(
'[b][color=yellow]In diesem Sektor befindet sich eine %s-Anomalie[/color][/b]',
$anomaly->getAnomalyType()->getName()
), $messages);
));
}
// add info about buyos
foreach ($leadWrapper->get()->getLocation()->getBuoys() as $buoy) {
$this->addInformation(sprintf('[b][color=yellow]Boje entdeckt: [/color][/b]%s', $buoy->getText()), $messages);
$messages->addInformation(sprintf('[b][color=yellow]Boje entdeckt: [/color][/b]%s', $buoy->getText()));
}
}

Expand All @@ -352,11 +351,6 @@ private function areAllShipsDestroyed(Collection $wrappers): bool
return !$wrappers->exists(fn(int $key, SpacecraftWrapperInterface $wrapper): bool => !$wrapper->get()->isDestroyed());
}

private function addInformation(string $value, MessageCollectionInterface $messages): void
{
$this->addInformationMerge([$value], $messages);
}

/**
* @param array<string> $value
*/
Expand Down
Loading

0 comments on commit 3f1691f

Please sign in to comment.