Passed
Push — dev ( 59981f...f2247e )
by Janko
47:08
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 10
    public function __construct(
16
        private PlayerRelationDeterminatorInterface $playerRelationDeterminator,
17
        private StuTime $stuTime
18
    ) {
19 10
    }
20
21 10
    public function isSkipped(
22
        ShipInterface $incomingShip,
23
        ShipInterface $alertedShip,
24
        ?ShipInterface $tractoringShip,
25
        Collection $usersToInformAboutTrojanHorse
26
    ): bool {
27 10
        $alertUser = $alertedShip->getUser();
28 10
        $incomingShipUser = $incomingShip->getUser();
29
30
        //alert yellow only attacks if incoming is foe
31
        if (
32 10
            $alertedShip->getAlertState() === ShipAlertStateEnum::ALERT_YELLOW
33 10
            && !$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 9
            $tractoringShip !== null
43 9
            && $this->playerRelationDeterminator->isFriend($alertUser, $tractoringShip->getUser())
44
        ) {
45
            $alertUserId = $alertUser->getId();
46
47
            if (
48
                $usersToInformAboutTrojanHorse->containsKey($alertUserId)
49
                && !$this->playerRelationDeterminator->isFriend($alertUser, $incomingShipUser)
50
            ) {
51
                $usersToInformAboutTrojanHorse->set($alertUserId, $alertUser);
52
            }
53
            return true;
54
        }
55
56
        //ships of friends dont attack
57 9
        if ($this->playerRelationDeterminator->isFriend($alertUser, $incomingShipUser)) {
58 1
            return true;
59
        }
60
61
        //ships in finished tholian web dont attack
62 8
        $holdingWeb = $alertedShip->getHoldingWeb();
63 8
        if ($holdingWeb !== null && $holdingWeb->isFinished()) {
64 1
            return true;
65
        }
66
67 7
        if ($this->skipDueToPirateProtection(
68 7
            $incomingShipUser,
69 7
            $alertedShip
70 7
        )) {
71 2
            return true;
72
        }
73
74 5
        return false;
75
    }
76
77 7
    private function skipDueToPirateProtection(
78
        UserInterface $incomingShipUser,
79
        ShipInterface $alertShip
80
    ): bool {
81
82
        //pirates don't attack if user is protected
83 7
        $pirateWrath = $incomingShipUser->getPirateWrath();
84
        if (
85 7
            $alertShip->getUserId() === UserEnum::USER_NPC_KAZON
86 7
            && $pirateWrath !== null
87 7
            && $pirateWrath->getProtectionTimeout() > $this->stuTime->time()
88
        ) {
89 1
            return true;
90
        }
91
92
        //players don't attack pirates if protection is active
93 6
        $pirateWrath = $alertShip->getUser()->getPirateWrath();
94
        if (
95 6
            $incomingShipUser->getId() === UserEnum::USER_NPC_KAZON
96 6
            && $pirateWrath !== null
97 6
            && $pirateWrath->getProtectionTimeout() > $this->stuTime->time()
98
        ) {
99 1
            return true;
100
        }
101
102 5
        return false;
103
    }
104
}
105