Lines
100.00%
14 / 14
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| RoomJoinedEvent | 100.00% | 14 / 14 | 100.00% | 3 / 3 | 8 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| from | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 6 | |||||
| summary | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Event\Map; | |
| 6 | ||
| 7 | use DOMElement; | |
| 8 | use Lorekeeper\Core\Player\PlayerName; | |
| 9 | use Lorekeeper\Server\Event\Event; | |
| 10 | use Lorekeeper\Server\Event\NamedAfterClass; | |
| 11 | use Lorekeeper\Server\Exception\MalformedEventException; | |
| 12 | use Lorekeeper\Server\Message\Message; | |
| 13 | use Lorekeeper\Server\Message\XmlMessage; | |
| 14 | use Override; | |
| 15 | use Psl\Option; | |
| 16 | use Psl\Type; | |
| 17 | use Psl\Type\Exception\CoercionException; | |
| 18 | ||
| 19 | /** | |
| 20 | * Our player joined a room, with the names of the players already in it. | |
| 21 | */ | |
| 22 | final readonly class RoomJoinedEvent implements Event | |
| 23 | { | |
| 24 | use NamedAfterClass; | |
| 25 | ||
| 26 | /** @param list<PlayerName> $players */ | |
| 27 | public function __construct( | |
| 28 | public array $players, | |
| 29 | ) {} | |
| 30 | ||
| 31 | /** @return Option\Option<static> */ | |
| 32 | #[Override] | |
| 33 | public static function from(Message $message): Option\Option | |
| 34 | { | |
| 35 | if (!$message instanceof XmlMessage) { | |
| 36 | return Option\none(); | |
| 37 | } | |
| 38 | ||
| 39 | $body = $message->dom->getElementsByTagName('body')->item(0); | |
| 40 | if (!$body instanceof DOMElement || $body->getAttribute('action') !== 'joinOK') { | |
| 41 | return Option\none(); | |
| 42 | } | |
| 43 | ||
| 44 | $players = []; | |
| 45 | foreach ($body->getElementsByTagName('u') as $user) { | |
| 46 | try { | |
| 47 | $name = Type\non_empty_string()->coerce($user->getElementsByTagName('n')->item(0)?->textContent); | |
| 48 | } catch (CoercionException $exception) { | |
| 49 | throw new MalformedEventException(static::class, 'a player name is missing or invalid', $exception); | |
| 50 | } | |
| 51 | ||
| 52 | $players[] = new PlayerName($name); | |
| 53 | } | |
| 54 | ||
| 55 | return Option\some(new static($players)); | |
| 56 | } | |
| 57 | ||
| 58 | #[Override] | |
| 59 | public function summary(): string | |
| 60 | { | |
| 61 | return 'Joined a room with ' . count($this->players) . ' players'; | |
| 62 | } | |
| 63 | } |