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

CombatLogEntry   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 81.69%

Importance

Changes 0
Metric Value
wmc 23
eloc 84
dl 0
loc 140
ccs 58
cts 71
cp 0.8169
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getCharacter1() 0 2 1
A __toString() 0 2 1
A getName() 0 2 1
A configureOptions() 0 15 1
A getAllowedActions() 0 2 1
A getAmount() 0 2 1
A getAction() 0 2 1
C parse() 0 44 12
A __construct() 0 12 1
A getMessage() 0 2 1
A isResult() 0 2 1
A getCharacter2() 0 2 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 CombatLogEntry {
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
    $resolver = new OptionsResolver();
50 1
    $this->configureOptions($resolver);
51 1
    $action = $resolver->resolve($action);
52 1
    $this->translator = $translator;
53 1
    $this->action = $action["action"];
1 ignored issue
show
Bug introduced by
The property action is declared read-only in HeroesofAbenez\Combat\CombatLogEntry.
Loading history...
54 1
    $this->result = $action["result"];
1 ignored issue
show
Bug introduced by
The property result is declared read-only in HeroesofAbenez\Combat\CombatLogEntry.
Loading history...
55 1
    $this->amount = $action["amount"];
1 ignored issue
show
Bug introduced by
The property amount is declared read-only in HeroesofAbenez\Combat\CombatLogEntry.
Loading history...
56 1
    $this->character1 = $action["character1"];
1 ignored issue
show
Bug introduced by
The property character1 is declared read-only in HeroesofAbenez\Combat\CombatLogEntry.
Loading history...
57 1
    $this->character2 = $action["character2"];
1 ignored issue
show
Bug introduced by
The property character2 is declared read-only in HeroesofAbenez\Combat\CombatLogEntry.
Loading history...
58 1
    $this->name = $action["name"];
1 ignored issue
show
Bug introduced by
The property name is declared read-only in HeroesofAbenez\Combat\CombatLogEntry.
Loading history...
59 1
    $this->parse();
60 1
  }
61
62
  protected function configureOptions(OptionsResolver $resolver): void {
63 1
    $requiredStats = ["action", "result", "character1", "character2",];
64 1
    $resolver->setDefined(["amount", "name",]);
65 1
    $resolver->setRequired($requiredStats);
66 1
    $resolver->setAllowedTypes("action", "string");
67 1
    $resolver->setAllowedValues("action", function(string $value) {
68 1
      return in_array($value, $this->getAllowedActions(), true);
69 1
    });
70 1
    $resolver->setAllowedTypes("result", "bool");
71 1
    $resolver->setAllowedTypes("amount", "integer");
72 1
    $resolver->setDefault("amount", 0);
73 1
    $resolver->setAllowedTypes("name", "string");
74 1
    $resolver->setDefault("name", "");
75 1
    $resolver->setAllowedTypes("character1", Character::class);
76 1
    $resolver->setAllowedTypes("character2", Character::class);
77 1
  }
78
79
  /**
80
   * @return string[]
81
   */
82
  protected function getAllowedActions(): array {
83 1
    return Constants::getConstantsValues(static::class, "ACTION_");
84
  }
85
  
86
  public function getCharacter1(): Character {
87
    return $this->character1;
88
  }
89
  
90
  public function getCharacter2(): Character {
91
    return $this->character2;
92
  }
93
  
94
  public function getAction(): string {
95
    return $this->action;
96
  }
97
  
98
  public function getName(): string {
99
    return $this->name;
100
  }
101
  
102
  public function isResult(): bool {
103
    return $this->result;
104
  }
105
  
106
  public function getAmount(): int {
107
    return $this->amount;
108
  }
109
  
110
  public function getMessage(): string {
111
    return $this->message;
112
  }
113
  
114
  protected function parse(): void {
0 ignored issues
show
introduced by
Function's cyclomatic complexity (12) exceeds 10; consider refactoring the function
Loading history...
115 1
    $character1 = $this->character1->name;
116 1
    $character2 = $this->character2->name;
117 1
    $text = "";
118 1
    switch($this->action) {
119 1
      case static::ACTION_ATTACK:
120 1
        if($this->result) {
121 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...
122 1
          if($this->character2->hitpoints < 1) {
123 1
            $text .= $this->translator->translate("combat.log.characterFalls");
124
          }
125
        } else {
126 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...
127
        }
128 1
        break;
129 1
      case static::ACTION_SKILL_ATTACK:
130 1
        if($this->result) {
131 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...
132 1
          if($this->character2->hitpoints < 1) {
133 1
            $text .= $this->translator->translate("combat.log.characterFalls");
134
          }
135
        } else {
136 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...
137
        }
138 1
        break;
139 1
      case static::ACTION_SKILL_SPECIAL:
140 1
        if($this->result) {
141 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...
142
        } else {
143
          $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...
144
        }
145 1
        break;
146 1
      case static::ACTION_HEALING:
147
        if($this->result) {
148
          $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...
149
        } else {
150
          $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...
151
        }
152
        break;
153 1
      case static::ACTION_POISON:
154 1
        $text = $this->translator->translate("combat.log.poison", $this->amount, ["character1" => $character1]);
155 1
        break;
156
    }
157 1
    $this->message =  $text;
1 ignored issue
show
Bug introduced by
The property message is declared read-only in HeroesofAbenez\Combat\CombatLogEntry.
Loading history...
158 1
  }
159
  
160
  public function __toString(): string {
161
    return $this->message;
162
  }
163
}
164
?>