Lines
100.00%
20 / 20
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ItemsIncreasedEvent | 100.00% | 20 / 20 | 100.00% | 3 / 3 | 8 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| from | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 5 | |||||
| summary | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Event\Inventory; | |
| 6 | ||
| 7 | use Lorekeeper\Core\Item\ItemId; | |
| 8 | use Lorekeeper\Core\Support\Quantity; | |
| 9 | use Lorekeeper\Server\Event\Inventory\Helper\ItemArrival; | |
| 10 | use Lorekeeper\Server\Event\Inventory\Helper\ItemArrivals; | |
| 11 | use Lorekeeper\Server\Message\Message; | |
| 12 | use Override; | |
| 13 | use Psl\Option; | |
| 14 | ||
| 15 | /** | |
| 16 | * Our player got more of one or more items they already had in their inventory. | |
| 17 | */ | |
| 18 | final readonly class ItemsIncreasedEvent extends InventoryUpdatedEvent | |
| 19 | { | |
| 20 | /** @param non-empty-list<ItemIncrease> $items */ | |
| 21 | public function __construct( | |
| 22 | public array $items, | |
| 23 | ) {} | |
| 24 | ||
| 25 | /** @return Option\Option<static> */ | |
| 26 | #[Override] | |
| 27 | public static function from(Message $message): Option\Option | |
| 28 | { | |
| 29 | $items = []; | |
| 30 | foreach (ItemArrivals::of($message, static::class) ?? [] as [$kind, $entry]) { | |
| 31 | $held = $entry['iQtyNow'] ?? null; | |
| 32 | if ($kind !== ItemArrival::ItemIncreased || $held === null) { | |
| 33 | continue; | |
| 34 | } | |
| 35 | ||
| 36 | $items[] = new ItemIncrease( | |
| 37 | new ItemId($entry['ItemID']), | |
| 38 | new Quantity($entry['iQty']), | |
| 39 | new Quantity($held), | |
| 40 | ); | |
| 41 | } | |
| 42 | ||
| 43 | return $items === [] ? Option\none() : Option\some(new static($items)); | |
| 44 | } | |
| 45 | ||
| 46 | #[Override] | |
| 47 | public function summary(): string | |
| 48 | { | |
| 49 | return 'Got ' | |
| 50 | . implode(', ', array_map( | |
| 51 | static fn(ItemIncrease $added): string => ( | |
| 52 | "{$added->quantity->value} more of item {$added->item->value}" | |
| 53 | . ($added->held === null ? '' : ", {$added->held->value} held now") | |
| 54 | ), | |
| 55 | $this->items, | |
| 56 | )); | |
| 57 | } | |
| 58 | } |