Passed
Push — master ( bc69e6...465240 )
by Jakub
12:35
created

SkillSpecial::do()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.351

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 10
dl 0
loc 12
rs 9.9332
c 1
b 0
f 1
ccs 5
cts 9
cp 0.5556
cc 2
nc 2
nop 2
crap 2.351
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 HeroesofAbenez\Combat\CharacterSpecialSkill;
9
use HeroesofAbenez\Combat\ICombatAction;
10
use HeroesofAbenez\Combat\SkillSpecial as Skill;
11
use HeroesofAbenez\Combat\NotImplementedException;
12
use HeroesofAbenez\Combat\CharacterEffect;
13
14 1
final class SkillSpecial implements ICombatAction {
15
  use \Nette\SmartObject;
16
17
  public const ACTION_NAME = "skill_special";
18
19
  public function getName(): string {
20 1
    return self::ACTION_NAME;
21
  }
22
23
  public function getPriority(): int {
24 1
    return 1000;
25
  }
26
27
  public function shouldUse(CombatBase $combat, Character $character): bool {
28
    $attackTarget = $combat->selectAttackTarget($character);
29
    if($attackTarget === null) {
30
      return false;
31
    }
32
    if(count($character->usableSkills) < 1) {
33
      return false;
34
    }
35
    return ($character->usableSkills[0] instanceof CharacterSpecialSkill);
36
  }
37
38
  protected function doSingleTarget(Character $character1, Character $target, CharacterSpecialSkill $skill, CombatBase $combat): void {
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 135 characters
Loading history...
39 1
    $result = [
40 1
      "result" => true, "amount" => 0, "action" => $this->getName(), "name" => $skill->skill->name,
41 1
      "character1" => $character1, "character2" => $target,
42
    ];
43 1
    $effect = new CharacterEffect([
44 1
      "id" => "skill{$skill->skill->id}Effect",
45 1
      "type" => $skill->skill->type,
46 1
      "stat" => ((in_array($skill->skill->type, Skill::NO_STAT_TYPES, true)) ? null : $skill->skill->stat),
47 1
      "value" => $skill->value,
48
      "valueAbsolute" => false,
49 1
      "duration" => $skill->skill->duration,
50
    ]);
51 1
    $target->effects[] = $effect;
52 1
    $combat->log->log($result);
53 1
    $skill->resetCooldown();
54 1
  }
55
56
  /**
57
   * @throws NotImplementedException
58
   */
59
  public function do(CombatBase $combat, Character $character): void {
60
    /** @var CharacterSpecialSkill $skill */
61 1
    $skill = $character->usableSkills[0];
62 1
    $targets = match($skill->skill->target) {
63
      Skill::TARGET_ENEMY => [$combat->selectAttackTarget($character)],
64 1
      Skill::TARGET_SELF => [$character],
65
      Skill::TARGET_PARTY => $combat->getTeam($character)->toArray(),
66
      Skill::TARGET_ENEMY_PARTY => $combat->getEnemyTeam($character)->toArray(),
67
      default => throw new NotImplementedException("Target {$skill->skill->target} for special skills is not implemented."),
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 124 characters
Loading history...
68
    };
69 1
    foreach($targets as $target) {
70 1
      $this->doSingleTarget($character, $target, $skill, $combat);
71
    }
72 1
  }
73
}
74
?>