Lines 100.00% 20 / 20
Functions and Methods 100.00% 4 / 4
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
SkillDetails 100.00% 20 / 20 100.00% 4 / 4 21 100.00% 1 / 1
 description 100.00% 1 / 1 100.00% 1 / 1 2
 damage 100.00% 6 / 6 100.00% 1 / 1 6
 slot 100.00% 8 / 8 100.00% 1 / 1 8
 target 100.00% 5 / 5 100.00% 1 / 1 5
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Server\Event\Combat\Helper;
6
7use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\Damage\DamageMultiplier;
8use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\Damage\DamageSource;
9use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\Damage\SkillDamage;
10use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\SkillDescription;
11use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\SkillSlot;
12use Lorekeeper\Core\Item\Equipment\CharacterClass\Skill\SkillTarget;
13use Lorekeeper\Server\Event\Combat\SkillsLoadedEvent;
14use Lorekeeper\Server\Exception\MalformedEventException;
15use Psl\Option;
16
17final readonly class SkillDetails
18{
19    /** @return Option\Option<SkillDescription> */
20    public static function description(string $description): Option\Option
21    {
22        return $description === '' ? Option\none() : Option\some(new SkillDescription($description));
23    }
24
25    /** @return Option\Option<SkillDamage> */
26    public static function damage(int|float $multiplier, string $source): Option\Option
27    {
28        $from = match (true) {
29            str_starts_with($source, 'AP') => DamageSource::Physical,
30            str_starts_with($source, 'SP') => DamageSource::Magical,
31            default => null,
32        };
33        if ($from === null || $multiplier <= 0) {
34            return Option\none();
35        }
36
37        return Option\some(new SkillDamage(new DamageMultiplier((float) $multiplier), $from));
38    }
39
40    public static function slot(string $ref): ?SkillSlot
41    {
42        return match ($ref) {
43            'aa' => SkillSlot::AutoAttack,
44            'a1' => SkillSlot::First,
45            'a2' => SkillSlot::Second,
46            'a3' => SkillSlot::Third,
47            'a4' => SkillSlot::Fourth,
48            'i1' => SkillSlot::Potion,
49            default => null,
50        };
51    }
52
53    /** @throws MalformedEventException */
54    public static function target(string $target): SkillTarget
55    {
56        return match ($target) {
57            'h' => SkillTarget::Enemy,
58            's' => SkillTarget::Self,
59            'f' => SkillTarget::Ally,
60            default => throw new MalformedEventException(SkillsLoadedEvent::class, "unknown skill target '{$target}'"),
61        };
62    }
63}