Lines
100.00%
34 / 34
Methods
100.00%
5 / 5
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| from | 100.00% | 21 / 21 | 100.00% | 1 / 1 | 5 | ||
| category | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 5 | ||
| summary | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [Lorekeeper\Server\Event\NamedAfterClass] name | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 1 | ||
| 25 | final readonly class ClassChangedEvent implements Event | |
| 26 | { | |
| 27 | use NamedAfterClass; | |
| 28 | ||
| 29 | /** @param Option\Option<ClassDescription> $description */ | |
| 30 | public function __construct( | |
| 31 | public PlayerSpawnId $player, | |
| 32 | public ClassName $className, | |
| 33 | public ClassCategory $category, | |
| 34 | public ClassPoints $points, | |
| 35 | public Option\Option $description, | |
| 36 | ) {} | |
| 37 | ||
| 38 | /** @return Option\Option<static> */ | |
| 39 | #[Override] | |
| 40 | public static function from(Message $message): Option\Option | |
| 41 | { | |
| 42 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'updateClass') { | |
| 43 | return Option\none(); | |
| 44 | } | |
| 45 | ||
| 46 | try { | |
| 47 | $data = Type\shape([ | |
| 48 | 'uid' => Type\positive_int(), | |
| 49 | 'sClassName' => Type\non_empty_string(), | |
| 50 | 'sClassCat' => Type\string(), | |
| 51 | 'iCP' => Type\uint(), | |
| 52 | 'sDesc' => Type\optional(Type\string()), | |
| 53 | ], allowUnknownFields: true)->coerce($message->data); | |
| 54 | } catch (CoercionException $exception) { | |
| 55 | throw new MalformedEventException(static::class, 'class is missing or invalid', $exception); | |
| 56 | } | |
| 57 | ||
| 58 | $description = trim($data['sDesc'] ?? ''); | |
| 59 | ||
| 60 | return Option\some( | |
| 61 | new static( | |
| 62 | new PlayerSpawnId($data['uid']), | |
| 63 | new ClassName($data['sClassName']), | |
| 64 | self::category($data['sClassCat']), | |
| 65 | new ClassPoints($data['iCP']), | |
| 66 | $description === '' ? Option\none() : Option\some(new ClassDescription($description)), | |
| 67 | ), | |
| 68 | ); | |
| 69 | } | |
| 70 | ||
| 71 | /** @throws MalformedEventException */ | |
| 72 | private static function category(string $code): ClassCategory | |
| 73 | { | |
| 74 | return match (substr($code, offset: 0, length: 1)) { | |
| 75 | 'M' => ClassCategory::Melee, | |
| 76 | 'C' => ClassCategory::Caster, | |
| 77 | 'S' => ClassCategory::Hybrid, | |
| 78 | default => throw new MalformedEventException(static::class, "unknown class category '{$code}'"), | |
| 79 | }; | |
| 80 | } | |
| 81 | ||
| 82 | #[Override] | |
| 83 | public function summary(): string | |
| 84 | { | |
| 85 | return "Spawn {$this->player->value} is {$this->className->value} ({$this->category->name}, {$this->points->value} CP)"; | |
| 86 | } | |
| 87 | } |
From Lorekeeper\Server\Event\NamedAfterClass
| 7 | trait NamedAfterClass | |
| 8 | { | |
| 9 | public function name(): string | |
| 10 | { | |
| 11 | $class = static::class; | |
| 12 | ||
| 13 | return (string) preg_replace( | |
| 14 | '/Event$/', | |
| 15 | replacement: '', | |
| 16 | subject: substr($class, (int) strrpos($class, needle: '\\') + 1), | |
| 17 | ); | |
| 18 | } | |
| 19 | } |