Passed
Push — dev ( b851e4...f48036 )
by Janko
48:24 queued 21:52
created

PirateWrathManager::decreaseWrath()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 23
nc 5
nop 2
dl 0
loc 35
ccs 0
cts 24
cp 0
crap 30
rs 9.2408
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Lib\Pirate\Component;
4
5
use Stu\Lib\Pirate\PirateReactionTriggerEnum;
6
use Stu\Module\Logging\LoggerUtilFactoryInterface;
7
use Stu\Module\Logging\PirateLoggerInterface;
8
use Stu\Module\PlayerSetting\Lib\UserEnum;
9
use Stu\Orm\Entity\UserInterface;
10
use Stu\Orm\Repository\PirateWrathRepositoryInterface;
11
12
class PirateWrathManager implements PirateWrathManagerInterface
13
{
14
    public const MINIMUM_WRATH = 50;
15
    public const MAXIMUM_WRATH = 200;
16
17
    private PirateLoggerInterface $logger;
18
19
    public function __construct(
20
        private PirateWrathRepositoryInterface $pirateWrathRepository,
21
        LoggerUtilFactoryInterface $loggerUtilFactory
22
    ) {
23
        $this->logger = $loggerUtilFactory->getPirateLogger();
24
    }
25
26
    public function increaseWrath(UserInterface $user, PirateReactionTriggerEnum $reactionTrigger): void
27
    {
28
        if (
29
            $user->isNpc()
30
            || $user->getId() === UserEnum::USER_NPC_KAZON
31
        ) {
32
            return;
33
        }
34
35
        $wrath = $user->getPirateWrath();
36
        if ($wrath === null) {
37
            $wrath = $this->pirateWrathRepository->prototype();
38
            $wrath->setUser($user);
39
            $user->setPirateWrath($wrath);
40
        }
41
42
        if ($wrath->getWrath() >= self::MAXIMUM_WRATH) {
43
            $this->logger->logf(
44
                'MAXIMUM_WRATH = %d of user %d already reached',
45
                self::MAXIMUM_WRATH,
46
                $user->getId()
47
            );
48
            return;
49
        }
50
51
        // reset protection timeout
52
        $timeout = $wrath->getProtectionTimeout();
53
        if (
54
            $timeout !== null
55
            && $timeout > time()
56
        ) {
57
            $this->logger->logf(
58
                'RESET protection timeout of user %d',
59
                $user->getId(),
60
            );
61
            $wrath->setProtectionTimeout(null);
62
        }
63
64
        // increase wrath
65
        $currentWrath = $wrath->getWrath();
66
        $wrath->setWrath($currentWrath + $reactionTrigger->getWrath());
67
        $this->pirateWrathRepository->save($wrath);
68
69
        $this->logger->logf(
70
            'INCREASED wrath of user %d from %d to %d',
71
            $user->getId(),
72
            $currentWrath,
73
            $wrath->getWrath()
74
        );
75
    }
76
77
    public function decreaseWrath(UserInterface $user, int $amount): void
78
    {
79
        if (
80
            $user->isNpc()
81
            || $user->getId() === UserEnum::USER_NPC_KAZON
82
        ) {
83
            return;
84
        }
85
86
        $wrath = $user->getPirateWrath();
87
        if ($wrath === null) {
88
            $wrath = $this->pirateWrathRepository->prototype();
89
            $wrath->setUser($user);
90
            $user->setPirateWrath($wrath);
91
        }
92
93
        if ($wrath->getWrath() <= self::MINIMUM_WRATH) {
94
            $this->logger->logf(
95
                'MINIMUM_WRATH = %d of user %d already reached',
96
                self::MINIMUM_WRATH,
97
                $user->getId()
98
            );
99
            return;
100
        }
101
102
        // decrease wrath
103
        $currentWrath = $wrath->getWrath();
104
        $wrath->setWrath($currentWrath - $amount);
105
        $this->pirateWrathRepository->save($wrath);
106
107
        $this->logger->logf(
108
            'DECREASED wrath of user %d from %d to %d',
109
            $user->getId(),
110
            $currentWrath,
111
            $wrath->getWrath()
112
        );
113
    }
114
}
115