Completed
Push — master ( 0dc17b...133d40 )
by Jakub
02:21
created

Equipment::getMaxDurability()   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
eloc 1
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
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 $id
16
 * @property-read string $name
17
 * @property-read string $slot
18
 * @property-read string|null $type
19
 * @property-read int $rawStrength
20
 * @property-read int $strength
21
 * @property bool $worn Is the item worn?
22
 * @property-read int $maxDurability
23
 * @property int $durability
24
 */
25 1
class Equipment implements ICharacterEffectsProvider {
26 1
  use \Nette\SmartObject;
27
  
28
  public const SLOT_WEAPON = "weapon";
29
  public const SLOT_ARMOR = "armor";
30
  public const SLOT_SHIELD = "shield";
31
  public const SLOT_AMULET = "amulet";
32
  public const SLOT_HELMET = "helmet";
33
  public const SLOT_RING = "ring";
34
  
35
  /** @var int */
36
  protected $id;
37
  /** @var string */
38
  protected $name;
39
  /** @var string */
40
  protected $slot;
41
  /** @var string|null */
42
  protected $type;
43
  /** @var int */
44
  protected $rawStrength;
45
  /** @var bool */
46
  protected $worn;
47
  /** @var int */
48
  protected $durability;
49
  /** @var int */
50
  protected $maxDurability;
51
  
52
  public function __construct(array $data) {
53 1
    $resolver = new OptionsResolver();
54 1
    $this->configureOptions($resolver);
55 1
    $data = $resolver->resolve($data);
56 1
    $this->id = $data["id"];
57 1
    $this->name = $data["name"];
58 1
    $this->slot = $data["slot"];
59 1
    $this->type = $data["type"];
60 1
    $this->rawStrength = $data["strength"];
61 1
    $this->worn = $data["worn"];
62 1
    $this->maxDurability = $data["maxDurability"];
63 1
    $this->durability = $data["durability"];
64 1
  }
65
  
66
  protected function configureOptions(OptionsResolver $resolver): void {
67 1
    $allStats = ["id", "name", "slot", "type", "strength", "worn",];
68 1
    $resolver->setRequired($allStats);
69 1
    $resolver->setAllowedTypes("id", "integer");
70 1
    $resolver->setAllowedTypes("name", "string");
71 1
    $resolver->setAllowedTypes("slot", "string");
72 1
    $resolver->setAllowedValues("slot", function(string $value) {
73 1
      return in_array($value, $this->getAllowedSlots(), true);
74 1
    });
75 1
    $resolver->setAllowedTypes("type", "null");
76 1
    $resolver->setDefault("type", null);
77 1
    $resolver->setAllowedTypes("strength", "integer");
78 1
    $resolver->setAllowedValues("strength", function(int $value) {
79 1
      return ($value >= 0);
80 1
    });
81 1
    $resolver->setAllowedTypes("worn", "boolean");
82 1
    $resolver->setDefault("maxDurability", 0);
83 1
    $resolver->setAllowedTypes("maxDurability", "integer");
84 1
    $resolver->setAllowedValues("maxDurability", function(int $value) {
85 1
      return ($value >= 0);
86 1
    });
87 1
    $resolver->setDefault("durability", function(Options $options) {
88 1
      return $options["maxDurability"];
89 1
    });
90 1
    $resolver->setAllowedTypes("durability", "integer");
91 1
  }
92
  
93
  protected function getAllowedSlots(): array {
94 1
    return Constants::getConstantsValues(static::class, "SLOT_");
95
  }
96
  
97
  public function getId(): int {
98 1
    return $this->id;
99
  }
100
  
101
  public function getName(): string {
102
    return $this->name;
103
  }
104
  
105
  public function getSlot(): string {
106
    return $this->slot;
107
  }
108
  
109
  public function getType(): ?string {
110
    return $this->type;
111
  }
112
113
  public function getRawStrength(): int {
114 1
    return $this->rawStrength;
115
  }
116
117
  public function getStrength(): int {
118 1
    if($this->durability >= $this->maxDurability * 0.7) {
119 1
      return $this->rawStrength;
120 1
    } elseif($this->durability >= $this->maxDurability / 2) {
121 1
      return (int) ($this->rawStrength * 0.75);
122 1
    } elseif($this->durability >= $this->maxDurability / 4) {
123 1
      return (int) ($this->rawStrength / 2);
124 1
    } elseif($this->durability >= $this->maxDurability / 10) {
125 1
      return (int) ($this->rawStrength / 4);
126
    }
127 1
    return 0;
128
  }
129
  
130
  public function isWorn(): bool {
131 1
    return $this->worn;
132
  }
133
  
134
  public function setWorn(bool $worn): void {
135 1
    $this->worn = $worn;
136 1
  }
137
138
  public function getMaxDurability(): int {
139 1
    return $this->maxDurability;
140
  }
141
142
  public function getDurability(): int {
143 1
    return $this->durability;
144
  }
145
146
  public function setDurability(int $durability): void {
147 1
    $this->durability = Numbers::range($durability, 0, $this->maxDurability);
148 1
  }
149
150
  protected function getDeployParams(): array {
151
    $stat = [
152 1
      static::SLOT_WEAPON => Character::STAT_DAMAGE, static::SLOT_ARMOR => Character::STAT_DEFENSE,
153 1
      static::SLOT_HELMET => Character::STAT_MAX_HITPOINTS, static::SLOT_SHIELD => Character::STAT_DODGE,
154 1
      static::SLOT_AMULET => Character::STAT_INITIATIVE, static::SLOT_RING => Character::STAT_HIT,
155
    ];
156
    $return = [
157 1
      "id" => "equipment" . $this->id . "bonusEffect",
158 1
      "type" => SkillSpecial::TYPE_BUFF,
159 1
      "stat" => $stat[$this->slot],
160 1
      "value" => $this->strength,
161
      "valueAbsolute" => true,
162 1
      "duration" => CharacterEffect::DURATION_COMBAT,
163
    ];
164 1
    return $return;
165
  }
166
  
167
  public function getCombatEffects(): array {
168 1
    if(!$this->worn) {
169 1
      return [];
170
    }
171 1
    return [new CharacterEffect($this->getDeployParams())];
172
  }
173
}
174
?>