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

AbstractEnergyAttacker   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 9.52%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 51
ccs 2
cts 21
cp 0.0952
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getFiringMode() 0 6 1
A getWeapon() 0 13 3
A getWeaponDamage() 0 15 4
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