Lines
100.00%
38 / 38
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% | 31 / 31 | 100.00% | 1 / 1 | 10 | ||
| summary | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 1 | ||
| 24 | final readonly class PlayerVitalsUpdatedEvent extends PlayerUpdatedEvent | |
| 25 | { | |
| 26 | public function __construct( | |
| 27 | PlayerName $name, | |
| 28 | public Vitals $vitals, | |
| 29 | ) { | |
| 30 | parent::__construct($name); | |
| 31 | } | |
| 32 | ||
| 33 | /** @return Option\Option<static> */ | |
| 34 | #[Override] | |
| 35 | public static function from(Message $message): Option\Option | |
| 36 | { | |
| 37 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'uotls') { | |
| 38 | return Option\none(); | |
| 39 | } | |
| 40 | ||
| 41 | try { | |
| 42 | $update = Type\shape([ | |
| 43 | 'unm' => Type\optional(Type\non_empty_string()), | |
| 44 | 'o' => Type\optional(Type\shape([ | |
| 45 | 'intHP' => Type\optional(Type\uint()), | |
| 46 | 'intHPMax' => Type\optional(Type\uint()), | |
| 47 | 'intMP' => Type\optional(Type\uint()), | |
| 48 | 'intMPMax' => Type\optional(Type\uint()), | |
| 49 | ], allowUnknownFields: true)), | |
| 50 | ], allowUnknownFields: true)->coerce($message->data); | |
| 51 | } catch (CoercionException $exception) { | |
| 52 | throw new MalformedEventException(static::class, 'player name or vitals are invalid', $exception); | |
| 53 | } | |
| 54 | ||
| 55 | $name = $update['unm'] ?? null; | |
| 56 | $health = $update['o']['intHP'] ?? null; | |
| 57 | $maxHealth = $update['o']['intHPMax'] ?? null; | |
| 58 | $mana = $update['o']['intMP'] ?? null; | |
| 59 | $maxMana = $update['o']['intMPMax'] ?? null; | |
| 60 | if ($name === null || $health === null || $maxHealth === null || $mana === null || $maxMana === null) { | |
| 61 | return Option\none(); | |
| 62 | } | |
| 63 | ||
| 64 | try { | |
| 65 | return Option\some( | |
| 66 | new static( | |
| 67 | new PlayerName($name), | |
| 68 | new Vitals( | |
| 69 | new Gauge(new Health($health), new Health($maxHealth)), | |
| 70 | new Gauge(new Mana($mana), new Mana($maxMana)), | |
| 71 | ), | |
| 72 | ), | |
| 73 | ); | |
| 74 | } catch (InvalidArgumentException $exception) { | |
| 75 | throw new MalformedEventException(static::class, 'health or mana is above its maximum', $exception); | |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 79 | #[Override] | |
| 80 | public function summary(): string | |
| 81 | { | |
| 82 | $health = $this->vitals->health; | |
| 83 | $mana = $this->vitals->mana; | |
| 84 | ||
| 85 | return ( | |
| 86 | "{$this->name->value} has {$health->current}/{$health->maximum} health" | |
| 87 | . " and {$mana->current}/{$mana->maximum} mana" | |
| 88 | ); | |
| 89 | } | |
| 90 | } |