Passed
Pull Request — master (#1833)
by Nico
34:40
created

DamageWrapper::calculateDamageShields()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 13
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 3
rs 9.8333
1
<?php
2
3
/*
4
 *
5
 * Copyright 2010 Daniel Jakob All Rights Reserved
6
 * This software is the proprietary information of Daniel Jakob
7
 * Use is subject to license terms
8
 *
9
 */
10
11
/* $Id:$ */
12
13
namespace Stu\Lib;
14
15
use Stu\Component\Ship\ShipEnum;
16
use Stu\Module\PlayerSetting\Lib\UserEnum;
17
use Stu\Orm\Entity\ColonyInterface;
18
use Stu\Orm\Entity\PirateWrathInterface;
19
use Stu\Orm\Entity\ShipInterface;
20
use Stu\Orm\Entity\UserInterface;
21
22
/**
23
 * @author Daniel Jakob <[email protected]>
24
 * @access public
25
 */
26
class DamageWrapper
27
{
28
    private float $netDamage = 0;
29
    private bool $isCrit = false;
30
    private bool $isShieldPenetration = false;
31
    private int $modificator = 100;
32
    private ?int $pirateWrath = null;
33
34 14
    public function __construct(int $netDamage)
35
    {
36 14
        $this->netDamage = $netDamage;
37
    }
38
39
    private int $hull_damage_factor = 100;
40
41
42 1
    public function setHullDamageFactor(int $value): void
43
    {
44 1
        $this->hull_damage_factor = $value;
45
    }
46
47
48 5
    public function getHullDamageFactor(): int
49
    {
50 5
        return $this->hull_damage_factor;
51
    }
52
53 1
    public function setCrit(bool $isCrit): void
54
    {
55 1
        $this->isCrit = $isCrit;
56
    }
57
58
    public function isCrit(): bool
59
    {
60
        return $this->isCrit;
61
    }
62
63
    public function setShieldPenetration(bool $isShieldPenetration): void
64
    {
65
        $this->isShieldPenetration = $isShieldPenetration;
66
    }
67
68
    public function isShieldPenetration(): bool
69
    {
70
        return $this->isShieldPenetration;
71
    }
72
73
    private int $shield_damage_factor = 100;
74
75
76 1
    public function setShieldDamageFactor(int $value): void
77
    {
78 1
        $this->shield_damage_factor = $value;
79
    }
80
81
82 6
    public function getShieldDamageFactor(): int
83
    {
84 6
        return $this->shield_damage_factor;
85
    }
86
87
    private bool $is_phaser_damage = false;
88
89
90 7
    public function setIsPhaserDamage(bool $value): void
91
    {
92 7
        $this->is_phaser_damage = $value;
93
    }
94
95
96 6
    public function getIsPhaserDamage(): bool
97
    {
98 6
        return $this->is_phaser_damage;
99
    }
100
101
    private bool $is_torpedo_damage = false;
102
103
104 5
    public function setIsTorpedoDamage(bool $value): void
105
    {
106 5
        $this->is_torpedo_damage = $value;
107
    }
108
109
110 5
    public function getIsTorpedoDamage(): bool
111
    {
112 5
        return $this->is_torpedo_damage;
113
    }
114
115 11
    public function setNetDamage(float $value): void
116
    {
117 11
        $this->netDamage = $value;
118
    }
119
120 13
    public function getNetDamage(): float
121
    {
122 13
        return $this->netDamage;
123
    }
124
125 3
    public function getModificator(): int
126
    {
127 3
        return $this->modificator;
128
    }
129
130 11
    public function setModificator(int $value): void
131
    {
132 11
        $this->modificator = $value;
133
    }
134
135 1
    public function setPirateWrath(UserInterface $attacker, ShipInterface $target): void
136
    {
137 1
        if ($attacker->getId() !== UserEnum::USER_NPC_KAZON) {
138 1
            return;
139
        }
140
141
        $pirateWrath = $target->getUser()->getPirateWrath();
142
        if ($pirateWrath === null) {
143
            return;
144
        }
145
146
        $this->pirateWrath = $pirateWrath->getWrath();
147
    }
148
149 11
    public function getDamageRelative(ColonyInterface|ShipInterface $target, int $mode): float
150
    {
151 11
        if ($target instanceof ColonyInterface) {
152
            if ($mode === ShipEnum::DAMAGE_MODE_HULL) {
153
                return $this->calculateDamageBuilding();
154
            }
155
            return $this->calculateDamageColonyShields($target);
156
        }
157 11
        if ($mode === ShipEnum::DAMAGE_MODE_HULL) {
158 5
            return $this->calculateDamageHull();
159
        }
160
161 6
        return $this->calculateDamageShields($target);
162
    }
163
164
165 6
    private function calculateDamageShields(ShipInterface $target): float
166
    {
167 6
        $netDamage = $this->getNetDamage();
168 6
        $netDamage = $this->mindPirateWrath($netDamage);
169
170 6
        $targetShields = $target->getShield();
171
172 6
        $grossModificator = round($this->getShieldDamageFactor() / 100);
173 6
        if ($this->getIsPhaserDamage() === true) {
174 3
            $grossModificator = round($grossModificator * $this->modificator / 100);
175
        }
176
177 6
        $neededNetDamageForShields = min($netDamage, (int)ceil($targetShields / $grossModificator));
178 6
        $grossDamage = min($targetShields, $neededNetDamageForShields * $grossModificator);
179
180 6
        if ($neededNetDamageForShields >= $netDamage) {
181 3
            $this->setNetDamage(0);
182
        } else {
183 3
            $this->setNetDamage($netDamage - $neededNetDamageForShields);
184
        }
185
186 6
        return $grossDamage;
187
    }
188
189
190
    private function calculateDamageColonyShields(ColonyInterface $target): float
191
    {
192
        $damage = round($this->getNetDamage() / 100 * $this->getShieldDamageFactor());
193
194
        if ($damage < $target->getShields()) {
195
            $this->setNetDamage(0);
196
        } else {
197
            $this->setNetDamage(round($damage - $target->getShields() / $this->getShieldDamageFactor() * 100));
198
        }
199
        return $damage;
200
    }
201
202
203 5
    private function calculateDamageHull(): float
204
    {
205 5
        $damage = round($this->getNetDamage() / 100 * $this->getHullDamageFactor());
206 5
        $damage = $this->mindPirateWrath($damage);
207
208 5
        if ($this->getIsTorpedoDamage() === true) {
209 3
            $damage = round($damage * ($this->getModificator() / 100));
210
        }
211 5
        return $damage;
212
    }
213
214
215
    private function calculateDamageBuilding(): float
216
    {
217
        return round($this->getNetDamage() / 100 * $this->getHullDamageFactor());
218
    }
219
220 11
    private function mindPirateWrath(float $damage): float
221
    {
222 11
        if ($this->pirateWrath === null) {
223 11
            return $damage;
224
        }
225
226
        return round($damage / PirateWrathInterface::DEFAULT_WRATH * $this->pirateWrath);
227
    }
228
}
229