Completed
Push — master ( 3d8890...b672a0 )
by Jakub
02:05
created

CharacterEffect::getId()   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
rs 10
c 0
b 0
f 0
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 Nexendrie\Utils\Constants,
7
    Symfony\Component\OptionsResolver\OptionsResolver;
8
9
/**
10
 * Data structure for effect on character
11
 *
12
 * @author Jakub Konečný
13
 * @property-read string $id
14
 * @property-read string $type
15
 * @property-read string $stat
16
 * @property-read int $value
17
 * @property-read string $source
18
 * @property int|string $duration
19
 * @method void onApply(Character $character, CharacterEffect $effect)
20
 * @method void onRemove(Character $character, CharacterEffect $effect)
21
 */
22 1
class CharacterEffect {
23 1
  use \Nette\SmartObject;
24
  
25
  public const SOURCE_PET = "pet";
26
  public const SOURCE_SKILL = "skill";
27
  public const SOURCE_EQUIPMENT = "equipment";
28
  public const DURATION_COMBAT = "combat";
29
  public const DURATION_FOREVER = "forever";
30
  
31
  /** @var string */
32
  protected $id;
33
  /** @var string */
34
  protected $type;
35
  /** @var string */
36
  protected $stat = "";
37
  /** @var int */
38
  protected $value = 0;
39
  /** @var string */
40
  protected $source;
41
  /** @var int|string */
42
  protected $duration;
43
  /** @var callable[] */
44
  public $onApply = [];
45
  /** @var callable[] */
46
  public $onRemove = [];
47
  
48
  public function __construct(array $effect) {
49 1
    $allStats = ["id", "type", "source", "value", "duration", "stat",];
50 1
    $resolver = new OptionsResolver();
51 1
    $resolver->setRequired($allStats);
52 1
    $resolver->setAllowedTypes("id", "string");
53 1
    $resolver->setAllowedTypes("type", "string");
54 1
    $resolver->setAllowedValues("type", function(string $value) {
55 1
      return in_array($value, $this->getAllowedTypes(), true);
56 1
    });
57 1
    $resolver->setAllowedTypes("stat", "string");
58 1
    $resolver->setDefault("stat", "");
59 1
    $resolver->setAllowedValues("stat", function(string $value) {
60 1
      return $value === "" OR in_array($value, $this->getAllowedStats(), true);
61 1
    });
62 1
    $resolver->setAllowedTypes("source", "string");
63 1
    $resolver->setAllowedValues("source", function(string $value) {
64 1
      return in_array($value, $this->getAllowedSources(), true);
65 1
    });
66 1
    $resolver->setAllowedTypes("value", "integer");
67 1
    $resolver->setDefault("value", 0);
68 1
    $resolver->setAllowedTypes("duration", ["string", "integer"]);
69 1
    $resolver->setAllowedValues("duration", function($value) {
70 1
      return (in_array($value, $this->getDurations(), true)) OR ($value > 0);
71 1
    });
72 1
    $effect = $resolver->resolve($effect);
73 1
    if(!in_array($effect["type"], SkillSpecial::NO_STAT_TYPES, true) AND $effect["stat"] === "") {
74
      throw new \InvalidArgumentException("The option stat with value '' is invalid.");
75
    }
76 1
    $this->id = $effect["id"];
77 1
    $this->type = $effect["type"];
78 1
    $this->stat = $effect["stat"];
79 1
    $this->value = $effect["value"];
80 1
    $this->source = $effect["source"];
81 1
    $this->duration = $effect["duration"];
82 1
    $this->onApply[] = function(Character $character, self $effect) {
83 1
      $character->recalculateStats();
84 1
      if($effect->stat === SkillSpecial::STAT_HITPOINTS) {
85 1
        $character->heal($effect->value);
86
      }
87 1
    };
88 1
    $this->onRemove[] = function(Character $character, self $effect) {
89 1
      $character->recalculateStats();
90 1
      if($effect->stat === SkillSpecial::STAT_HITPOINTS) {
91 1
        $character->harm($effect->value);
92
      }
93 1
    };
94 1
  }
95
  
96
  protected function getAllowedStats(): array {
97 1
    $stats = Constants::getConstantsValues(SkillSpecial::class, "STAT_");
98 1
    $stats = array_merge($stats, ["strength", "dexterity", "constitution", "intelligence", "charisma",]);
99 1
    return $stats;
100
  }
101
  
102
  /**
103
   * @return string[]
104
   */
105
  protected function getAllowedSources(): array {
106 1
    return Constants::getConstantsValues(static::class, "SOURCE_");
107
  }
108
  
109
  /**
110
   * @return string[]
111
   */
112
  protected function getAllowedTypes(): array {
113 1
    return Constants::getConstantsValues(SkillSpecial::class, "TYPE_");
114
  }
115
  
116
  /**
117
   * @return string[]
118
   */
119
  protected function getDurations(): array {
120 1
    return Constants::getConstantsValues(static::class, "DURATION_");
121
  }
122
  
123
  public function getId(): string {
124 1
    return $this->id;
125
  }
126
  
127
  public function getType(): string {
128 1
    return $this->type;
129
  }
130
  
131
  public function getStat(): string {
132 1
    return $this->stat;
133
  }
134
  
135
  public function getValue(): int {
136 1
    return $this->value;
137
  }
138
  
139
  public function getSource(): string {
140 1
    return $this->source;
141
  }
142
  
143
  /**
144
   * @return int|string
145
   */
146
  public function getDuration() {
147 1
    return $this->duration;
148
  }
149
  
150
  /**
151
   * @param string|int $value
152
   * @throws \InvalidArgumentException
153
   */
154
  public function setDuration($value) {
155
    if(!is_int($value) AND !in_array($value, $this->getDurations(), true)) {
156
      throw new \InvalidArgumentException("Invalid value set to CharacterEffect::\$duration. Expected string or integer.");
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 123 characters
Loading history...
157
    }
158
    $this->duration = $value;
159
  }
160
}
161
?>