Passed
Push — master ( f544cb...b3a3d9 )
by Nico
36:43 queued 09:10
created

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