Passed
Pull Request — master (#1825)
by Nico
59:43 queued 25:38
created

LaunchEscapePods::fly4()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
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\Ship\Lib\Crew;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use RuntimeException;
9
use Stu\Component\Ship\ShipRumpEnum;
10
use Stu\Orm\Entity\ShipInterface;
11
use Stu\Orm\Repository\MapRepositoryInterface;
12
use Stu\Orm\Repository\ShipRepositoryInterface;
13
use Stu\Orm\Repository\ShipRumpRepositoryInterface;
14
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
15
use Stu\Orm\Repository\UserRepositoryInterface;
16
17
//TODO unit tests
18
final class LaunchEscapePods implements LaunchEscapePodsInterface
19
{
20
    private ShipRepositoryInterface $shipRepository;
21
22
    private UserRepositoryInterface $userRepository;
23
24
    private ShipRumpRepositoryInterface $shipRumpRepository;
25
26
    private StarSystemMapRepositoryInterface $starSystemMapRepository;
27
28
    private MapRepositoryInterface $mapRepository;
29
30
    private EntityManagerInterface $entityManager;
31
32 1
    public function __construct(
33
        ShipRepositoryInterface $shipRepository,
34
        UserRepositoryInterface $userRepository,
35
        ShipRumpRepositoryInterface $shipRumpRepository,
36
        StarSystemMapRepositoryInterface $starSystemMapRepository,
37
        MapRepositoryInterface $mapRepository,
38
        EntityManagerInterface $entityManager
39
    ) {
40 1
        $this->shipRepository = $shipRepository;
41 1
        $this->userRepository = $userRepository;
42 1
        $this->shipRumpRepository = $shipRumpRepository;
43 1
        $this->starSystemMapRepository = $starSystemMapRepository;
44 1
        $this->mapRepository = $mapRepository;
45 1
        $this->entityManager = $entityManager;
46
    }
47
48
    public function launch(ShipInterface $ship): ?ShipInterface
49
    {
50
        $shipRump = $this->shipRumpRepository->find($ship->getUser()->getFactionId() + ShipRumpEnum::SHIP_RUMP_BASE_ID_ESCAPE_PODS);
51
52
        // faction does not have escape pods
53
        if ($shipRump == null) {
54
            return null;
55
        }
56
57
        $pods = $this->shipRepository->prototype();
58
        $pods->setUser($this->userRepository->getFallbackUser());
59
        $pods->setRump($shipRump);
60
        $pods->setName(sprintf(_('Rettungskapseln von (%d)'), $ship->getId()));
61
        $pods->setHuell(1);
62
        $pods->setMaxHuell(1);
63
        $pods->setAlertStateGreen();
64
65
        $pods->updateLocation($ship->getLocation());
66
67
        //return to save place
68
        $this->returnToSafety($pods, $ship);
69
70
        $this->shipRepository->save($pods);
71
        $this->entityManager->flush();
72
        return $pods;
73
    }
74
75
    private function returnToSafety(ShipInterface $pods, ShipInterface $ship): void
76
    {
77
        $field = $pods->getCurrentMapField();
78
79
        if ($field->getFieldType()->getSpecialDamage()) {
80
            $met = 'fly' . $ship->getFlightDirection();
81
            $newXY = $this->$met($pods);
82
83
            if ($pods->getSystem() !== null) {
84
                $map = $this->starSystemMapRepository->getByCoordinates(
85
                    $pods->getSystem()->getId(),
86
                    $newXY[0],
87
                    $newXY[1]
88
                );
89
                $pods->setStarsystemMap($map);
90
            } else {
91
                $layer = $pods->getLayer();
92
                if ($layer === null) {
93
                    throw new RuntimeException('this should not happen');
94
                }
95
96
                $map = $this->mapRepository->getByCoordinates(
97
                    $layer,
98
                    $newXY[0],
99
                    $newXY[1]
100
                );
101
                $pods->setMap($map);
102
            }
103
        }
104
    }
105
106
    //flee upwards
107
    /**
108
     * @return array<int>
109
     */
110
    private function fly2(ShipInterface $pods): array
111
    {
112
        return [$pods->getPosX(), $pods->getPosY() - 1];
113
    }
114
115
    //flee downwards
116
    /**
117
     * @return array<int>
118
     */
119
    private function fly4(ShipInterface $pods): array
120
    {
121
        return [$pods->getPosX(), $pods->getPosY() + 1];
122
    }
123
124
    //flee right
125
    /**
126
     * @return array<int>
127
     */
128
    private function fly3(ShipInterface $pods): array
129
    {
130
        return [$pods->getPosX() - 1, $pods->getPosY()];
131
    }
132
133
    //flee left
134
    /**
135
     * @return array<int>
136
     */
137
    private function fly1(ShipInterface $pods): array
138
    {
139
        return [$pods->getPosX() + 1, $pods->getPosY()];
140
    }
141
}
142