Lines
100.00%
149 / 149
Methods
100.00%
11 / 11
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| from | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 4 | ||
| questType | 100.00% | 43 / 43 | 100.00% | 1 / 1 | 1 | ||
| quest | 100.00% | 27 / 27 | 100.00% | 1 / 1 | 6 | ||
| requirements | 100.00% | 24 / 24 | 100.00% | 1 / 1 | 10 | ||
| rewards | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 6 | ||
| items | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 4 | ||
| described | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| rewardType | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 4 | ||
| summary | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| [Lorekeeper\Server\Event\NamedAfterClass] name | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 1 | ||
| 62 | final readonly class QuestLoadedEvent implements Event | |
| 63 | { | |
| 64 | use NamedAfterClass; | |
| 65 | ||
| 66 | private const int NO_FACTION = 1; | |
| 67 | ||
| 68 | /** @param list<Quest> $quests */ | |
| 69 | public function __construct( | |
| 70 | public array $quests, | |
| 71 | ) {} | |
| 72 | ||
| 73 | /** @return Option\Option<static> */ | |
| 74 | #[Override] | |
| 75 | public static function from(Message $message): Option\Option | |
| 76 | { | |
| 77 | if (!$message instanceof JsonMessage || ($message->data['cmd'] ?? null) !== 'getQuests') { | |
| 78 | return Option\none(); | |
| 79 | } | |
| 80 | ||
| 81 | try { | |
| 82 | $quests = array_values(Coerce\dict(Coerce\array_key(), self::questType())->coerce( | |
| 83 | $message->data['quests'] ?? null, | |
| 84 | )); | |
| 85 | } catch (CoercionException $exception) { | |
| 86 | throw new MalformedEventException(static::class, 'quests are missing or invalid', $exception); | |
| 87 | } | |
| 88 | ||
| 89 | return Option\some(new static(array_map(self::quest(...), $quests))); | |
| 90 | } | |
| 91 | ||
| 92 | /** @return Coerce\TypeInterface<QuestData> */ | |
| 93 | private static function questType(): Coerce\TypeInterface | |
| 94 | { | |
| 95 | $items = Coerce\optional(Coerce\dict(Coerce\array_key(), Coerce\shape([ | |
| 96 | 'ItemID' => Coerce\positive_int(), | |
| 97 | 'sName' => Coerce\non_empty_string(), | |
| 98 | 'sType' => Coerce\string(), | |
| 99 | 'bUpg' => Coerce\optional(Coerce\bool()), | |
| 100 | ], allowUnknownFields: true))); | |
| 101 | $entry = Coerce\shape([ | |
| 102 | 'ItemID' => Coerce\positive_int(), | |
| 103 | 'iQty' => Coerce\positive_int(), | |
| 104 | ], allowUnknownFields: true); | |
| 105 | ||
| 106 | return Coerce\shape([ | |
| 107 | 'QuestID' => Coerce\positive_int(), | |
| 108 | 'sName' => Coerce\non_empty_string(), | |
| 109 | 'sDesc' => Coerce\string(), | |
| 110 | 'sEndText' => Coerce\optional(Coerce\string()), | |
| 111 | 'iLvl' => Coerce\uint(), | |
| 112 | 'iExp' => Coerce\uint(), | |
| 113 | 'iGold' => Coerce\uint(), | |
| 114 | 'iClass' => Coerce\uint(), | |
| 115 | 'iReqCP' => Coerce\uint(), | |
| 116 | 'FactionID' => Coerce\positive_int(), | |
| 117 | 'iReqRep' => Coerce\uint(), | |
| 118 | 'iRep' => Coerce\uint(), | |
| 119 | 'bUpg' => Coerce\bool(), | |
| 120 | 'bQuest' => Coerce\optional(Coerce\union(Coerce\bool(), Coerce\int())), | |
| 121 | 'strPrevious' => Coerce\optional(Coerce\string()), | |
| 122 | 'sField' => Coerce\optional(Coerce\string()), | |
| 123 | 'iIndex' => Coerce\optional(Coerce\int()), | |
| 124 | 'turnin' => Coerce\optional(Coerce\vec($entry)), | |
| 125 | 'reward' => Coerce\optional(Coerce\vec(Coerce\shape([ | |
| 126 | 'ItemID' => Coerce\positive_int(), | |
| 127 | 'iQty' => Coerce\positive_int(), | |
| 128 | 'iRate' => Coerce\num(), | |
| 129 | 'iType' => Coerce\int(), | |
| 130 | ], allowUnknownFields: true))), | |
| 131 | 'reqd' => Coerce\optional(Coerce\vec($entry)), | |
| 132 | 'oItems' => $items, | |
| 133 | 'oReqd' => $items, | |
| 134 | 'oRewards' => Coerce\optional(Coerce\shape([ | |
| 135 | 'itemsS' => $items, | |
| 136 | 'itemsR' => $items, | |
| 137 | ], allowUnknownFields: true)), | |
| 138 | ], allowUnknownFields: true); | |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * @param QuestData $quest | |
| 143 | * | |
| 144 | * @throws MalformedEventException | |
| 145 | */ | |
| 146 | private static function quest(array $quest): Quest | |
| 147 | { | |
| 148 | $description = trim($quest['sDesc']); | |
| 149 | $items = self::items($quest); | |
| 150 | ||
| 151 | $groupId = QuestGroups::of($quest['sField'] ?? '', $quest['iIndex'] ?? -1); | |
| 152 | $loaded = new Quest( | |
| 153 | new QuestId($quest['QuestID']), | |
| 154 | new QuestName($quest['sName']), | |
| 155 | self::requirements($quest, $items), | |
| 156 | array_map( | |
| 157 | static fn(array $turnIn): QuestObjective => new ItemObjective( | |
| 158 | self::described($items, $turnIn['ItemID']), | |
| 159 | new Quantity($turnIn['iQty']), | |
| 160 | ), | |
| 161 | $quest['turnin'] ?? [], | |
| 162 | ), | |
| 163 | self::rewards($quest, $items), | |
| 164 | $description === '' | |
| 165 | ? Option\none() | |
| 166 | : Option\some(new QuestDescription($quest['sDesc'], trim($quest['sEndText'] ?? ''))), | |
| 167 | $groupId, | |
| 168 | ); | |
| 169 | ||
| 170 | if ($quest['bUpg']) { | |
| 171 | $loaded->tags->add(QuestTag::MemberOnly); | |
| 172 | } | |
| 173 | ||
| 174 | $groupId->map(static fn(QuestGroupId $id) => $loaded->tags->add(match ($id->group) { | |
| 175 | QuestGroup::Daily => QuestTag::Daily, | |
| 176 | QuestGroup::Weekly => QuestTag::Weekly, | |
| 177 | QuestGroup::Monthly => QuestTag::Monthly, | |
| 178 | })); | |
| 179 | ||
| 180 | return $loaded; | |
| 181 | } | |
| 182 | ||
| 183 | /** | |
| 184 | * @param QuestData $quest | |
| 185 | * @param array<int, Item> $items | |
| 186 | * | |
| 187 | * @throws MalformedEventException | |
| 188 | * | |
| 189 | * @return list<QuestRequirement> | |
| 190 | */ | |
| 191 | private static function requirements(array $quest, array $items): array | |
| 192 | { | |
| 193 | $requirements = []; | |
| 194 | ||
| 195 | if ($quest['iLvl'] > 1) { | |
| 196 | $requirements[] = new PlayerLevelRequirement(new PlayerLevel($quest['iLvl'])); | |
| 197 | } | |
| 198 | ||
| 199 | if ($quest['iClass'] > 0) { | |
| 200 | $requirements[] = new ClassRankRequirement( | |
| 201 | new ClassId($quest['iClass']), | |
| 202 | Rank::of(new ClassPoints($quest['iReqCP'])), | |
| 203 | ); | |
| 204 | } | |
| 205 | ||
| 206 | if ($quest['iReqRep'] > 0 && $quest['FactionID'] !== self::NO_FACTION) { | |
| 207 | $requirements[] = new FactionRankRequirement( | |
| 208 | new FactionId($quest['FactionID']), | |
| 209 | Rank::of(new Reputation($quest['iReqRep'])), | |
| 210 | ); | |
| 211 | } | |
| 212 | ||
| 213 | if ($quest['bUpg']) { | |
| 214 | $requirements[] = new MemberRequirement(); | |
| 215 | } | |
| 216 | ||
| 217 | $previous = $quest['strPrevious'] ?? ''; | |
| 218 | if (($quest['bQuest'] ?? false) && ctype_digit($previous) && (int) $previous > 0) { | |
| 219 | $requirements[] = new PreviousQuestRequirement(new QuestId((int) $previous)); | |
| 220 | } | |
| 221 | ||
| 222 | foreach ($quest['reqd'] ?? [] as $item) { | |
| 223 | $requirements[] = new ItemRequirement( | |
| 224 | self::described($items, $item['ItemID']), | |
| 225 | new Quantity($item['iQty']), | |
| 226 | ); | |
| 227 | } | |
| 228 | ||
| 229 | return $requirements; | |
| 230 | } | |
| 231 | ||
| 232 | /** | |
| 233 | * @param QuestData $quest | |
| 234 | * @param array<int, Item> $items | |
| 235 | * | |
| 236 | * @throws MalformedEventException | |
| 237 | * | |
| 238 | * @return list<QuestReward> | |
| 239 | */ | |
| 240 | private static function rewards(array $quest, array $items): array | |
| 241 | { | |
| 242 | $rewards = []; | |
| 243 | ||
| 244 | if ($quest['iGold'] > 0) { | |
| 245 | $rewards[] = new GoldReward(new Gold($quest['iGold'])); | |
| 246 | } | |
| 247 | ||
| 248 | if ($quest['iExp'] > 0) { | |
| 249 | $rewards[] = new ExperienceReward(new Experience($quest['iExp'])); | |
| 250 | } | |
| 251 | ||
| 252 | if ($quest['iRep'] > 0 && $quest['FactionID'] !== self::NO_FACTION) { | |
| 253 | $rewards[] = new ReputationReward(new FactionId($quest['FactionID']), new Reputation($quest['iRep'])); | |
| 254 | } | |
| 255 | ||
| 256 | foreach ($quest['reward'] ?? [] as $item) { | |
| 257 | $rewards[] = new ItemReward( | |
| 258 | self::described($items, $item['ItemID']), | |
| 259 | new DropRate((float) $item['iRate']), | |
| 260 | new Quantity($item['iQty']), | |
| 261 | self::rewardType($item['iType']), | |
| 262 | ); | |
| 263 | } | |
| 264 | ||
| 265 | return $rewards; | |
| 266 | } | |
| 267 | ||
| 268 | /** | |
| 269 | * @param QuestData $quest | |
| 270 | * | |
| 271 | * @throws MalformedEventException | |
| 272 | * | |
| 273 | * @return array<int, Item> | |
| 274 | */ | |
| 275 | private static function items(array $quest): array | |
| 276 | { | |
| 277 | $rewards = $quest['oRewards'] ?? []; | |
| 278 | $items = []; | |
| 279 | foreach ([ | |
| 280 | ...array_values($quest['oItems'] ?? []), | |
| 281 | ...array_values($quest['oReqd'] ?? []), | |
| 282 | ...array_values($rewards['itemsS'] ?? []), | |
| 283 | ...array_values($rewards['itemsR'] ?? []), | |
| 284 | ] as $described) { | |
| 285 | if (array_key_exists($described['ItemID'], $items)) { | |
| 286 | continue; | |
| 287 | } | |
| 288 | ||
| 289 | $item = ItemKinds::read($described, static::class); | |
| 290 | if ($described['bUpg'] ?? false) { | |
| 291 | $item->tags->add(ItemTag::MemberOnly); | |
| 292 | } | |
| 293 | ||
| 294 | $items[$described['ItemID']] = $item; | |
| 295 | } | |
| 296 | ||
| 297 | return $items; | |
| 298 | } | |
| 299 | ||
| 300 | /** | |
| 301 | * @param array<int, Item> $items | |
| 302 | * | |
| 303 | * @throws MalformedEventException | |
| 304 | */ | |
| 305 | private static function described(array $items, int $id): Item | |
| 306 | { | |
| 307 | return $items[$id] ?? throw new MalformedEventException(static::class, "item {$id} is not described"); | |
| 308 | } | |
| 309 | ||
| 310 | /** @throws MalformedEventException */ | |
| 311 | private static function rewardType(int $type): ItemRewardType | |
| 312 | { | |
| 313 | return match ($type) { | |
| 314 | 0 => ItemRewardType::Guaranteed, | |
| 315 | 1 => ItemRewardType::Random, | |
| 316 | default => throw new MalformedEventException(static::class, "unknown reward type '{$type}'"), | |
| 317 | }; | |
| 318 | } | |
| 319 | ||
| 320 | #[Override] | |
| 321 | public function summary(): string | |
| 322 | { | |
| 323 | return ( | |
| 324 | count($this->quests) | |
| 325 | . ' quests: ' | |
| 326 | . implode(', ', array_map(static fn(Quest $quest): string => $quest->name->value, $this->quests)) | |
| 327 | ); | |
| 328 | } | |
| 329 | } |
From Lorekeeper\Server\Event\NamedAfterClass
| 7 | trait NamedAfterClass | |
| 8 | { | |
| 9 | public function name(): string | |
| 10 | { | |
| 11 | $class = static::class; | |
| 12 | ||
| 13 | return (string) preg_replace( | |
| 14 | '/Event$/', | |
| 15 | replacement: '', | |
| 16 | subject: substr($class, (int) strrpos($class, needle: '\\') + 1), | |
| 17 | ); | |
| 18 | } | |
| 19 | } |