Lines 100.00% 33 / 33
Functions and Methods 100.00% 4 / 4
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
HeroSettingsLoadedEvent 100.00% 33 / 33 100.00% 4 / 4 11 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 25 / 25 100.00% 1 / 1 6
 summary 100.00% 1 / 1 100.00% 1 / 1 1
 settings 100.00% 6 / 6 100.00% 1 / 1 3
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Server\Event\Player\Hero;
6
7use Lorekeeper\Core\Game\Setting\GameSetting;
8use Lorekeeper\Core\Player\Hero\HeroSettings;
9use Lorekeeper\Core\Player\PlayerName;
10use Lorekeeper\Core\Player\PlayerSpawnId;
11use Lorekeeper\Server\Event\Event;
12use Lorekeeper\Server\Event\NamedAfterClass;
13use Lorekeeper\Server\Event\Player\Helper\PlayerEntries;
14use Lorekeeper\Server\Exception\MalformedEventException;
15use Lorekeeper\Server\Message\JsonMessage;
16use Lorekeeper\Server\Message\Message;
17use Override;
18use Psl\Option;
19use Psl\Type;
20use Psl\Type\Exception\CoercionException;
21
22/**
23 * The server sent our player's game settings.
24 */
25final readonly class HeroSettingsLoadedEvent implements Event
26{
27    use NamedAfterClass;
28
29    private const array BITS = [
30        4 => GameSetting::Goto,
31        7 => GameSetting::FriendRequests,
32        8 => GameSetting::PartyInvites,
33        9 => GameSetting::GuildInvites,
34        10 => GameSetting::Whispers,
35        13 => GameSetting::Duels,
36    ];
37
38    public function __construct(
39        public HeroSettingsData $data,
40    ) {}
41
42    /** @return Option\Option<static> */
43    #[Override]
44    public static function from(Message $message): Option\Option
45    {
46        if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'initUserDatas') {
47            return Option\none();
48        }
49
50        $hero = PlayerEntries::hero($message, static::class);
51        if ($hero->isNone()) {
52            return Option\none();
53        }
54
55        try {
56            $entry = Type\shape([
57                'uid' => Type\positive_int(),
58                'data' => Type\shape([
59                    'strUsername' => Type\non_empty_string(),
60                    'ia1' => Type\optional(Type\int()),
61                ], allowUnknownFields: true),
62            ], allowUnknownFields: true)->coerce($hero->unwrap());
63        } catch (CoercionException $exception) {
64            throw new MalformedEventException(static::class, 'hero settings are invalid', $exception);
65        }
66
67        if (!array_key_exists('ia1', $entry['data'])) {
68            return Option\none();
69        }
70
71        return Option\some(
72            new static(
73                new HeroSettingsData(
74                    new PlayerSpawnId($entry['uid']),
75                    new PlayerName($entry['data']['strUsername']),
76                    self::settings($entry['data']['ia1']),
77                ),
78            ),
79        );
80    }
81
82    #[Override]
83    public function summary(): string
84    {
85        return "Settings of {$this->data->name->value}";
86    }
87
88    private static function settings(int $bits): HeroSettings
89    {
90        $allowed = [];
91        foreach (self::BITS as $bit => $setting) {
92            if (($bits & (1 << $bit)) !== 0) {
93                continue;
94            }
95
96            $allowed[] = $setting;
97        }
98
99        return new HeroSettings($allowed);
100    }
101}