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

CombatActionSelector   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 6
eloc 14
dl 0
loc 35
ccs 10
cts 15
cp 0.6667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaultAction() 0 2 1
A chooseAction() 0 15 4
A getDefaultAction() 0 2 1
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
?>