Equipment::getDeployParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 12
c 3
b 0
f 0
dl 0
loc 16
rs 9.8666
ccs 11
cts 11
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
use Symfony\Component\OptionsResolver\Options;
8
use Nexendrie\Utils\Constants;
9
use Nexendrie\Utils\Numbers;
10
11
/**
12
 * Equipment
13
 *
14
 * @author Jakub Konečný
15
 * @property-read int $strength
16
 * @property int $durability
17
 */
18 1
class Equipment implements ICharacterEffectsProvider
19
{
20
    use \Nette\SmartObject;
21
22
    public const SLOT_WEAPON = "weapon";
23
    public const SLOT_ARMOR = "armor";
24
    public const SLOT_SHIELD = "shield";
25
    public const SLOT_AMULET = "amulet";
26
    public const SLOT_HELMET = "helmet";
27
    public const SLOT_RING = "ring";
28
29
    public readonly int $id;
30
    public readonly string $name;
31
    public readonly string $slot;
32
    public readonly ?string $type;
33
    public readonly int $rawStrength;
34
    public bool $worn;
35
    protected int $durability;
36
    public readonly int $maxDurability;
37
38
    public function __construct(array $data)
39
    {
40 1
        $resolver = new OptionsResolver();
41 1
        $this->configureOptions($resolver);
42 1
        $data = $resolver->resolve($data);
43 1
        $this->id = $data["id"];
44 1
        $this->name = $data["name"];
45 1
        $this->slot = $data["slot"];
46 1
        $this->type = $data["type"];
47 1
        $this->rawStrength = $data["strength"];
48 1
        $this->worn = $data["worn"];
49 1
        $this->maxDurability = $data["maxDurability"];
50 1
        $this->durability = $data["durability"];
51 1
    }
52
53
    protected function configureOptions(OptionsResolver $resolver): void
54
    {
55 1
        $allStats = ["id", "name", "slot", "type", "strength", "worn",];
56 1
        $resolver->setRequired($allStats);
57 1
        $resolver->setAllowedTypes("id", "integer");
58 1
        $resolver->setAllowedTypes("name", "string");
59 1
        $resolver->setAllowedTypes("slot", "string");
60 1
        $resolver->setAllowedValues("slot", function (string $value): bool {
61 1
            return in_array($value, $this->getAllowedSlots(), true);
62 1
        });
63 1
        $resolver->setAllowedTypes("type", "null");
64 1
        $resolver->setDefault("type", null);
65 1
        $resolver->setAllowedTypes("strength", "integer");
66 1
        $resolver->setAllowedValues("strength", function (int $value): bool {
67 1
            return ($value >= 0);
68 1
        });
69 1
        $resolver->setAllowedTypes("worn", "boolean");
70 1
        $resolver->setDefault("maxDurability", 0);
71 1
        $resolver->setAllowedTypes("maxDurability", "integer");
72 1
        $resolver->setAllowedValues("maxDurability", function (int $value): bool {
73 1
            return ($value >= 0);
74 1
        });
75 1
        $resolver->setDefault("durability", function (Options $options) {
76 1
            return $options["maxDurability"];
77 1
        });
78 1
        $resolver->setAllowedTypes("durability", "integer");
79 1
    }
80
81
    protected function getAllowedSlots(): array
82
    {
83 1
        return Constants::getConstantsValues(static::class, "SLOT_");
84
    }
85
86
    protected function getStrength(): int
87
    {
88 1
        if ($this->durability >= $this->maxDurability * 0.7) {
89 1
            return $this->rawStrength;
90 1
        } elseif ($this->durability >= $this->maxDurability / 2) {
91 1
            return (int) ($this->rawStrength * 0.75);
92 1
        } elseif ($this->durability >= $this->maxDurability / 4) {
93 1
            return (int) ($this->rawStrength / 2);
94 1
        } elseif ($this->durability >= $this->maxDurability / 10) {
95 1
            return (int) ($this->rawStrength / 4);
96
        }
97 1
        return 0;
98
    }
99
100
    protected function getDurability(): int
101
    {
102 1
        return $this->durability;
103
    }
104
105
    protected function setDurability(int $durability): void
106
    {
107 1
        $this->durability = Numbers::range($durability, 0, $this->maxDurability);
108 1
    }
109
110
    protected function getDeployParams(): array
111
    {
112 1
        $stat = [
113 1
            static::SLOT_WEAPON => Character::STAT_DAMAGE, static::SLOT_ARMOR => Character::STAT_DEFENSE,
114 1
            static::SLOT_HELMET => Character::STAT_MAX_HITPOINTS, static::SLOT_SHIELD => Character::STAT_DODGE,
115 1
            static::SLOT_AMULET => Character::STAT_INITIATIVE, static::SLOT_RING => Character::STAT_HIT,
116
        ];
117 1
        $return = [
118 1
            "id" => "equipment" . $this->id . "bonusEffect",
119 1
            "type" => SkillSpecial::TYPE_BUFF,
120 1
            "stat" => $stat[$this->slot],
121 1
            "value" => $this->strength,
122
            "valueAbsolute" => true,
123 1
            "duration" => CharacterEffect::DURATION_COMBAT,
124
        ];
125 1
        return $return;
126
    }
127
128
    public function getCombatEffects(): array
129
    {
130 1
        if (!$this->worn) {
131 1
            return [];
132
        }
133 1
        return [new CharacterEffect($this->getDeployParams())];
134
    }
135
}
136