Lines 91.66% 33 / 36
Methods 60.00% 3 / 5
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 summary 0.00% 0 / 1 0.00% 0 / 1 2
 read 91.30% 21 / 23 0.00% 0 / 1 2.00
 race 100.00% 8 / 8 100.00% 1 / 1 8
 [Lorekeeper\Server\Event\Map\MapArrivalEvent] from 100.00% 3 / 3 100.00% 1 / 1 3
18final readonly class MonstersDescribedEvent extends MapArrivalEvent
19{
20    /** @param list<MapMonster> $monsters */
21    public function __construct(
22        public array $monsters,
23    ) {}
24
25    #[Override]
26    public function summary(): string
27    {
28        return count($this->monsters) . ' kinds of monsters live in the map';
29    }
30
31    #[Override]
32    protected static function read(array $data): static
33    {
34        try {
35            $monsters = Type\shape([
36                'mondef' => Type\optional(Type\vec(Type\shape([
37                    'MonID' => Type\positive_int(),
38                    'strMonName' => Type\non_empty_string(),
39                    'intLevel' => Type\positive_int(),
40                    'sRace' => Type\string(),
41                    'strMonFileName' => Type\non_empty_string(),
42                    'strLinkage' => Type\non_empty_string(),
43                ], allowUnknownFields: true))),
44            ], allowUnknownFields: true)->coerce($data)['mondef'] ?? [];
45        } catch (CoercionException $exception) {
46            throw new MalformedEventException(static::class, 'monsters are invalid', $exception);
47        }
48
49        return new static(array_map(
50            static fn(array $monster): MapMonster => new MapMonster(
51                new MonsterId($monster['MonID']),
52                new MonsterName($monster['strMonName']),
53                new MonsterLevel($monster['intLevel']),
54                self::race($monster['sRace']),
55                new Gamefile($monster['strMonFileName']),
56                new Linkage($monster['strLinkage']),
57            ),
58            $monsters,
59        ));
60    }
61
62    /** @throws MalformedEventException */
63    private static function race(string $race): Race
64    {
65        return match ($race) {
66            'None' => Race::None,
67            'Undead' => Race::Undead,
68            'Human' => Race::Human,
69            'Elemental' => Race::Elemental,
70            'Dragonkin' => Race::Dragonkin,
71            'Unknown' => Race::Unknown,
72            default => throw new MalformedEventException(static::class, "unknown race '{$race}'"),
73        };
74    }
75}

Inherited from Lorekeeper\Server\Event\Map\MapArrivalEvent

28    final public static function from(Message $message): Option\Option
29    {
30        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'moveToArea') {
31            return Option\none();
32        }
33
34        return Option\some(static::read($message->data));
35    }