Passed
Push — dev ( 7dee43...836565 )
by Nico
06:34
created

StationConstructionHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 28.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 73
ccs 11
cts 39
cp 0.2821
rs 10
c 1
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handleSpacecraftTick() 0 11 3
B doConstructionStuff() 0 53 10
1
<?php
2
3
namespace Stu\Module\Tick\Spacecraft\Handler;
4
5
use Stu\Component\Spacecraft\SpacecraftStateEnum;
6
use Stu\Component\Station\StationUtilityInterface;
7
use Stu\Lib\Information\InformationInterface;
8
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
9
use Stu\Module\Station\Lib\StationWrapperInterface;
10
use Stu\Module\Tick\Spacecraft\SpacecraftTickFinishedException;
11
12
class StationConstructionHandler implements SpacecraftTickHandlerInterface
13
{
14 1
    public function __construct(
15
        private StationUtilityInterface $stationUtility
16 1
    ) {}
17
18 1
    #[\Override]
19
    public function handleSpacecraftTick(
20
        SpacecraftWrapperInterface $wrapper,
21
        InformationInterface $information
22
    ): void {
23
24
        if (
25 1
            $wrapper instanceof StationWrapperInterface
26 1
            && $this->doConstructionStuff($wrapper, $information)
27
        ) {
28
            throw new SpacecraftTickFinishedException();
29
        }
30
    }
31
32 1
    private function doConstructionStuff(StationWrapperInterface $wrapper, InformationInterface $information): bool
33
    {
34 1
        $station = $wrapper->get();
35
36 1
        $progress =  $station->getConstructionProgress();
37 1
        if ($progress === null) {
38
            return false;
39
        }
40
41 1
        if ($progress->getRemainingTicks() === 0) {
42 1
            return false;
43
        }
44
45
        $isUnderConstruction = $station->getState() === SpacecraftStateEnum::UNDER_CONSTRUCTION;
46
47
        if (!$this->stationUtility->hasEnoughDockedWorkbees($station, $station->getRump())) {
48
            $neededWorkbees = $isUnderConstruction ? $station->getRump()->getNeededWorkbees() :
49
                (int)ceil($station->getRump()->getNeededWorkbees() / 2);
50
51
            $information->addInformationf(
52
                'Nicht genügend Workbees (%d/%d) angedockt um %s weiterführen zu können',
53
                $this->stationUtility->getDockedWorkbeeCount($station),
54
                $neededWorkbees ?? 0,
55
                $isUnderConstruction ? 'den Bau' : 'die Demontage'
56
            );
57
            return true;
58
        }
59
60
        if ($isUnderConstruction) {
61
            // raise hull
62
            $increase = (int)ceil($station->getMaxHull() / (2 * $station->getRump()->getBuildtime()));
63
            $station->getCondition()->changeHull($increase);
64
        }
65
66
        if ($progress->getRemainingTicks() === 1) {
67
68
            $information->addInformationf(
69
                '%s: %s bei %s fertiggestellt',
70
                $station->getRump()->getName(),
71
                $isUnderConstruction ? 'Bau' : 'Demontage',
72
                $station->getSectorString()
73
            );
74
75
            if ($isUnderConstruction) {
76
                $this->stationUtility->finishStation($progress);
77
            } else {
78
                $this->stationUtility->finishScrapping($progress, $information);
79
            }
80
        } else {
81
            $this->stationUtility->reduceRemainingTicks($progress);
82
        }
83
84
        return true;
85
    }
86
}
87