Lines
90.90%
30 / 33
Functions and Methods
50.00%
2 / 4
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| MonstersDescribedEvent | 90.90% | 30 / 33 | 50.00% | 2 / 4 | 12.11 | 0.00% | 0 / 1 | |||
| __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 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Event\Map; | |
| 6 | ||
| 7 | use Lorekeeper\Core\Asset\Gamefile; | |
| 8 | use Lorekeeper\Core\Asset\Linkage; | |
| 9 | use Lorekeeper\Core\World\Monster\MonsterId; | |
| 10 | use Lorekeeper\Core\World\Monster\MonsterLevel; | |
| 11 | use Lorekeeper\Core\World\Monster\MonsterName; | |
| 12 | use Lorekeeper\Core\World\Monster\Race; | |
| 13 | use Lorekeeper\Server\Exception\MalformedEventException; | |
| 14 | use Override; | |
| 15 | use Psl\Type; | |
| 16 | use Psl\Type\Exception\CoercionException; | |
| 17 | ||
| 18 | final 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 | } |