RandomSuccessCalculator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
eloc 15
c 3
b 0
f 0
dl 0
loc 25
rs 10
ccs 13
cts 13
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasHit() 0 13 3
A hasHealed() 0 5 1
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