Passed
Push — dev ( b0a7f5...f0d097 )
by Janko
20:47
created

PirateTick::reloadMinimalEps()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Tick\Pirate;
4
5
use Stu\Module\Control\StuRandom;
6
use Stu\Module\Logging\LoggerUtilFactoryInterface;
7
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
8
use Stu\Lib\Pirate\Behaviour\PirateBehaviourInterface;
9
use Stu\Lib\Pirate\Component\ReloadMinimalEpsInterface;
10
use Stu\Lib\Pirate\PirateBehaviourEnum;
11
use Stu\Lib\Pirate\PirateCreationInterface;
12
use Stu\Lib\Pirate\PirateReactionInterface;
13
use Stu\Module\Logging\PirateLoggerInterface;
14
15
final class PirateTick implements PirateTickInterface
16
{
17
    private const BEHAVIOUR_PROBABILITIES = [
18
        PirateBehaviourEnum::DO_NOTHING->value => 30,
19
        PirateBehaviourEnum::FLY->value => 40,
20
        PirateBehaviourEnum::RUB_COLONY->value => 5,
21
        PirateBehaviourEnum::ATTACK_SHIP->value => 5,
22
        PirateBehaviourEnum::HIDE->value => 20,
23
        PirateBehaviourEnum::RAGE->value => 2,
24
        PirateBehaviourEnum::CALL_FOR_SUPPORT->value => 1,
25
    ];
26
27
    private PirateLoggerInterface $logger;
28
29
    /** @param array<int, PirateBehaviourInterface> $behaviours */
30
    public function __construct(
31
        private PirateCreationInterface $pirateCreation,
32
        private PirateReactionInterface $pirateReaction,
33
        private ShipWrapperFactoryInterface $shipWrapperFactory,
34
        private ReloadMinimalEpsInterface $reloadMinimalEps,
35
        private StuRandom $stuRandom,
36
        LoggerUtilFactoryInterface $loggerUtilFactory,
37
        private array $behaviours
38
    ) {
39
        $this->logger = $loggerUtilFactory->getPirateLogger();
40
    }
41
42
    public function work(): void
43
    {
44
        $this->logger->log('PIRATE TICK:');
45
46
        // create new pirates (max 5 fleets)
47
        $pirateFleets = $this->pirateCreation->createPirateFleetsIfNeeded();
48
49
        // process pirate fleets
50
        foreach ($pirateFleets as $fleet) {
51
            $behaviourType = $this->getRandomBehaviourType();
52
53
            if ($behaviourType === PirateBehaviourEnum::DO_NOTHING) {
54
                continue;
55
            }
56
57
            $this->logger->log(sprintf('pirateFleetId %d does %s', $fleet->getId(), $behaviourType->name));
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Stu\Lib\Pirate\PirateBehaviourEnum.
Loading history...
58
59
            $fleetWrapper = $this->shipWrapperFactory->wrapFleet($fleet);
60
61
            $this->behaviours[$behaviourType->value]->action($fleetWrapper, $this->pirateReaction);
62
63
            $this->reloadMinimalEps->reload($fleetWrapper);
64
        }
65
    }
66
67
    private function getRandomBehaviourType(): PirateBehaviourEnum
68
    {
69
        $value = $this->stuRandom->randomOfProbabilities(self::BEHAVIOUR_PROBABILITIES);
70
71
        return PirateBehaviourEnum::from($value);
72
    }
73
}
74