Passed
Push — master ( 6d8b29...d7e053 )
by Janko
27:40
created

TrapDetection::getPrestigeOfAlertedSpacecrafts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Stu\Lib\Pirate\Component;
4
5
use Doctrine\Common\Collections\Collection;
6
use Stu\Module\Control\StuRandom;
7
use Stu\Module\Prestige\Lib\PrestigeCalculationInterface;
8
use Stu\Module\Ship\Lib\Battle\AlertDetection\AlertedShipsDetectionInterface;
9
use Stu\Module\Ship\Lib\ShipWrapperInterface;
10
use Stu\Orm\Entity\LocationInterface;
11
use Stu\Orm\Entity\ShipInterface;
12
13
class TrapDetection implements TrapDetectionInterface
14
{
15 4
    public function __construct(
16
        private AlertedShipsDetectionInterface $alertedShipsDetection,
17
        private PrestigeCalculationInterface $prestigeCalculation,
18
        private StuRandom $stuRandom
19 4
    ) {}
20
21 4
    public function isAlertTrap(LocationInterface $location, ShipInterface $leadShip): bool
22
    {
23 4
        $alertedWrappers = $this->alertedShipsDetection->getAlertedShipsOnLocation(
24 4
            $location,
25 4
            $leadShip->getUser()
26 4
        );
27 4
        if ($alertedWrappers->isEmpty()) {
28 1
            return false;
29
        }
30
31 3
        $piratePrestige = $this->prestigeCalculation->getPrestigeOfSpacecraftOrFleet($leadShip);
32 3
        $alertedPrestige = $this->getPrestigeOfAlertedSpacecrafts($alertedWrappers);
33
34 3
        if ($alertedPrestige <= 3 * $piratePrestige) {
35 1
            return false;
36
        }
37
38 2
        return $this->stuRandom->rand(0, $alertedPrestige) > $piratePrestige;
39
    }
40
41
    /** @param Collection<int, ShipWrapperInterface> $alertedWrappers */
42 3
    private function getPrestigeOfAlertedSpacecrafts(Collection $alertedWrappers): int
43
    {
44 3
        return $alertedWrappers
45 3
            ->map(fn(ShipWrapperInterface $wrapper) => $this->prestigeCalculation->getPrestigeOfSpacecraftOrFleet($wrapper))
46 3
            ->reduce(
47 3
                fn(int $sum, int $prestige) => $sum + $prestige,
48 3
                0
49 3
            );
50
    }
51
}
52