Lines 100.00% 23 / 23
Functions and Methods 100.00% 4 / 4
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
PlayerDataLoadedEvent 100.00% 23 / 23 100.00% 4 / 4 8 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 6 / 6 100.00% 1 / 1 4
 summary 100.00% 2 / 2 100.00% 1 / 1 1
 player 100.00% 14 / 14 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Server\Event\Player;
6
7use Lorekeeper\Core\Player\PlayerLevel;
8use Lorekeeper\Core\Player\PlayerName;
9use Lorekeeper\Core\Player\PlayerSpawnId;
10use Lorekeeper\Server\Event\Event;
11use Lorekeeper\Server\Event\NamedAfterClass;
12use Lorekeeper\Server\Event\Player\Helper\PlayerEntries;
13use Lorekeeper\Server\Exception\MalformedEventException;
14use Lorekeeper\Server\Message\JsonMessage;
15use Lorekeeper\Server\Message\Message;
16use Override;
17use Psl\Option;
18use Psl\Type;
19use Psl\Type\Exception\CoercionException;
20
21/**
22 * The server sent what it shows about other players in the room.
23 */
24final readonly class PlayerDataLoadedEvent implements Event
25{
26    use NamedAfterClass;
27
28    /** @param non-empty-list<PlayerData> $players */
29    public function __construct(
30        public array $players,
31    ) {}
32
33    /** @return Option\Option<static> */
34    #[Override]
35    public static function from(Message $message): Option\Option
36    {
37        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'initUserDatas') {
38            return Option\none();
39        }
40
41        $players = array_map(self::player(...), PlayerEntries::others($message, static::class));
42        if ($players === []) {
43            return Option\none();
44        }
45
46        return Option\some(new static($players));
47    }
48
49    #[Override]
50    public function summary(): string
51    {
52        $names = array_map(static fn(PlayerData $player): string => $player->name->value, $this->players);
53
54        return 'Data of ' . implode(', ', $names);
55    }
56
57    /**
58     * @param array{data: array<string, mixed>, ...} $entry
59     *
60     * @throws MalformedEventException
61     */
62    private static function player(array $entry): PlayerData
63    {
64        try {
65            $entry = Type\shape([
66                'uid' => Type\positive_int(),
67                'data' => Type\shape([
68                    'strUsername' => Type\non_empty_string(),
69                    'intLevel' => Type\int_range(min: 1, max: 100),
70                ], allowUnknownFields: true),
71            ], allowUnknownFields: true)->coerce($entry);
72        } catch (CoercionException $exception) {
73            throw new MalformedEventException(static::class, 'player data is missing or invalid', $exception);
74        }
75
76        return new PlayerData(
77            new PlayerSpawnId($entry['uid']),
78            new PlayerName($entry['data']['strUsername']),
79            new PlayerLevel($entry['data']['intLevel']),
80        );
81    }
82}