Completed
Push — master ( 29e2cf...033bf5 )
by Jakub
02:40
created

CombatActionSelector::chooseAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 15
ccs 8
cts 10
cp 0.8
rs 9.9666
c 0
b 0
f 0
nc 4
nop 2
cc 4
crap 4.128
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use HeroesofAbenez\Combat\CombatActions\ICombatAction;
7
8
/**
9
 * CombatActionSelector
10
 *
11
 * @author Jakub Konečný
12
 * @property string $defaultAction
13
 */
14 1
final class CombatActionSelector implements ICombatActionSelector {
15 1
  use \Nette\SmartObject;
16
17
  /** @var string */
18
  protected $defaultAction = CombatLogEntry::ACTION_ATTACK;
19
20
  /**
21
   * @return string
22
   */
23
  public function getDefaultAction(): string {
24
    return $this->defaultAction;
25
  }
26
27
  /**
28
   * @param string $defaultAction
29
   */
30
  public function setDefaultAction(string $defaultAction): void {
31
    $this->defaultAction = $defaultAction;
32
  }
33
34
  public function chooseAction(CombatBase $combat, Character $character): ?string {
35 1
    if($character->hitpoints < 1) {
36
      return null;
37
    }
38
    /** @var ICombatAction[] $actions */
39 1
    $actions = $combat->combatActions->toArray();
40 1
    usort($actions, function(ICombatAction $a, ICombatAction $b) {
41 1
      return $a->getPriority() < $b->getPriority();
42 1
    });
43 1
    foreach($actions as $action) {
44 1
      if($action->shouldUse($combat, $character)) {
45 1
        return $action->getName();
46
      }
47
    }
48
    return $this->defaultAction;
49
  }
50
}
51
?>