Passed
Push — master ( bf860f...1dd8f7 )
by Nico
57:55 queued 29:36
created

LeaveIntactModules   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 8.69%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 51
ccs 2
cts 23
cp 0.0869
rs 10
c 1
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B handleShipDestruction() 0 44 8
1
<?php
2
3
namespace Stu\Module\Ship\Lib\Destruction\Handler;
4
5
use Stu\Component\Ship\Storage\ShipStorageManagerInterface;
6
use Stu\Lib\Information\InformationInterface;
7
use Stu\Module\PlayerSetting\Lib\UserEnum;
8
use Stu\Module\Ship\Lib\Destruction\ShipDestroyerInterface;
9
use Stu\Module\Ship\Lib\Destruction\ShipDestructionCauseEnum;
10
use Stu\Module\Ship\Lib\ShipWrapperInterface;
11
12
class LeaveIntactModules implements ShipDestructionHandlerInterface
13
{
14 1
    public function __construct(
15
        private ShipStorageManagerInterface $shipStorageManager
16
    ) {
17 1
    }
18
19
    public function handleShipDestruction(
20
        ?ShipDestroyerInterface $destroyer,
21
        ShipWrapperInterface $destroyedShipWrapper,
22
        ShipDestructionCauseEnum $cause,
23
        InformationInterface $informations
24
    ): void {
25
26
        $ship = $destroyedShipWrapper->get();
27
28
        if ($ship->isShuttle()) {
29
            return;
30
        }
31
32
        $intactModules = [];
33
34
        foreach ($ship->getSystems() as $system) {
35
            if (
36
                $system->getModule() !== null
37
                && $system->getStatus() == 100
38
            ) {
39
                $module = $system->getModule();
40
41
                if (!array_key_exists($module->getId(), $intactModules)) {
42
                    $intactModules[$module->getId()] = $module;
43
                }
44
            }
45
        }
46
47
        //leave 50% of all intact modules
48
        $leaveCount = (int) ceil(count($intactModules) / 2);
49
50
        //maximum of 1 if ship is pirate
51
        if ($ship->getUser()->getId() === UserEnum::USER_NPC_KAZON) {
52
            $leaveCount = min(1, $leaveCount);
53
        }
54
55
        for ($i = 1; $i <= $leaveCount; $i++) {
56
            $module = $intactModules[array_rand($intactModules)];
57
            unset($intactModules[$module->getId()]);
58
59
            $this->shipStorageManager->upperStorage(
60
                $ship,
61
                $module->getCommodity(),
62
                1
63
            );
64
        }
65
    }
66
}
67