Lines 100.00% 13 / 13
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
DropTracking 100.00% 13 / 13 100.00% 2 / 2 8 100.00% 1 / 1
 follow 100.00% 6 / 6 100.00% 1 / 1 5
 dropped 100.00% 7 / 7 100.00% 1 / 1 3
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Client\Tracking;
6
7use Lorekeeper\Client\Drops;
8use Lorekeeper\Core\Support\Quantity;
9use Lorekeeper\Server\Event\Event;
10use Lorekeeper\Server\Event\Inventory\DropPickedUpEvent;
11use Lorekeeper\Server\Event\Inventory\HeldItem;
12use Lorekeeper\Server\Event\Inventory\ItemDroppedEvent;
13
14final readonly class DropTracking
15{
16    public static function follow(Drops $drops, Event $event): Drops
17    {
18        return match (true) {
19            $event instanceof ItemDroppedEvent => self::dropped($drops, $event->items),
20            $event instanceof DropPickedUpEvent && $drops->find($event->item) !== null => new Drops(array_diff_key(
21                $drops->items,
22                [$event->item->value => true],
23            )),
24            default => $drops,
25        };
26    }
27
28    /** @param non-empty-list<HeldItem> $dropped */
29    private static function dropped(Drops $drops, array $dropped): Drops
30    {
31        $items = $drops->items;
32        foreach ($dropped as $item) {
33            $before = $items[$item->item->id->value] ?? null;
34            $items[$item->item->id->value] = $before === null
35                ? $item
36                : new HeldItem($before->item, new Quantity($before->quantity->value + $item->quantity->value));
37        }
38
39        return new Drops($items);
40    }
41}