Passed
Push — dev ( 59981f...f2247e )
by Janko
47:08
created

EnergyWeaponPhase   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Test Coverage

Coverage 49.04%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 97
dl 0
loc 180
ccs 51
cts 104
cp 0.4904
rs 10
c 2
b 0
f 0
wmc 23

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setWeaponShieldModificator() 0 15 3
A isCritical() 0 4 2
A getEnergyWeaponEnergyCosts() 0 4 1
B fireAtBuilding() 0 71 8
B fire() 0 72 9
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\Battle\Party\BattlePartyInterface;
11
use Stu\Module\Ship\Lib\Message\Message;
12
use Stu\Module\Ship\Lib\Battle\Provider\EnergyAttackerInterface;
13
use Stu\Module\Ship\Lib\Battle\ShipAttackCauseEnum;
14
use Stu\Orm\Entity\PlanetFieldInterface;
15
use Stu\Orm\Entity\ShipInterface;
16
use Stu\Orm\Entity\WeaponInterface;
17
18
//TODO unit tests
19
final class EnergyWeaponPhase extends AbstractWeaponPhase implements EnergyWeaponPhaseInterface
20
{
21
    public const FIRINGMODE_RANDOM = 1;
22
    public const FIRINGMODE_FOCUS = 2;
23
24 1
    public function fire(
25
        EnergyAttackerInterface $attacker,
26
        BattlePartyInterface $targetPool,
27
        ShipAttackCauseEnum $attackCause
28
    ): array {
29 1
        $messages = [];
30
31 1
        $targetWrapper = $targetPool->getRandomActiveMember();
32
33 1
        $phaserVolleys = $attacker->getPhaserVolleys();
34 1
        for ($i = 1; $i <= $phaserVolleys; $i++) {
35 1
            if ($targetPool->isDefeated()) {
36 1
                break;
37
            }
38 1
            if (!$attacker->getPhaserState() || !$attacker->hasSufficientEnergy($this->getEnergyWeaponEnergyCosts())) {
39
                break;
40
            }
41
42 1
            $weapon = $attacker->getWeapon();
43
44 1
            $attacker->reduceEps($this->getEnergyWeaponEnergyCosts());
45 1
            if ($attacker->getFiringMode() === self::FIRINGMODE_RANDOM) {
46 1
                $targetWrapper = $targetPool->getRandomActiveMember();
47
            }
48
49 1
            $target = $targetWrapper->get();
50
51 1
            $message = new Message($attacker->getUser()->getId(), $target->getUser()->getId());
52 1
            $messages[] = $message;
53
54 1
            $message->add(sprintf(
55 1
                "Die %s feuert mit einem %s auf die %s",
56 1
                $attacker->getName(),
57 1
                $weapon->getName(),
58 1
                $target->getName()
59 1
            ));
60
61
            if (
62 1
                $attacker->getHitChance() * (100 - $target->getEvadeChance()) < $this->stuRandom->rand(1, 10000)
63
            ) {
64
                $message->add("Die " . $target->getName() . " wurde verfehlt");
65
                continue;
66
            }
67 1
            $isCritical = $this->isCritical($weapon, $target->getCloakState());
68 1
            $damage_wrapper = new DamageWrapper(
69 1
                $attacker->getWeaponDamage($isCritical)
70 1
            );
71 1
            $damage_wrapper->setCrit($isCritical);
72 1
            $damage_wrapper->setShieldDamageFactor($attacker->getPhaserShieldDamageFactor());
73 1
            $damage_wrapper->setHullDamageFactor($attacker->getPhaserHullDamageFactor());
74 1
            $damage_wrapper->setIsPhaserDamage(true);
75 1
            $damage_wrapper->setPirateWrath($attacker->getUser(), $target);
76 1
            $this->setWeaponShieldModificator($target, $weapon, $damage_wrapper);
77
78 1
            $message->addMessageMerge($this->applyDamage->damage($damage_wrapper, $targetWrapper)->getInformations());
79
80 1
            if ($target->isDestroyed()) {
81
82 1
                $this->checkForShipDestruction(
83 1
                    $attacker,
84 1
                    $targetWrapper,
85 1
                    $attackCause->getDestructionCause(),
86 1
                    $message
87 1
                );
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