Completed
Push — master ( 97de22...5eb6e2 )
by Jakub
02:02
created

CombatAction::getAllowedActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 2
ccs 1
cts 1
cp 1
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nette\Localization\ITranslator;
7
use Nexendrie\Utils\Constants;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
/**
11
 * Data structure for combat action
12
 *
13
 * @author Jakub Konečný
14
 * @property-read Character $character1
15
 * @property-read Character $character2
16
 * @property-read string $action
17
 * @property-read string $name
18
 * @property-read bool $result
19
 * @property-read int $amount
20
 * @property-read string $message
21
 */
22 1
class CombatAction {
23 1
  use \Nette\SmartObject;
24
  
25
  public const ACTION_ATTACK = "attack";
26
  public const ACTION_SKILL_ATTACK = "skill_attack";
27
  public const ACTION_SKILL_SPECIAL = "skill_special";
28
  public const ACTION_HEALING = "healing";
29
  public const ACTION_POISON = "poison";
30
  
31
  /** @var ITranslator */
32
  protected $translator;
33
  /** @var Character */
34
  protected $character1;
35
  /** @var Character */
36
  protected $character2;
37
  /** @var  string */
38
  protected $action;
39
  /** @var string */
40
  protected $name;
41
  /** @var bool */
42
  protected $result;
43
  /** @var int */
44
  protected $amount;
45
  /** @var string */
46
  protected $message;
47
  
48
  public function __construct(ITranslator $translator, array $action) {
49 1
    $requiredStats = ["action", "result", "character1", "character2",];
50 1
    $resolver = new OptionsResolver();
51 1
    $resolver->setDefined(["amount", "name",]);
52 1
    $resolver->setRequired($requiredStats);
53 1
    $resolver->setAllowedTypes("action", "string");
54 1
    $resolver->setAllowedValues("action", function(string $value) {
55 1
      return in_array($value, $this->getAllowedActions(), true);
56 1
    });
57 1
    $resolver->setAllowedTypes("result", "bool");
58 1
    $resolver->setAllowedTypes("amount", "integer");
59 1
    $resolver->setDefault("amount", 0);
60 1
    $resolver->setAllowedTypes("name", "string");
61 1
    $resolver->setDefault("name", "");
62 1
    $resolver->setAllowedTypes("character1", Character::class);
63 1
    $resolver->setAllowedTypes("character2", Character::class);
64 1
    $action = $resolver->resolve($action);
65 1
    $this->translator = $translator;
66 1
    $this->action = $action["action"];
67 1
    $this->result = $action["result"];
68 1
    $this->amount = $action["amount"];
69 1
    $this->character1 = $action["character1"];
70 1
    $this->character2 = $action["character2"];
71 1
    $this->name = $action["name"];
72 1
    $this->parse();
73 1
  }
74
  
75
  /**
76
   * @return string[]
77
   */
78
  protected function getAllowedActions(): array {
79 1
    return Constants::getConstantsValues(static::class, "ACTION_");
80
  }
81
  
82
  public function getCharacter1(): Character {
83
    return $this->character1;
84
  }
85
  
86
  public function getCharacter2(): Character {
87
    return $this->character2;
88
  }
89
  
90
  public function getAction(): string {
91
    return $this->action;
92
  }
93
  
94
  public function getName(): string {
95
    return $this->name;
96
  }
97
  
98
  public function isResult(): bool {
99
    return $this->result;
100
  }
101
  
102
  public function getAmount(): int {
103
    return $this->amount;
104
  }
105
  
106
  public function getMessage(): string {
107
    return $this->message;
108
  }
109
  
110
  protected function parse(): void {
0 ignored issues
show
introduced by
Function's cyclomatic complexity (12) exceeds 10; consider refactoring the function
Loading history...
111 1
    $character1 = $this->character1->name;
112 1
    $character2 = $this->character2->name;
113 1
    $text = "";
114 1
    switch($this->action) {
115 1
      case static::ACTION_ATTACK:
116 1
        if($this->result) {
117 1
          $text = $this->translator->translate("combat.log.attackHits", $this->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 147 characters
Loading history...
118 1
          if($this->character2->hitpoints < 1) {
119 1
            $text .= $this->translator->translate("combat.log.characterFalls");
120
          }
121
        } else {
122 1
          $text = $this->translator->translate("combat.log.attackFails", $this->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 148 characters
Loading history...
123
        }
124 1
        break;
125 1
      case static::ACTION_SKILL_ATTACK:
126 1
        if($this->result) {
127 1
          $text = $this->translator->translate("combat.log.specialAttackHits", $this->amount, ["character1" => $character1, "character2" => $character2, "name" => $this->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 177 characters
Loading history...
128 1
          if($this->character2->hitpoints < 1) {
129 1
            $text .= $this->translator->translate("combat.log.characterFalls");
130
          }
131
        } else {
132 1
          $text = $this->translator->translate("combat.log.specialAttackFails", $this->amount, ["character1" => $character1, "character2" => $character2, "name" => $this->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 178 characters
Loading history...
133
        }
134 1
        break;
135 1
      case static::ACTION_SKILL_SPECIAL:
136 1
        if($this->result) {
137 1
          $text = $this->translator->translate("combat.log.specialSkillSuccess", 0, ["character1" => $character1, "character2" => $character2, "name" => $this->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 167 characters
Loading history...
138
        } else {
139
          $text = $this->translator->translate("combat.log.specialSKillFailure", 0, ["character1" => $character1, "character2" => $character2, "name" => $this->name]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 167 characters
Loading history...
140
        }
141 1
        break;
142 1
      case static::ACTION_HEALING:
143 1
        if($this->result) {
144 1
          $text = $this->translator->translate("combat.log.healingSuccess", $this->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 151 characters
Loading history...
145
        } else {
146 1
          $text = $this->translator->translate("combat.log.healingFailure", $this->amount, ["character1" => $character1, "character2" => $character2]);
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 151 characters
Loading history...
147
        }
148 1
        break;
149 1
      case static::ACTION_POISON:
150 1
        $text = $this->translator->translate("combat.log.poison", $this->amount, ["character1" => $character1]);
151 1
        break;
152
    }
153 1
    $this->message =  $text;
154 1
  }
155
  
156
  public function __toString(): string {
157
    return $this->message;
158
  }
159
}
160
?>