Lines 100.00% 31 / 31
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
InventoryLoadedEvent 100.00% 31 / 31 100.00% 3 / 3 10 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 29 / 29 100.00% 1 / 1 8
 summary 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Server\Event\Inventory;
6
7use Lorekeeper\Core\Item\ItemTag;
8use Lorekeeper\Core\Item\Traits\Stackable;
9use Lorekeeper\Core\Support\Quantity;
10use Lorekeeper\Server\Event\Item\Helper\ItemKinds;
11use Lorekeeper\Server\Exception\MalformedEventException;
12use Lorekeeper\Server\Message\JsonMessage;
13use Lorekeeper\Server\Message\Message;
14use Override;
15use Psl\Option;
16use Psl\Type;
17use Psl\Type\Exception\CoercionException;
18
19/**
20 * The server sent every item in our player's inventory.
21 */
22final readonly class InventoryLoadedEvent extends InventoryUpdatedEvent
23{
24    /** @param list<HeldItem> $items */
25    public function __construct(
26        public array $items,
27    ) {}
28
29    /** @return Option\Option<static> */
30    #[Override]
31    public static function from(Message $message): Option\Option
32    {
33        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'loadInventoryBig') {
34            return Option\none();
35        }
36
37        try {
38            $items = Type\vec(Type\shape([
39                'ItemID' => Type\positive_int(),
40                'sName' => Type\non_empty_string(),
41                'sType' => Type\string(),
42                'iQty' => Type\optional(Type\positive_int()),
43                'bUpg' => Type\optional(Type\bool()),
44                'bEquip' => Type\optional(Type\bool()),
45                'bWear' => Type\optional(Type\bool()),
46            ], allowUnknownFields: true))
47                ->coerce($message->data['items'] ?? null);
48            $procs = Type\optional(Type\dict(Type\array_key(), Type\mixed()))->coerce($message->data['procs'] ?? []);
49        } catch (CoercionException $exception) {
50            throw new MalformedEventException(static::class, 'items are missing or invalid', $exception);
51        }
52
53        $held = [];
54        foreach ($items as $entry) {
55            if (array_key_exists($entry['ItemID'], $held)) {
56                throw new MalformedEventException(static::class, 'an item is in the inventory more than once');
57            }
58
59            $item = ItemKinds::read($entry, static::class, $procs);
60            if ($entry['bUpg'] ?? false) {
61                $item->tags->add(ItemTag::MemberOnly);
62            }
63
64            $held[$entry['ItemID']] = new HeldItem(
65                $item,
66                new Quantity($item instanceof Stackable ? $entry['iQty'] ?? 1 : 1),
67                $entry['bEquip'] ?? false,
68                $entry['bWear'] ?? false,
69            );
70        }
71
72        return Option\some(new static(array_values($held)));
73    }
74
75    #[Override]
76    public function summary(): string
77    {
78        return 'Inventory loaded: ' . count($this->items) . ' items';
79    }
80}