Passed
Push — master ( 98f021...0d9edd )
by Jakub
01:48
created

Equipment::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
use Nexendrie\Utils\Constants;
8
9
/**
10
 * Equipment
11
 *
12
 * @author Jakub Konečný
13
 * @property-read int $id
14
 * @property-read string $name
15
 * @property-read string $slot
16
 * @property-read string|null $type
17
 * @property-read int $strength
18
 * @property bool $worn Is the item worn?
19
 */
20 1
class Equipment implements ICharacterEffectsProvider {
21 1
  use \Nette\SmartObject;
22
  
23
  public const SLOT_WEAPON = "weapon";
24
  public const SLOT_ARMOR = "armor";
25
  public const SLOT_SHIELD = "shield";
26
  public const SLOT_AMULET = "amulet";
27
  public const SLOT_HELMET = "helmet";
28
  
29
  /** @var int */
30
  protected $id;
31
  /** @var string */
32
  protected $name;
33
  /** @var string */
34
  protected $slot;
35
  /** @var string|null */
36
  protected $type;
37
  /** @var int */
38
  protected $strength;
39
  /** @var bool */
40
  protected $worn;
41
  
42
  public function __construct(array $data) {
43 1
    $resolver = new OptionsResolver();
44 1
    $this->configureOptions($resolver);
45 1
    $data = $resolver->resolve($data);
46 1
    $this->id = $data["id"];
47 1
    $this->name = $data["name"];
48 1
    $this->slot = $data["slot"];
49 1
    $this->type = $data["type"];
50 1
    $this->strength = $data["strength"];
51 1
    $this->worn = $data["worn"];
52 1
  }
53
  
54
  protected function configureOptions(OptionsResolver $resolver): void {
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) {
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) {
67 1
      return ($value >= 0);
68 1
    });
69 1
    $resolver->setAllowedTypes("worn", "boolean");
70 1
  }
71
  
72
  protected function getAllowedSlots(): array {
73 1
    return Constants::getConstantsValues(static::class, "SLOT_");
74
  }
75
  
76
  public function getId(): int {
77 1
    return $this->id;
78
  }
79
  
80
  public function getName(): string {
81
    return $this->name;
82
  }
83
  
84
  public function getSlot(): string {
85
    return $this->slot;
86
  }
87
  
88
  public function getType(): ?string {
89
    return $this->type;
90
  }
91
  
92
  public function getStrength(): int {
93
    return $this->strength;
94
  }
95
  
96
  public function isWorn(): bool {
97 1
    return $this->worn;
98
  }
99
  
100
  public function setWorn(bool $worn): void {
101 1
    $this->worn = $worn;
102 1
  }
103
  
104
  protected function getDeployParams(): array {
105
    $stat = [
106 1
      static::SLOT_WEAPON => Character::STAT_DAMAGE, static::SLOT_ARMOR => Character::STAT_DEFENSE,
107 1
      static::SLOT_HELMET => Character::STAT_MAX_HITPOINTS, static::SLOT_SHIELD => Character::STAT_DODGE,
108 1
      static::SLOT_AMULET => Character::STAT_INITIATIVE,
109
    ];
110
    $return = [
111 1
      "id" => "equipment" . $this->id . "bonusEffect",
112 1
      "type" => SkillSpecial::TYPE_BUFF,
113 1
      "stat" => $stat[$this->slot],
114 1
      "value" => $this->strength,
115 1
      "source" => CharacterEffect::SOURCE_EQUIPMENT,
116 1
      "duration" => CharacterEffect::DURATION_COMBAT,
117
    ];
118 1
    return $return;
119
  }
120
  
121
  public function getCombatEffects(): array {
122 1
    if(!$this->worn) {
123 1
      return [];
124
    }
125 1
    return [new CharacterEffect($this->getDeployParams())];
126
  }
127
}
128
?>