Lines
100.00%
13 / 13
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| PlayerLeftEvent | 100.00% | 13 / 13 | 100.00% | 3 / 3 | 6 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| from | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 4 | |||||
| summary | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Event\Player; | |
| 6 | ||
| 7 | use Lorekeeper\Core\Player\PlayerName; | |
| 8 | use Lorekeeper\Core\Player\PlayerSpawnId; | |
| 9 | use Lorekeeper\Server\Event\Event; | |
| 10 | use Lorekeeper\Server\Event\NamedAfterClass; | |
| 11 | use Lorekeeper\Server\Exception\MalformedEventException; | |
| 12 | use Lorekeeper\Server\Message\DelimitedMessage; | |
| 13 | use Lorekeeper\Server\Message\Message; | |
| 14 | use Override; | |
| 15 | use Psl\Option; | |
| 16 | use Psl\Type; | |
| 17 | use Psl\Type\Exception\CoercionException; | |
| 18 | ||
| 19 | /** | |
| 20 | * A player left the room. | |
| 21 | */ | |
| 22 | final readonly class PlayerLeftEvent implements Event | |
| 23 | { | |
| 24 | use NamedAfterClass; | |
| 25 | ||
| 26 | public function __construct( | |
| 27 | public PlayerSpawnId $spawnId, | |
| 28 | public PlayerName $name, | |
| 29 | ) {} | |
| 30 | ||
| 31 | /** @return Option\Option<static> */ | |
| 32 | #[Override] | |
| 33 | public static function from(Message $message): Option\Option | |
| 34 | { | |
| 35 | if (!$message instanceof DelimitedMessage || ($message->data[1] ?? null) !== 'exitArea') { | |
| 36 | return Option\none(); | |
| 37 | } | |
| 38 | ||
| 39 | try { | |
| 40 | $spawnId = Type\positive_int()->coerce($message->data[3] ?? null); | |
| 41 | $name = Type\non_empty_string()->coerce($message->data[4] ?? null); | |
| 42 | } catch (CoercionException $exception) { | |
| 43 | throw new MalformedEventException( | |
| 44 | static::class, | |
| 45 | 'player spawn id or name is missing or invalid', | |
| 46 | $exception, | |
| 47 | ); | |
| 48 | } | |
| 49 | ||
| 50 | return Option\some(new static(new PlayerSpawnId($spawnId), new PlayerName($name))); | |
| 51 | } | |
| 52 | ||
| 53 | #[Override] | |
| 54 | public function summary(): string | |
| 55 | { | |
| 56 | return "{$this->name->value} (spawn {$this->spawnId->value}) left"; | |
| 57 | } | |
| 58 | } |