Lines 100.00% 27 / 27
Functions and Methods 100.00% 6 / 6
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
PlayerAwayUpdatedEvent 100.00% 27 / 27 100.00% 6 / 6 18 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 5 / 5 100.00% 1 / 1 6
 summary 100.00% 1 / 1 100.00% 1 / 1 2
 delimited 100.00% 7 / 7 100.00% 1 / 1 4
 json 100.00% 10 / 10 100.00% 1 / 1 3
 player 100.00% 3 / 3 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Server\Event\Player;
6
7use Lorekeeper\Core\Player\PlayerName;
8use Lorekeeper\Server\Exception\MalformedEventException;
9use Lorekeeper\Server\Message\DelimitedMessage;
10use Lorekeeper\Server\Message\JsonMessage;
11use Lorekeeper\Server\Message\Message;
12use Override;
13use Psl\Option;
14use Psl\Type;
15use Psl\Type\Exception\CoercionException;
16
17/**
18 * A player in the room went away or came back.
19 */
20final readonly class PlayerAwayUpdatedEvent extends PlayerUpdatedEvent
21{
22    public function __construct(
23        PlayerName $name,
24        public bool $away,
25    ) {
26        parent::__construct($name);
27    }
28
29    /** @return Option\Option<static> */
30    #[Override]
31    public static function from(Message $message): Option\Option
32    {
33        return match (true) {
34            $message instanceof DelimitedMessage && ($message->data[1] ?? null) === 'uotls' => self::delimited(
35                $message,
36            ),
37            $message instanceof JsonMessage && ($message->data['cmd'] ?? null) === 'uotls' => self::json($message),
38            default => Option\none(),
39        };
40    }
41
42    #[Override]
43    public function summary(): string
44    {
45        return $this->away ? "{$this->name->value} is away" : "{$this->name->value} is back";
46    }
47
48    /**
49     * @throws MalformedEventException
50     *
51     * @return Option\Option<static>
52     */
53    private static function delimited(DelimitedMessage $message): Option\Option
54    {
55        $matches = [];
56        if (preg_match('/(?:^|,)afk:([^,]*)/', $message->data[4] ?? '', $matches) !== 1) {
57            return Option\none();
58        }
59
60        $away = $matches[1];
61        if ($away !== 'true' && $away !== 'false') {
62            throw new MalformedEventException(static::class, "away is '{$away}', not true or false");
63        }
64
65        return Option\some(new static(self::player($message->data[3] ?? null), $away === 'true'));
66    }
67
68    /**
69     * @throws MalformedEventException
70     *
71     * @return Option\Option<static>
72     */
73    private static function json(JsonMessage $message): Option\Option
74    {
75        try {
76            $update = Type\shape([
77                'unm' => Type\optional(Type\mixed()),
78                'o' => Type\optional(Type\shape(['afk' => Type\optional(Type\bool())], allowUnknownFields: true)),
79            ], allowUnknownFields: true)->coerce($message->data);
80        } catch (CoercionException $exception) {
81            throw new MalformedEventException(static::class, 'away is not true or false', $exception);
82        }
83
84        $away = $update['o']['afk'] ?? null;
85        if ($away === null) {
86            return Option\none();
87        }
88
89        return Option\some(new static(self::player($update['unm'] ?? null), $away));
90    }
91
92    /** @throws MalformedEventException */
93    private static function player(mixed $name): PlayerName
94    {
95        try {
96            return new PlayerName(Type\non_empty_string()->coerce($name));
97        } catch (CoercionException $exception) {
98            throw new MalformedEventException(static::class, 'player name is missing or invalid', $exception);
99        }
100    }
101}