Lines
84.21%
16 / 19
Functions and Methods
33.33%
1 / 3
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| MonstersPlacedEvent | 84.21% | 16 / 19 | 33.33% | 1 / 3 | 4.06 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| read | 88.23% | 15 / 17 | 0.00% | 0 / 1 | 2.01 | |||||
| summary | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Event\Map; | |
| 6 | ||
| 7 | use Lorekeeper\Core\World\Map\Cell; | |
| 8 | use Lorekeeper\Core\World\Monster\MonsterId; | |
| 9 | use Lorekeeper\Core\World\Monster\MonsterSpawnId; | |
| 10 | use Lorekeeper\Server\Exception\MalformedEventException; | |
| 11 | use Override; | |
| 12 | use Psl\Type; | |
| 13 | use Psl\Type\Exception\CoercionException; | |
| 14 | ||
| 15 | final readonly class MonstersPlacedEvent extends MapArrivalEvent | |
| 16 | { | |
| 17 | /** @param list<MonsterPlacement> $placements */ | |
| 18 | public function __construct( | |
| 19 | public array $placements, | |
| 20 | ) {} | |
| 21 | ||
| 22 | #[Override] | |
| 23 | protected static function read(array $data): static | |
| 24 | { | |
| 25 | try { | |
| 26 | $data = Type\shape([ | |
| 27 | 'monmap' => Type\optional(Type\vec(Type\shape([ | |
| 28 | 'MonMapID' => Type\positive_int(), | |
| 29 | 'MonID' => Type\positive_int(), | |
| 30 | 'strFrame' => Type\non_empty_string(), | |
| 31 | ], allowUnknownFields: true))), | |
| 32 | ], allowUnknownFields: true)->coerce($data); | |
| 33 | } catch (CoercionException $exception) { | |
| 34 | throw new MalformedEventException(static::class, 'monster placements are invalid', $exception); | |
| 35 | } | |
| 36 | ||
| 37 | return new static(array_map( | |
| 38 | static fn(array $placement): MonsterPlacement => new MonsterPlacement( | |
| 39 | new MonsterSpawnId($placement['MonMapID']), | |
| 40 | new MonsterId($placement['MonID']), | |
| 41 | new Cell($placement['strFrame']), | |
| 42 | ), | |
| 43 | $data['monmap'] ?? [], | |
| 44 | )); | |
| 45 | } | |
| 46 | ||
| 47 | #[Override] | |
| 48 | public function summary(): string | |
| 49 | { | |
| 50 | return count($this->placements) . ' monsters stand in the map'; | |
| 51 | } | |
| 52 | } |