Passed
Push — master ( bf860f...1dd8f7 )
by Nico
57:55 queued 29:36
created

PirateWrathManager::increaseWrathViaTrigger()   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 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
        $wrath->setWrath($currentWrath + $amount);
74
75
        // reset protection timeout
76
        $timeout = $wrath->getProtectionTimeout();
77
        if (
78
            $timeout !== null
79
            && $timeout > time()
80
        ) {
81
            $this->makePiratesReallyAngry($wrath);
82
        } else {
83
84
            $this->logger->logf(
85
                'INCREASED wrath of user %d from %d to %d',
86
                $user->getId(),
87
                $currentWrath,
88
                $wrath->getWrath()
89
            );
90
        }
91
92
        $this->pirateWrathRepository->save($wrath);
93
    }
94
95
    private function makePiratesReallyAngry(PirateWrathInterface $wrath): void
96
    {
97
        $user = $wrath->getUser();
98
99
        $this->logger->logf(
100
            'RESET protection timeout of user %d and set wrath to MAXIMUM of %d',
101
            $user->getId(),
102
            self::MAXIMUM_WRATH
103
        );
104
        $wrath->setProtectionTimeout(null);
105
        $wrath->setWrath(self::MAXIMUM_WRATH);
106
107
        $this->privateMessageSender->send(
108
            UserEnum::USER_NPC_KAZON,
109
            $user->getId(),
110
            sprintf('Wie kannst du es wagen? Ich werde meine Horden auf dich hetzen bis du winselnd am Boden liegst! Der Nichtangriffspakt ist hinfällig!'),
111
            PrivateMessageFolderSpecialEnum::PM_SPECIAL_MAIN
112
        );
113
    }
114
115
    public function decreaseWrath(UserInterface $user, int $amount): void
116
    {
117
        if (
118
            $user->isNpc()
119
            || $user->getId() === UserEnum::USER_NPC_KAZON
120
        ) {
121
            return;
122
        }
123
124
        $wrath = $this->getPirateWrathOfUser($user);
125
126
        if ($wrath->getWrath() <= self::MINIMUM_WRATH) {
127
            $this->logger->logf(
128
                'MINIMUM_WRATH = %d of user %d already reached',
129
                self::MINIMUM_WRATH,
130
                $user->getId()
131
            );
132
            return;
133
        }
134
135
        // decrease wrath
136
        $currentWrath = $wrath->getWrath();
137
        $wrath->setWrath($currentWrath - $amount);
138
        $this->pirateWrathRepository->save($wrath);
139
140
        $this->logger->logf(
141
            'DECREASED wrath of user %d from %d to %d',
142
            $user->getId(),
143
            $currentWrath,
144
            $wrath->getWrath()
145
        );
146
    }
147
148
    public function setProtectionTimeoutFromPrestige(UserInterface $user, int $prestige, GameControllerInterface $game): void
149
    {
150
        $wrath = $this->getPirateWrathOfUser($user);
151
152
        $wrathFactor = $wrath->getWrath() / PirateWrathInterface::DEFAULT_WRATH;
153
154
        // 1 Prestige = 2.88 Stunden = 10368 Sekunden
155
        $baseTimeoutInSeconds = max(1, ((1 / $wrathFactor) ** 2) * ($prestige * 10368));
156
        $minTimeout = $baseTimeoutInSeconds * 0.95;
157
        $maxTimeout = $baseTimeoutInSeconds * 1.05;
158
159
        $timestamp = $this->stuRandom->rand((int)$minTimeout, (int)$maxTimeout);
160
161
        $currentTimeout = $wrath->getProtectionTimeout();
162
        if ($currentTimeout !== null && $currentTimeout > time()) {
163
            $timestamp += $currentTimeout;
164
        } else {
165
            $timestamp += time();
166
        }
167
168
        $wrath->setProtectionTimeout($timestamp);
169
        $this->pirateWrathRepository->save($wrath);
170
171
        $this->createPrestigeLog->createLog(
172
            -$prestige,
173
            sprintf('-%d Prestige: Großer Nagus garantiert Schutz vor Piraten bis zum %s', $prestige, $this->stuTime->transformToStuDate($timestamp)),
174
            $user,
175
            time()
176
        );
177
178
179
        $game->addInformation(sprintf(
180
            _('Der Nagus konnte einen Nichtangriffspakt mit den Kazon bis zum %s Uhr aushandeln'),
181
            $this->stuTime->transformToStuDate($timestamp)
182
        ));
183
    }
184
185
    private function getPirateWrathOfUser(UserInterface $user): PirateWrathInterface
186
    {
187
        $wrath = $user->getPirateWrath();
188
189
        if ($wrath === null) {
190
            $wrath = $this->pirateWrathRepository->prototype();
191
            $wrath->setUser($user);
192
            $user->setPirateWrath($wrath);
193
        }
194
195
        return $wrath;
196
    }
197
}
198