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