Lines
100.00%
28 / 28
Methods
100.00%
3 / 3
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| from | 100.00% | 26 / 26 | 100.00% | 1 / 1 | 7 | ||
| summary | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| 23 | final readonly class PlayerStateUpdatedEvent extends PlayerUpdatedEvent | |
| 24 | { | |
| 25 | public function __construct( | |
| 26 | PlayerName $name, | |
| 27 | public CombatState $state, | |
| 28 | public ?Health $health = null, | |
| 29 | public ?Mana $mana = null, | |
| 30 | ) { | |
| 31 | parent::__construct($name); | |
| 32 | } | |
| 33 | ||
| 34 | /** @return Option\Option<static> */ | |
| 35 | #[Override] | |
| 36 | public static function from(Message $message): Option\Option | |
| 37 | { | |
| 38 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'uotls') { | |
| 39 | return Option\none(); | |
| 40 | } | |
| 41 | ||
| 42 | try { | |
| 43 | $update = Type\shape([ | |
| 44 | 'unm' => Type\optional(Type\mixed()), | |
| 45 | 'o' => Type\optional(Type\shape([ | |
| 46 | 'intState' => Type\optional(Type\int_range(min: 0, max: 2)), | |
| 47 | 'intHP' => Type\optional(Type\uint()), | |
| 48 | 'intMP' => Type\optional(Type\uint()), | |
| 49 | ], allowUnknownFields: true)), | |
| 50 | ], allowUnknownFields: true)->coerce($message->data); | |
| 51 | $state = $update['o']['intState'] ?? null; | |
| 52 | if ($state === null) { | |
| 53 | return Option\none(); | |
| 54 | } | |
| 55 | ||
| 56 | $name = Type\non_empty_string()->coerce($update['unm'] ?? null); | |
| 57 | } catch (CoercionException $exception) { | |
| 58 | throw new MalformedEventException(static::class, 'player name or state is invalid', $exception); | |
| 59 | } | |
| 60 | ||
| 61 | $health = $update['o']['intHP'] ?? null; | |
| 62 | $mana = $update['o']['intMP'] ?? null; | |
| 63 | ||
| 64 | return Option\some( | |
| 65 | new static( | |
| 66 | new PlayerName($name), | |
| 67 | CombatStates::read($state), | |
| 68 | $health === null ? null : new Health($health), | |
| 69 | $mana === null ? null : new Mana($mana), | |
| 70 | ), | |
| 71 | ); | |
| 72 | } | |
| 73 | ||
| 74 | #[Override] | |
| 75 | public function summary(): string | |
| 76 | { | |
| 77 | return "{$this->name->value} is " . strtolower($this->state->name); | |
| 78 | } | |
| 79 | } |