Passed
Pull Request — master (#1830)
by Nico
30:56
created

LaunchEscapePods::fly3()   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->setLocation($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
                $field = $this->starSystemMapRepository->getByCoordinates(
85
                    $pods->getSystem()->getId(),
86
                    $newXY[0],
87
                    $newXY[1]
88
                );
89
            } else {
90
                $layer = $pods->getLayer();
91
                if ($layer === null) {
92
                    throw new RuntimeException('this should not happen');
93
                }
94
95
                $field = $this->mapRepository->getByCoordinates(
96
                    $layer,
97
                    $newXY[0],
98
                    $newXY[1]
99
                );
100
            }
101
            if ($field === null) {
102
                throw new RuntimeException('this should not happen');
103
            }
104
            $pods->setLocation($field);
105
        }
106
    }
107
108
    //flee upwards
109
    /**
110
     * @return array<int>
111
     */
112
    private function fly2(ShipInterface $pods): array
113
    {
114
        return [$pods->getPosX(), $pods->getPosY() - 1];
115
    }
116
117
    //flee downwards
118
    /**
119
     * @return array<int>
120
     */
121
    private function fly4(ShipInterface $pods): array
122
    {
123
        return [$pods->getPosX(), $pods->getPosY() + 1];
124
    }
125
126
    //flee right
127
    /**
128
     * @return array<int>
129
     */
130
    private function fly3(ShipInterface $pods): array
131
    {
132
        return [$pods->getPosX() - 1, $pods->getPosY()];
133
    }
134
135
    //flee left
136
    /**
137
     * @return array<int>
138
     */
139
    private function fly1(ShipInterface $pods): array
140
    {
141
        return [$pods->getPosX() + 1, $pods->getPosY()];
142
    }
143
}
144