Passed
Pull Request — master (#1821)
by Nico
52:02 queued 25:23
created

PirateWrathManager   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Test Coverage

Coverage 1.94%

Importance

Changes 7
Bugs 3 Features 0
Metric Value
eloc 97
c 7
b 3
f 0
dl 0
loc 179
ccs 2
cts 103
cp 0.0194
rs 10
wmc 20

7 Methods

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