Lines
100.00%
70 / 70
Methods
100.00%
4 / 4
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| read | 100.00% | 18 / 18 | 100.00% | 1 / 1 | 3 | ||
| kind | 100.00% | 18 / 18 | 100.00% | 1 / 1 | 16 | ||
| weaponType | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 15 | ||
| weapon | 100.00% | 19 / 19 | 100.00% | 1 / 1 | 2 | ||
| 33 | final readonly class ItemKinds | |
| 34 | { | |
| 35 | private const int STEADINESS_THE_GAME_ASSUMES = 10; | |
| 36 | private const int LEVEL_OF_AN_ITEM_WITHOUT_ONE = 1; | |
| 37 | ||
| 38 | /** | |
| 39 | * @param array<array-key, mixed> $entry | |
| 40 | * @param class-string<Event> $event | |
| 41 | * @param array<array-key, mixed> $procs | |
| 42 | * | |
| 43 | * @throws MalformedEventException | |
| 44 | */ | |
| 45 | public static function read(array $entry, string $event, array $procs = []): Item | |
| 46 | { | |
| 47 | try { | |
| 48 | $data = Type\shape([ | |
| 49 | 'ItemID' => Type\positive_int(), | |
| 50 | 'sName' => Type\non_empty_string(), | |
| 51 | 'sType' => Type\optional(Type\string()), | |
| 52 | 'iLvl' => Type\int_range(min: 0, max: 100), | |
| 53 | 'iStk' => Type\optional(Type\positive_int()), | |
| 54 | 'bTemp' => Type\optional(Type\bool()), | |
| 55 | ], allowUnknownFields: true)->coerce($entry); | |
| 56 | } catch (CoercionException $exception) { | |
| 57 | throw new MalformedEventException($event, 'an item has no id, name or level', $exception); | |
| 58 | } | |
| 59 | ||
| 60 | $id = new ItemId($data['ItemID']); | |
| 61 | $name = new ItemName($data['sName']); | |
| 62 | $level = new ItemLevel(max($data['iLvl'], self::LEVEL_OF_AN_ITEM_WITHOUT_ONE)); | |
| 63 | $stack = new Quantity($data['iStk'] ?? 1); | |
| 64 | $item = self::kind($data['sType'] ?? '', $id, $name, $level, $stack, $entry, $procs, $event); | |
| 65 | if ($data['bTemp'] ?? false) { | |
| 66 | $item->tags->add(ItemTag::Temporary); | |
| 67 | } | |
| 68 | ||
| 69 | return $item; | |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * @param array<array-key, mixed> $entry | |
| 74 | * @param array<array-key, mixed> $procs | |
| 75 | * @param class-string<Event> $event | |
| 76 | * | |
| 77 | * @throws MalformedEventException | |
| 78 | * | |
| 79 | * @mago-expect lint:excessive-parameter-list | |
| 80 | */ | |
| 81 | private static function kind( | |
| 82 | string $type, | |
| 83 | ItemId $id, | |
| 84 | ItemName $name, | |
| 85 | ItemLevel $level, | |
| 86 | Quantity $stack, | |
| 87 | array $entry, | |
| 88 | array $procs, | |
| 89 | string $event, | |
| 90 | ): Item { | |
| 91 | $weapon = self::weaponType($type); | |
| 92 | if ($weapon !== null) { | |
| 93 | return self::weapon($id, $name, $level, $weapon, $entry, $procs, $event); | |
| 94 | } | |
| 95 | ||
| 96 | return match ($type) { | |
| 97 | 'Class' => new ClassItem($id, $name, $level, Enhancements::applied($entry, $procs, $event)), | |
| 98 | 'Armor' => new Armor($id, $name, $level), | |
| 99 | 'Helm' => new Helm($id, $name, $level, Enhancements::applied($entry, $procs, $event)), | |
| 100 | 'Cape' => new Cape($id, $name, $level, Enhancements::applied($entry, $procs, $event)), | |
| 101 | 'Pet' => new Pet($id, $name, $level), | |
| 102 | 'Necklace' => new Necklace($id, $name, $level), | |
| 103 | 'Misc' => new GroundItem($id, $name, $level), | |
| 104 | 'Floor Item' => new HouseItem($id, $name, $level, HouseItemType::Floor), | |
| 105 | 'Wall Item' => new HouseItem($id, $name, $level, HouseItemType::Wall), | |
| 106 | 'House' => new HouseItem($id, $name, $level, HouseItemType::House), | |
| 107 | 'ServerUse' => new Boost($id, $name, $level, $stack), | |
| 108 | 'Enhancement' => Enhancements::sold($id, $name, $level, $entry, $event), | |
| 109 | 'Item', 'Note', 'Quest Item', 'Resource' => new Resource($id, $name, $level, $stack), | |
| 110 | default => throw new MalformedEventException($event, "unknown item type '{$type}'"), | |
| 111 | }; | |
| 112 | } | |
| 113 | ||
| 114 | private static function weaponType(string $type): ?WeaponType | |
| 115 | { | |
| 116 | return match ($type) { | |
| 117 | 'Axe' => WeaponType::Axe, | |
| 118 | 'Bow' => WeaponType::Bow, | |
| 119 | 'Dagger' => WeaponType::Dagger, | |
| 120 | 'Gauntlet' => WeaponType::Gauntlet, | |
| 121 | 'Gun' => WeaponType::Gun, | |
| 122 | 'HandGun' => WeaponType::HandGun, | |
| 123 | 'Mace' => WeaponType::Mace, | |
| 124 | 'Polearm' => WeaponType::Polearm, | |
| 125 | 'Rifle' => WeaponType::Rifle, | |
| 126 | 'Staff' => WeaponType::Staff, | |
| 127 | 'Sword' => WeaponType::Sword, | |
| 128 | 'Wand' => WeaponType::Wand, | |
| 129 | 'Whip' => WeaponType::Whip, | |
| 130 | default => null, | |
| 131 | }; | |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * @param array<array-key, mixed> $entry | |
| 136 | * @param array<array-key, mixed> $procs | |
| 137 | * @param class-string<Event> $event | |
| 138 | * | |
| 139 | * @throws MalformedEventException | |
| 140 | * | |
| 141 | * @mago-expect lint:excessive-parameter-list | |
| 142 | */ | |
| 143 | private static function weapon( | |
| 144 | ItemId $id, | |
| 145 | ItemName $name, | |
| 146 | ItemLevel $level, | |
| 147 | WeaponType $type, | |
| 148 | array $entry, | |
| 149 | array $procs, | |
| 150 | string $event, | |
| 151 | ): Weapon { | |
| 152 | try { | |
| 153 | $variance = | |
| 154 | Type\shape(['iRng' => Type\optional(Type\nullable(Type\int_range( | |
| 155 | min: 0, | |
| 156 | max: 100, | |
| 157 | )))], allowUnknownFields: true)->coerce($entry)['iRng'] ?? self::STEADINESS_THE_GAME_ASSUMES; | |
| 158 | } catch (CoercionException $exception) { | |
| 159 | throw new MalformedEventException( | |
| 160 | $event, | |
| 161 | "weapon {$id->value} varies its damage by an invalid amount", | |
| 162 | $exception, | |
| 163 | ); | |
| 164 | } | |
| 165 | ||
| 166 | return new Weapon( | |
| 167 | $id, | |
| 168 | $name, | |
| 169 | $level, | |
| 170 | $type, | |
| 171 | new WeaponDamage(new DamageVariance($variance)), | |
| 172 | Enhancements::applied($entry, $procs, $event), | |
| 173 | ); | |
| 174 | } | |
| 175 | } |