Completed
Push — master ( ce79d3...3d8890 )
by Jakub
02:31
created

Equipment::setWorn()   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
dl 0
loc 2
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace HeroesofAbenez\Combat;
5
6
use Symfony\Component\OptionsResolver\OptionsResolver,
7
    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 ICharacterEffectProvider {
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
  public const TYPE_SWORD = "sword";
29
  public const TYPE_AXE = "axe";
30
  public const TYPE_CLUB = "club";
31
  public const TYPE_DAGGER = "dagger";
32
  public const TYPE_SPEAR = "spear";
33
  public const TYPE_STAFF = "staff";
34
  public const TYPE_BOW = "bow";
35
  public const TYPE_CROSSBOW = "crossbow";
36
  public const TYPE_THROWING_KNIFE = "throwing knife";
37
  
38
  /** @var int */
39
  protected $id;
40
  /** @var string */
41
  protected $name;
42
  /** @var string */
43
  protected $slot;
44
  /** @var string|NULL */
45
  protected $type;
46
  /** @var int */
47
  protected $strength;
48
  /** @var bool */
49
  protected $worn;
50
  
51
  public function __construct(array $data) {
52 1
    $allStats = ["id", "name", "slot", "type", "strength", "worn",];
53 1
    $resolver = new OptionsResolver();
54 1
    $resolver->setRequired($allStats);
55 1
    $resolver->setAllowedTypes("id", "integer");
56 1
    $resolver->setAllowedTypes("name", "string");
57 1
    $resolver->setAllowedTypes("slot", "string");
58 1
    $resolver->setAllowedValues("slot", function(string $value) {
59 1
      return in_array($value, $this->getAllowedSlots(), true);
60 1
    });
61 1
    $resolver->setAllowedTypes("type", ["string", "null"]);
62 1
    $resolver->setAllowedValues("type", function(?string $value) {
63 1
      return is_null($value) OR in_array($value, $this->getAllowedTypes(), true);
64 1
    });
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
    $data = $resolver->resolve($data);
71 1
    $this->id = $data["id"];
72 1
    $this->name = $data["name"];
73 1
    $this->slot = $data["slot"];
74 1
    $this->type = $data["type"];
75 1
    $this->strength = $data["strength"];
76 1
    $this->worn = $data["worn"];
77 1
  }
78
  
79
  protected function getAllowedSlots(): array {
80 1
    return Constants::getConstantsValues(static::class, "SLOT_");
81
  }
82
  
83
  protected function getAllowedTypes(): array {
84 1
    return Constants::getConstantsValues(static::class, "TYPE_");
85
  }
86
  
87
  public function getId(): int {
88 1
    return $this->id;
89
  }
90
  
91
  public function getName(): string {
92
    return $this->name;
93
  }
94
  
95
  public function getSlot(): string {
96 1
    return $this->slot;
97
  }
98
  
99
  public function getType(): ?string {
100 1
    return $this->type;
101
  }
102
  
103
  public function getStrength(): int {
104
    return $this->strength;
105
  }
106
  
107
  public function isWorn(): bool {
108 1
    return $this->worn;
109
  }
110
  
111
  public function setWorn(bool $worn): void {
112 1
    $this->worn = $worn;
113 1
  }
114
  
115
  protected function getDeployParams(): array {
116
    $stat = [
117 1
      static::SLOT_WEAPON => SkillSpecial::STAT_DAMAGE, static::SLOT_ARMOR => SkillSpecial::STAT_DEFENSE,
118 1
      static::SLOT_HELMET => SkillSpecial::STAT_HITPOINTS, static::SLOT_SHIELD => SkillSpecial::STAT_DODGE,
119 1
      static::SLOT_AMULET => SkillSpecial::STAT_INITIATIVE,
120
    ];
121
    $return = [
122 1
      "id" => "equipment" . $this->id . "bonusEffect",
123 1
      "type" => "buff",
124 1
      "stat" => $stat[$this->slot],
125 1
      "value" => $this->strength,
126 1
      "source" => CharacterEffect::SOURCE_EQUIPMENT,
127 1
      "duration" => CharacterEffect::DURATION_COMBAT,
128
    ];
129 1
    return $return;
130
  }
131
  
132
  public function toCombatEffect(): ?CharacterEffect {
133 1
    if(!$this->worn) {
134 1
      return NULL;
135
    }
136 1
    return new CharacterEffect($this->getDeployParams());
137
  }
138
}
139
?>