Lines
100.00%
19 / 19
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| DropPickedUpEvent | 100.00% | 19 / 19 | 100.00% | 3 / 3 | 9 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| from | 100.00% | 17 / 17 | 100.00% | 1 / 1 | 7 | |||||
| summary | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 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\Exception\MalformedEventException; | |
| 10 | use Lorekeeper\Server\Message\JsonMessage; | |
| 11 | use Lorekeeper\Server\Message\Message; | |
| 12 | use Override; | |
| 13 | use Psl\Option; | |
| 14 | use Psl\Type; | |
| 15 | use Psl\Type\Exception\CoercionException; | |
| 16 | ||
| 17 | /** | |
| 18 | * Our player picked up an item from the drop list into their inventory. | |
| 19 | */ | |
| 20 | final readonly class DropPickedUpEvent extends InventoryUpdatedEvent | |
| 21 | { | |
| 22 | public function __construct( | |
| 23 | public ItemId $item, | |
| 24 | public Quantity $picked, | |
| 25 | public Quantity $held, | |
| 26 | ) {} | |
| 27 | ||
| 28 | /** @return Option\Option<static> */ | |
| 29 | #[Override] | |
| 30 | public static function from(Message $message): Option\Option | |
| 31 | { | |
| 32 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'getDrop') { | |
| 33 | return Option\none(); | |
| 34 | } | |
| 35 | ||
| 36 | try { | |
| 37 | $pickup = Type\shape([ | |
| 38 | 'ItemID' => Type\positive_int(), | |
| 39 | 'bSuccess' => Type\int(), | |
| 40 | 'iQty' => Type\optional(Type\positive_int()), | |
| 41 | 'iQtyNow' => Type\optional(Type\positive_int()), | |
| 42 | ], allowUnknownFields: true)->coerce($message->data); | |
| 43 | } catch (CoercionException $exception) { | |
| 44 | throw new MalformedEventException(static::class, 'picked up drop is invalid', $exception); | |
| 45 | } | |
| 46 | ||
| 47 | if ($pickup['bSuccess'] !== 1) { | |
| 48 | return Option\none(); | |
| 49 | } | |
| 50 | ||
| 51 | if (!array_key_exists('iQty', $pickup) || !array_key_exists('iQtyNow', $pickup)) { | |
| 52 | throw new MalformedEventException(static::class, 'picked up drop has no quantities'); | |
| 53 | } | |
| 54 | ||
| 55 | return Option\some( | |
| 56 | new static(new ItemId($pickup['ItemID']), new Quantity($pickup['iQty']), new Quantity($pickup['iQtyNow'])), | |
| 57 | ); | |
| 58 | } | |
| 59 | ||
| 60 | #[Override] | |
| 61 | public function summary(): string | |
| 62 | { | |
| 63 | return "Picked up {$this->picked->value} of item {$this->item->value}, holding {$this->held->value}"; | |
| 64 | } | |
| 65 | } |