Lines
100.00%
43 / 43
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| HeroDataLoadedEvent | 100.00% | 43 / 43 | 100.00% | 3 / 3 | 8 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| from | 100.00% | 41 / 41 | 100.00% | 1 / 1 | 6 | |||||
| summary | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Event\Player\Hero; | |
| 6 | ||
| 7 | use Lorekeeper\Core\Game\Level\Experience; | |
| 8 | use Lorekeeper\Core\Player\Hero\Inventory\HeroWallet; | |
| 9 | use Lorekeeper\Core\Player\PlayerLevel; | |
| 10 | use Lorekeeper\Core\Player\PlayerName; | |
| 11 | use Lorekeeper\Core\Player\PlayerSpawnId; | |
| 12 | use Lorekeeper\Core\Support\Gauge; | |
| 13 | use Lorekeeper\Core\Support\Quantity; | |
| 14 | use Lorekeeper\Core\World\Currency\AdventureCoins; | |
| 15 | use Lorekeeper\Core\World\Currency\Gold; | |
| 16 | use Lorekeeper\Server\Event\Event; | |
| 17 | use Lorekeeper\Server\Event\NamedAfterClass; | |
| 18 | use Lorekeeper\Server\Event\Player\Helper\PlayerEntries; | |
| 19 | use Lorekeeper\Server\Event\Quest\Helper\QuestGroups; | |
| 20 | use Lorekeeper\Server\Exception\MalformedEventException; | |
| 21 | use Lorekeeper\Server\Message\JsonMessage; | |
| 22 | use Lorekeeper\Server\Message\Message; | |
| 23 | use Override; | |
| 24 | use Psl\Option; | |
| 25 | use Psl\Type; | |
| 26 | use Psl\Type\Exception\CoercionException; | |
| 27 | ||
| 28 | /** | |
| 29 | * The server sent our player's level, wallet, membership, bag slots and quests. | |
| 30 | */ | |
| 31 | final readonly class HeroDataLoadedEvent implements Event | |
| 32 | { | |
| 33 | use NamedAfterClass; | |
| 34 | ||
| 35 | public function __construct( | |
| 36 | public HeroData $data, | |
| 37 | ) {} | |
| 38 | ||
| 39 | /** @return Option\Option<static> */ | |
| 40 | #[Override] | |
| 41 | public static function from(Message $message): Option\Option | |
| 42 | { | |
| 43 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'initUserDatas') { | |
| 44 | return Option\none(); | |
| 45 | } | |
| 46 | ||
| 47 | $hero = PlayerEntries::hero($message, static::class); | |
| 48 | if ($hero->isNone()) { | |
| 49 | return Option\none(); | |
| 50 | } | |
| 51 | ||
| 52 | try { | |
| 53 | $entry = Type\shape([ | |
| 54 | 'uid' => Type\positive_int(), | |
| 55 | 'data' => Type\shape([ | |
| 56 | 'strUsername' => Type\non_empty_string(), | |
| 57 | 'intLevel' => Type\int_range(min: 1, max: 100), | |
| 58 | 'intExp' => Type\uint(), | |
| 59 | 'intExpToLevel' => Type\uint(), | |
| 60 | 'intGold' => Type\uint(), | |
| 61 | 'intCoins' => Type\uint(), | |
| 62 | 'iUpg' => Type\int(), | |
| 63 | 'iUpgDays' => Type\int(), | |
| 64 | 'iBagSlots' => Type\positive_int(), | |
| 65 | 'iBankSlots' => Type\positive_int(), | |
| 66 | ], allowUnknownFields: true), | |
| 67 | ], allowUnknownFields: true)->coerce($hero->unwrap()); | |
| 68 | } catch (CoercionException $exception) { | |
| 69 | throw new MalformedEventException(static::class, 'hero data is missing or invalid', $exception); | |
| 70 | } | |
| 71 | ||
| 72 | $data = $entry['data']; | |
| 73 | ||
| 74 | return Option\some( | |
| 75 | new static( | |
| 76 | new HeroData( | |
| 77 | new PlayerSpawnId($entry['uid']), | |
| 78 | new PlayerName($data['strUsername']), | |
| 79 | new PlayerLevel($data['intLevel']), | |
| 80 | new Gauge( | |
| 81 | new Experience($data['intExp']), | |
| 82 | new Experience(max($data['intExp'], $data['intExpToLevel'])), | |
| 83 | ), | |
| 84 | new HeroWallet(new Gold($data['intGold']), new AdventureCoins($data['intCoins'])), | |
| 85 | $data['iUpg'] > 0 && $data['iUpgDays'] >= 0, | |
| 86 | new Quantity($data['iBagSlots']), | |
| 87 | new Quantity($data['iBankSlots']), | |
| 88 | QuestGroups::held($data), | |
| 89 | ), | |
| 90 | ), | |
| 91 | ); | |
| 92 | } | |
| 93 | ||
| 94 | #[Override] | |
| 95 | public function summary(): string | |
| 96 | { | |
| 97 | return "Data of {$this->data->name->value}"; | |
| 98 | } | |
| 99 | } |