Passed
Push — master ( 0d8198...7c8b7e )
by Jakub
03:48
created

CharacterEffect::increaseHitpoints()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
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[] = [$this, "increaseHitpoints"];
83 1
    $this->onRemove[] = [$this, "decreaseHitpoints"];
84 1
  }
85
  
86
  protected function getAllowedStats(): array {
87 1
    $stats = Constants::getConstantsValues(SkillSpecial::class, "STAT_");
88 1
    $stats = array_merge($stats, ["strength", "dexterity", "constitution", "intelligence", "charisma",]);
89 1
    return $stats;
90
  }
91
  
92
  /**
93
   * @return string[]
94
   */
95
  protected function getAllowedSources(): array {
96 1
    return Constants::getConstantsValues(static::class, "SOURCE_");
97
  }
98
  
99
  /**
100
   * @return string[]
101
   */
102
  protected function getAllowedTypes(): array {
103 1
    return Constants::getConstantsValues(SkillSpecial::class, "TYPE_");
104
  }
105
  
106
  /**
107
   * @return string[]
108
   */
109
  protected function getDurations(): array {
110 1
    return Constants::getConstantsValues(static::class, "DURATION_");
111
  }
112
  
113
  public function getId(): string {
114 1
    return $this->id;
115
  }
116
  
117
  public function getType(): string {
118 1
    return $this->type;
119
  }
120
  
121
  public function getStat(): string {
122 1
    return $this->stat;
123
  }
124
  
125
  public function getValue(): int {
126 1
    return $this->value;
127
  }
128
  
129
  public function getSource(): string {
130 1
    return $this->source;
131
  }
132
  
133
  /**
134
   * @return int|string
135
   */
136
  public function getDuration() {
137 1
    return $this->duration;
138
  }
139
  
140
  /**
141
   * @param string|int $value
142
   * @throws \InvalidArgumentException
143
   */
144
  public function setDuration($value) {
145
    if(!is_int($value) AND !in_array($value, $this->getDurations(), true)) {
146
      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...
147
    }
148
    $this->duration = $value;
149
  }
150
  
151
  public function increaseHitpoints(Character $character, CharacterEffect $effect): void {
152 1
    if($effect->stat !== SkillSpecial::STAT_HITPOINTS) {
153 1
      return;
154
    }
155 1
    $character->heal($effect->value);
156 1
  }
157
  
158
  public function decreaseHitpoints(Character $character, CharacterEffect $effect): void {
159 1
    if($effect->stat !== SkillSpecial::STAT_HITPOINTS) {
160 1
      return;
161
    }
162 1
    $character->harm($effect->value);
163 1
  }
164
}
165
?>