Passed
Pull Request — master (#1801)
by Nico
23:50
created

PirateWrathManager::increaseWrath()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 5
Bugs 2 Features 0
Metric Value
cc 8
eloc 32
c 5
b 2
f 0
nc 8
nop 2
dl 0
loc 52
ccs 0
cts 32
cp 0
crap 72
rs 8.1635

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Stu\Lib\Pirate\Component;
4
5
use Stu\Module\Control\StuRandom;
6
use Stu\Module\Control\StuTime;
7
use Stu\Lib\Pirate\PirateReactionTriggerEnum;
8
use Stu\Module\Logging\LoggerUtilFactoryInterface;
9
use Stu\Module\Logging\PirateLoggerInterface;
10
use Stu\Module\PlayerSetting\Lib\UserEnum;
11
use Stu\Module\Prestige\Lib\CreatePrestigeLogInterface;
12
use Stu\Orm\Entity\UserInterface;
13
use Stu\Orm\Entity\PirateWrathInterface;
14
use Stu\Orm\Repository\PirateWrathRepositoryInterface;
15
use Stu\Module\Control\GameControllerInterface;
16
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
17
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
18
19
class PirateWrathManager implements PirateWrathManagerInterface
20
{
21
    public const MINIMUM_WRATH = 500;
22
    public const MAXIMUM_WRATH = 2000;
23
24
    private PirateLoggerInterface $logger;
25
26
    public function __construct(
27
        private PirateWrathRepositoryInterface $pirateWrathRepository,
28
        private StuRandom $stuRandom,
29
        private CreatePrestigeLogInterface $createPrestigeLog,
30
        private StuTime $stuTime,
31
        private PrivateMessageSenderInterface $privateMessageSender,
32
        LoggerUtilFactoryInterface $loggerUtilFactory,
33
    ) {
34
        $this->logger = $loggerUtilFactory->getPirateLogger();
35
    }
36
37
    public function increaseWrath(UserInterface $user, PirateReactionTriggerEnum $reactionTrigger): void
38
    {
39
        if (
40
            $user->isNpc()
41
            || $user->getId() === UserEnum::USER_NPC_KAZON
42
        ) {
43
            return;
44
        }
45
46
        $reactionTriggerWrath = $reactionTrigger->getWrath();
47
        if ($reactionTriggerWrath < 1) {
48
            return;
49
        }
50
51
        $wrath = $user->getPirateWrath();
52
        if ($wrath === null) {
53
            $wrath = $this->pirateWrathRepository->prototype();
54
            $wrath->setUser($user);
55
            $user->setPirateWrath($wrath);
56
        }
57
58
        if ($wrath->getWrath() >= self::MAXIMUM_WRATH) {
59
            $this->logger->logf(
60
                'MAXIMUM_WRATH = %d of user %d already reached',
61
                self::MAXIMUM_WRATH,
62
                $user->getId()
63
            );
64
            return;
65
        }
66
67
        // increase wrath
68
        $currentWrath = $wrath->getWrath();
69
        $wrath->setWrath($currentWrath + $reactionTriggerWrath);
70
71
        // reset protection timeout
72
        $timeout = $wrath->getProtectionTimeout();
73
        if (
74
            $timeout !== null
75
            && $timeout > time()
76
        ) {
77
            $this->makePiratesReallyAngry($wrath);
78
        } else {
79
80
            $this->logger->logf(
81
                'INCREASED wrath of user %d from %d to %d',
82
                $user->getId(),
83
                $currentWrath,
84
                $wrath->getWrath()
85
            );
86
        }
87
88
        $this->pirateWrathRepository->save($wrath);
89
    }
90
91
    private function makePiratesReallyAngry(PirateWrathInterface $wrath): void
92
    {
93
        $user = $wrath->getUser();
94
95
        $this->logger->logf(
96
            'RESET protection timeout of user %d and set wrath to MAXIMUM of %d',
97
            $user->getId(),
98
            self::MAXIMUM_WRATH
99
        );
100
        $wrath->setProtectionTimeout(null);
101
        $wrath->setWrath(self::MAXIMUM_WRATH);
102
103
        $this->privateMessageSender->send(
104
            UserEnum::USER_NPC_KAZON,
105
            $user->getId(),
106
            sprintf('Wie kannst du es wagen? Ich werde meine Horden auf dich hetzen bis du winselnd am Boden liegst! Der Nichtangriffspakt ist hinfällig!'),
107
            PrivateMessageFolderSpecialEnum::PM_SPECIAL_MAIN
108
        );
109
    }
110
111
    public function decreaseWrath(UserInterface $user, int $amount): void
112
    {
113
        if (
114
            $user->isNpc()
115
            || $user->getId() === UserEnum::USER_NPC_KAZON
116
        ) {
117
            return;
118
        }
119
120
        $wrath = $this->getPirateWrathOfUser($user);
121
122
        if ($wrath->getWrath() <= self::MINIMUM_WRATH) {
123
            $this->logger->logf(
124
                'MINIMUM_WRATH = %d of user %d already reached',
125
                self::MINIMUM_WRATH,
126
                $user->getId()
127
            );
128
            return;
129
        }
130
131
        // decrease wrath
132
        $currentWrath = $wrath->getWrath();
133
        $wrath->setWrath($currentWrath - $amount);
134
        $this->pirateWrathRepository->save($wrath);
135
136
        $this->logger->logf(
137
            'DECREASED wrath of user %d from %d to %d',
138
            $user->getId(),
139
            $currentWrath,
140
            $wrath->getWrath()
141
        );
142
    }
143
144
    public function setProtectionTimeoutFromPrestige(UserInterface $user, int $prestige, GameControllerInterface $game): void
145
    {
146
        $wrath = $this->getPirateWrathOfUser($user);
147
148
        $wrathFactor = $wrath->getWrath() / PirateWrathInterface::DEFAULT_WRATH;
149
150
        // 1 Prestige = 1.44 Stunden = 5184 Sekunden
151
        $baseTimeoutInSeconds = max(1, ((1 / $wrathFactor) ** 2) * ($prestige * 5184));
152
        $minTimeout = $baseTimeoutInSeconds * 0.95;
153
        $maxTimeout = $baseTimeoutInSeconds * 1.05;
154
155
        $timestamp = $this->stuRandom->rand((int)$minTimeout, (int)$maxTimeout);
156
157
        $currentTimeout = $wrath->getProtectionTimeout();
158
        if ($currentTimeout !== null && $currentTimeout > time()) {
159
            $timestamp += $currentTimeout;
160
        } else {
161
            $timestamp += time();
162
        }
163
164
        $wrath->setProtectionTimeout($timestamp);
165
        $this->pirateWrathRepository->save($wrath);
166
167
        $this->createPrestigeLog->createLog(
168
            -$prestige,
169
            sprintf('-%d Prestige: Großer Nagus garantiert Schutz vor Piraten bis zum %s', $prestige, $this->stuTime->transformToStuDate($timestamp)),
170
            $user,
171
            time()
172
        );
173
174
175
        $game->addInformation(sprintf(
176
            _('Der Nagus konnte einen Nichtangriffspakt mit den Kazon bis zum %s Uhr aushandeln'),
177
            $this->stuTime->transformToStuDate($timestamp)
178
        ));
179
    }
180
181
    private function getPirateWrathOfUser(UserInterface $user): PirateWrathInterface
182
    {
183
        $wrath = $user->getPirateWrath();
184
185
        if ($wrath === null) {
186
            $wrath = $this->pirateWrathRepository->prototype();
187
            $wrath->setUser($user);
188
            $user->setPirateWrath($wrath);
189
        }
190
191
        return $wrath;
192
    }
193
}
194