Lines
100.00%
76 / 76
Methods
100.00%
9 / 9
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| hear | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 13 | ||
| missing | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 1 | ||
| character | 100.00% | 42 / 42 | 100.00% | 1 / 1 | 10 | ||
| me | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 2 | ||
| mine | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | ||
| settingsOf | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | ||
| isMine | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| isMe | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| 44 | final class Entrance | |
| 45 | { | |
| 46 | private ?PlayerSpawnId $spawnId = null; | |
| 47 | ||
| 48 | private ?MovedToMapEvent $arrival = null; | |
| 49 | ||
| 50 | /** @var ?list<HeldItem> */ | |
| 51 | private ?array $inventory = null; | |
| 52 | ||
| 53 | private ?HeroData $data = null; | |
| 54 | ||
| 55 | private ?ClassChangedEvent $class = null; | |
| 56 | ||
| 57 | private ?SkillsLoadedEvent $skills = null; | |
| 58 | ||
| 59 | private ?Vitals $vitals = null; | |
| 60 | ||
| 61 | private ?HeroSettings $settings = null; | |
| 62 | ||
| 63 | private Stats $stats; | |
| 64 | ||
| 65 | public function __construct( | |
| 66 | private readonly PlayerName $name, | |
| 67 | ) { | |
| 68 | $this->stats = new Stats(); | |
| 69 | } | |
| 70 | ||
| 71 | /** @mago-expect lint:halstead */ | |
| 72 | public function hear(Event $event): void | |
| 73 | { | |
| 74 | match (true) { | |
| 75 | $event instanceof LoginSucceededEvent => $this->spawnId = $event->spawnId, | |
| 76 | $event instanceof MovedToMapEvent => $this->arrival = $event, | |
| 77 | $event instanceof InventoryLoadedEvent => $this->inventory = $event->items, | |
| 78 | $event instanceof HeroDataLoadedEvent => $this->data = $this->mine($event) ?? $this->data, | |
| 79 | $event instanceof HeroSettingsLoadedEvent => $this->settings = $this->settingsOf($event) ?? $this->settings, | |
| 80 | $event instanceof ClassChangedEvent => $this->isMine($event->player) ? ($this->class = $event) : null, | |
| 81 | $event instanceof SkillsLoadedEvent => $this->skills = $event, | |
| 82 | $event instanceof HeroStatsUpdatedEvent => StatsTracking::follow($this->stats, $event), | |
| 83 | $event instanceof PlayerVitalsUpdatedEvent => $this->isMe($event->name) | |
| 84 | ? ($this->vitals = $event->vitals) | |
| 85 | : null, | |
| 86 | default => null, | |
| 87 | }; | |
| 88 | } | |
| 89 | ||
| 90 | /** @return list<class-string<Event>> */ | |
| 91 | public function missing(): array | |
| 92 | { | |
| 93 | return array_keys(array_filter([ | |
| 94 | LoginSucceededEvent::class => $this->spawnId === null, | |
| 95 | MovedToMapEvent::class => $this->me() === null, | |
| 96 | InventoryLoadedEvent::class => $this->inventory === null, | |
| 97 | HeroDataLoadedEvent::class => $this->data === null, | |
| 98 | ClassChangedEvent::class => $this->class === null, | |
| 99 | SkillsLoadedEvent::class => $this->skills === null, | |
| 100 | PlayerVitalsUpdatedEvent::class => $this->vitals === null, | |
| 101 | ])); | |
| 102 | } | |
| 103 | ||
| 104 | /** @throws LogicException */ | |
| 105 | public function character(): Character | |
| 106 | { | |
| 107 | $spawnId = $this->spawnId; | |
| 108 | $arrival = $this->arrival; | |
| 109 | $me = $this->me(); | |
| 110 | $items = $this->inventory; | |
| 111 | $class = $this->class; | |
| 112 | $skills = $this->skills; | |
| 113 | $vitals = $this->vitals; | |
| 114 | $data = $this->data; | |
| 115 | ||
| 116 | if ( | |
| 117 | $spawnId === null | |
| 118 | || $arrival === null | |
| 119 | || $me === null | |
| 120 | || $items === null | |
| 121 | || $class === null | |
| 122 | || $skills === null | |
| 123 | || $vitals === null | |
| 124 | || $data === null | |
| 125 | ) { | |
| 126 | throw new LogicException('Still waiting for ' . implode(', ', $this->missing()) . '.'); | |
| 127 | } | |
| 128 | ||
| 129 | $inventory = new HeroInventory($data->bagSlots, $data->bankSlots, $data->wallet); | |
| 130 | InventoryTracking::loaded($inventory, $items); | |
| 131 | ||
| 132 | $hero = new Hero( | |
| 133 | $spawnId, | |
| 134 | $this->name, | |
| 135 | $data->level, | |
| 136 | new HeroAttributes($vitals, $this->stats), | |
| 137 | new HeroClass( | |
| 138 | new CharacterClass( | |
| 139 | $class->className, | |
| 140 | $class->category, | |
| 141 | $class->description, | |
| 142 | array_map(static fn(HeroSkill $skill): Skill => $skill->skill, $skills->skills), | |
| 143 | $skills->passives, | |
| 144 | ), | |
| 145 | $class->points, | |
| 146 | $skills->skills, | |
| 147 | ), | |
| 148 | new HeroProgression(new LevelProgression($data->experience), new QuestsProgression($data->quests)), | |
| 149 | $inventory, | |
| 150 | $this->settings ?? new HeroSettings(GameSetting::cases()), | |
| 151 | ); | |
| 152 | if ($data->member) { | |
| 153 | $hero->tags->add(PlayerTag::Member); | |
| 154 | } | |
| 155 | ||
| 156 | return new Character($hero, new Location($arrival->areaId, $arrival->map, $me->spot), $me->state, away: false); | |
| 157 | } | |
| 158 | ||
| 159 | private function me(): ?MapPlayer | |
| 160 | { | |
| 161 | $spawnId = $this->spawnId; | |
| 162 | if ($spawnId === null) { | |
| 163 | return null; | |
| 164 | } | |
| 165 | ||
| 166 | return $this->arrival | |
| 167 | ?->map | |
| 168 | ->players | |
| 169 | ->find($spawnId) | |
| 170 | ->unwrapOr(null); | |
| 171 | } | |
| 172 | ||
| 173 | private function mine(HeroDataLoadedEvent $event): ?HeroData | |
| 174 | { | |
| 175 | return $this->isMine($event->data->spawnId) ? $event->data : null; | |
| 176 | } | |
| 177 | ||
| 178 | private function settingsOf(HeroSettingsLoadedEvent $event): ?HeroSettings | |
| 179 | { | |
| 180 | return $this->isMine($event->data->spawnId) ? $event->data->settings : null; | |
| 181 | } | |
| 182 | ||
| 183 | private function isMine(PlayerSpawnId $spawnId): bool | |
| 184 | { | |
| 185 | return $this->spawnId?->equals($spawnId) ?? false; | |
| 186 | } | |
| 187 | ||
| 188 | private function isMe(PlayerName $name): bool | |
| 189 | { | |
| 190 | return strcasecmp($name->value, $this->name->value) === 0; | |
| 191 | } | |
| 192 | } |