Completed
Push — master ( 0472d3...6656bb )
by Jakub
02:13
created

Heal::findHealers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
crap 2.0625
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat\CombatActions;
5
6
use HeroesofAbenez\Combat\Character;
7
use HeroesofAbenez\Combat\CombatBase;
8
use Nexendrie\Utils\Numbers;
9
use HeroesofAbenez\Combat\Team;
10
11 1
final class Heal implements ICombatAction {
12 1
  use \Nette\SmartObject;
13
14
  public const ACTION_NAME = "healing";
15
16
  public function getName(): string {
17 1
    return static::ACTION_NAME;
18
  }
19
20
  public function getPriority(): int {
21 1
    return 1000;
22
  }
23
24
  public function shouldUse(CombatBase $combat, Character $character): bool {
25 1
    return (in_array($character, $this->findHealers($combat)->toArray(), true) AND !is_null($this->selectHealingTarget($character, $combat)));
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 142 characters
Loading history...
26
  }
27
28
  public function do(CombatBase $combat, Character $character): void {
29 1
    $result = [];
30
    /** @var Character $patient */
31 1
    $patient = $this->selectHealingTarget($character, $combat);
32 1
    $result["result"] = $combat->successCalculator->hasHealed($character);
33 1
    $amount = ($result["result"]) ? (int) ($character->intelligence / 2) : 0;
34 1
    $result["amount"] = Numbers::range($amount, 0, $patient->maxHitpoints - $patient->hitpoints);
35 1
    if($result["amount"] > 0) {
36 1
      $patient->heal($result["amount"]);
37
    }
38 1
    $result["action"] = $this->getName();
39 1
    $result["name"] = "";
40 1
    $result["character1"] = $character;
41 1
    $result["character2"] = $patient;
42 1
    $combat->log->log($result);
43 1
  }
44
45
  protected function findHealers(CombatBase $combat): Team {
46 1
    $healers = call_user_func($combat->healers, $combat->team1, $combat->team2);
47 1
    if($healers instanceof Team) {
48 1
      return $healers;
49
    }
50
    return new Team("healers");
51
  }
52
53
  protected function selectHealingTarget(Character $healer, CombatBase $combat): ?Character {
54 1
    return $combat->getTeam($healer)->getLowestHpCharacter();
55
  }
56
}
57
?>