Skip to content

Commit

Permalink
use map encoding where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
g5bot committed Sep 8, 2023
1 parent 6ffe031 commit 64d07e0
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 72 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5401,6 +5401,11 @@ parameters:
count: 1
path: src/Orm/Repository/ColonyScanRepositoryInterface.php

-
message: "#^Method Stu\\\\Orm\\\\Repository\\\\MapRepository\\:\\:getForSubspaceEllipseCreation\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Orm/Repository/MapRepository.php

-
message: "#^Method Stu\\\\Orm\\\\Repository\\\\PlanetFieldRepository\\:\\:getWorkerConsumingByColonyAndState\\(\\) has parameter \\$state with no value type specified in iterable type array\\.$#"
count: 1
Expand Down
14 changes: 9 additions & 5 deletions src/Module/Admin/View/Map/EditSection/EditSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Stu\Module\Admin\View\Map\EditSection;

use RuntimeException;
use Stu\Component\Map\MapEnum;
use Stu\Module\Control\GameControllerInterface;
use Stu\Module\Control\ViewControllerInterface;
Expand Down Expand Up @@ -55,6 +56,9 @@ public function handle(GameControllerInterface $game): void
{
$layerId = $this->request->getLayerId();
$layer = $this->layerRepository->find($layerId);
if ($layer === null) {
throw new RuntimeException(sprintf('layerId %d does not exist', $layerId));
}

$xCoordinate = $this->request->getXCoordinate($layer);
$yCoordinate = $this->request->getYCoordinate($layer);
Expand All @@ -67,21 +71,21 @@ public function handle(GameControllerInterface $game): void

$fields = [];
foreach (range($miny, $maxy) as $value) {
$fields[] = $this->starmapUiFactory->createYRow($layerId, $value, $minx, $maxx, 0);
$fields[] = $this->starmapUiFactory->createYRow($layer, $value, $minx, $maxx, 0);
}

if ($yCoordinate - 1 >= 1) {
$game->setTemplateVar(
'TOP_PREVIEW_ROW',
$this->starmapUiFactory->createYRow($layerId, $yCoordinate * MapEnum::FIELDS_PER_SECTION - MapEnum::FIELDS_PER_SECTION, $minx, $maxx, 0)->getFields()
$this->starmapUiFactory->createYRow($layer, $yCoordinate * MapEnum::FIELDS_PER_SECTION - MapEnum::FIELDS_PER_SECTION, $minx, $maxx, 0)->getFields()
);
} else {
$game->setTemplateVar('TOP_PREVIEW_ROW', false);
}
if ($yCoordinate * MapEnum::FIELDS_PER_SECTION + 1 <= $layer->getHeight()) {
$game->setTemplateVar(
'BOTTOM_PREVIEW_ROW',
$this->starmapUiFactory->createYRow($layerId, $yCoordinate * MapEnum::FIELDS_PER_SECTION + 1, $minx, $maxx, 0)->getFields()
$this->starmapUiFactory->createYRow($layer, $yCoordinate * MapEnum::FIELDS_PER_SECTION + 1, $minx, $maxx, 0)->getFields()
);
} else {
$game->setTemplateVar(
Expand All @@ -92,7 +96,7 @@ public function handle(GameControllerInterface $game): void
if ($xCoordinate - 1 >= 1) {
$row = [];
for ($i = $miny; $i <= $maxy; $i++) {
$row[] = $this->starmapUiFactory->createYRow($layerId, $i, $minx - 1, $minx - 1, 0);
$row[] = $this->starmapUiFactory->createYRow($layer, $i, $minx - 1, $minx - 1, 0);
}

$game->setTemplateVar(
Expand All @@ -109,7 +113,7 @@ public function handle(GameControllerInterface $game): void
if ($xCoordinate * MapEnum::FIELDS_PER_SECTION + 1 <= $layer->getWidth()) {
$row = [];
for ($i = $miny; $i <= $maxy; $i++) {
$row[] = $this->starmapUiFactory->createYRow($layerId, $i, $maxx + 1, $maxx + 1, 0);
$row[] = $this->starmapUiFactory->createYRow($layer, $i, $maxx + 1, $maxx + 1, 0);
}

$game->setTemplateVar(
Expand Down
41 changes: 41 additions & 0 deletions src/Module/Admin/View/Map/EditSection/StarMapItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Stu\Module\Admin\View\Map\EditSection;

use Stu\Component\Map\EncodedMapInterface;
use Stu\Orm\Entity\MapInterface;

class StarMapItem
{
private EncodedMapInterface $encodedMap;

private MapInterface $map;

public function __construct(
EncodedMapInterface $encodedMap,
MapInterface $map
) {
$this->encodedMap = $encodedMap;
$this->map = $map;
}

public function getMap(): MapInterface
{
return $this->map;
}

public function getMapGraphicPath(): string
{
$layer = $this->map->getLayer();

if ($layer->isEncoded()) {
return $this->encodedMap->getEncodedMapPath(
$this->map->getFieldId(),
$layer
);
}
return sprintf('%d/%d.png', $layer->getId(), $this->map->getFieldId());
}
}
4 changes: 3 additions & 1 deletion src/Module/Starmap/Lib/ExplorableStarMapItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ public function getFieldStyle(): string
{
if ($this->hide === true) {
$imageUrl = '0.png';
} else {
} else if ($this->layer->isEncoded()) {
$imageUrl = $this->encodedMap->getEncodedMapPath($this->getFieldId(), $this->getLayer());
} else {
$imageUrl = sprintf('%d/%d.png', $this->getLayer()->getId(), $this->getFieldId());
}

$style = "background-image: url('assets/map/" . $imageUrl . "'); opacity:1;";
Expand Down
2 changes: 2 additions & 0 deletions src/Module/Starmap/Lib/StarmapUiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function createYRow(
return new YRow(
$this->mapRepository,
$this->starSystemMapRepository,
$this->encodedMap,
$layer,
$cury,
$minx,
Expand All @@ -79,6 +80,7 @@ public function createUserYRow(
$this,
$this->mapRepository,
$this->starSystemMapRepository,
$this->encodedMap,
$user,
$layer,
$cury,
Expand Down
2 changes: 1 addition & 1 deletion src/Module/Starmap/Lib/StarmapUiFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public function createUserYRow(
public function createExplorableStarmapItem(
ExploreableStarMapInterface $exploreableStarMap,
LayerInterface $layer
): ExplorableStarMapItem;
): ExplorableStarMapItemInterface;
}
22 changes: 13 additions & 9 deletions src/Module/Starmap/Lib/UserYRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Stu\Module\Starmap\Lib;

use Generator;
use RuntimeException;
use Stu\Component\Map\EncodedMapInterface;
use Stu\Orm\Entity\LayerInterface;
use Stu\Orm\Entity\MapInterface;
use Stu\Orm\Entity\StarSystemMapInterface;
use Stu\Orm\Entity\UserInterface;
use Stu\Orm\Repository\MapRepositoryInterface;
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
Expand All @@ -24,6 +22,7 @@ public function __construct(
StarmapUiFactoryInterface $starmapUiFactory,
MapRepositoryInterface $mapRepository,
StarSystemMapRepositoryInterface $starSystemMapRepository,
EncodedMapInterface $encodedMap,
UserInterface $user,
LayerInterface $layer,
int $cury,
Expand All @@ -34,6 +33,7 @@ public function __construct(
parent::__construct(
$mapRepository,
$starSystemMapRepository,
$encodedMap,
$layer,
$cury,
$minx,
Expand All @@ -46,16 +46,16 @@ public function __construct(
}

/**
* @return Generator<MapInterface|null>|Generator<StarSystemMapInterface|null>|Generator<ExplorableStarMapItemInterface|null>
* @return array<ExplorableStarMapItemInterface>
*/
public function getFields(): Generator
public function getFields(): array
{
$layer = $this->layer;
if ($layer === null) {
throw new RuntimeException('this should not happen');
}

$result = $this->mapRepository->getExplored(
$maps = $this->mapRepository->getExplored(
$this->user->getId(),
$layer->getId(),
$this->minx,
Expand All @@ -64,14 +64,18 @@ public function getFields(): Generator
);
$hasExploredLayer = $this->user->hasExplored($layer->getId());

/** @var ExploreableStarMap $item */
foreach ($result as $item) {
$result = [];

foreach ($maps as $item) {

$starmapItem = $this->starmapUiFactory->createExplorableStarmapItem($item, $layer);
if (!$hasExploredLayer && $item->getUserId() === null) {
$starmapItem->setHide(true);
}

yield $item->getCx() => $starmapItem;
$result[$item->getCx()] = $starmapItem;
}

return $result;
}
}
31 changes: 23 additions & 8 deletions src/Module/Starmap/Lib/YRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace Stu\Module\Starmap\Lib;

use RuntimeException;
use Stu\Component\Map\EncodedMapInterface;
use Stu\Module\Admin\View\Map\EditSection\StarMapItem;
use Stu\Orm\Entity\LayerInterface;
use Stu\Orm\Entity\MapInterface;
use Stu\Orm\Entity\StarSystemInterface;
use Stu\Orm\Entity\StarSystemMapInterface;
use Stu\Orm\Repository\MapRepositoryInterface;
Expand All @@ -24,16 +25,19 @@ class YRow

protected int|StarSystemInterface $system;

/** @var null|array<MapInterface|null>|array<StarSystemMapInterface|null> */
/** @var null|array<StarMapItem>|array<StarSystemMapInterface> */
protected $fields;

private MapRepositoryInterface $mapRepository;

private StarSystemMapRepositoryInterface $systemMapRepository;

private EncodedMapInterface $encodedMap;

public function __construct(
MapRepositoryInterface $mapRepository,
StarSystemMapRepositoryInterface $systemMapRepository,
EncodedMapInterface $encodedMap,
?LayerInterface $layer,
int $cury,
int $minx,
Expand All @@ -47,12 +51,13 @@ public function __construct(
$this->system = $system;
$this->mapRepository = $mapRepository;
$this->systemMapRepository = $systemMapRepository;
$this->encodedMap = $encodedMap;
}

/**
* @return array<StarSystemMapInterface|null>|array<MapInterface|null>
* @return array<StarMapItem>|array<StarSystemMapInterface>
*/
public function getFields(): iterable
public function getFields(): array
{
if ($this->fields === null) {
$this->fields = [];
Expand All @@ -62,20 +67,27 @@ public function getFields(): iterable
throw new RuntimeException('this should not happen');
}

$this->fields[] = $this->mapRepository->getByCoordinates(
$map = $this->mapRepository->getByCoordinates(
$layer->getId(),
$i,
$this->row
);

if ($map !== null) {
$this->fields[] = new StarMapItem(
$this->encodedMap,
$map
);
}
}
}
return $this->fields;
}

/**
* @return array<StarSystemMapInterface|null>|array<MapInterface|null>
* @return array<StarMapItem>|array<StarSystemMapInterface>
*/
public function getSystemFields()
public function getSystemFields(): array
{
if ($this->fields === null) {
$this->fields = [];
Expand All @@ -88,11 +100,14 @@ public function getSystemFields()
} else {

for ($i = $this->minx; $i <= $this->maxx; $i++) {
$this->fields[] = $this->systemMapRepository->getByCoordinates(
$systemMap = $this->systemMapRepository->getByCoordinates(
$this->system,
$i,
$this->row
);
if ($systemMap !== null) {
$this->fields[] = $systemMap;
}
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions src/Orm/Entity/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,6 @@ public function getBorder(): string
return 'border: 1px solid ' . $borderType->getColor();
}

public function getFieldStyle(): string
{
// @todo hide unexplored fields
$style = "background-image: url('/assets/map/" . $this->getLayer()->getId() . "/" . $this->getFieldId() . ".png'); opacity:1;";
return $style . $this->getBorder();
}

public function getShips(): Collection
{
return $this->ships;
Expand Down Expand Up @@ -422,4 +415,4 @@ public function getSectorString(): string
{
return SectorString::getForMap($this);
}
}
}
24 changes: 9 additions & 15 deletions src/Orm/Repository/MapRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Stu\Orm\Repository;

use Doctrine\Persistence\ObjectRepository;
use Stu\Module\Starmap\Lib\ExploreableStarMapInterface;
use Stu\Orm\Entity\LayerInterface;
use Stu\Orm\Entity\Map;
use Stu\Orm\Entity\MapInterface;
Expand Down Expand Up @@ -48,22 +49,15 @@ public function getByCoordinateRange(
public function save(MapInterface $map): void;

/**
* @return array<array{
* id: int,
* cx: int,
* cy: int,
* field_id: int,
* bordertype_id: int,
* user_id: int,
* mapped: int,
* system_name: string,
* influence_area_id: int,
* region_id: int,
* tradepost_id: int,
* region_description: string
* }>
* @return array<ExploreableStarMapInterface>
*/
public function getExplored(int $userId, int $layerId, int $startX, int $endX, int $cy): array;
public function getExplored(
int $userId,
int $layerId,
int $startX,
int $endX,
int $cy
): array;

/**
* @return array<MapInterface>
Expand Down
Loading

0 comments on commit 64d07e0

Please sign in to comment.