Passed
Pull Request — master (#1821)
by Nico
52:02 queued 25:23
created

AttackMatchup::getMatchupInternal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Battle;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Stu\Module\Control\StuRandom;
9
use Stu\Module\Ship\Lib\Battle\Party\RoundBasedBattleParty;
10
11
final class AttackMatchup implements AttackMatchupInterface
12
{
13 7
    public function __construct(
14
        private StuRandom $stuRandom
15
    ) {
16 7
    }
17
18 7
    public function getMatchup(
19
        RoundBasedBattleParty $attackers,
20
        RoundBasedBattleParty $defenders,
21
        bool $firstStrike = false,
22
        bool $oneWay = false
23
    ): ?Matchup {
24
25
        // Check if there're any useable ships at all
26 7
        if ($attackers->isDone() && $defenders->isDone()) {
27 1
            return null;
28
        }
29
30 6
        if ($firstStrike) {
31 1
            return $this->getMatchupInternal($attackers, $defenders);
32
        } else {
33 5
            return $this->getMatchupForFurtherStrike(
34 5
                $attackers,
35 5
                $defenders,
36 5
                $oneWay
37 5
            );
38
        }
39
    }
40
41 5
    private function getMatchupInternal(
42
        RoundBasedBattleParty $attackers,
43
        RoundBasedBattleParty $defenders
44
    ): ?Matchup {
45
46 5
        if ($attackers->isDone()) {
47
            return null;
48
        }
49
50 5
        return new Matchup(
51 5
            $attackers->getRandomUnused(),
52 5
            $defenders->get()
53 5
        );
54
    }
55
56 5
    private function getMatchupForFurtherStrike(
57
        RoundBasedBattleParty $attackers,
58
        RoundBasedBattleParty $defenders,
59
        bool $oneWay
60
    ): ?Matchup {
61 5
        $attackersDone = $attackers->isDone();
62 5
        $defendersDone = $oneWay ? new ArrayCollection() : $defenders->isDone();
63
64 5
        if ($attackersDone && $defendersDone) {
65 1
            return null;
66
        }
67
68 4
        if ($attackersDone) {
69 1
            return $this->getMatchupInternal($defenders, $attackers);
70 3
        } elseif ($defendersDone || $this->stuRandom->rand(1, 2) === 1) {
71 2
            return $this->getMatchupInternal($attackers, $defenders);
72
        } else {
73 1
            return $this->getMatchupInternal($defenders, $attackers);
74
        }
75
    }
76
}
77