Lines
86.95%
20 / 23
Methods
50.00%
2 / 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 | 3 | ||
| summary | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| [Lorekeeper\Server\Event\Reward\RewardedEvent] from | 86.66% | 13 / 15 | 0.00% | 0 / 1 | 4.04 | ||
| 17 | final readonly class MonsterRewardedEvent extends RewardedEvent | |
| 18 | { | |
| 19 | public function __construct( | |
| 20 | Gold $gold, | |
| 21 | Experience $experience, | |
| 22 | ClassPoints $classPoints, | |
| 23 | public MonsterSpawnId $monster, | |
| 24 | ) { | |
| 25 | parent::__construct($gold, $experience, $classPoints); | |
| 26 | } | |
| 27 | ||
| 28 | #[Override] | |
| 29 | protected static function read( | |
| 30 | array $data, | |
| 31 | Gold $gold, | |
| 32 | Experience $experience, | |
| 33 | ClassPoints $classPoints, | |
| 34 | ): Option\Option { | |
| 35 | if (($data['typ'] ?? null) !== 'm') { | |
| 36 | return Option\none(); | |
| 37 | } | |
| 38 | ||
| 39 | try { | |
| 40 | $monster = Type\positive_int()->coerce($data['id'] ?? null); | |
| 41 | } catch (CoercionException $exception) { | |
| 42 | throw new MalformedEventException(static::class, 'rewarding monster is missing or invalid', $exception); | |
| 43 | } | |
| 44 | ||
| 45 | return Option\some(new static($gold, $experience, $classPoints, new MonsterSpawnId($monster))); | |
| 46 | } | |
| 47 | ||
| 48 | #[Override] | |
| 49 | public function summary(): string | |
| 50 | { | |
| 51 | return "Earned {$this->experience->value} experience and {$this->gold->value} gold from monster {$this->monster->value}"; | |
| 52 | } | |
| 53 | } |
Inherited from Lorekeeper\Server\Event\Reward\RewardedEvent
| 46 | final public static function from(Message $message): Option\Option | |
| 47 | { | |
| 48 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'addGoldExp') { | |
| 49 | return Option\none(); | |
| 50 | } | |
| 51 | ||
| 52 | try { | |
| 53 | $reward = Type\shape([ | |
| 54 | 'intGold' => Type\optional(Type\uint()), | |
| 55 | 'intExp' => Type\optional(Type\uint()), | |
| 56 | 'iCP' => Type\optional(Type\uint()), | |
| 57 | ], allowUnknownFields: true)->coerce($message->data); | |
| 58 | } catch (CoercionException $exception) { | |
| 59 | throw new MalformedEventException(static::class, 'reward is invalid', $exception); | |
| 60 | } | |
| 61 | ||
| 62 | return static::read( | |
| 63 | $message->data, | |
| 64 | new Gold($reward['intGold'] ?? 0), | |
| 65 | new Experience($reward['intExp'] ?? 0), | |
| 66 | new ClassPoints($reward['iCP'] ?? 0), | |
| 67 | ); | |
| 68 | } |