Passed
Pull Request — master (#1819)
by Nico
52:14 queued 25:19
created

SkipDetection::skipDueToPirateProtection()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
nc 3
nop 2
dl 0
loc 26
ccs 12
cts 12
cp 1
crap 7
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Ship\Lib\Battle\AlertDetection;
4
5
use Doctrine\Common\Collections\Collection;
6
use Stu\Component\Player\Relation\PlayerRelationDeterminatorInterface;
7
use Stu\Component\Ship\ShipAlertStateEnum;
8
use Stu\Module\Control\StuTime;
9
use Stu\Module\PlayerSetting\Lib\UserEnum;
10
use Stu\Orm\Entity\ShipInterface;
11
use Stu\Orm\Entity\UserInterface;
12
13
class SkipDetection implements SkipDetectionInterface
14
{
15 12
    public function __construct(
16
        private PlayerRelationDeterminatorInterface $playerRelationDeterminator,
17
        private StuTime $stuTime
18
    ) {
19 12
    }
20
21 12
    public function isSkipped(
22
        ShipInterface $incomingShip,
23
        ShipInterface $alertedShip,
24
        ?ShipInterface $tractoringShip,
25
        Collection $usersToInformAboutTrojanHorse
26
    ): bool {
27 12
        $alertUser = $alertedShip->getUser();
28 12
        $incomingShipUser = $incomingShip->getUser();
29
30
        //alert yellow only attacks if incoming is foe
31
        if (
32 12
            $alertedShip->getAlertState() === ShipAlertStateEnum::ALERT_YELLOW
33 12
            && !$this->playerRelationDeterminator->isEnemy($alertUser, $incomingShipUser)
34
        ) {
35 1
            return true;
36
        }
37
38
        //now ALERT-MODE: RED or YELLOW+ENEMY
39
40
        //ships of friends from tractoring ship dont attack
41
        if (
42 11
            $tractoringShip !== null
43 11
            && $this->playerRelationDeterminator->isFriend($alertUser, $tractoringShip->getUser())
44
        ) {
45
            if (
46 2
                !$usersToInformAboutTrojanHorse->contains($alertUser)
47 2
                && !$this->playerRelationDeterminator->isFriend($alertUser, $incomingShipUser)
48
            ) {
49 1
                $usersToInformAboutTrojanHorse->add($alertUser);
50
            }
51 2
            return true;
52
        }
53
54
        //ships of friends dont attack
55 9
        if ($this->playerRelationDeterminator->isFriend($alertUser, $incomingShipUser)) {
56 1
            return true;
57
        }
58
59
        //ships in finished tholian web dont attack
60 8
        $holdingWeb = $alertedShip->getHoldingWeb();
61 8
        if ($holdingWeb !== null && $holdingWeb->isFinished()) {
62 1
            return true;
63
        }
64
65 7
        if ($this->skipDueToPirateProtection(
66 7
            $incomingShipUser,
67 7
            $alertedShip
68 7
        )) {
69 2
            return true;
70
        }
71
72 5
        return false;
73
    }
74
75 7
    private function skipDueToPirateProtection(
76
        UserInterface $incomingShipUser,
77
        ShipInterface $alertShip
78
    ): bool {
79
80
        //pirates don't attack if user is protected
81 7
        $pirateWrath = $incomingShipUser->getPirateWrath();
82
        if (
83 7
            $alertShip->getUserId() === UserEnum::USER_NPC_KAZON
84 7
            && $pirateWrath !== null
85 7
            && $pirateWrath->getProtectionTimeout() > $this->stuTime->time()
86
        ) {
87 1
            return true;
88
        }
89
90
        //players don't attack pirates if protection is active
91 6
        $pirateWrath = $alertShip->getUser()->getPirateWrath();
92
        if (
93 6
            $incomingShipUser->getId() === UserEnum::USER_NPC_KAZON
94 6
            && $pirateWrath !== null
95 6
            && $pirateWrath->getProtectionTimeout() > $this->stuTime->time()
96
        ) {
97 1
            return true;
98
        }
99
100 5
        return false;
101
    }
102
}
103