Lines 100.00% 28 / 28
Functions and Methods 100.00% 4 / 4
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ClassChangedEvent 100.00% 28 / 28 100.00% 4 / 4 12 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 21 / 21 100.00% 1 / 1 5
 category 100.00% 5 / 5 100.00% 1 / 1 5
 summary 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Server\Event\CharacterClass;
6
7use Lorekeeper\Core\Item\Equipment\CharacterClass\ClassCategory;
8use Lorekeeper\Core\Item\Equipment\CharacterClass\ClassDescription;
9use Lorekeeper\Core\Item\Equipment\CharacterClass\ClassName;
10use Lorekeeper\Core\Item\Equipment\CharacterClass\ClassPoints;
11use Lorekeeper\Core\Player\PlayerSpawnId;
12use Lorekeeper\Server\Event\Event;
13use Lorekeeper\Server\Event\NamedAfterClass;
14use Lorekeeper\Server\Exception\MalformedEventException;
15use Lorekeeper\Server\Message\JsonMessage;
16use Lorekeeper\Server\Message\Message;
17use Override;
18use Psl\Option;
19use Psl\Type;
20use Psl\Type\Exception\CoercionException;
21
22/**
23 * A player in the room changed their class.
24 */
25final readonly class ClassChangedEvent implements Event
26{
27    use NamedAfterClass;
28
29    /** @param Option\Option<ClassDescription> $description */
30    public function __construct(
31        public PlayerSpawnId $player,
32        public ClassName $className,
33        public ClassCategory $category,
34        public ClassPoints $points,
35        public Option\Option $description,
36    ) {}
37
38    /** @return Option\Option<static> */
39    #[Override]
40    public static function from(Message $message): Option\Option
41    {
42        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'updateClass') {
43            return Option\none();
44        }
45
46        try {
47            $data = Type\shape([
48                'uid' => Type\positive_int(),
49                'sClassName' => Type\non_empty_string(),
50                'sClassCat' => Type\string(),
51                'iCP' => Type\uint(),
52                'sDesc' => Type\optional(Type\string()),
53            ], allowUnknownFields: true)->coerce($message->data);
54        } catch (CoercionException $exception) {
55            throw new MalformedEventException(static::class, 'class is missing or invalid', $exception);
56        }
57
58        $description = trim($data['sDesc'] ?? '');
59
60        return Option\some(
61            new static(
62                new PlayerSpawnId($data['uid']),
63                new ClassName($data['sClassName']),
64                self::category($data['sClassCat']),
65                new ClassPoints($data['iCP']),
66                $description === '' ? Option\none() : Option\some(new ClassDescription($description)),
67            ),
68        );
69    }
70
71    /** @throws MalformedEventException */
72    private static function category(string $code): ClassCategory
73    {
74        return match (substr($code, offset: 0, length: 1)) {
75            'M' => ClassCategory::Melee,
76            'C' => ClassCategory::Caster,
77            'S' => ClassCategory::Hybrid,
78            default => throw new MalformedEventException(static::class, "unknown class category '{$code}'"),
79        };
80    }
81
82    #[Override]
83    public function summary(): string
84    {
85        return "Spawn {$this->player->value} is {$this->className->value} ({$this->category->name}, {$this->points->value} CP)";
86    }
87}