Lines 100.00% 8 / 8
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
MonsterRewardedEvent 100.00% 8 / 8 100.00% 3 / 3 5 100.00% 1 / 1
 __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
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Server\Event\Reward;
6
7use Lorekeeper\Core\Game\Level\Experience;
8use Lorekeeper\Core\Item\Equipment\CharacterClass\ClassPoints;
9use Lorekeeper\Core\World\Currency\Gold;
10use Lorekeeper\Core\World\Monster\MonsterSpawnId;
11use Lorekeeper\Server\Exception\MalformedEventException;
12use Override;
13use Psl\Option;
14use Psl\Type;
15use Psl\Type\Exception\CoercionException;
16
17/**
18 * Our player earned gold, experience or class points for killing a monster.
19 */
20final 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}