Completed
Push — master ( 5eb6e2...5eaa75 )
by Jakub
05:29
created

CharacterEffect::setDuration()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.3332

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 2
cts 3
cp 0.6667
c 0
b 0
f 0
rs 10
cc 3
nc 2
nop 1
crap 3.3332
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Nexendrie\Utils\Constants;
7
use 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
    $resolver = new OptionsResolver();
50 1
    $this->configureOptions($resolver);
51 1
    $effect = $resolver->resolve($effect);
52 1
    if(!in_array($effect["type"], SkillSpecial::NO_STAT_TYPES, true) AND $effect["stat"] === "") {
53
      throw new \InvalidArgumentException("The option stat with value '' is invalid.");
54
    }
55 1
    $this->id = $effect["id"];
56 1
    $this->type = $effect["type"];
57 1
    $this->stat = $effect["stat"];
58 1
    $this->value = $effect["value"];
59 1
    $this->source = $effect["source"];
60 1
    $this->duration = $effect["duration"];
61 1
    $this->onApply[] = function(Character $character, self $effect) {
62 1
      $character->recalculateStats();
63 1
      if($effect->stat === Character::STAT_MAX_HITPOINTS) {
64 1
        $character->heal($effect->value);
65
      }
66 1
    };
67 1
    $this->onRemove[] = function(Character $character, self $effect) {
68 1
      $character->recalculateStats();
69 1
      if($effect->stat === Character::STAT_MAX_HITPOINTS) {
70 1
        $character->harm($effect->value);
71
      }
72 1
    };
73 1
  }
74
75
  protected function configureOptions(OptionsResolver $resolver): void {
76 1
    $allStats = ["id", "type", "source", "value", "duration", "stat",];
77 1
    $resolver->setRequired($allStats);
78 1
    $resolver->setAllowedTypes("id", "string");
79 1
    $resolver->setAllowedTypes("type", "string");
80 1
    $resolver->setAllowedValues("type", function(string $value) {
81 1
      return in_array($value, $this->getAllowedTypes(), true);
82 1
    });
83 1
    $resolver->setAllowedTypes("stat", "string");
84 1
    $resolver->setDefault("stat", "");
85 1
    $resolver->setAllowedValues("stat", function(string $value) {
86 1
      return $value === "" OR in_array($value, $this->getAllowedStats(), true);
87 1
    });
88 1
    $resolver->setAllowedTypes("source", "string");
89 1
    $resolver->setAllowedValues("source", function(string $value) {
90 1
      return in_array($value, $this->getAllowedSources(), true);
91 1
    });
92 1
    $resolver->setAllowedTypes("value", "integer");
93 1
    $resolver->setDefault("value", 0);
94 1
    $resolver->setAllowedTypes("duration", ["string", "integer"]);
95 1
    $resolver->setAllowedValues("duration", function($value) {
96 1
      return (in_array($value, $this->getDurations(), true)) OR ($value > 0);
97 1
    });
98 1
  }
99
  
100
  protected function getAllowedStats(): array {
101 1
    $stats = Constants::getConstantsValues(Character::class, "STAT_");
102 1
    $stats = array_merge($stats, ["strength", "dexterity", "constitution", "intelligence", "charisma",]);
103 1
    return $stats;
104
  }
105
  
106
  /**
107
   * @return string[]
108
   */
109
  protected function getAllowedSources(): array {
110 1
    return Constants::getConstantsValues(static::class, "SOURCE_");
111
  }
112
  
113
  /**
114
   * @return string[]
115
   */
116
  protected function getAllowedTypes(): array {
117 1
    return Constants::getConstantsValues(SkillSpecial::class, "TYPE_");
118
  }
119
  
120
  /**
121
   * @return string[]
122
   */
123
  protected function getDurations(): array {
124 1
    return Constants::getConstantsValues(static::class, "DURATION_");
125
  }
126
  
127
  public function getId(): string {
128 1
    return $this->id;
129
  }
130
  
131
  public function getType(): string {
132 1
    return $this->type;
133
  }
134
  
135
  public function getStat(): string {
136 1
    return $this->stat;
137
  }
138
  
139
  public function getValue(): int {
140 1
    return $this->value;
141
  }
142
  
143
  public function getSource(): string {
144 1
    return $this->source;
145
  }
146
  
147
  /**
148
   * @return int|string
149
   */
150
  public function getDuration() {
151 1
    return $this->duration;
152
  }
153
  
154
  /**
155
   * @param string|int $value
156
   * @throws \InvalidArgumentException
157
   */
158
  public function setDuration($value): void {
159 1
    if(!is_int($value) AND !in_array($value, $this->getDurations(), true)) {
160
      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...
161
    }
162 1
    $this->duration = $value;
163 1
  }
164
}
165
?>