Test Failed
Push — dev ( 79dec8...6e2bec )
by Nico
12:33
created

ShowSectorScan   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 69
c 3
b 1
f 0
dl 0
loc 128
ccs 0
cts 55
cp 0
rs 10
wmc 21

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
C handle() 0 55 12
A getMapPath() 0 8 2
A getSignatures() 0 28 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\View\ShowSectorScan;
6
7
use request;
8
use Stu\Component\Map\EncodedMapInterface;
9
use Stu\Component\Ship\FlightSignatureVisibilityEnum;
10
use Stu\Lib\SignatureWrapper;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Control\ViewControllerInterface;
13
use Stu\Module\Ship\Lib\ShipLoaderInterface;
14
use Stu\Orm\Entity\MapInterface;
15
use Stu\Orm\Entity\ShipInterface;
16
use Stu\Orm\Repository\FlightSignatureRepositoryInterface;
17
use Stu\Orm\Repository\BuoyRepositoryInterface;
18
19
final class ShowSectorScan implements ViewControllerInterface
20
{
21
    public const VIEW_IDENTIFIER = 'SHOW_SECTOR_SCAN';
22
23
    private ShipLoaderInterface $shipLoader;
24
25
    private FlightSignatureRepositoryInterface $flightSignatureRepository;
26
27
    private EncodedMapInterface $encodedMap;
28
29
    private BuoyRepositoryInterface $buoyRepository;
30
31
    /** @var array<int> */
32
    private array $fadedSignaturesUncloaked = [];
33
34
    /** @var array<int> */
35
    private array $fadedSignaturesCloaked = [];
36
37
    public function __construct(
38
        ShipLoaderInterface $shipLoader,
39
        FlightSignatureRepositoryInterface $flightSignatureRepository,
40
        EncodedMapInterface $encodedMap,
41
        BuoyRepositoryInterface $buoyRepository
42
    ) {
43
        $this->shipLoader = $shipLoader;
44
        $this->flightSignatureRepository = $flightSignatureRepository;
45
        $this->encodedMap = $encodedMap;
46
        $this->buoyRepository = $buoyRepository;
47
    }
48
49
    public function handle(GameControllerInterface $game): void
50
    {
51
        $userId = $game->getUser()->getId();
52
53
        $buoy = null;
54
55
        $game->setPageTitle("Sektor Scan");
56
        $game->setMacroInAjaxWindow('html/shipmacros.xhtml/sectorscan');
57
        $game->setTemplateVar('ERROR', true);
58
59
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
60
            request::indInt('id'),
61
            $userId,
62
            true
63
        );
64
        $ship = $wrapper->get();
65
66
        if (!$ship->getNbs()) {
67
            $game->addInformation("Die Nahbereichssensoren sind nicht aktiv");
68
            return;
69
        }
70
71
        $epsSystem = $wrapper->getEpsSystemData();
72
        if ($epsSystem === null || $epsSystem->getEps() < 1) {
73
            $game->addInformation("Nicht genügend Energie vorhanden (1 benötigt)");
74
            return;
75
        }
76
77
        $epsSystem->lowerEps(1)->update();
78
79
        $mapField = $ship->getCurrentMapField();
80
81
        $colonyClass = $mapField->getFieldType()->getColonyClass();
82
        if ($colonyClass !== null) {
83
            $game->checkDatabaseItem($colonyClass->getDatabaseId());
84
        }
85
        if ($mapField->getSystem() !== null && $mapField->getFieldType()->getIsSystem()) {
86
            $game->checkDatabaseItem($mapField->getSystem()->getSystemType()->getDatabaseEntryId());
87
        }
88
        if ($ship->getStarsystemMap() !== null) {
89
            $buoy = $this->buoyRepository->findBySysMapId($ship->getStarsystemMap()->getId());
90
        }
91
        if ($ship->getMap() !== null) {
92
            $buoy = $this->buoyRepository->findByMapId($ship->getMap()->getId());
93
        }
94
95
        $game->setTemplateVar('SIGNATURES', $this->getSignatures($mapField->getId(), $ship->getSystem() !== null, $userId));
96
        $game->setTemplateVar('OTHER_SIG_COUNT', empty($this->fadedSignaturesUncloaked) ? null : count($this->fadedSignaturesUncloaked));
97
        $game->setTemplateVar('OTHER_CLOAKED_COUNT', empty($this->fadedSignaturesCloaked) ? null : count($this->fadedSignaturesCloaked));
98
        $game->setTemplateVar('SHIP', $ship);
99
        $game->setTemplateVar('MAP_PATH', $this->getMapPath($ship));
100
        if ($buoy != null) {
101
            $game->setTemplateVar('BUOY', $buoy);
102
        }
103
        $game->setTemplateVar('ERROR', false);
104
    }
105
106
    /**
107
     * @return array<int, SignatureWrapper>
108
     */
109
    private function getSignatures(int $fieldId, bool $isSystem, int $ignoreId): array
110
    {
111
        $allSigs = $this->flightSignatureRepository->getVisibleSignatures($fieldId, $isSystem, $ignoreId);
112
113
        $filteredSigs = [];
114
115
        foreach ($allSigs as $sig) {
116
            $id = $sig->getShipId();
117
            $name = $sig->getShipName();
118
119
            if (!array_key_exists($id . '_' . $name, $filteredSigs)) {
120
                $wrapper = new SignatureWrapper($sig);
121
122
                if ($wrapper->getRump() === null) {
123
                    if ($sig->isCloaked()) {
124
                        if ($sig->getTime() > (time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_CLOAKED)) {
125
                            $this->fadedSignaturesCloaked[$id] = $id;
126
                        }
127
                    } else {
128
                        $this->fadedSignaturesUncloaked[$id] = $id;
129
                    }
130
                } else {
131
                    $filteredSigs[$id . '_' . $name] = $wrapper;
132
                }
133
            }
134
        }
135
136
        return $filteredSigs;
137
    }
138
139
    private function getMapPath(ShipInterface $ship): string
140
    {
141
        $currentMapField = $ship->getCurrentMapField();
142
143
        if ($currentMapField instanceof MapInterface) {
144
            return $this->encodedMap->getEncodedMapPath($currentMapField->getFieldId(), $currentMapField->getLayer());
145
        } else {
146
            return sprintf('%d.png', $currentMapField->getFieldId());
147
        }
148
    }
149
}