Passed
Pull Request — master (#1904)
by Nico
39:29 queued 12:01
created

PirateWrathManager::getPirateWrathOfUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Stu\Lib\Pirate\Component;
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 Stu\Lib\Pirate\PirateReactionTriggerEnum;
7
use Stu\Module\Control\GameControllerInterface;
8
use Stu\Module\Control\StuRandom;
9
use Stu\Module\Control\StuTime;
10
use Stu\Module\Logging\LoggerUtilFactoryInterface;
11
use Stu\Module\Logging\PirateLoggerInterface;
12
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
13
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
14
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
15
use Stu\Module\Prestige\Lib\CreatePrestigeLogInterface;
16
use Stu\Orm\Entity\PirateWrathInterface;
17
use Stu\Orm\Entity\UserInterface;
18
use Stu\Orm\Repository\PirateWrathRepositoryInterface;
19
20
class PirateWrathManager implements PirateWrathManagerInterface
21
{
22
    public const MINIMUM_WRATH = 500;
23
    public const MAXIMUM_WRATH = 2000;
24
25
    private PirateLoggerInterface $logger;
26
27 1
    public function __construct(
28
        private PirateWrathRepositoryInterface $pirateWrathRepository,
29
        private StuRandom $stuRandom,
30
        private CreatePrestigeLogInterface $createPrestigeLog,
31
        private StuTime $stuTime,
32
        private PrivateMessageSenderInterface $privateMessageSender,
33
        LoggerUtilFactoryInterface $loggerUtilFactory,
34
    ) {
35 1
        $this->logger = $loggerUtilFactory->getPirateLogger();
36
    }
37
38
    #[Override]
39
    public function increaseWrathViaTrigger(UserInterface $user, PirateReactionTriggerEnum $reactionTrigger): void
40
    {
41
        $this->increaseWrath($user, $reactionTrigger->getWrath());
42
    }
43
44
    #[Override]
45
    public function increaseWrath(UserInterface $user, int $amount): void
46
    {
47
        if (
48
            $user->isNpc()
49
            || $user->getId() === UserEnum::USER_NPC_KAZON
50
        ) {
51
            return;
52
        }
53
54
        if ($amount < 1) {
55
            return;
56
        }
57
58
        $wrath = $user->getPirateWrath();
59
        if ($wrath === null) {
60
            $wrath = $this->pirateWrathRepository->prototype();
61
            $wrath->setUser($user);
62
            $user->setPirateWrath($wrath);
63
        }
64
65
        if ($wrath->getWrath() >= self::MAXIMUM_WRATH) {
66
            $this->logger->logf(
67
                'MAXIMUM_WRATH = %d of user %d already reached',
68
                self::MAXIMUM_WRATH,
69
                $user->getId()
70
            );
71
            return;
72
        }
73
74
        // increase wrath
75
        $currentWrath = $wrath->getWrath();
76
        $newWrath = min(self::MAXIMUM_WRATH, $currentWrath + $amount);
77
        $wrath->setWrath($newWrath);
78
79
        // reset protection timeout
80
        $timeout = $wrath->getProtectionTimeout();
81
        if (
82
            $timeout !== null
83
            && $timeout > time()
84
        ) {
85
            $this->makePiratesReallyAngry($wrath);
86
        } else {
87
88
            $this->logger->logf(
89
                'INCREASED wrath of user %d from %d to %d',
90
                $user->getId(),
91
                $currentWrath,
92
                $wrath->getWrath()
93
            );
94
        }
95
96
        $this->pirateWrathRepository->save($wrath);
97
    }
98
99
    private function makePiratesReallyAngry(PirateWrathInterface $wrath): void
100
    {
101
        $user = $wrath->getUser();
102
103
        $this->logger->logf(
104
            'RESET protection timeout of user %d and set wrath to MAXIMUM of %d',
105
            $user->getId(),
106
            self::MAXIMUM_WRATH
107
        );
108
        $wrath->setProtectionTimeout(null);
109
        $wrath->setWrath(self::MAXIMUM_WRATH);
110
111
        $this->privateMessageSender->send(
112
            UserEnum::USER_NPC_KAZON,
113
            $user->getId(),
114
            'Wie kannst du es wagen? Ich werde meine Horden auf dich hetzen bis du winselnd am Boden liegst! Der Nichtangriffspakt ist hinfällig!',
115
            PrivateMessageFolderTypeEnum::SPECIAL_MAIN
116
        );
117
    }
118
119
    #[Override]
120
    public function decreaseWrath(UserInterface $user, int $amount): void
121
    {
122
        if (
123
            $user->isNpc()
124
            || $user->getId() === UserEnum::USER_NPC_KAZON
125
        ) {
126
            return;
127
        }
128
129
        $wrath = $this->getPirateWrathOfUser($user);
130
131
        if ($wrath->getWrath() <= self::MINIMUM_WRATH) {
132
            $this->logger->logf(
133
                'MINIMUM_WRATH = %d of user %d already reached',
134
                self::MINIMUM_WRATH,
135
                $user->getId()
136
            );
137
            return;
138
        }
139
140
        // decrease wrath
141
        $currentWrath = $wrath->getWrath();
142
        $newWrath = max(self::MINIMUM_WRATH, $currentWrath - $amount);
143
        $wrath->setWrath($newWrath);
144
        $this->pirateWrathRepository->save($wrath);
145
146
        $this->logger->logf(
147
            'DECREASED wrath of user %d from %d to %d',
148
            $user->getId(),
149
            $currentWrath,
150
            $wrath->getWrath()
151
        );
152
    }
153
154
    #[Override]
155
    public function setProtectionTimeoutFromPrestige(UserInterface $user, int $prestige, GameControllerInterface $game): void
156
    {
157
        $wrath = $this->getPirateWrathOfUser($user);
158
159
        $wrathFactor = $wrath->getWrath() / PirateWrathInterface::DEFAULT_WRATH;
160
161
        // 1 Prestige = 2.88 Stunden = 10368 Sekunden
162
        $baseTimeoutInSeconds = max(1, ((1 / $wrathFactor) ** 2) * ($prestige * 10368));
163
        $minTimeout = $baseTimeoutInSeconds * 0.95;
164
        $maxTimeout = $baseTimeoutInSeconds * 1.05;
165
166
        $timestamp = $this->stuRandom->rand((int)$minTimeout, (int)$maxTimeout);
167
168
        $currentTimeout = $wrath->getProtectionTimeout();
169
        if ($currentTimeout !== null && $currentTimeout > time()) {
170
            $timestamp += $currentTimeout;
171
        } else {
172
            $timestamp += time();
173
        }
174
175
        $wrath->setProtectionTimeout($timestamp);
176
        $this->pirateWrathRepository->save($wrath);
177
178
        $this->createPrestigeLog->createLog(
179
            -$prestige,
180
            sprintf('-%d Prestige: Großer Nagus garantiert Schutz vor Piraten bis zum %s', $prestige, $this->stuTime->transformToStuDateTime($timestamp)),
181
            $user,
182
            time()
183
        );
184
185
186
        $game->addInformation(sprintf(
187
            _('Der Nagus konnte einen Nichtangriffspakt mit den Kazon bis zum %s Uhr aushandeln'),
188
            $this->stuTime->transformToStuDateTime($timestamp)
189
        ));
190
191
        $this->privateMessageSender->send(
192
            UserEnum::USER_NPC_KAZON,
193
            $user->getId(),
194
            sprintf(
195
                'Ihr habt Euch einen Nicht-Angriffs-Pakt mit uns erkauft, dieser gilt bis zum %s Uhr.
196
                
197
Denkt aber immer daran: Wir mögen es nicht, wenn man uns in die Quere kommt!
198
Jede Provokation und sei es auch nur ein übereifriger Sensoroffizier der unsere Schiffe scannt oder gar ein AR-Warnschuss vor den Bug unserer Schiffe, stellt einen Vertragsbruch dar. Ein solcher Vertragsbruch würde Euch auf unserer roten Liste ganz nach oben katapultieren und die Jagd auf Euch wäre wieder freigegeben.
199
Es wäre doch zu Schade wenn Eure Investition dadurch völlig verpuffen würde, nicht wahr?',
200
            date('d.m.Y H:i', $timestamp)
201
            ),
202
            PrivateMessageFolderTypeEnum::SPECIAL_MAIN
203
        );
204
    }
205
206
    private function getPirateWrathOfUser(UserInterface $user): PirateWrathInterface
207
    {
208
        $wrath = $user->getPirateWrath();
209
210
        if ($wrath === null) {
211
            $wrath = $this->pirateWrathRepository->prototype();
212
            $wrath->setUser($user);
213
            $user->setPirateWrath($wrath);
214
        }
215
216
        return $wrath;
217
    }
218
}
219