Passed
Push — master ( 981eff...8f57e5 )
by Nico
17:25 queued 11:44
created

MaindeskProvider::setActiveQuests()   B

Complexity

Conditions 9
Paths 34

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 20.3892

Importance

Changes 0
Metric Value
cc 9
eloc 24
nc 34
nop 1
dl 0
loc 42
ccs 12
cts 25
cp 0.48
crap 20.3892
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Game\Lib\View\Provider;
6
7
use Stu\Component\Colony\ColonyTypeEnum;
8
use Stu\Component\Communication\Kn\KnFactoryInterface;
9
use Stu\Component\Communication\Kn\KnItemInterface;
10
use Stu\Component\Crew\CrewCountRetrieverInterface;
11
use Stu\Component\Game\GameEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\GameEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Stu\Component\Player\ColonyLimitCalculatorInterface;
13
use Stu\Component\Player\CrewLimitCalculatorInterface;
14
use Stu\Component\Player\Relation\PlayerRelationDeterminatorInterface;
15
use Stu\Component\Player\Settings\UserSettingsProviderInterface;
16
use Stu\Module\Control\GameControllerInterface;
17
use Stu\Module\PlayerSetting\Lib\UserStateEnum;
18
use Stu\Module\Spacecraft\Lib\EmergencyWrapper;
19
use Stu\Orm\Entity\KnPost;
20
use Stu\Orm\Repository\AllianceBoardTopicRepositoryInterface;
21
use Stu\Orm\Repository\ColonyShipQueueRepositoryInterface;
22
use Stu\Orm\Repository\HistoryRepositoryInterface;
23
use Stu\Orm\Repository\KnPostRepositoryInterface;
24
use Stu\Orm\Repository\NPCQuestRepositoryInterface;
25
use Stu\Orm\Repository\NPCQuestUserRepositoryInterface;
26
use Stu\Orm\Repository\ShipyardShipQueueRepositoryInterface;
27
use Stu\Orm\Repository\SpacecraftEmergencyRepositoryInterface;
28
use Stu\Orm\Repository\UserProfileVisitorRepositoryInterface;
29
use Stu\Orm\Repository\UserRepositoryInterface;
30
31
final class MaindeskProvider implements ViewComponentProviderInterface
32
{
33 1
    public function __construct(
34
        private readonly HistoryRepositoryInterface $historyRepository,
35
        private readonly AllianceBoardTopicRepositoryInterface $allianceBoardTopicRepository,
36
        private readonly UserProfileVisitorRepositoryInterface $userProfileVisitorRepository,
37
        private readonly KnPostRepositoryInterface $knPostRepository,
38
        private readonly ColonyShipQueueRepositoryInterface $colonyShipQueueRepository,
39
        private readonly ShipyardShipQueueRepositoryInterface $shipyardShipQueueRepository,
40
        private readonly UserRepositoryInterface $userRepository,
41
        private readonly SpacecraftEmergencyRepositoryInterface $spacecraftEmergencyRepository,
42
        private readonly UserSettingsProviderInterface $userSettingsProvider,
43
        private readonly KnFactoryInterface $knFactory,
44
        private readonly ColonyLimitCalculatorInterface $colonyLimitCalculator,
45
        private readonly PlayerRelationDeterminatorInterface $playerRelationDeterminator,
46
        private readonly CrewLimitCalculatorInterface $crewLimitCalculator,
47
        private readonly CrewCountRetrieverInterface $crewCountRetriever,
48
        private readonly NPCQuestRepositoryInterface $npcQuestRepository,
49
        private readonly NPCQuestUserRepositoryInterface $npcQuestUserRepository
50 1
    ) {}
51
52 2
    #[\Override]
53
    public function setTemplateVariables(GameControllerInterface $game): void
54
    {
55 2
        $user = $game->getUser();
56 2
        $userId = $user->getId();
57
58 2
        $game->setTemplateVar(
59 2
            'DISPLAY_FIRST_COLONY_DIALOGUE',
60 2
            $user->getState() === UserStateEnum::UNCOLONIZED
61 2
        );
62
63 2
        $game->setTemplateVar(
64 2
            'DISPLAY_COLONIZATION_SHIP_DIALOGUE',
65 2
            $user->getState() === UserStateEnum::COLONIZATION_SHIP
66 2
        );
67
68 2
        $newAmount = $this->knPostRepository->getAmountSince($user->getKnMark());
69
70 2
        $game->setTemplateVar(
71 2
            'NEW_KN_POSTING_COUNT',
72 2
            $newAmount
73 2
        );
74 2
        $newKnPostings = $this->knPostRepository->getNewerThenMark($user->getKnMark());
75 2
        if ($newKnPostings !== []) {
76 2
            $game->setTemplateVar('MARKED_KN_ID', $newKnPostings[0]->getId());
77
        }
78 2
        $game->setTemplateVar(
79 2
            'NEW_KN_POSTINGS',
80 2
            array_map(
81 2
                function (KnPost $knPost) use ($user, $newAmount): KnItemInterface {
82 2
                    $newAmount--;
83 2
                    $knItem = $this->knFactory->createKnItem(
84 2
                        $knPost,
85 2
                        $user
86 2
                    );
87 2
                    $knItem->setMark(((int)floor($newAmount / GameEnum::KN_PER_SITE)) * 6);
88 2
                    return $knItem;
89 2
                },
90 2
                $newKnPostings
91 2
            )
92 2
        );
93 2
        $game->setTemplateVar(
94 2
            'RECENT_PROFILE_VISITORS',
95 2
            $this->userProfileVisitorRepository->getRecent($userId)
96 2
        );
97 2
        $game->setTemplateVar(
98 2
            'RANDOM_ONLINE_USER',
99 2
            $this->userRepository->getOrderedByLastaction(35, $userId, time() - GameEnum::USER_ONLINE_PERIOD)
100 2
        );
101 2
        $game->setTemplateVar(
102 2
            'SHIP_BUILD_PROGRESS',
103 2
            [...$this->colonyShipQueueRepository->getByUserAndMode($userId, 1), ...$this->shipyardShipQueueRepository->getByUser($userId)]
104 2
        );
105 2
        $game->setTemplateVar(
106 2
            'SHIP_RETROFIT_PROGRESS',
107 2
            [...$this->colonyShipQueueRepository->getByUserAndMode($userId, 2)]
108 2
        );
109
110 2
        $alliance = $user->getAlliance();
111 2
        if ($alliance !== null) {
112 2
            $game->setTemplateVar('ALLIANCE', true);
113
114 2
            $game->setTemplateVar(
115 2
                'RECENT_ALLIANCE_BOARD_TOPICS',
116 2
                $this->allianceBoardTopicRepository->getRecentByAlliance($alliance->getId())
117 2
            );
118
        }
119
120 2
        if ($this->userSettingsProvider->isShowPirateHistoryEntrys($user)) {
121
            $game->setTemplateVar('RECENT_HISTORY', $this->historyRepository->getRecent());
122
        } else {
123 2
            $game->setTemplateVar('RECENT_HISTORY', $this->historyRepository->getRecentWithoutPirate());
124
        }
125
126
        //emergencies
127 2
        $this->setPotentialEmergencies($game);
128
129
        //planet
130 2
        $game->setTemplateVar('PLANET_LIMIT', $this->colonyLimitCalculator->getColonyLimitWithType($user, ColonyTypeEnum::PLANET));
131 2
        $game->setTemplateVar('PLANET_COUNT', $this->colonyLimitCalculator->getColonyCountWithType($user, ColonyTypeEnum::PLANET));
132
133
        //moon
134 2
        $game->setTemplateVar('MOON_LIMIT', $this->colonyLimitCalculator->getColonyLimitWithType($user, ColonyTypeEnum::MOON));
135 2
        $game->setTemplateVar('MOON_COUNT', $this->colonyLimitCalculator->getColonyCountWithType($user, ColonyTypeEnum::MOON));
136
137
        //asteroid
138 2
        $game->setTemplateVar('ASTEROID_LIMIT', $this->colonyLimitCalculator->getColonyLimitWithType($user, ColonyTypeEnum::ASTEROID));
139 2
        $game->setTemplateVar('ASTEROID_COUNT', $this->colonyLimitCalculator->getColonyCountWithType($user, ColonyTypeEnum::ASTEROID));
140
141 2
        $game->setTemplateVar(
142 2
            'CREW_LIMIT',
143 2
            $this->crewLimitCalculator->getGlobalCrewLimit($user)
144 2
        );
145
146
        // crew count
147 2
        $game->setTemplateVar(
148 2
            'CREW_COUNT_SHIPS',
149 2
            $this->crewCountRetriever->getAssignedToShipsCount($user)
150 2
        );
151
152 2
        $this->setActiveQuests($game);
153
    }
154
155 2
    private function setPotentialEmergencies(GameControllerInterface $game): void
156
    {
157 2
        $emergencies = $this->spacecraftEmergencyRepository->getActive();
158
159 2
        if ($emergencies === []) {
160 2
            return;
161
        }
162
163
        $emergencyWrappers = [];
164
165
        foreach ($emergencies as $emergency) {
166
            $emergencyWrappers[] = new EmergencyWrapper($this->playerRelationDeterminator, $emergency, $game->getUser());
167
        }
168
169
        $game->setTemplateVar('EMERGENCYWRAPPERS', $emergencyWrappers);
170
    }
171
172 2
    private function setActiveQuests(GameControllerInterface $game): void
173
    {
174 2
        $user = $game->getUser();
175 2
        $userFactionId = $user->getFactionId();
176
177 2
        $userQuestIds = [];
178 2
        $userQuests = $this->npcQuestUserRepository->findBy(['user_id' => $user->getId()]);
179 2
        foreach ($userQuests as $userQuest) {
180
            $userQuestIds[] = $userQuest->getQuestId();
181
        }
182
183 2
        $allActiveQuests = $this->npcQuestRepository->getActiveQuests();
184 2
        $visibleQuests = [];
185
186 2
        foreach ($allActiveQuests as $quest) {
187
            $isParticipant = in_array($quest->getId(), $userQuestIds);
188
189
            $canSeeFactions = $quest->getFactions();
190
            $secretFactions = $quest->getSecret();
191
192
            $canSeeQuest = false;
193
            if ($canSeeFactions === null || in_array($userFactionId, $canSeeFactions)) {
194
                $canSeeQuest = true;
195
            }
196
197
            if ($secretFactions !== null && !in_array($userFactionId, $secretFactions)) {
198
                $canSeeQuest = false;
199
            }
200
201
            if ($isParticipant) {
202
                $canSeeQuest = true;
203
            }
204
205
            if ($canSeeQuest) {
206
                $visibleQuests[] = $quest;
207
            }
208
        }
209
210 2
        $recentQuests = array_slice($visibleQuests, 0, 3);
211
212 2
        $game->setTemplateVar('ACTIVE_QUESTS', $recentQuests);
213 2
        $game->setTemplateVar('ACTIVE_QUEST_COUNT', count($visibleQuests));
214
    }
215
}