Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| AreaJoinedEvent | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| from | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AqwSocketClient\Events; |
| 6 | |
| 7 | use AqwSocketClient\Enums\JsonMessageType; |
| 8 | use AqwSocketClient\Interfaces\EventInterface; |
| 9 | use AqwSocketClient\Interfaces\MessageInterface; |
| 10 | use AqwSocketClient\Messages\JsonMessage; |
| 11 | use AqwSocketClient\Objects\Area\Area; |
| 12 | use AqwSocketClient\Objects\Identifiers\AreaIdentifier; |
| 13 | use AqwSocketClient\Objects\Identifiers\RoomIdentifier; |
| 14 | use AqwSocketClient\Objects\Names\AreaName; |
| 15 | use Override; |
| 16 | use Psl\Str; |
| 17 | |
| 18 | /** |
| 19 | * Represents an event triggered after the client successfully joined a specific |
| 20 | * map in the game world. |
| 21 | */ |
| 22 | final class AreaJoinedEvent implements EventInterface |
| 23 | { |
| 24 | public function __construct( |
| 25 | public readonly Area $area, |
| 26 | ) {} |
| 27 | |
| 28 | /** |
| 29 | * @return ?AreaJoinedEvent |
| 30 | */ |
| 31 | #[Override] |
| 32 | public static function from(MessageInterface $message): ?EventInterface |
| 33 | { |
| 34 | if ($message instanceof JsonMessage && $message->type === JsonMessageType::JoinedArea) { |
| 35 | /** @var array{strMapName: string, areaName: string, areaId: numeric-string} $data */ |
| 36 | $data = $message->data; |
| 37 | |
| 38 | $name = new AreaName($data['strMapName']); |
| 39 | $identifier = new AreaIdentifier((int) $data['areaId']); |
| 40 | $roomParts = Str\split($data['areaName'], '-'); |
| 41 | $roomPart = $roomParts[1] ?? null; |
| 42 | $room = $roomPart !== null ? new RoomIdentifier((int) $roomPart) : null; |
| 43 | |
| 44 | $area = new Area($identifier, $name, $room); |
| 45 | |
| 46 | return new self($area); |
| 47 | } |
| 48 | |
| 49 | return null; |
| 50 | } |
| 51 | } |