Passed
Push — master ( bc69e6...465240 )
by Jakub
12:35
created

Equipment   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 94.37%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 19
eloc 80
dl 0
loc 131
rs 10
c 5
b 0
f 2
ccs 67
cts 71
cp 0.9437

14 Methods

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