Passed
Push — dev ( f2f2f9...283256 )
by Nico
09:10
created

ShowSectorScan::handle()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 1
nop 1
dl 0
loc 25
ccs 0
cts 19
cp 0
crap 12
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Colony\View\ShowSectorScan;
6
7
use request;
8
use Stu\Component\Ship\FlightSignatureVisibilityEnum;
9
use Stu\Lib\SignatureWrapper;
10
use Stu\Module\Colony\Lib\ColonyLoaderInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Control\ViewControllerInterface;
13
use Stu\Orm\Repository\FlightSignatureRepositoryInterface;
14
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
15
16
final class ShowSectorScan implements ViewControllerInterface
17
{
18
    public const VIEW_IDENTIFIER = 'SHOW_SECTOR_SCAN';
19
20
    private ColonyLoaderInterface $colonyLoader;
21
22
    private StarSystemMapRepositoryInterface $mapRepository;
23
24
    private FlightSignatureRepositoryInterface $flightSignatureRepository;
25
26
    private $fadedSignaturesUncloaked = [];
27
    private $fadedSignaturesCloaked = [];
28
29
    public function __construct(
30
        ColonyLoaderInterface $colonyLoader,
31
        StarSystemMapRepositoryInterface $mapRepository,
32
        FlightSignatureRepositoryInterface $flightSignatureRepository
33
    ) {
34
        $this->colonyLoader = $colonyLoader;
35
        $this->mapRepository = $mapRepository;
36
        $this->flightSignatureRepository = $flightSignatureRepository;
37
    }
38
39
    public function handle(GameControllerInterface $game): void
40
    {
41
        $userId = $game->getUser()->getId();
42
43
        $colony = $this->colonyLoader->byIdAndUser(
44
            request::indInt('id'),
45
            $userId,
46
            false
47
        );
48
49
        $game->setPageTitle("Sektor Scan");
50
        $game->setMacroInAjaxWindow('html/colonymacros.xhtml/sectorscan');
51
52
        $game->setTemplateVar('ERROR', true);
53
54
        $mapField = $this->mapRepository->getByCoordinates(
55
            $colony->getSystem()->getId(),
56
            $colony->getSx(),
57
            $colony->getSy()
58
        );
59
60
        $game->setTemplateVar('SIGNATURES', $this->getSignatures($mapField, true, $userId));
61
        $game->setTemplateVar('OTHER_SIG_COUNT', empty($this->fadedSignaturesUncloaked) ? null : count($this->fadedSignaturesUncloaked));
62
        $game->setTemplateVar('OTHER_CLOAKED_COUNT', empty($this->fadedSignaturesCloaked) ? null : count($this->fadedSignaturesCloaked));
63
        $game->setTemplateVar('ERROR', false);
64
    }
65
66
    private function getSignatures($field, $isSystem, $ignoreId)
67
    {
68
        $allSigs = $this->flightSignatureRepository->getVisibleSignatures((int) $field->getId(), $isSystem, $ignoreId);
69
70
        $filteredSigs = [];
71
72
        foreach ($allSigs as $sig) {
73
            $id = $sig->getShipId();
74
            $name = $sig->getShipName();
75
76
            if (!array_key_exists($id . '_' . $name, $filteredSigs)) {
77
                $wrapper = new SignatureWrapper($sig);
78
79
                if ($wrapper->getRump() === null) {
80
                    if ($sig->isCloaked()) {
81
                        if ($sig->getTime() > (time() - FlightSignatureVisibilityEnum::SIG_VISIBILITY_CLOAKED)) {
82
                            $this->fadedSignaturesCloaked[$id] = $id;
83
                        }
84
                    } else {
85
                        $this->fadedSignaturesUncloaked[$id] = $id;
86
                    }
87
                } else {
88
                    $filteredSigs[$id . '_' . $name] = $wrapper;
89
                }
90
            }
91
        }
92
93
        return $filteredSigs;
94
    }
95
}
96