Passed
Push — dev ( 91d807...b97e85 )
by Janko
28:41
created

DamageWrapper   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 76.32%

Importance

Changes 0
Metric Value
eloc 65
c 0
b 0
f 0
dl 0
loc 190
ccs 58
cts 76
cp 0.7632
rs 9.84
wmc 32

22 Methods

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