Passed
Push — dev ( 83c248...96a6f5 )
by Janko
10:53
created

AbstractEnergyAttacker::getFiringMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Battle\Provider;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use RuntimeException;
9
use Stu\Lib\Map\FieldTypeEffectEnum;
10
use Stu\Module\Control\StuRandom;
11
use Stu\Orm\Entity\ModuleInterface;
12
use Stu\Orm\Entity\WeaponInterface;
13
14
abstract class AbstractEnergyAttacker implements EnergyAttackerInterface
15
{
16
    protected ?ModuleInterface $module = null;
17
    private ?WeaponInterface $weapon = null;
18
19 24
    public function __construct(
20
        protected StuRandom $stuRandom
21 24
    ) {}
22
23
    #[Override]
24
    public function getFiringMode(): int
25
    {
26
        $weapon = $this->getWeapon();
27
28
        return $weapon->getFiringMode();
29
    }
30
31
    abstract public function getWeaponModule(): ModuleInterface;
32
33
    #[Override]
34
    public function getWeapon(): WeaponInterface
35
    {
36
        if ($this->weapon === null) {
37
            $weapon = $this->getWeaponModule()->getWeapon();
38
            if ($weapon === null) {
39
                throw new RuntimeException('module system should have a weapon');
40
            }
41
42
            $this->weapon = $weapon;
43
        }
44
45
        return $this->weapon;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->weapon could return the type null which is incompatible with the type-hinted return Stu\Orm\Entity\WeaponInterface. Consider adding an additional type-check to rule them out.
Loading history...
46
    }
47
48
    abstract public function getEnergyWeaponBaseDamage(): int;
49
50
    #[Override]
51
    public function getWeaponDamage(bool $isCritical): int
52
    {
53
        $basedamage = $this->getEnergyWeaponBaseDamage();
54
        $variance = (int) round($basedamage / 100 * $this->getWeapon()->getVariance());
55
        $damage = random_int($basedamage - $variance, $basedamage + $variance);
56
57
        if ($this->getLocation()->getFieldType()->hasEffect(FieldTypeEffectEnum::ENERGY_WEAPON_BUFF)) {
58
            $damage = (int)ceil($damage / 100 * $this->stuRandom->rand(115, 170, true, 125));
59
        }
60
        if ($this->getLocation()->getFieldType()->hasEffect(FieldTypeEffectEnum::ENERGY_WEAPON_NERF)) {
61
            $damage = (int)ceil($damage / 100 * $this->stuRandom->rand(30, 85, true, 75));
62
        }
63
64
        return $isCritical ? $damage * 2 : $damage;
65
    }
66
}
67