Passed
Push — master ( 48bfaa...170129 )
by Nico
25:44
created

PirateAttack::unwarpIfNeccessary()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 9
c 1
b 1
f 0
nc 3
nop 1
dl 0
loc 15
ccs 0
cts 11
cp 0
crap 12
rs 9.9666
1
<?php
2
3
namespace Stu\Lib\Pirate\Component;
4
5
use Stu\Component\Ship\System\ShipSystemTypeEnum;
6
use Stu\Lib\Information\InformationWrapper;
7
use Stu\Module\Logging\LoggerUtilFactoryInterface;
8
use Stu\Module\Logging\PirateLoggerInterface;
9
use Stu\Module\Ship\Lib\ActivatorDeactivatorHelperInterface;
10
use Stu\Module\Ship\Lib\Battle\AlertRedHelperInterface;
11
use Stu\Module\Ship\Lib\Battle\ShipAttackCoreInterface;
12
use Stu\Module\Ship\Lib\FleetWrapperInterface;
13
use Stu\Module\Ship\Lib\Interaction\InterceptShipCoreInterface;
14
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
15
use Stu\Module\Ship\Lib\ShipWrapperInterface;
16
use Stu\Orm\Entity\ShipInterface;
17
18
class PirateAttack implements PirateAttackInterface
19
{
20
    private PirateLoggerInterface $logger;
21
22
    public function __construct(
23
        private InterceptShipCoreInterface $interceptShipCore,
24
        private ShipAttackCoreInterface $shipAttackCore,
25
        private ShipWrapperFactoryInterface $shipWrapperFactory,
26
        private ActivatorDeactivatorHelperInterface $helper,
27
        private AlertRedHelperInterface $alertRedHelper,
28
        LoggerUtilFactoryInterface $loggerUtilFactory
29
    ) {
30
        $this->logger = $loggerUtilFactory->getPirateLogger();
31
    }
32
33
    public function attackShip(FleetWrapperInterface $fleetWrapper, ShipInterface $target): void
34
    {
35
        $leadWrapper = $fleetWrapper->getLeadWrapper();
36
37
        $this->interceptIfNeccessary($leadWrapper->get(), $target);
38
39
        if ($fleetWrapper->get()->getShips()->isEmpty()) {
40
            $this->logger->log('    cancel attack, no ships left');
41
            return;
42
        }
43
44
        $this->unwarpIfNeccessary($leadWrapper);
45
46
        if ($fleetWrapper->get()->getShips()->isEmpty()) {
47
            $this->logger->log('    cancel attack, no ships left');
48
            return;
49
        }
50
51
        $isFleetFight = false;
52
        $informations = new InformationWrapper();
53
54
        $this->logger->logf('    attacking target with shipId: %d', $target->getId());
55
56
        $this->shipAttackCore->attack(
57
            $leadWrapper,
58
            $this->shipWrapperFactory->wrapShip($target),
59
            $isFleetFight,
60
            $informations
61
        );
62
    }
63
64
65
    private function interceptIfNeccessary(ShipInterface $ship, ShipInterface $target): void
66
    {
67
        //TODO what about tractored ships?
68
        if (!$target->getWarpDriveState()) {
69
            return;
70
        }
71
72
        $this->logger->logf('    intercepting target with shipId: %d', $target->getId());
73
        $this->interceptShipCore->intercept($ship, $target, new InformationWrapper());
74
    }
75
76
    private function unwarpIfNeccessary(ShipWrapperInterface $wrapper): void
77
    {
78
        if (!$wrapper->get()->getWarpDriveState()) {
79
            return;
80
        }
81
82
        $informationWrapper = new InformationWrapper();
83
84
        if ($this->helper->deactivateFleet(
85
            $wrapper,
86
            ShipSystemTypeEnum::SYSTEM_WARPDRIVE,
87
            $informationWrapper
88
        )) {
89
            $this->logger->log('    deactivated warp');
90
            $this->alertRedHelper->doItAll($wrapper->get(), $informationWrapper);
91
        }
92
    }
93
}
94