Passed
Push — dev ( 4b95d0...971998 )
by Janko
45:08 queued 21:43
created

DamageWrapper::setHullDamageFactor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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