Lines
92.00%
23 / 25
Methods
75.00%
3 / 4
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| read | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 4 | ||
| summary | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| [Lorekeeper\Server\Event\Combat\CombatRoundEvent] from | 85.71% | 12 / 14 | 0.00% | 0 / 1 | 4.05 | ||
| 12 | final readonly class PlayersHealthChangedEvent extends CombatRoundEvent | |
| 13 | { | |
| 14 | /** @param non-empty-list<PlayerHealth> $players */ | |
| 15 | public function __construct( | |
| 16 | public array $players, | |
| 17 | ) {} | |
| 18 | ||
| 19 | #[Override] | |
| 20 | protected static function read(array $monsters, array $players): Option\Option | |
| 21 | { | |
| 22 | $changed = []; | |
| 23 | foreach ($players as $name => $player) { | |
| 24 | if (!array_key_exists('intHP', $player)) { | |
| 25 | continue; | |
| 26 | } | |
| 27 | ||
| 28 | $changed[] = new PlayerHealth(new PlayerName($name), new Health($player['intHP'])); | |
| 29 | } | |
| 30 | ||
| 31 | return $changed === [] ? Option\none() : Option\some(new static($changed)); | |
| 32 | } | |
| 33 | ||
| 34 | #[Override] | |
| 35 | public function summary(): string | |
| 36 | { | |
| 37 | return implode(', ', array_map( | |
| 38 | static fn(PlayerHealth $player): string => "{$player->player->value} has {$player->health->value} health", | |
| 39 | $this->players, | |
| 40 | )); | |
| 41 | } | |
| 42 | } |
Inherited from Lorekeeper\Server\Event\Combat\CombatRoundEvent
| 31 | final public static function from(Message $message): Option\Option | |
| 32 | { | |
| 33 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'ct') { | |
| 34 | return Option\none(); | |
| 35 | } | |
| 36 | ||
| 37 | $status = Type\shape([ | |
| 38 | 'intHP' => Type\optional(Type\uint()), | |
| 39 | 'intMP' => Type\optional(Type\uint()), | |
| 40 | 'intState' => Type\optional(Type\int_range(min: 0, max: 2)), | |
| 41 | ], allowUnknownFields: true); | |
| 42 | ||
| 43 | try { | |
| 44 | $round = Type\shape([ | |
| 45 | 'm' => Type\optional(Type\dict(Type\positive_int(), $status)), | |
| 46 | 'p' => Type\optional(Type\dict(Type\non_empty_string(), $status)), | |
| 47 | ], allowUnknownFields: true)->coerce($message->data); | |
| 48 | } catch (CoercionException $exception) { | |
| 49 | throw new MalformedEventException(static::class, 'combat round is invalid', $exception); | |
| 50 | } | |
| 51 | ||
| 52 | return static::read($round['m'] ?? [], $round['p'] ?? []); | |
| 53 | } |