Passed
Push — dev ( 766009...bcc49c )
by Janko
09:42
created

ColonyTickManager::getLoggerUtil()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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 Stu\Component\Building\BuildingEnum;
8
use Stu\Component\Colony\ColonyFunctionManagerInterface;
9
use Stu\Component\Crew\CrewCountRetrieverInterface;
10
use Stu\Component\Player\CrewLimitCalculatorInterface;
11
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
12
use Stu\Module\Crew\Lib\CrewCreatorInterface;
13
use Stu\Module\Logging\LoggerEnum;
14
use Stu\Module\Logging\LoggerUtilFactoryInterface;
15
use Stu\Module\Logging\LoggerUtilInterface;
16
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
17
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
18
use Stu\Module\PlayerSetting\Lib\UserEnum;
19
use Stu\Module\Tick\AbstractTickManager;
20
use Stu\Module\Tick\Lock\LockEnum;
21
use Stu\Module\Tick\Lock\LockManagerInterface;
22
use Stu\Orm\Repository\ColonyRepositoryInterface;
23
use Stu\Orm\Repository\CrewTrainingRepositoryInterface;
24
use Ubench;
25
26
final class ColonyTickManager extends AbstractTickManager implements ColonyTickManagerInterface
27
{
28
    private ColonyTickInterface $colonyTick;
29
30
    private CrewCreatorInterface $crewCreator;
31
32
    private CrewTrainingRepositoryInterface $crewTrainingRepository;
33
34
    private ColonyRepositoryInterface $colonyRepository;
35
36
    private PrivateMessageSenderInterface $privateMessageSender;
37
38
    private CrewCountRetrieverInterface $crewCountRetriever;
39
40
    private LockManagerInterface $lockManager;
41
42
    private ColonyFunctionManagerInterface $colonyFunctionManager;
43
44
    private CrewLimitCalculatorInterface $crewLimitCalculator;
45
46
    private ColonyLibFactoryInterface $colonyLibFactory;
47
48
    private LoggerUtilInterface $loggerUtil;
49
50
    private Ubench $benchmark;
51
52 1
    public function __construct(
53
        ColonyTickInterface $colonyTick,
54
        CrewCreatorInterface $crewCreator,
55
        CrewTrainingRepositoryInterface $crewTrainingRepository,
56
        ColonyRepositoryInterface $colonyRepository,
57
        PrivateMessageSenderInterface $privateMessageSender,
58
        CrewCountRetrieverInterface $crewCountRetriever,
59
        ColonyFunctionManagerInterface $colonyFunctionManager,
60
        CrewLimitCalculatorInterface $crewLimitCalculator,
61
        ColonyLibFactoryInterface $colonyLibFactory,
62
        LockManagerInterface $lockManager,
63
        LoggerUtilFactoryInterface $loggerUtilFactory,
64
        Ubench $benchmark
65
    ) {
66 1
        $this->colonyTick = $colonyTick;
67 1
        $this->crewCreator = $crewCreator;
68 1
        $this->crewTrainingRepository = $crewTrainingRepository;
69 1
        $this->colonyRepository = $colonyRepository;
70 1
        $this->privateMessageSender = $privateMessageSender;
71 1
        $this->crewCountRetriever = $crewCountRetriever;
72 1
        $this->lockManager = $lockManager;
73 1
        $this->colonyFunctionManager = $colonyFunctionManager;
74 1
        $this->crewLimitCalculator = $crewLimitCalculator;
75 1
        $this->colonyLibFactory = $colonyLibFactory;
76 1
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
77 1
        $this->benchmark = $benchmark;
78
    }
79
80 1
    public function work(int $batchGroup, int $batchGroupCount): void
81
    {
82 1
        $this->setLock($batchGroup);
83
        try {
84 1
            $entityCount = $this->colonyLoop($batchGroup, $batchGroupCount);
85
            $this->proceedCrewTraining($batchGroup, $batchGroupCount);
86
87
            $this->loggerUtil->init(sprintf(
88
                'COLOTICK_%dof%d',
89
                $batchGroup,
90
                $batchGroupCount
91
            ), LoggerEnum::LEVEL_WARNING);
92
            $this->logBenchmarkResult($entityCount);
93
        } finally {
94 1
            $this->clearLock($batchGroup);
95
        }
96
    }
97
98 1
    private function colonyLoop(int $batchGroup, int $batchGroupCount): int
99
    {
100 1
        $colonyList = $this->colonyRepository->getByBatchGroup($batchGroup, $batchGroupCount);
101
102
        $entityCount = 0;
103
        foreach ($colonyList as $colony) {
104
            //echo "Processing Colony ".$colony->getId()." at ".microtime()."\n";
105
106
            //handle colony only if vacation mode not active
107
            if (!$colony->getUser()->isVacationRequestOldEnough()) {
108
                $this->colonyTick->work($colony);
109
            }
110
111
            $entityCount++;
112
        }
113
114
        return $entityCount;
115
    }
116
117
    private function proceedCrewTraining(int $batchGroup, int $batchGroupCount): void
118
    {
119
        $user = [];
120
121
        foreach ($this->crewTrainingRepository->getByBatchGroup($batchGroup, $batchGroupCount) as $obj) {
122
            if (!isset($user[$obj->getUserId()])) {
123
                $user[$obj->getUserId()] = 0;
124
            }
125
            if ($user[$obj->getUserId()] >= $this->crewCountRetriever->getTrainableCount($obj->getUser())) {
126
                continue;
127
            }
128
            $colony = $obj->getColony();
129
130
            $freeAssignmentCount = $this->colonyLibFactory->createColonyPopulationCalculator(
131
                $colony
132
            )->getFreeAssignmentCount();
133
134
            //colony can't hold more crew
135
            if ($freeAssignmentCount === 0) {
136
                $this->crewTrainingRepository->delete($obj);
137
                continue;
138
            }
139
140
            $globalCrewLimit = $this->crewLimitCalculator->getGlobalCrewLimit($obj->getUser());
141
142
            //user has too much crew
143
            if ($globalCrewLimit - $this->crewCountRetriever->getAssignedCount($obj->getUser()) <= 0) {
144
                $this->crewTrainingRepository->delete($obj);
145
                continue;
146
            }
147
148
            //no academy online
149
            if (!$this->colonyFunctionManager->hasActiveFunction($colony, BuildingEnum::BUILDING_FUNCTION_ACADEMY)) {
150
                continue;
151
            }
152
            $this->crewCreator->create($obj->getUserId(), $colony);
153
154
            $this->crewTrainingRepository->delete($obj);
155
            $user[$obj->getUserId()]++;
156
        }
157
158
        // send message for crew training
159
        foreach ($user as $userId => $count) {
160
            if ($count === 0) {
161
                continue;
162
            }
163
164
            $this->privateMessageSender->send(
165
                UserEnum::USER_NOONE,
166
                $userId,
167
                sprintf(
168
                    "Es wurden erfolgreich %d Crewman ausgebildet.",
169
                    $count
170
                ),
171
                PrivateMessageFolderSpecialEnum::PM_SPECIAL_COLONY
172
            );
173
        }
174
    }
175
176 1
    private function setLock(int $batchGroupId): void
177
    {
178 1
        $this->lockManager->setLock($batchGroupId, LockEnum::LOCK_TYPE_COLONY_GROUP);
179
    }
180
181 1
    private function clearLock(int $batchGroupId): void
182
    {
183 1
        $this->lockManager->clearLock($batchGroupId, LockEnum::LOCK_TYPE_COLONY_GROUP);
184
    }
185
186
    protected function getBenchmark(): Ubench
187
    {
188
        return $this->benchmark;
189
    }
190
191
    protected function getLoggerUtil(): LoggerUtilInterface
192
    {
193
        return $this->loggerUtil;
194
    }
195
}
196