Lines 100.00% 28 / 28
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
PlayerStateUpdatedEvent 100.00% 28 / 28 100.00% 3 / 3 9 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 26 / 26 100.00% 1 / 1 7
 summary 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Server\Event\Player;
6
7use Lorekeeper\Core\Game\Attribute\Health;
8use Lorekeeper\Core\Game\Attribute\Mana;
9use Lorekeeper\Core\Game\Combat\CombatState;
10use Lorekeeper\Core\Player\PlayerName;
11use Lorekeeper\Server\Event\Combat\Helper\CombatStates;
12use Lorekeeper\Server\Exception\MalformedEventException;
13use Lorekeeper\Server\Message\JsonMessage;
14use Lorekeeper\Server\Message\Message;
15use Override;
16use Psl\Option;
17use Psl\Type;
18use Psl\Type\Exception\CoercionException;
19
20/**
21 * A player in the room died, went idle or started fighting.
22 */
23final readonly class PlayerStateUpdatedEvent extends PlayerUpdatedEvent
24{
25    public function __construct(
26        PlayerName $name,
27        public CombatState $state,
28        public ?Health $health = null,
29        public ?Mana $mana = null,
30    ) {
31        parent::__construct($name);
32    }
33
34    /** @return Option\Option<static> */
35    #[Override]
36    public static function from(Message $message): Option\Option
37    {
38        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'uotls') {
39            return Option\none();
40        }
41
42        try {
43            $update = Type\shape([
44                'unm' => Type\optional(Type\mixed()),
45                'o' => Type\optional(Type\shape([
46                    'intState' => Type\optional(Type\int_range(min: 0, max: 2)),
47                    'intHP' => Type\optional(Type\uint()),
48                    'intMP' => Type\optional(Type\uint()),
49                ], allowUnknownFields: true)),
50            ], allowUnknownFields: true)->coerce($message->data);
51            $state = $update['o']['intState'] ?? null;
52            if ($state === null) {
53                return Option\none();
54            }
55
56            $name = Type\non_empty_string()->coerce($update['unm'] ?? null);
57        } catch (CoercionException $exception) {
58            throw new MalformedEventException(static::class, 'player name or state is invalid', $exception);
59        }
60
61        $health = $update['o']['intHP'] ?? null;
62        $mana = $update['o']['intMP'] ?? null;
63
64        return Option\some(
65            new static(
66                new PlayerName($name),
67                CombatStates::read($state),
68                $health === null ? null : new Health($health),
69                $mana === null ? null : new Mana($mana),
70            ),
71        );
72    }
73
74    #[Override]
75    public function summary(): string
76    {
77        return "{$this->name->value} is " . strtolower($this->state->name);
78    }
79}