Lines 100.00% 73 / 73
Functions and Methods 100.00% 6 / 6
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Enhancements 100.00% 73 / 73 100.00% 6 / 6 42 100.00% 1 / 1
 applied 100.00% 16 / 16 100.00% 1 / 1 5
 sold 100.00% 20 / 20 100.00% 1 / 1 4
 type 100.00% 10 / 10 100.00% 1 / 1 11
 rarity 100.00% 12 / 12 100.00% 1 / 1 12
 slot 100.00% 6 / 6 100.00% 1 / 1 6
 effect 100.00% 9 / 9 100.00% 1 / 1 4
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Server\Event\Item\Helper;
6
7use Lorekeeper\Core\Item\EquipmentSlot;
8use Lorekeeper\Core\Item\ItemId;
9use Lorekeeper\Core\Item\ItemLevel;
10use Lorekeeper\Core\Item\ItemName;
11use Lorekeeper\Core\Item\Misc\Enhancement\EffectDescription;
12use Lorekeeper\Core\Item\Misc\Enhancement\EffectName;
13use Lorekeeper\Core\Item\Misc\Enhancement\Enhancement;
14use Lorekeeper\Core\Item\Misc\Enhancement\EnhancementEffect;
15use Lorekeeper\Core\Item\Misc\Enhancement\EnhancementItem;
16use Lorekeeper\Core\Item\Misc\Enhancement\EnhancementRarity;
17use Lorekeeper\Core\Item\Misc\Enhancement\EnhancementType;
18use Lorekeeper\Server\Event\Event;
19use Lorekeeper\Server\Exception\MalformedEventException;
20use Psl\Option;
21use Psl\Type;
22use Psl\Type\Exception\CoercionException;
23
24final readonly class Enhancements
25{
26    /**
27     * @param class-string<Event> $event
28     * @param array<array-key, mixed> $entry
29     * @param array<array-key, mixed> $procs
30     *
31     * @throws MalformedEventException
32     *
33     * @return Option\Option<Enhancement>
34     */
35    public static function applied(array $entry, array $procs, string $event): Option\Option
36    {
37        try {
38            $data = Type\shape([
39                'EnhPatternID' => Type\optional(Type\nullable(Type\int())),
40                'EnhLvl' => Type\optional(Type\nullable(Type\int_range(min: 0, max: 100))),
41                'EnhRty' => Type\optional(Type\nullable(Type\int())),
42                'ProcID' => Type\optional(Type\nullable(Type\int())),
43            ], allowUnknownFields: true)->coerce($entry);
44        } catch (CoercionException $exception) {
45            throw new MalformedEventException($event, 'an enhancement on an item is invalid', $exception);
46        }
47
48        $type = self::type($data['EnhPatternID'] ?? 0);
49        $rarity = self::rarity($data['EnhRty'] ?? 0, $event);
50        $level = $data['EnhLvl'] ?? 0;
51        if ($type === null || $rarity === null || $level < 1) {
52            return Option\none();
53        }
54
55        return Option\some(
56            new Enhancement($type, $rarity, new ItemLevel($level), self::effect($data['ProcID'] ?? 0, $procs, $event)),
57        );
58    }
59
60    /**
61     * @param class-string<Event> $event
62     * @param array<array-key, mixed> $entry
63     *
64     * @throws MalformedEventException
65     */
66    public static function sold(
67        ItemId $id,
68        ItemName $name,
69        ItemLevel $level,
70        array $entry,
71        string $event,
72    ): EnhancementItem {
73        try {
74            $data = Type\shape([
75                'PatternID' => Type\int(),
76                'iRty' => Type\int(),
77                'sES' => Type\string(),
78            ], allowUnknownFields: true)->coerce($entry);
79        } catch (CoercionException $exception) {
80            throw new MalformedEventException($event, 'an enhancement has no type, rarity or slot', $exception);
81        }
82
83        $type = self::type($data['PatternID']);
84        $rarity = self::rarity($data['iRty'], $event);
85        if ($type === null || $rarity === null) {
86            throw new MalformedEventException(
87                $event,
88                "enhancement {$id->value} has the unknown type {$data['PatternID']} or rarity {$data['iRty']}",
89            );
90        }
91
92        return new EnhancementItem(
93            $id,
94            $name,
95            new Enhancement($type, $rarity, $level, Option\none()),
96            self::slot($data['sES'], $event),
97        );
98    }
99
100    public static function type(int $type): ?EnhancementType
101    {
102        return match ($type) {
103            1 => EnhancementType::Adventurer,
104            2 => EnhancementType::Fighter,
105            3 => EnhancementType::Thief,
106            5 => EnhancementType::Hybrid,
107            6 => EnhancementType::Wizard,
108            7 => EnhancementType::Healer,
109            8 => EnhancementType::SpellBreaker,
110            9 => EnhancementType::Lucky,
111            default => $type > 0 ? EnhancementType::Unhandled : null,
112        };
113    }
114
115    /**
116     * @param class-string<Event> $event
117     *
118     * @throws MalformedEventException
119     */
120    public static function rarity(int $rarity, string $event): ?EnhancementRarity
121    {
122        return match ($rarity) {
123            1 => EnhancementRarity::PlusZero,
124            2 => EnhancementRarity::PlusOne,
125            3 => EnhancementRarity::PlusTwo,
126            4 => EnhancementRarity::PlusThree,
127            5 => EnhancementRarity::PlusFour,
128            6 => EnhancementRarity::PlusFive,
129            7 => EnhancementRarity::PlusSix,
130            8 => EnhancementRarity::PlusOneHundred,
131            9 => EnhancementRarity::PlusTwoHundred,
132            0 => null,
133            default => throw new MalformedEventException($event, "an enhancement has the unknown rarity {$rarity}"),
134        };
135    }
136
137    /**
138     * @param class-string<Event> $event
139     *
140     * @throws MalformedEventException
141     */
142    private static function slot(string $slot, string $event): EquipmentSlot
143    {
144        return match ($slot) {
145            'Weapon' => EquipmentSlot::Weapon,
146            'ar' => EquipmentSlot::CharacterClass,
147            'he' => EquipmentSlot::Helm,
148            'ba' => EquipmentSlot::Cape,
149            default => throw new MalformedEventException($event, "an enhancement fits the unknown slot '{$slot}'"),
150        };
151    }
152
153    /**
154     * @param array<array-key, mixed> $procs
155     * @param class-string<Event> $event
156     *
157     * @throws MalformedEventException
158     *
159     * @return Option\Option<EnhancementEffect>
160     */
161    private static function effect(int $proc, array $procs, string $event): Option\Option
162    {
163        if ($proc < 1 || !array_key_exists($proc, $procs)) {
164            return Option\none();
165        }
166
167        try {
168            $data = Type\shape([
169                'nam' => Type\non_empty_string(),
170                'desc' => Type\non_empty_string(),
171            ], allowUnknownFields: true)->coerce($procs[$proc]);
172        } catch (CoercionException $exception) {
173            throw new MalformedEventException($event, "effect {$proc} has no name or description", $exception);
174        }
175
176        return Option\some(new EnhancementEffect(new EffectName($data['nam']), new EffectDescription($data['desc'])));
177    }
178}