Passed
Push — master ( 465240...c8cc94 )
by Jakub
02:15
created

Equipment::getStrength()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.6111
cc 5
nc 5
nop 0
crap 5
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 $strength
16
 * @property int $durability
17
 */
18 1
class Equipment implements ICharacterEffectsProvider {
19
  use \Nette\SmartObject;
20
  
21
  public const SLOT_WEAPON = "weapon";
22
  public const SLOT_ARMOR = "armor";
23
  public const SLOT_SHIELD = "shield";
24
  public const SLOT_AMULET = "amulet";
25
  public const SLOT_HELMET = "helmet";
26
  public const SLOT_RING = "ring";
27
28
  public readonly int $id;
29
  public readonly string $name;
30
  public readonly string $slot;
31
  public readonly ?string $type;
32
  public readonly int $rawStrength;
33
  public bool $worn;
34
  protected int $durability;
35
  public readonly int $maxDurability;
36
  
37
  public function __construct(array $data) {
38 1
    $resolver = new OptionsResolver();
39 1
    $this->configureOptions($resolver);
40 1
    $data = $resolver->resolve($data);
41 1
    $this->id = $data["id"];
42 1
    $this->name = $data["name"];
43 1
    $this->slot = $data["slot"];
44 1
    $this->type = $data["type"];
45 1
    $this->rawStrength = $data["strength"];
46 1
    $this->worn = $data["worn"];
47 1
    $this->maxDurability = $data["maxDurability"];
48 1
    $this->durability = $data["durability"];
49 1
  }
50
  
51
  protected function configureOptions(OptionsResolver $resolver): void {
52 1
    $allStats = ["id", "name", "slot", "type", "strength", "worn", ];
53 1
    $resolver->setRequired($allStats);
54 1
    $resolver->setAllowedTypes("id", "integer");
55 1
    $resolver->setAllowedTypes("name", "string");
56 1
    $resolver->setAllowedTypes("slot", "string");
57 1
    $resolver->setAllowedValues("slot", function(string $value) : bool{
58 1
      return in_array($value, $this->getAllowedSlots(), true);
59 1
    });
60 1
    $resolver->setAllowedTypes("type", "null");
61 1
    $resolver->setDefault("type", null);
62 1
    $resolver->setAllowedTypes("strength", "integer");
63 1
    $resolver->setAllowedValues("strength", function(int $value): bool {
64 1
      return ($value >= 0);
65 1
    });
66 1
    $resolver->setAllowedTypes("worn", "boolean");
67 1
    $resolver->setDefault("maxDurability", 0);
68 1
    $resolver->setAllowedTypes("maxDurability", "integer");
69 1
    $resolver->setAllowedValues("maxDurability", function(int $value): bool {
70 1
      return ($value >= 0);
71 1
    });
72 1
    $resolver->setDefault("durability", function(Options $options) {
73 1
      return $options["maxDurability"];
74 1
    });
75 1
    $resolver->setAllowedTypes("durability", "integer");
76 1
  }
77
  
78
  protected function getAllowedSlots(): array {
79 1
    return Constants::getConstantsValues(static::class, "SLOT_");
80
  }
81
82
  protected function getStrength(): int {
83 1
    if($this->durability >= $this->maxDurability * 0.7) {
84 1
      return $this->rawStrength;
85 1
    } elseif($this->durability >= $this->maxDurability / 2) {
86 1
      return (int) ($this->rawStrength * 0.75);
87 1
    } elseif($this->durability >= $this->maxDurability / 4) {
88 1
      return (int) ($this->rawStrength / 2);
89 1
    } elseif($this->durability >= $this->maxDurability / 10) {
90 1
      return (int) ($this->rawStrength / 4);
91
    }
92 1
    return 0;
93
  }
94
95
  protected function getDurability(): int {
96 1
    return $this->durability;
97
  }
98
99
  protected function setDurability(int $durability): void {
100 1
    $this->durability = Numbers::range($durability, 0, $this->maxDurability);
101 1
  }
102
103
  protected function getDeployParams(): array {
104 1
    $stat = [
105 1
      static::SLOT_WEAPON => Character::STAT_DAMAGE, static::SLOT_ARMOR => Character::STAT_DEFENSE,
106 1
      static::SLOT_HELMET => Character::STAT_MAX_HITPOINTS, static::SLOT_SHIELD => Character::STAT_DODGE,
107 1
      static::SLOT_AMULET => Character::STAT_INITIATIVE, static::SLOT_RING => Character::STAT_HIT,
108
    ];
109 1
    $return = [
110 1
      "id" => "equipment" . $this->id . "bonusEffect",
111 1
      "type" => SkillSpecial::TYPE_BUFF,
112 1
      "stat" => $stat[$this->slot],
113 1
      "value" => $this->strength,
114
      "valueAbsolute" => true,
115 1
      "duration" => CharacterEffect::DURATION_COMBAT,
116
    ];
117 1
    return $return;
118
  }
119
  
120
  public function getCombatEffects(): array {
121 1
    if(!$this->worn) {
122 1
      return [];
123
    }
124 1
    return [new CharacterEffect($this->getDeployParams())];
125
  }
126
}
127
?>