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 bool $valueAbsolute |
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 DURATION_COMBAT = "combat"; |
26
|
|
|
public const DURATION_FOREVER = "forever"; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
protected $id; |
30
|
|
|
/** @var string */ |
31
|
|
|
protected $type; |
32
|
|
|
/** @var string */ |
33
|
|
|
protected $stat = ""; |
34
|
|
|
/** @var int */ |
35
|
|
|
protected $value = 0; |
36
|
|
|
/** @var bool */ |
37
|
|
|
protected $valueAbsolute; |
38
|
|
|
/** @var int|string */ |
39
|
|
|
protected $duration; |
40
|
|
|
/** @var callable[] */ |
41
|
|
|
public $onApply = []; |
42
|
|
|
/** @var callable[] */ |
43
|
|
|
public $onRemove = []; |
44
|
|
|
|
45
|
|
|
public function __construct(array $effect) { |
46
|
1 |
|
$resolver = new OptionsResolver(); |
47
|
1 |
|
$this->configureOptions($resolver); |
48
|
1 |
|
$effect = $resolver->resolve($effect); |
49
|
1 |
|
if(!in_array($effect["type"], SkillSpecial::NO_STAT_TYPES, true) AND $effect["stat"] === "") { |
50
|
|
|
throw new \InvalidArgumentException("The option stat with value '' is invalid."); |
51
|
|
|
} |
52
|
1 |
|
$this->id = $effect["id"]; |
53
|
1 |
|
$this->type = $effect["type"]; |
54
|
1 |
|
$this->stat = $effect["stat"]; |
55
|
1 |
|
$this->value = $effect["value"]; |
56
|
1 |
|
$this->valueAbsolute = $effect["valueAbsolute"]; |
57
|
1 |
|
$this->duration = $effect["duration"]; |
58
|
1 |
|
$this->onApply[] = function(Character $character, self $effect) { |
59
|
1 |
|
$character->recalculateStats(); |
60
|
1 |
|
if($effect->stat === Character::STAT_MAX_HITPOINTS) { |
61
|
1 |
|
$character->heal($effect->value); |
62
|
|
|
} |
63
|
1 |
|
}; |
64
|
1 |
|
$this->onRemove[] = function(Character $character, self $effect) { |
65
|
1 |
|
$character->recalculateStats(); |
66
|
1 |
|
if($effect->stat === Character::STAT_MAX_HITPOINTS) { |
67
|
1 |
|
$character->harm($effect->value); |
68
|
|
|
} |
69
|
1 |
|
}; |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
protected function configureOptions(OptionsResolver $resolver): void { |
73
|
1 |
|
$allStats = ["id", "type", "value", "valueAbsolute", "duration", "stat",]; |
74
|
1 |
|
$resolver->setRequired($allStats); |
75
|
1 |
|
$resolver->setAllowedTypes("id", "string"); |
76
|
1 |
|
$resolver->setAllowedTypes("type", "string"); |
77
|
1 |
|
$resolver->setAllowedValues("type", function(string $value) { |
78
|
1 |
|
return in_array($value, $this->getAllowedTypes(), true); |
79
|
1 |
|
}); |
80
|
1 |
|
$resolver->setAllowedTypes("stat", "string"); |
81
|
1 |
|
$resolver->setDefault("stat", ""); |
82
|
1 |
|
$resolver->setAllowedValues("stat", function(string $value) { |
83
|
1 |
|
return $value === "" OR in_array($value, $this->getAllowedStats(), true); |
84
|
1 |
|
}); |
85
|
1 |
|
$resolver->setAllowedTypes("value", "integer"); |
86
|
1 |
|
$resolver->setAllowedTypes("valueAbsolute", "bool"); |
87
|
1 |
|
$resolver->setDefault("value", 0); |
88
|
1 |
|
$resolver->setAllowedTypes("duration", ["string", "integer"]); |
89
|
1 |
|
$resolver->setAllowedValues("duration", function($value) { |
90
|
1 |
|
return (in_array($value, $this->getDurations(), true)) OR ($value > 0); |
91
|
1 |
|
}); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
protected function getAllowedStats(): array { |
95
|
1 |
|
return Constants::getConstantsValues(Character::class, "STAT_"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string[] |
100
|
|
|
*/ |
101
|
|
|
protected function getAllowedSources(): array { |
102
|
|
|
return Constants::getConstantsValues(static::class, "SOURCE_"); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string[] |
107
|
|
|
*/ |
108
|
|
|
protected function getAllowedTypes(): array { |
109
|
1 |
|
return Constants::getConstantsValues(SkillSpecial::class, "TYPE_"); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string[] |
114
|
|
|
*/ |
115
|
|
|
protected function getDurations(): array { |
116
|
1 |
|
return Constants::getConstantsValues(static::class, "DURATION_"); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getId(): string { |
120
|
1 |
|
return $this->id; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getType(): string { |
124
|
1 |
|
return $this->type; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getStat(): string { |
128
|
1 |
|
return $this->stat; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getValue(): int { |
132
|
1 |
|
return $this->value; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function isValueAbsolute(): bool { |
136
|
1 |
|
return $this->valueAbsolute; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return int|string |
141
|
|
|
*/ |
142
|
|
|
public function getDuration() { |
143
|
1 |
|
return $this->duration; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string|int $value |
148
|
|
|
* @throws \InvalidArgumentException |
149
|
|
|
*/ |
150
|
|
|
public function setDuration($value): void { |
151
|
1 |
|
if(!is_int($value) AND !in_array($value, $this->getDurations(), true)) { |
152
|
|
|
throw new \InvalidArgumentException("Invalid value set to CharacterEffect::\$duration. Expected string or integer."); |
|
|
|
|
153
|
|
|
} |
154
|
1 |
|
$this->duration = $value; |
155
|
1 |
|
} |
156
|
|
|
} |
157
|
|
|
?> |