Skip to content

Commit

Permalink
more field type effects
Browse files Browse the repository at this point in the history
  • Loading branch information
g5bot committed Feb 3, 2025
1 parent fa7a2a2 commit 6c5ef55
Show file tree
Hide file tree
Showing 25 changed files with 356 additions and 115 deletions.
8 changes: 6 additions & 2 deletions src/Component/Map/Effects/EffectHandling.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ public function __construct(
public function handleSpacecraftTick(SpacecraftWrapperInterface $wrapper, InformationInterface $information): void
{
foreach ($wrapper->get()->getLocation()->getFieldType()->getEffects() as $effect) {
$this->getHandler($effect)->handleSpacecraftTick($wrapper, $information);
if ($effect->hasHandler()) {
$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);
if ($effect->hasHandler()) {
$this->getHandler($effect)->handleIncomingSpacecraft($wrapper, $messages);
}
}
}

Expand Down
42 changes: 42 additions & 0 deletions src/Component/Map/Effects/Type/WarpdriveLeakEffectHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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;

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

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

$warpdrive = $wrapper->getWarpDriveSystemData();
if (
$warpdrive === null
|| !$spacecraft->getWarpDriveState()
) {
return;
}

$loss = min($warpdrive->getWarpDrive(), (int)ceil($warpdrive->getTheoreticalMaxWarpdrive() / 10));
if ($loss === 0) {
return;
}

$warpdrive->lowerWarpDrive($loss)->update();

$messages->addInformation(
sprintf("[color=yellow]Leck im Warpantrieb durch %s. (Verlust: %d)[/color]", $spacecraft->getLocation()->getFieldType()->getName(), $loss),
$wrapper->get()->getUser()->getId()
);
}
}
6 changes: 5 additions & 1 deletion src/Component/Map/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Stu\Component\Map\Effects\EffectHandling;
use Stu\Component\Map\Effects\EffectHandlingInterface;
use Stu\Component\Map\Effects\Type\CloakUnuseableEffectHandler;
use Stu\Component\Map\Effects\Type\WarpdriveLeakEffectHandler;
use Stu\Lib\Map\FieldTypeEffectEnum;

use function DI\autowire;
Expand All @@ -15,7 +16,10 @@
EffectHandlingInterface::class => autowire(EffectHandling::class)
->constructorParameter(
'handlerList',
[FieldTypeEffectEnum::CLOAK_UNUSEABLE->value => autowire(CloakUnuseableEffectHandler::class)]
[
FieldTypeEffectEnum::CLOAK_UNUSEABLE->value => autowire(CloakUnuseableEffectHandler::class),
FieldTypeEffectEnum::WARPDRIVE_LEAK->value => autowire(WarpdriveLeakEffectHandler::class)
]
),
EncodedMapInterface::class => autowire(EncodedMap::class),
];
15 changes: 12 additions & 3 deletions src/Component/Spacecraft/SpacecraftAlertStateEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ enum SpacecraftAlertStateEnum: int
public function getDescription(): string
{
return match ($this) {
SpacecraftAlertStateEnum::ALERT_GREEN => "Alarm Grün",
SpacecraftAlertStateEnum::ALERT_YELLOW => "Alarm Gelb",
SpacecraftAlertStateEnum::ALERT_RED => "Alarm Rot"
self::ALERT_GREEN => "Alarm Grün",
self::ALERT_YELLOW => "Alarm Gelb",
self::ALERT_RED => "Alarm Rot"
};
}

public function getEpsUsage(): int
{
return match ($this) {
self::ALERT_GREEN => 0,
self::ALERT_YELLOW => 1,
self::ALERT_RED => 2
};
}

Expand Down
12 changes: 12 additions & 0 deletions src/Lib/Map/FieldTypeEffectEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@ enum FieldTypeEffectEnum: string
case CLOAK_UNUSEABLE = 'CLOAK_UNUSEABLE';
case WARPDRIVE_LEAK = 'WARPDRIVE_LEAK';
case LSS_MALFUNCTION = 'LSS_MALFUNCTION';
case NO_SPACECRAFT_COUNT = 'NO_SPACECRAFT_COUNT'; // don't ever show signature info
case DUBIOUS_SPACECRAFT_COUNT = 'DUBIOUS_SPACECRAFT_COUNT'; // always show '!" sign, if at least one signature

public function hasHandler(): bool
{
return match ($this) {
self::LSS_MALFUNCTION => false,
self::NO_SPACECRAFT_COUNT => false,
self::DUBIOUS_SPACECRAFT_COUNT => false,
default => true
};
}
}
17 changes: 17 additions & 0 deletions src/Lib/Map/VisualPanel/Layer/Data/SpacecraftCountData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Stu\Lib\Map\FieldTypeEffectEnum;

#[Entity]
class SpacecraftCountData extends AbstractData
Expand All @@ -15,6 +16,10 @@ class SpacecraftCountData extends AbstractData
#[Column(type: 'integer')]
private int $cloakcount = 0;

/** @var null|array<string> */
#[Column(type: 'json', nullable: true)]
private ?array $effects = null;

public function getSpacecraftCount(): int
{
return $this->spacecraftcount;
Expand All @@ -24,4 +29,16 @@ public function hasCloakedShips(): bool
{
return $this->cloakcount > 0;
}

public function isEnabled(): bool
{
return $this->effects === null
|| !in_array(FieldTypeEffectEnum::NO_SPACECRAFT_COUNT->value, $this->effects);
}

public function isDubious(): bool
{
return $this->effects !== null
&& in_array(FieldTypeEffectEnum::DUBIOUS_SPACECRAFT_COUNT->value, $this->effects);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ protected function addFieldResults(ResultSetMapping $rsm): void
{
$rsm->addFieldResult('d', 'spacecraftcount', 'spacecraftcount');
$rsm->addFieldResult('d', 'cloakcount', 'cloakcount');
$rsm->addFieldResult('d', 'effects', 'effects');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@ public function render(CellDataInterface $data, PanelAttributesInterface $panel)

private function getDisplayCount(SpacecraftCountData $data): ?string
{
if ($data->getSpacecraftCount() > 0) {
return (string) $data->getSpacecraftCount();
if ($data->isDubious()) {
return "!";
}

if (!$data->isEnabled()) {
return null;
}

$spacecraftCount = $data->getSpacecraftCount();
if ($spacecraftCount > 0) {
return (string) $spacecraftCount;
}
if ($data->hasCloakedShips()) {
if ($this->showCloakedEverywhere) {
Expand Down
2 changes: 1 addition & 1 deletion src/Lib/Pirate/PirateReactionTriggerEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getBehaviourProbabilities(): array
],
self::ON_SUPPORT_CALL => [
PirateBehaviourEnum::RAGE->value => 100,
PirateBehaviourEnum::CALL_FOR_SUPPORT->value => 10
PirateBehaviourEnum::CALL_FOR_SUPPORT->value => 15
],
self::ON_RAGE => [
PirateBehaviourEnum::RAGE->value => 50,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250130172650 extends AbstractMigration
final class Version20250203092919 extends AbstractMigration
{
public function getDescription(): string
{
Expand Down Expand Up @@ -163,12 +163,12 @@ public function up(Schema $schema): void
$this->addSql('CREATE INDEX IDX_167BE6E46E59D40D ON stu_crew (race_id)');
$this->addSql('CREATE INDEX IDX_167BE6E4A76ED395 ON stu_crew (user_id)');
$this->addSql('CREATE TABLE stu_crew_assign (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, spacecraft_id INTEGER DEFAULT NULL, colony_id INTEGER DEFAULT NULL, tradepost_id INTEGER DEFAULT NULL, crew_id INTEGER NOT NULL, slot SMALLINT DEFAULT NULL, user_id INTEGER NOT NULL, repair_task_id INTEGER DEFAULT NULL, CONSTRAINT FK_4793ED245FE259F6 FOREIGN KEY (crew_id) REFERENCES stu_crew (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_4793ED241C6AF6FD FOREIGN KEY (spacecraft_id) REFERENCES stu_spacecraft (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_4793ED2496ADBADE FOREIGN KEY (colony_id) REFERENCES stu_colonies (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_4793ED248B935ABD FOREIGN KEY (tradepost_id) REFERENCES stu_trade_posts (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_4793ED24A76ED395 FOREIGN KEY (user_id) REFERENCES stu_user (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_4793ED24130D5415 FOREIGN KEY (repair_task_id) REFERENCES stu_repair_task (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_4793ED241C6AF6FD ON stu_crew_assign (spacecraft_id)');
$this->addSql('CREATE INDEX IDX_4793ED2496ADBADE ON stu_crew_assign (colony_id)');
$this->addSql('CREATE INDEX IDX_4793ED248B935ABD ON stu_crew_assign (tradepost_id)');
$this->addSql('CREATE INDEX IDX_4793ED24A76ED395 ON stu_crew_assign (user_id)');
$this->addSql('CREATE INDEX IDX_4793ED24130D5415 ON stu_crew_assign (repair_task_id)');
$this->addSql('CREATE INDEX ship_crew_colony_idx ON stu_crew_assign (colony_id)');
$this->addSql('CREATE INDEX ship_crew_spacecraft_idx ON stu_crew_assign (spacecraft_id)');
$this->addSql('CREATE INDEX ship_crew_tradepost_idx ON stu_crew_assign (tradepost_id)');
$this->addSql('CREATE INDEX ship_crew_user_idx ON stu_crew_assign (user_id)');
$this->addSql('CREATE UNIQUE INDEX ship_crew_crew_idx ON stu_crew_assign (crew_id)');
$this->addSql('CREATE UNIQUE INDEX crew_assign_crew_idx ON stu_crew_assign (crew_id)');
$this->addSql('CREATE TABLE stu_crew_race (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, faction_id INTEGER NOT NULL, description VARCHAR(255) NOT NULL, chance SMALLINT NOT NULL, maleratio SMALLINT NOT NULL, define VARCHAR(255) NOT NULL, CONSTRAINT FK_ED3686294448F8DA FOREIGN KEY (faction_id) REFERENCES stu_factions (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_ED3686294448F8DA ON stu_crew_race (faction_id)');
$this->addSql('CREATE TABLE stu_crew_training (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, user_id INTEGER NOT NULL, colony_id INTEGER NOT NULL, CONSTRAINT FK_E25756B996ADBADE FOREIGN KEY (colony_id) REFERENCES stu_colonies (id) NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_E25756B9A76ED395 FOREIGN KEY (user_id) REFERENCES stu_user (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)');
Expand Down
35 changes: 35 additions & 0 deletions src/Migrations/pgsql/Version20250203092918.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Stu\Migrations\Pgsql;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20250203092918 extends AbstractMigration
{
public function getDescription(): string
{
return 'Renaming some indices.';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER INDEX ship_crew_spacecraft_idx RENAME TO IDX_4793ED241C6AF6FD');
$this->addSql('ALTER INDEX ship_crew_colony_idx RENAME TO IDX_4793ED2496ADBADE');
$this->addSql('ALTER INDEX ship_crew_tradepost_idx RENAME TO IDX_4793ED248B935ABD');
$this->addSql('ALTER INDEX ship_crew_user_idx RENAME TO IDX_4793ED24A76ED395');
$this->addSql('ALTER INDEX ship_crew_crew_idx RENAME TO crew_assign_crew_idx');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER INDEX idx_4793ed24a76ed395 RENAME TO ship_crew_user_idx');
$this->addSql('ALTER INDEX idx_4793ed248b935abd RENAME TO ship_crew_tradepost_idx');
$this->addSql('ALTER INDEX idx_4793ed241c6af6fd RENAME TO ship_crew_spacecraft_idx');
$this->addSql('ALTER INDEX crew_assign_crew_idx RENAME TO ship_crew_crew_idx');
$this->addSql('ALTER INDEX idx_4793ed2496adbade RENAME TO ship_crew_colony_idx');
}
}
10 changes: 5 additions & 5 deletions src/Module/Spacecraft/Lib/SpacecraftStateChanger.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ public function changeAlertState(

//check if enough energy
if (
$alertState == SpacecraftAlertStateEnum::ALERT_YELLOW
&& $currentAlertState == SpacecraftAlertStateEnum::ALERT_GREEN
$alertState === SpacecraftAlertStateEnum::ALERT_YELLOW
&& $currentAlertState === SpacecraftAlertStateEnum::ALERT_GREEN
) {
$this->consumeEnergyForAlertChange($wrapper, SpacecraftStateChangerInterface::ALERT_YELLOW_EPS_USAGE);
$this->consumeEnergyForAlertChange($wrapper, $alertState->getEpsUsage());
}
if (
$alertState == SpacecraftAlertStateEnum::ALERT_RED
$alertState === SpacecraftAlertStateEnum::ALERT_RED
&& $currentAlertState !== SpacecraftAlertStateEnum::ALERT_RED
) {
$this->consumeEnergyForAlertChange($wrapper, SpacecraftStateChangerInterface::ALERT_RED_EPS_USAGE);
$this->consumeEnergyForAlertChange($wrapper, $alertState->getEpsUsage());
}

// cancel repair if not on alert green
Expand Down
4 changes: 0 additions & 4 deletions src/Module/Spacecraft/Lib/SpacecraftStateChangerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

interface SpacecraftStateChangerInterface
{
// alert energy consumption
public const ALERT_YELLOW_EPS_USAGE = 1;
public const ALERT_RED_EPS_USAGE = 2;

public function changeState(
SpacecraftWrapperInterface $wrapper,
SpacecraftStateEnum $newState
Expand Down
7 changes: 1 addition & 6 deletions src/Module/Spacecraft/Lib/SpacecraftWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,7 @@ private function reloadEpsUsage(): int
$result += $this->spacecraftSystemManager->getEnergyConsumption($shipSystem->getSystemType());
}

if ($this->spacecraft->getAlertState() == SpacecraftAlertStateEnum::ALERT_YELLOW) {
$result += SpacecraftStateChangerInterface::ALERT_YELLOW_EPS_USAGE;
}
if ($this->spacecraft->getAlertState() == SpacecraftAlertStateEnum::ALERT_RED) {
$result += SpacecraftStateChangerInterface::ALERT_RED_EPS_USAGE;
}
$result += $this->spacecraft->getAlertState()->getEpsUsage();

return $result;
}
Expand Down
92 changes: 92 additions & 0 deletions src/Module/Spacecraft/Lib/Ui/PanelLayerConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace Stu\Module\Spacecraft\Lib\Ui;

use RuntimeException;
use Stu\Lib\Map\FieldTypeEffectEnum;
use Stu\Lib\Map\VisualPanel\Layer\DataProvider\Spacecraftcount\SpacecraftCountLayerTypeEnum;
use Stu\Lib\Map\VisualPanel\Layer\DataProvider\Subspace\SubspaceLayerTypeEnum;
use Stu\Lib\Map\VisualPanel\Layer\PanelLayerCreationInterface;
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
use Stu\Orm\Entity\LayerInterface;
use Stu\Orm\Entity\LocationInterface;
use Stu\Orm\Entity\MapInterface;
use Stu\Orm\Entity\UserInterface;
use Stu\Orm\Repository\UserMapRepositoryInterface;

class PanelLayerConfiguration
{
public function __construct(private UserMapRepositoryInterface $userMapRepository) {}

public function configureLayers(
PanelLayerCreationInterface $panelLayerCreation,
SpacecraftWrapperInterface $wrapper,
LocationInterface $panelCenter,
UserInterface $currentUser,
bool $tachyonFresh,
bool $isShipOnLevel
): void {

$spacecraft = $wrapper->get();

if ($wrapper->get()->getSubspaceState()) {
$panelLayerCreation->addSubspaceLayer($currentUser->getId(), SubspaceLayerTypeEnum::IGNORE_USER);
}

$isLssMalfunctioning = $spacecraft->getLocation()->getFieldType()->hasEffect(FieldTypeEffectEnum::LSS_MALFUNCTION);
if ($isLssMalfunctioning) {
return;
}

$panelLayerCreation
->addShipCountLayer($tachyonFresh, $spacecraft, SpacecraftCountLayerTypeEnum::ALL, 0)
->addBorderLayer($wrapper->get(), $isShipOnLevel)
->addAnomalyLayer();

if ($panelCenter instanceof MapInterface) {
$layer = $panelCenter->getLayer();
if ($layer === null) {
throw new RuntimeException('this should not happen');
}
$panelLayerCreation->addMapLayer($layer);
$this->createUserMapEntries($wrapper, $layer, $currentUser);
} else {
$panelLayerCreation
->addSystemLayer()
->addColonyShieldLayer();
}
}

private function createUserMapEntries(SpacecraftWrapperInterface $wrapper, LayerInterface $layer, UserInterface $currentUser): void
{
$map = $wrapper->get()->getMap();
if ($map === null) {
return;
}

$cx = $map->getX();
$cy = $map->getY();
$range = $wrapper->getSensorRange();

if ($this->isUserMapActive($layer->getId(), $currentUser)) {
$this->userMapRepository->insertMapFieldsForUser(
$currentUser->getId(),
$layer->getId(),
$cx,
$cy,
$range
);
}
}

private function isUserMapActive(int $layerId, UserInterface $currentUser): bool
{
if (!$currentUser->hasColony()) {
return false;
}

return !$currentUser->hasExplored($layerId);
}
}
Loading

0 comments on commit 6c5ef55

Please sign in to comment.