Test Failed
Push — dev ( 0a21cd...88787c )
by Janko
08:43
created

ColonyTickManager::getLoggerUtil()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Tick\Colony;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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...
8
use Stu\Component\Building\BuildingFunctionEnum;
9
use Stu\Component\Colony\ColonyFunctionManagerInterface;
10
use Stu\Component\Crew\CrewCountRetrieverInterface;
11
use Stu\Component\Player\CrewLimitCalculatorInterface;
12
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
13
use Stu\Module\Crew\Lib\CrewCreatorInterface;
14
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
15
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
16
use Stu\Module\PlayerSetting\Lib\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants 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...
17
use Stu\Module\Tick\AbstractTickManager;
18
use Stu\Module\Tick\Lock\LockManagerInterface;
19
use Stu\Module\Tick\Lock\LockTypeEnum;
20
use Stu\Orm\Repository\ColonyRepositoryInterface;
21
use Stu\Orm\Repository\CrewTrainingRepositoryInterface;
22
use Ubench;
23
24
final class ColonyTickManager extends AbstractTickManager implements ColonyTickManagerInterface
25
{
26
    public function __construct(
27
        private ColonyTickInterface $colonyTick,
28
        private CrewCreatorInterface $crewCreator,
29
        private CrewTrainingRepositoryInterface $crewTrainingRepository,
30
        private ColonyRepositoryInterface $colonyRepository,
31 2
        private PrivateMessageSenderInterface $privateMessageSender,
32
        private CrewCountRetrieverInterface $crewCountRetriever,
33
        private ColonyFunctionManagerInterface $colonyFunctionManager,
34
        private CrewLimitCalculatorInterface $crewLimitCalculator,
35
        private ColonyLibFactoryInterface $colonyLibFactory,
36
        private LockManagerInterface $lockManager,
37
        private Ubench $benchmark
38
    ) {}
39
40
    #[Override]
41
    public function work(int $batchGroup, int $batchGroupCount): void
42
    {
43
        $this->setLock($batchGroup);
44
        try {
45 2
            $entityCount = $this->colonyLoop($batchGroup, $batchGroupCount);
46
            $this->proceedCrewTraining($batchGroup, $batchGroupCount);
47
48 1
            $this->logBenchmarkResult($entityCount);
49
        } finally {
50
            $this->clearLock($batchGroup);
51 1
        }
52
    }
53 1
54
    private function colonyLoop(int $batchGroup, int $batchGroupCount): int
55
    {
56
        $colonyList = $this->colonyRepository->getByBatchGroup($batchGroup, $batchGroupCount);
57
58
        $entityCount = 0;
59
        foreach ($colonyList as $colony) {
60
61
            //handle colony only if vacation mode not active
62
            if (!$colony->getUser()->isVacationRequestOldEnough()) {
63 1
                $this->colonyTick->work($colony);
64
            }
65
66
            $entityCount++;
67 1
        }
68
69 1
        return $entityCount;
70
    }
71
72
    private function proceedCrewTraining(int $batchGroup, int $batchGroupCount): void
73
    {
74
        $user = [];
75
76
        foreach ($this->crewTrainingRepository->getByBatchGroup($batchGroup, $batchGroupCount) as $obj) {
77
            if (!isset($user[$obj->getUserId()])) {
78
                $user[$obj->getUserId()] = 0;
79
            }
80
            if ($user[$obj->getUserId()] >= $this->crewCountRetriever->getTrainableCount($obj->getUser())) {
81
                continue;
82
            }
83
            $colony = $obj->getColony();
84
85
            $freeAssignmentCount = $this->colonyLibFactory->createColonyPopulationCalculator(
86
                $colony
87
            )->getFreeAssignmentCount();
88
89
            //colony can't hold more crew
90
            if ($freeAssignmentCount === 0) {
91
                $this->crewTrainingRepository->delete($obj);
92
                continue;
93
            }
94
95
            $globalCrewLimit = $this->crewLimitCalculator->getGlobalCrewLimit($obj->getUser());
96
97
            //user has too much crew
98
            if ($globalCrewLimit - $this->crewCountRetriever->getAssignedCount($obj->getUser()) <= 0) {
99
                $this->crewTrainingRepository->delete($obj);
100
                continue;
101
            }
102
103
            //no academy online
104
            if (!$this->colonyFunctionManager->hasActiveFunction($colony, BuildingFunctionEnum::ACADEMY)) {
105
                continue;
106
            }
107
            $this->crewCreator->create($obj->getUserId(), $colony);
108
109
            $this->crewTrainingRepository->delete($obj);
110
            $user[$obj->getUserId()]++;
111
        }
112
113
        // send message for crew training
114
        foreach ($user as $userId => $count) {
115
            if ($count === 0) {
116
                continue;
117
            }
118
119
            $this->privateMessageSender->send(
120
                UserConstants::USER_NOONE,
121
                $userId,
122
                sprintf(
123
                    "Es wurden erfolgreich %d Crewman ausgebildet.",
124
                    $count
125
                ),
126
                PrivateMessageFolderTypeEnum::SPECIAL_COLONY
127
            );
128
        }
129
    }
130
131
    private function setLock(int $batchGroupId): void
132
    {
133
        $this->lockManager->setLock($batchGroupId, LockTypeEnum::COLONY_GROUP);
134
    }
135
136
    private function clearLock(int $batchGroupId): void
137
    {
138
        $this->lockManager->clearLock($batchGroupId, LockTypeEnum::COLONY_GROUP);
139
    }
140
141
    #[Override]
142
    protected function getBenchmark(): Ubench
143
    {
144
        return $this->benchmark;
145 1
    }
146
}
147