RandomSuccessCalculator::hasHit()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 13
rs 9.9666
ccs 9
cts 9
cp 1
cc 3
nc 3
nop 3
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nexendrie\Utils\Numbers;
7
8
/**
9
 * RandomSuccessCalculator
10
 *
11
 * @author Jakub Konečný
12
 */
13 1
final class RandomSuccessCalculator implements ISuccessCalculator
14
{
15
    public const MAX_HIT_CHANCE = 100;
16
    public const MIN_HIT_CHANCE = 15;
17
18
    public function hasHit(Character $character1, Character $character2, ?CharacterAttackSkill $skill = null): bool
19
    {
20 1
        if (!$character2->canDefend()) {
21 1
            return true;
22
        }
23 1
        $hitRate = $character1->hit;
24 1
        $dodgeRate = $character2->dodge;
25 1
        if ($skill !== null) {
26 1
            $hitRate = $hitRate / 100 * $skill->hitRate;
27
        }
28 1
        $hitChance = Numbers::range((int) ($hitRate - $dodgeRate), self::MIN_HIT_CHANCE, self::MAX_HIT_CHANCE);
29 1
        $roll = rand(0, 100);
30 1
        return ($roll <= $hitChance);
31
    }
32
33
    public function hasHealed(Character $healer): bool
34
    {
35 1
        $chance = $healer->intelligence * (int) round($healer->level / 5) + 30;
36 1
        $roll = rand(0, 100);
37 1
        return ($roll <= $chance);
38
    }
39
}
40