Passed
Push — master ( bf860f...1dd8f7 )
by Nico
57:55 queued 29:36
created

EnergyWeaponPhase::isCritical()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Battle\Weapon;
6
7
use Stu\Component\Ship\ShipModuleTypeEnum;
8
use Stu\Lib\DamageWrapper;
9
use Stu\Lib\Information\InformationWrapper;
10
use Stu\Module\Ship\Lib\Message\Message;
11
use Stu\Module\Ship\Lib\Battle\Provider\EnergyAttackerInterface;
12
use Stu\Orm\Entity\PlanetFieldInterface;
13
use Stu\Orm\Entity\ShipInterface;
14
use Stu\Orm\Entity\WeaponInterface;
15
16
//TODO unit tests
17
final class EnergyWeaponPhase extends AbstractWeaponPhase implements EnergyWeaponPhaseInterface
18
{
19
    public const FIRINGMODE_RANDOM = 1;
20
    public const FIRINGMODE_FOCUS = 2;
21
22 1
    public function fire(
23
        EnergyAttackerInterface $attacker,
24
        array $targetPool,
25
        bool $isAlertRed = false
26
    ): array {
27 1
        $messages = [];
28
29 1
        $targetWrapper = $targetPool[array_rand($targetPool)];
30
31 1
        $phaserVolleys = $attacker->getPhaserVolleys();
32 1
        for ($i = 1; $i <= $phaserVolleys; $i++) {
33 1
            if (empty($targetPool)) {
34 1
                break;
35
            }
36 1
            if (!$attacker->getPhaserState() || !$attacker->hasSufficientEnergy($this->getEnergyWeaponEnergyCosts())) {
37
                break;
38
            }
39
40 1
            $weapon = $attacker->getWeapon();
41
42 1
            $attacker->reduceEps($this->getEnergyWeaponEnergyCosts());
43 1
            if ($attacker->getFiringMode() === self::FIRINGMODE_RANDOM) {
44 1
                $targetWrapper = $targetPool[array_rand($targetPool)];
45
            }
46
47 1
            $target = $targetWrapper->get();
48
49 1
            $message = new Message($attacker->getUser()->getId(), $target->getUser()->getId());
50 1
            $messages[] = $message;
51
52 1
            $message->add(sprintf(
53 1
                "Die %s feuert mit einem %s auf die %s",
54 1
                $attacker->getName(),
55 1
                $weapon->getName(),
56 1
                $target->getName()
57 1
            ));
58
59
            if (
60 1
                $attacker->getHitChance() * (100 - $target->getEvadeChance()) < $this->stuRandom->rand(1, 10000)
61
            ) {
62
                $message->add("Die " . $target->getName() . " wurde verfehlt");
63
                continue;
64
            }
65 1
            $isCritical = $this->isCritical($weapon, $target->getCloakState());
66 1
            $damage_wrapper = new DamageWrapper(
67 1
                $attacker->getWeaponDamage($isCritical)
68 1
            );
69 1
            $damage_wrapper->setCrit($isCritical);
70 1
            $damage_wrapper->setShieldDamageFactor($attacker->getPhaserShieldDamageFactor());
71 1
            $damage_wrapper->setHullDamageFactor($attacker->getPhaserHullDamageFactor());
72 1
            $damage_wrapper->setIsPhaserDamage(true);
73 1
            $damage_wrapper->setPirateWrath($attacker->getUser(), $target);
74 1
            $this->setWeaponShieldModificator($target, $weapon, $damage_wrapper);
75
76 1
            $message->addMessageMerge($this->applyDamage->damage($damage_wrapper, $targetWrapper)->getInformations());
77
78 1
            if ($target->isDestroyed()) {
79
80 1
                $this->checkForShipDestruction(
81 1
                    $attacker,
82 1
                    $targetWrapper,
83 1
                    $isAlertRed,
84 1
                    $message
85 1
                );
86
87 1
                unset($targetPool[$target->getId()]);
88
89 1
                if ($weapon->getFiringMode() === self::FIRINGMODE_FOCUS) {
90
                    break;
91
                }
92
            }
93
        }
94
95 1
        return $messages;
96
    }
97
98
    public function fireAtBuilding(
99
        EnergyAttackerInterface $attacker,
100
        PlanetFieldInterface $target,
101
        bool $isOrbitField
102
    ): InformationWrapper {
103
        $informations = new InformationWrapper();
104
105
        $building = $target->getBuilding();
106
        if ($building === null) {
107
            $informations->addInformation(_("Kein Gebäude vorhanden"));
108
109
            return $informations;
110
        }
111
112
        for ($i = 1; $i <= $attacker->getPhaserVolleys(); $i++) {
113
            if (!$attacker->getPhaserState() || !$attacker->hasSufficientEnergy($this->getEnergyWeaponEnergyCosts())) {
114
                break;
115
            }
116
            $attacker->reduceEps($this->getEnergyWeaponEnergyCosts());
117
118
            $weapon = $attacker->getWeapon();
119
            $informations->addInformation(sprintf(
120
                _("Die %s feuert mit einem %s auf das Gebäude %s auf Feld %d"),
121
                $attacker->getName(),
122
                $weapon->getName(),
123
                $building->getName(),
124
                $target->getFieldId()
125
            ));
126
127
            if (
128
                $attacker->getHitChance() < random_int(1, 100)
129
            ) {
130
                $informations->addInformation(_("Das Gebäude wurde verfehlt"));
131
                continue;
132
            }
133
134
            $isCritical = random_int(1, 100) <= $weapon->getCriticalChance();
135
136
            $damage_wrapper = new DamageWrapper(
137
                $attacker->getWeaponDamage($isCritical)
138
            );
139
            $damage_wrapper->setCrit($isCritical);
140
            $damage_wrapper->setShieldDamageFactor($attacker->getPhaserShieldDamageFactor());
141
            $damage_wrapper->setHullDamageFactor($attacker->getPhaserHullDamageFactor());
142
            $damage_wrapper->setIsPhaserDamage(true);
143
144
145
            $informations->addInformationWrapper($this->applyDamage->damageBuilding($damage_wrapper, $target, $isOrbitField));
146
147
            if ($target->getIntegrity() === 0) {
148
                $this->entryCreator->addEntry(
149
                    sprintf(
150
                        _('Das Gebäude %s auf Kolonie %s wurde von der %s zerstört'),
151
                        $building->getName(),
152
                        $target->getHost()->getName(),
153
                        $attacker->getName()
154
                    ),
155
                    $attacker->getUser()->getId(),
156
                    $target->getHost()
0 ignored issues
show
Bug introduced by
It seems like $target->getHost() can also be of type Stu\Orm\Entity\ColonySandboxInterface; however, parameter $target of Stu\Module\History\Lib\E...orInterface::addEntry() does only seem to accept Stu\Orm\Entity\AllianceI...rm\Entity\ShipInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
                    /** @scrutinizer ignore-type */ $target->getHost()
Loading history...
157
                );
158
159
                $this->buildingManager->remove($target);
160
                break;
161
            }
162
            //deactivate if high damage
163
            elseif ($target->hasHighDamage()) {
164
                $this->buildingManager->deactivate($target);
165
            }
166
        }
167
168
        return $informations;
169
    }
170
171 1
    private function isCritical(WeaponInterface $weapon, bool $isTargetCloaked): bool
172
    {
173 1
        $critChance = $isTargetCloaked ? $weapon->getCriticalChance() * 2 : $weapon->getCriticalChance();
174 1
        return $this->stuRandom->rand(1, 100) <= $critChance;
175
    }
176
177 1
    private function setWeaponShieldModificator(
178
        ShipInterface $target,
179
        WeaponInterface $weapon,
180
        DamageWrapper $damageWrapper
181
    ): void {
182
183 1
        $targetShieldModule = $this->getModule($target, ShipModuleTypeEnum::SHIELDS);
184 1
        if ($targetShieldModule === null) {
185 1
            return;
186
        }
187
188
        $weaponShield = $targetShieldModule->getWeaponShield()->get($weapon->getId());
189
190
        if ($weaponShield !== null) {
191
            $damageWrapper->setModificator($weaponShield->getModificator());
192
        }
193
    }
194
195 1
    private function getEnergyWeaponEnergyCosts(): int
196
    {
197
        // @todo
198 1
        return 1;
199
    }
200
}
201