Completed
Push — master ( b2877d...f2d70f )
by Jakub
02:19
created

Equipment   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 95.92%

Importance

Changes 0
Metric Value
wmc 13
dl 0
loc 113
ccs 47
cts 49
cp 0.9592
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A isWorn() 0 2 1
A getType() 0 2 1
A getId() 0 2 1
A getAllowedSlots() 0 2 1
A getSlot() 0 2 1
A getAllowedTypes() 0 2 1
A getName() 0 2 1
A getStrength() 0 2 1
B __construct() 0 26 2
A getDeployParams() 0 15 1
A toCombatEffect() 0 5 2
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-read array $deployParams Deploy params of the equipment
19
 * @property-read bool $worn Is the item worn?
20
 */
21 1
class Equipment implements ICharacterEffectProvider {
22 1
  use \Nette\SmartObject;
23
  
24
  public const SLOT_WEAPON = "weapon";
25
  public const SLOT_ARMOR = "armor";
26
  public const SLOT_SHIELD = "shield";
27
  public const SLOT_AMULET = "amulet";
28
  public const SLOT_HELMET = "helmet";
29
  public const TYPE_SWORD = "sword";
30
  public const TYPE_AXE = "axe";
31
  public const TYPE_CLUB = "club";
32
  public const TYPE_DAGGER = "dagger";
33
  public const TYPE_SPEAR = "spear";
34
  public const TYPE_STAFF = "staff";
35
  public const TYPE_BOW = "bow";
36
  public const TYPE_CROSSBOW = "crossbow";
37
  public const TYPE_THROWING_KNIFE = "throwing knife";
38
  
39
  /** @var int */
40
  protected $id;
41
  /** @var string */
42
  protected $name;
43
  /** @var string */
44
  protected $slot;
45
  /** @var string|NULL */
46
  protected $type;
47
  /** @var int */
48
  protected $strength;
49
  /** @var bool */
50
  protected $worn;
51
  
52
  public function __construct(array $data) {
53 1
    $allStats = ["id", "name", "slot", "type", "strength", "worn",];
54 1
    $resolver = new OptionsResolver();
55 1
    $resolver->setRequired($allStats);
56 1
    $resolver->setAllowedTypes("id", "integer");
57 1
    $resolver->setAllowedTypes("name", "string");
58 1
    $resolver->setAllowedTypes("slot", "string");
59 1
    $resolver->setAllowedValues("slot", function(string $value) {
60 1
      return in_array($value, $this->getAllowedSlots(), true);
61 1
    });
62 1
    $resolver->setAllowedTypes("type", ["string", "null"]);
63 1
    $resolver->setAllowedValues("type", function(?string $value) {
64 1
      return is_null($value) OR in_array($value, $this->getAllowedTypes(), true);
65 1
    });
66 1
    $resolver->setAllowedTypes("strength", "integer");
67 1
    $resolver->setAllowedValues("strength", function(int $value) {
68 1
      return ($value >= 0);
69 1
    });
70 1
    $resolver->setAllowedTypes("worn", "boolean");
71 1
    $data = $resolver->resolve($data);
72 1
    $this->id = $data["id"];
73 1
    $this->name = $data["name"];
74 1
    $this->slot = $data["slot"];
75 1
    $this->type = $data["type"];
76 1
    $this->strength = $data["strength"];
77 1
    $this->worn = $data["worn"];
78 1
  }
79
  
80
  protected function getAllowedSlots(): array {
81 1
    return Constants::getConstantsValues(static::class, "SLOT_");
82
  }
83
  
84
  protected function getAllowedTypes(): array {
85 1
    return Constants::getConstantsValues(static::class, "TYPE_");
86
  }
87
  
88
  public function getId(): int {
89 1
    return $this->id;
90
  }
91
  
92
  public function getName(): string {
93
    return $this->name;
94
  }
95
  
96
  public function getSlot(): string {
97 1
    return $this->slot;
98
  }
99
  
100
  public function getType(): ?string {
101 1
    return $this->type;
102
  }
103
  
104
  public function getStrength(): int {
105
    return $this->strength;
106
  }
107
  
108
  public function isWorn(): bool {
109 1
    return $this->worn;
110
  }
111
  
112
  public function getDeployParams(): array {
113
    $stat = [
114 1
      static::SLOT_WEAPON => SkillSpecial::STAT_DAMAGE, static::SLOT_ARMOR => SkillSpecial::STAT_DEFENSE,
115 1
      static::SLOT_HELMET => SkillSpecial::STAT_HITPOINTS, static::SLOT_SHIELD => SkillSpecial::STAT_DODGE,
116 1
      static::SLOT_AMULET => SkillSpecial::STAT_INITIATIVE,
117
    ];
118
    $return = [
119 1
      "id" => "equipment" . $this->id . "bonusEffect",
120 1
      "type" => "buff",
121 1
      "stat" => $stat[$this->slot],
122 1
      "value" => $this->strength,
123 1
      "source" => CharacterEffect::SOURCE_EQUIPMENT,
124 1
      "duration" => CharacterEffect::DURATION_COMBAT,
125
    ];
126 1
    return $return;
127
  }
128
  
129
  public function toCombatEffect(): ?CharacterEffect {
130 1
    if(!$this->worn) {
131 1
      return NULL;
132
    }
133 1
    return new CharacterEffect($this->deployParams);
134
  }
135
}
136
?>