Passed
Push — dev ( 2b2fdd...e049ad )
by Nico
45:08
created

setProtectionTimeoutFromPrestige()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 24
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 38
ccs 0
cts 25
cp 0
crap 20
rs 9.536
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
17
class PirateWrathManager implements PirateWrathManagerInterface
18
{
19
    public const MINIMUM_WRATH = 500;
20
    public const MAXIMUM_WRATH = 2000;
21
22
    private PirateLoggerInterface $logger;
23
24
    private CreatePrestigeLogInterface $createPrestigeLog;
25
26
    private StuRandom $stuRandom;
27
28
    private StuTime $stuTime;
29
30
    public function __construct(
31
        private PirateWrathRepositoryInterface $pirateWrathRepository,
32
        LoggerUtilFactoryInterface $loggerUtilFactory,
33
        StuRandom $stuRandom,
34
        CreatePrestigeLogInterface $createPrestigeLog,
35
        StuTime $stuTime
36
    ) {
37
        $this->logger = $loggerUtilFactory->getPirateLogger();
38
        $this->createPrestigeLog = $createPrestigeLog;
39
        $this->stuRandom = $stuRandom;
40
        $this->stuTime = $stuTime;
41
    }
42
43
    public function increaseWrath(UserInterface $user, PirateReactionTriggerEnum $reactionTrigger): void
44
    {
45
        if (
46
            $user->isNpc()
47
            || $user->getId() === UserEnum::USER_NPC_KAZON
48
        ) {
49
            return;
50
        }
51
52
        $wrath = $user->getPirateWrath();
53
        if ($wrath === null) {
54
            $wrath = $this->pirateWrathRepository->prototype();
55
            $wrath->setUser($user);
56
            $user->setPirateWrath($wrath);
57
        }
58
59
        if ($wrath->getWrath() >= self::MAXIMUM_WRATH) {
60
            $this->logger->logf(
61
                'MAXIMUM_WRATH = %d of user %d already reached',
62
                self::MAXIMUM_WRATH,
63
                $user->getId()
64
            );
65
            return;
66
        }
67
68
        // reset protection timeout
69
        $timeout = $wrath->getProtectionTimeout();
70
        if (
71
            $timeout !== null
72
            && $timeout > time()
73
        ) {
74
            $this->logger->logf(
75
                'RESET protection timeout of user %d',
76
                $user->getId(),
77
            );
78
            $wrath->setProtectionTimeout(null);
79
        }
80
81
        // increase wrath
82
        $currentWrath = $wrath->getWrath();
83
        $wrath->setWrath($currentWrath + $reactionTrigger->getWrath());
84
        $this->pirateWrathRepository->save($wrath);
85
86
        $this->logger->logf(
87
            'INCREASED wrath of user %d from %d to %d',
88
            $user->getId(),
89
            $currentWrath,
90
            $wrath->getWrath()
91
        );
92
    }
93
94
    public function decreaseWrath(UserInterface $user, int $amount): void
95
    {
96
        if (
97
            $user->isNpc()
98
            || $user->getId() === UserEnum::USER_NPC_KAZON
99
        ) {
100
            return;
101
        }
102
103
        $wrath = $user->getPirateWrath();
104
        if ($wrath === null) {
105
            $wrath = $this->pirateWrathRepository->prototype();
106
            $wrath->setUser($user);
107
            $user->setPirateWrath($wrath);
108
        }
109
110
        if ($wrath->getWrath() <= self::MINIMUM_WRATH) {
111
            $this->logger->logf(
112
                'MINIMUM_WRATH = %d of user %d already reached',
113
                self::MINIMUM_WRATH,
114
                $user->getId()
115
            );
116
            return;
117
        }
118
119
        // decrease wrath
120
        $currentWrath = $wrath->getWrath();
121
        $wrath->setWrath($currentWrath - $amount);
122
        $this->pirateWrathRepository->save($wrath);
123
124
        $this->logger->logf(
125
            'DECREASED wrath of user %d from %d to %d',
126
            $user->getId(),
127
            $currentWrath,
128
            $wrath->getWrath()
129
        );
130
    }
131
132
    public function setProtectionTimeoutFromPrestige(UserInterface $user, int $prestige, GameControllerInterface $game): void
133
    {
134
        $wrath = $user->getPirateWrath();
135
        if ($wrath === null) {
136
            $wrath = $this->pirateWrathRepository->prototype();
137
            $wrath->setUser($user);
138
            $user->setPirateWrath($wrath);
139
        }
140
141
        $userwrath = $wrath->getWrath() / PirateWrathInterface::DEFAULT_WRATH;
142
143
        // 1 Prestige = 1.44 Stunden = 5184 Sekunden
144
        $defaultTimeout = max(1, ((1 / $userwrath) ** 2) * ($prestige * 5184));
145
146
        $randomFactor = $this->stuRandom->rand(95, 105) / 100;
147
        $timeout = (int)($defaultTimeout * $randomFactor);
148
149
        $currentTimeout = $wrath->getProtectionTimeout();
150
        if ($currentTimeout !== null && $currentTimeout > time()) {
151
            $timeout += $currentTimeout;
152
        } else {
153
            $timeout += time();
154
        }
155
156
        $wrath->setProtectionTimeout($timeout);
157
        $this->pirateWrathRepository->save($wrath);
158
159
        $this->createPrestigeLog->createLog(
160
            -$prestige,
161
            sprintf('-%d Prestige: Großer Nagus garantiert Schutz vor Piraten bis zum %s', $prestige, $this->stuTime->transformToStuDate($timeout)),
162
            $user,
163
            time()
164
        );
165
166
167
        $game->addInformation(sprintf(
168
            _('Der Nagus konnte einen Nichtangriffspakt mit den Kazon bis zum %s Uhr aushandeln'),
169
            $this->stuTime->transformToStuDate($timeout)
170
        ));
171
    }
172
}
173