Passed
Push — dev ( 91d807...b97e85 )
by Janko
28:41
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 1
                if ($isAlertRed) {
80
                    $this->entryCreator->addEntry(
81
                        '[b][color=red]Alarm-Rot:[/color][/b] Die ' . $target->getName() . ' (' . $target->getRump()->getName() . ') wurde in Sektor ' . $target->getSectorString() . ' von der ' . $attacker->getName() . ' zerstört',
82
                        $attacker->getUser()->getId(),
83
                        $target
84
                    );
85
                } else {
86 1
                    $entryMsg = sprintf(
87 1
                        'Die %s (%s) wurde in Sektor %s von der %s zerstört',
88 1
                        $target->getName(),
89 1
                        $target->getRump()->getName(),
90 1
                        $target->getSectorString(),
91 1
                        $attacker->getName()
92 1
                    );
93 1
                    $this->entryCreator->addEntry(
94 1
                        $entryMsg,
95 1
                        $attacker->getUser()->getId(),
96 1
                        $target
97 1
                    );
98
                }
99
100 1
                $this->checkForPrestige($attacker->getUser(), $target);
101
102 1
                $targetId = $target->getId();
103 1
                $message->add($this->shipRemover->destroy($targetWrapper));
104
105 1
                unset($targetPool[$targetId]);
106
107 1
                if ($weapon->getFiringMode() === self::FIRINGMODE_FOCUS) {
108
                    break;
109
                }
110
            }
111
        }
112
113 1
        return $messages;
114
    }
115
116
    public function fireAtBuilding(
117
        EnergyAttackerInterface $attacker,
118
        PlanetFieldInterface $target,
119
        bool $isOrbitField
120
    ): InformationWrapper {
121
        $informations = new InformationWrapper();
122
123
        $building = $target->getBuilding();
124
        if ($building === null) {
125
            $informations->addInformation(_("Kein Gebäude vorhanden"));
126
127
            return $informations;
128
        }
129
130
        for ($i = 1; $i <= $attacker->getPhaserVolleys(); $i++) {
131
            if (!$attacker->getPhaserState() || !$attacker->hasSufficientEnergy($this->getEnergyWeaponEnergyCosts())) {
132
                break;
133
            }
134
            $attacker->reduceEps($this->getEnergyWeaponEnergyCosts());
135
136
            $weapon = $attacker->getWeapon();
137
            $informations->addInformation(sprintf(
138
                _("Die %s feuert mit einem %s auf das Gebäude %s auf Feld %d"),
139
                $attacker->getName(),
140
                $weapon->getName(),
141
                $building->getName(),
142
                $target->getFieldId()
143
            ));
144
145
            if (
146
                $attacker->getHitChance() < random_int(1, 100)
147
            ) {
148
                $informations->addInformation(_("Das Gebäude wurde verfehlt"));
149
                continue;
150
            }
151
152
            $isCritical = random_int(1, 100) <= $weapon->getCriticalChance();
153
154
            $damage_wrapper = new DamageWrapper(
155
                $attacker->getWeaponDamage($isCritical)
156
            );
157
            $damage_wrapper->setCrit($isCritical);
158
            $damage_wrapper->setShieldDamageFactor($attacker->getPhaserShieldDamageFactor());
159
            $damage_wrapper->setHullDamageFactor($attacker->getPhaserHullDamageFactor());
160
            $damage_wrapper->setIsPhaserDamage(true);
161
162
163
            $informations->addInformationWrapper($this->applyDamage->damageBuilding($damage_wrapper, $target, $isOrbitField));
164
165
            if ($target->getIntegrity() === 0) {
166
                $this->entryCreator->addEntry(
167
                    sprintf(
168
                        _('Das Gebäude %s auf Kolonie %s wurde von der %s zerstört'),
169
                        $building->getName(),
170
                        $target->getHost()->getName(),
171
                        $attacker->getName()
172
                    ),
173
                    $attacker->getUser()->getId(),
174
                    $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

174
                    /** @scrutinizer ignore-type */ $target->getHost()
Loading history...
175
                );
176
177
                $this->buildingManager->remove($target);
178
                break;
179
            }
180
            //deactivate if high damage
181
            elseif ($target->hasHighDamage()) {
182
                $this->buildingManager->deactivate($target);
183
            }
184
        }
185
186
        return $informations;
187
    }
188
189 1
    private function isCritical(WeaponInterface $weapon, bool $isTargetCloaked): bool
190
    {
191 1
        $critChance = $isTargetCloaked ? $weapon->getCriticalChance() * 2 : $weapon->getCriticalChance();
192 1
        return $this->stuRandom->rand(1, 100) <= $critChance;
193
    }
194
195 1
    private function setWeaponShieldModificator(
196
        ShipInterface $target,
197
        WeaponInterface $weapon,
198
        DamageWrapper $damageWrapper
199
    ): void {
200
201 1
        $targetShieldModule = $this->getModule($target, ShipModuleTypeEnum::SHIELDS);
202 1
        if ($targetShieldModule === null) {
203 1
            return;
204
        }
205
206
        $weaponShield = $targetShieldModule->getWeaponShield()->get($weapon->getId());
207
208
        if ($weaponShield !== null) {
209
            $damageWrapper->setModificator($weaponShield->getModificator());
210
        }
211
    }
212
213 1
    private function getEnergyWeaponEnergyCosts(): int
214
    {
215
        // @todo
216 1
        return 1;
217
    }
218
}
219