Passed
Push — dev ( 6fa4a9...182017 )
by Janko
16:15 queued 15s
created

TickManager::startTurn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Tick;
4
5
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...
6
use RuntimeException;
7
use Stu\Component\Game\TimeConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\TimeConstants 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\Module\Logging\LoggerUtilFactoryInterface;
9
use Stu\Module\Logging\LoggerUtilInterface;
10
use Stu\Orm\Entity\GameTurnInterface;
11
use Stu\Orm\Repository\GameTurnRepositoryInterface;
12
use Stu\Orm\Repository\GameTurnStatsRepositoryInterface;
13
use Stu\Orm\Repository\KnPostRepositoryInterface;
14
use Stu\Orm\Repository\PrivateMessageRepositoryInterface;
15
use Stu\Orm\Repository\UserLockRepositoryInterface;
16
use Stu\Orm\Repository\UserRepositoryInterface;
17
18
final class TickManager implements TickManagerInterface
19
{
20
    private LoggerUtilInterface $loggerUtil;
21
22 1
    public function __construct(
23
        private readonly GameTurnRepositoryInterface $gameTurnRepository,
24
        private readonly UserLockRepositoryInterface $userLockRepository,
25
        private readonly GameTurnStatsRepositoryInterface $gameTurnStatsRepository,
26
        private readonly UserRepositoryInterface $userRepository,
27
        private readonly KnPostRepositoryInterface $knPostRepository,
28
        private readonly PrivateMessageRepositoryInterface $privateMessageRepository,
29
        LoggerUtilFactoryInterface $loggerUtilFactory
30
    ) {
31 1
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
32
    }
33
34
    #[Override]
35
    public function work(): void
36
    {
37
        $oldTurn = $this->gameTurnRepository->getCurrent();
38
        if ($oldTurn === null) {
39
            throw new RuntimeException('no current turn existent');
40
        }
41
42
        $this->endTurn($oldTurn);
43
        $this->reduceUserLocks();
44
        $newTurn = $this->startTurn($oldTurn);
45
        $this->createGameTurnStats($oldTurn, $newTurn);
46
    }
47
48
    private function endTurn(GameTurnInterface $turn): void
49
    {
50
        $turn->setEnd(time());
51
52
        $this->gameTurnRepository->save($turn);
53
    }
54
55
    private function startTurn(GameTurnInterface $oldTurn): GameTurnInterface
56
    {
57
        $obj = $this->gameTurnRepository->prototype();
58
        $obj->setStart(time());
59
        $obj->setEnd(0);
60
        $obj->setTurn($oldTurn->getTurn() + 1);
61
62
        $this->gameTurnRepository->save($obj);
63
64
        return $obj;
65
    }
66
67
    private function reduceUserLocks(): void
68
    {
69
        $locks = $this->userLockRepository->getActive();
70
71
        foreach ($locks as $lock) {
72
            $remainingTicks = $lock->getRemainingTicks();
73
74
            if ($remainingTicks === 1) {
75
                $userId = $lock->getUser()->getId();
76
77
                $lock->setUser(null);
78
                $lock->setUserId(null);
79
                $lock->setFormerUserId($userId);
80
                $lock->setRemainingTicks(0);
81
            } else {
82
                $lock->setRemainingTicks($remainingTicks - 1);
83
            }
84
85
            $this->userLockRepository->save($lock);
86
        }
87
    }
88
89
    private function createGameTurnStats(GameTurnInterface $oldTurn, GameTurnInterface $newTurn): void
90
    {
91
        $stats = $this->gameTurnStatsRepository->prototype();
92
93
        $this->loggerUtil->log('setting stats values');
94
95
        $stats->setTurn($newTurn);
96
        $stats->setUserCount($this->userRepository->getActiveAmount());
97
        $stats->setLogins24h($this->userRepository->getActiveAmountRecentlyOnline(time() - TimeConstants::ONE_DAY_IN_SECONDS));
98
        $stats->setInactiveCount($this->userRepository->getInactiveAmount(14));
99
        $stats->setVacationCount($this->userRepository->getVacationAmount());
100
        $stats->setShipCount($this->gameTurnStatsRepository->getShipCount());
101
        $stats->setShipCountManned($this->gameTurnStatsRepository->getShipCountManned());
102
        $stats->setShipCountNpc($this->gameTurnStatsRepository->getShipCountNpc());
103
        $stats->setKnCount($this->knPostRepository->getAmount());
104
        $stats->setFlightSig24h($this->gameTurnStatsRepository->getFlightSigs24h());
105
        $stats->setFlightSigSystem24h($this->gameTurnStatsRepository->getFlightSigsSystem24h());
106
        $stats->setNewPmCount($this->privateMessageRepository->getAmountSince($oldTurn->getStart()));
107
108
        $this->gameTurnStatsRepository->save($stats);
109
        $this->loggerUtil->log('saved stats');
110
    }
111
}
112