Lines
100.00%
15 / 15
Functions and Methods
100.00%
4 / 4
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| PlayerEntries | 100.00% | 15 / 15 | 100.00% | 4 / 4 | 7 | 100.00% | 1 / 1 | |||
| hero | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 3 | |||||
| others | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | |||||
| all | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 2 | |||||
| isHero | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Event\Player\Helper; | |
| 6 | ||
| 7 | use Lorekeeper\Server\Event\Event; | |
| 8 | use Lorekeeper\Server\Exception\MalformedEventException; | |
| 9 | use Lorekeeper\Server\Message\JsonMessage; | |
| 10 | use Psl\Option; | |
| 11 | use Psl\Type; | |
| 12 | use Psl\Type\Exception\CoercionException; | |
| 13 | ||
| 14 | final readonly class PlayerEntries | |
| 15 | { | |
| 16 | /** | |
| 17 | * @param class-string<Event> $event | |
| 18 | * | |
| 19 | * @throws MalformedEventException | |
| 20 | * | |
| 21 | * @return Option\Option<array{data: array<string, mixed>, ...}> | |
| 22 | */ | |
| 23 | public static function hero(JsonMessage $message, string $event): Option\Option | |
| 24 | { | |
| 25 | $heroes = array_values(array_filter(self::all($message, $event), self::isHero(...))); | |
| 26 | if (count($heroes) > 1) { | |
| 27 | throw new MalformedEventException($event, 'the data is of more than one hero'); | |
| 28 | } | |
| 29 | ||
| 30 | return $heroes === [] ? Option\none() : Option\some($heroes[0]); | |
| 31 | } | |
| 32 | ||
| 33 | /** | |
| 34 | * @param class-string<Event> $event | |
| 35 | * | |
| 36 | * @throws MalformedEventException | |
| 37 | * | |
| 38 | * @return list<array{data: array<string, mixed>, ...}> | |
| 39 | */ | |
| 40 | public static function others(JsonMessage $message, string $event): array | |
| 41 | { | |
| 42 | return array_values(array_filter( | |
| 43 | self::all($message, $event), | |
| 44 | static fn(array $entry): bool => !self::isHero($entry), | |
| 45 | )); | |
| 46 | } | |
| 47 | ||
| 48 | /** | |
| 49 | * @param class-string<Event> $event | |
| 50 | * | |
| 51 | * @throws MalformedEventException | |
| 52 | * | |
| 53 | * @return list<array{data: array<string, mixed>, ...}> | |
| 54 | */ | |
| 55 | private static function all(JsonMessage $message, string $event): array | |
| 56 | { | |
| 57 | try { | |
| 58 | return Type\vec(Type\shape([ | |
| 59 | 'data' => Type\dict(Type\string(), Type\mixed()), | |
| 60 | ], allowUnknownFields: true)) | |
| 61 | ->coerce($message->data['a'] ?? null); | |
| 62 | } catch (CoercionException $exception) { | |
| 63 | throw new MalformedEventException($event, 'player data is missing or invalid', $exception); | |
| 64 | } | |
| 65 | } | |
| 66 | ||
| 67 | /** @param array{data: array<string, mixed>, ...} $entry */ | |
| 68 | private static function isHero(array $entry): bool | |
| 69 | { | |
| 70 | return array_key_exists('intGold', $entry['data']); | |
| 71 | } | |
| 72 | } |