Skip to content

Commit

Permalink
show anomaly warning
Browse files Browse the repository at this point in the history
  • Loading branch information
g5bot committed Dec 2, 2023
1 parent 2606807 commit fb63451
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 10 deletions.
12 changes: 7 additions & 5 deletions src/Module/Ship/View/ShowShip/ShowShip.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Stu\Module\Database\View\Category\Tal\DatabaseCategoryTalFactoryInterface;
use Stu\Module\Logging\LoggerUtilFactoryInterface;
use Stu\Module\Logging\LoggerUtilInterface;
use Stu\Module\Ship\Lib\Battle\FightLibInterface;
use Stu\Module\Ship\Lib\ShipLoaderInterface;
use Stu\Module\Ship\Lib\ShipRumpSpecialAbilityEnum;
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
Expand All @@ -33,6 +32,7 @@
use Stu\Orm\Entity\ShipInterface;
use Stu\Orm\Entity\StationShipRepairInterface;
use Stu\Orm\Entity\UserInterface;
use Stu\Orm\Repository\AnomalyRepositoryInterface;
use Stu\Orm\Repository\AstroEntryRepositoryInterface;
use Stu\Orm\Repository\DatabaseUserRepositoryInterface;
use Stu\Orm\Repository\ShipyardShipQueueRepositoryInterface;
Expand Down Expand Up @@ -77,7 +77,7 @@ final class ShowShip implements ViewControllerInterface

private ShipCrewCalculatorInterface $shipCrewCalculator;

private FightLibInterface $fightLib;
private AnomalyRepositoryInterface $anomalyRepository;

public function __construct(
SessionInterface $session,
Expand All @@ -95,7 +95,7 @@ public function __construct(
ColonyLibFactoryInterface $colonyLibFactory,
ShipUiFactoryInterface $shipUiFactory,
ShipCrewCalculatorInterface $shipCrewCalculator,
FightLibInterface $fightLib,
AnomalyRepositoryInterface $anomalyRepository,
LoggerUtilFactoryInterface $loggerUtilFactory
) {
$this->session = $session;
Expand All @@ -115,7 +115,9 @@ public function __construct(
$this->colonyLibFactory = $colonyLibFactory;
$this->shipUiFactory = $shipUiFactory;
$this->shipCrewCalculator = $shipCrewCalculator;
$this->fightLib = $fightLib;
$this->anomalyRepository = $anomalyRepository;

//$this->loggerUtil->init('SHOW', LoggerEnum::LEVEL_ERROR);
}

public function handle(GameControllerInterface $game): void
Expand Down Expand Up @@ -196,7 +198,7 @@ public function handle(GameControllerInterface $game): void
$game->setTemplateVar('CAN_COLONIZE', $canColonize);
$game->setTemplateVar('OWNS_CURRENT_COLONY', $ownsCurrentColony);
$game->setTemplateVar('CURRENT_COLONY', $colony);
$game->setTemplateVar('FIGHT_LIB', $this->fightLib);
$game->setTemplateVar('CLOSEST_ANOMALY_DISTANCE', $this->anomalyRepository->getClosestAnomalyDistance($ship));

$userLayers = $user->getUserLayers();
if ($ship->hasTranswarp()) {
Expand Down
12 changes: 11 additions & 1 deletion src/Module/Twig/TwigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Stu\Component\Colony\ColonyMenuEnum;
use Stu\Component\Game\ModuleViewEnum;
use Stu\Lib\Colony\PlanetFieldHostInterface;
use Stu\Lib\ModuleScreen\GradientColorInterface;
use Stu\Module\Colony\Lib\ColonyEpsProductionPreviewWrapper;
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
use Stu\Module\Colony\Lib\ColonyProductionPreviewWrapper;
Expand All @@ -35,18 +36,22 @@ class TwigHelper

private ColonyLibFactoryInterface $colonyLibFactory;

private GradientColorInterface $gradientColor;

public function __construct(
Environment $environment,
Parser $parser,
ConfigInterface $config,
FightLibInterface $fightLib,
ColonyLibFactoryInterface $colonyLibFactory
ColonyLibFactoryInterface $colonyLibFactory,
GradientColorInterface $gradientColor
) {
$this->environment = $environment;
$this->parser = $parser;
$this->config = $config;
$this->fightLib = $fightLib;
$this->colonyLibFactory = $colonyLibFactory;
$this->gradientColor = $gradientColor;
}

public function registerGlobalVariables(): void
Expand Down Expand Up @@ -189,5 +194,10 @@ private function registerFunctions(): void
return uniqid();
});
$this->environment->addFunction($getUniqIdFunction);

$gradientColorFunction = new TwigFunction('gradientColor', function (int $value, int $lowest, int $highest): string {
return $this->gradientColor->calculateGradientColor($value, $lowest, $highest);
});
$this->environment->addFunction($gradientColorFunction);
}
}
43 changes: 43 additions & 0 deletions src/Orm/Repository/AnomalyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
namespace Stu\Orm\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
use Stu\Orm\Entity\Anomaly;
use Stu\Orm\Entity\AnomalyInterface;
use Stu\Orm\Entity\Map;
use Stu\Orm\Entity\ShipInterface;

/**
* @extends EntityRepository<Anomaly>
Expand Down Expand Up @@ -48,4 +51,44 @@ public function findAllActive(): array
)
->getResult();
}

public function getClosestAnomalyDistance(ShipInterface $ship): ?int
{
$map = $ship->getMap();
if ($map === null) {
return null;
}

$range = $ship->getSensorRange() * 2;

try {
return (int)$this->getEntityManager()->createQuery(
sprintf(
'SELECT SQRT(ABS(m.cx - :x) * ABS(m.cx - :x) + ABS(m.cy - :y) * ABS(m.cy - :y)) as foo
FROM %s a
JOIN %s m
WITH a.map_id = m.id
WHERE (m.cx BETWEEN :startX AND :endX)
AND (m.cy BETWEEN :startY AND :endY)
AND m.layer = :layer
ORDER BY foo ASC',
Anomaly::class,
Map::class
)
)
->setMaxResults(1)
->setParameters([
'layer' => $map->getLayer(),
'x' => $map->getX(),
'y' => $map->getY(),
'startX' => $map->getX() - $range,
'endX' => $map->getX() + $range,
'startY' => $map->getY() - $range,
'endY' => $map->getY() + $range
])
->getSingleScalarResult();
} catch (NoResultException) {
return null;
}
}
}
3 changes: 3 additions & 0 deletions src/Orm/Repository/AnomalyRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\Persistence\ObjectRepository;
use Stu\Orm\Entity\Anomaly;
use Stu\Orm\Entity\AnomalyInterface;
use Stu\Orm\Entity\ShipInterface;

/**
* @extends ObjectRepository<Anomaly>
Expand All @@ -24,4 +25,6 @@ public function delete(AnomalyInterface $anomaly): void;
* @return array<AnomalyInterface>
*/
public function findAllActive(): array;

public function getClosestAnomalyDistance(ShipInterface $ship): ?int;
}
4 changes: 2 additions & 2 deletions src/html/ship/ship.twig
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
</div>
<br />
{{ control_mid(WRAPPER, ASTRO_STATE_SYSTEM, ASTRO_STATE_REGION, TACHYON_ACTIVE, STARSYSTEM_ENTRY_TAL,
CURRENT_COLONY, CAN_COLONIZE)
CURRENT_COLONY, CAN_COLONIZE, CLOSEST_ANOMALY_DISTANCE)
}}
{{ control_right(WRAPPER, CONSTRUCTION_PROGRESS_WRAPPER, POSSIBLE_STATIONS, MODULE_SELECTORS,
USER_LAYERS)
Expand All @@ -275,7 +275,7 @@
</td>
<td style="vertical-align: top; width: 33%;">
{{ control_mid(WRAPPER, ASTRO_STATE_SYSTEM, ASTRO_STATE_REGION, TACHYON_ACTIVE, STARSYSTEM_ENTRY_TAL,
CURRENT_COLONY, CAN_COLONIZE)
CURRENT_COLONY, CAN_COLONIZE, CLOSEST_ANOMALY_DISTANCE)
}}
</td>
<td style="vertical-align: top; width: 33%;">
Expand Down
9 changes: 7 additions & 2 deletions src/html/shipcontrol/control_mid.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% macro control_mid(wrapper, astroStateSystem, astroStateRegion, isTachyonActive, starSystemEntryTal, currentColony,
canColonize) %}
canColonize, closestAnomalyDistance) %}

{% from 'html/macros.twig' import onClickShip %}

Expand Down Expand Up @@ -292,11 +292,16 @@ canColonize) %}
{{ _self.influenceArea(ship.getCurrentMapField.getInfluenceArea.getBase.getUser) }}
{% endif %}
{% set anomalies = ship.getLocation.getAnomalies %}
{% if not anomalies.isEmpty %}
{% for anomaly in anomalies %}
<br />
{% include anomaly.getAnomalyType.getTemplate %}
{% endfor %}

{% elseif closestAnomalyDistance %}
<br />
<span style="border: 2px solid {{ gradientColor(closestAnomalyDistance, ship.getSensorRange * 2, 1) }}"
title="Es wurde eine Anomalie in der Nähe detektiert">Anomalie in der Nähe</span>
{% endif %}
</td>
{% if starSystem %}
<td>
Expand Down

0 comments on commit fb63451

Please sign in to comment.