Lines
100.00%
63 / 63
Methods
100.00%
8 / 8
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| follow | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 10 | ||
| loaded | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 6 | ||
| resized | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 3 | ||
| received | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| grown | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 4 | ||
| enhanced | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 3 | ||
| taken | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 3 | ||
| pickedUp | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 3 | ||
| 32 | final readonly class InventoryTracking | |
| 33 | { | |
| 34 | /** @throws InvalidArgumentException */ | |
| 35 | public static function follow( | |
| 36 | HeroInventory $inventory, | |
| 37 | Event $event, | |
| 38 | Identity $me, | |
| 39 | Drops $drops = new Drops(), | |
| 40 | ): bool { | |
| 41 | return match (true) { | |
| 42 | $event instanceof InventoryLoadedEvent => self::loaded($inventory, $event->items), | |
| 43 | $event instanceof HeroDataLoadedEvent => self::resized($inventory, HeroDataTracking::mine($event, $me)), | |
| 44 | $event instanceof QuestItemsReceivedEvent => self::received($inventory, $event->items), | |
| 45 | $event instanceof ItemsIncreasedEvent => self::grown($inventory->permanent, $event->items), | |
| 46 | $event instanceof QuestItemsIncreasedEvent => self::grown($inventory->temporary, $event->items), | |
| 47 | $event instanceof ItemsEnhancedEvent => self::enhanced($inventory, $event), | |
| 48 | $event instanceof ItemsTurnedInEvent => self::taken($inventory, $event->items), | |
| 49 | $event instanceof DropPickedUpEvent => self::pickedUp($inventory, $event, $drops), | |
| 50 | default => false, | |
| 51 | }; | |
| 52 | } | |
| 53 | ||
| 54 | /** | |
| 55 | * @param list<HeldItem> $items | |
| 56 | * | |
| 57 | * @throws InvalidArgumentException | |
| 58 | */ | |
| 59 | public static function loaded(HeroInventory $inventory, array $items): bool | |
| 60 | { | |
| 61 | foreach (iterator_to_array($inventory->permanent) as $kept) { | |
| 62 | $inventory->take($kept->id, new Quantity($inventory->quantity($kept->id))); | |
| 63 | } | |
| 64 | ||
| 65 | foreach ($items as $held) { | |
| 66 | $inventory->add($held->item, $held->quantity); | |
| 67 | if (!$held->item instanceof Equipment) { | |
| 68 | continue; | |
| 69 | } | |
| 70 | ||
| 71 | if ($held->equipped) { | |
| 72 | $inventory->equipment->equip($held->item); | |
| 73 | } | |
| 74 | ||
| 75 | if ($held->worn) { | |
| 76 | $inventory->costume->wear($held->item); | |
| 77 | } | |
| 78 | } | |
| 79 | ||
| 80 | return true; | |
| 81 | } | |
| 82 | ||
| 83 | private static function resized(HeroInventory $inventory, ?HeroData $data): bool | |
| 84 | { | |
| 85 | if ($data === null) { | |
| 86 | return false; | |
| 87 | } | |
| 88 | ||
| 89 | $changed = !$data->bagSlots->equals($inventory->slots) || !$data->bankSlots->equals($inventory->bank->slots); | |
| 90 | $inventory->resize($data->bagSlots); | |
| 91 | $inventory->bank->resize($data->bankSlots); | |
| 92 | ||
| 93 | return $changed; | |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * @param non-empty-list<HeldItem> $items | |
| 98 | * | |
| 99 | * @throws InvalidArgumentException | |
| 100 | */ | |
| 101 | private static function received(HeroInventory $inventory, array $items): bool | |
| 102 | { | |
| 103 | foreach ($items as $held) { | |
| 104 | $inventory->receive($held->item, $held->quantity); | |
| 105 | } | |
| 106 | ||
| 107 | return true; | |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * @param non-empty-list<ItemIncrease> $more | |
| 112 | * | |
| 113 | * @throws InvalidArgumentException | |
| 114 | */ | |
| 115 | private static function grown(Stock $stock, array $more): bool | |
| 116 | { | |
| 117 | $changed = false; | |
| 118 | foreach ($more as $added) { | |
| 119 | $item = $stock->find($added->item)->unwrapOr(null); | |
| 120 | if ($item === null) { | |
| 121 | continue; | |
| 122 | } | |
| 123 | ||
| 124 | $changed = true; | |
| 125 | if ($added->held !== null) { | |
| 126 | $stock->hold($added->item, $added->held); | |
| 127 | continue; | |
| 128 | } | |
| 129 | ||
| 130 | $stock->receive($item, $added->quantity); | |
| 131 | } | |
| 132 | ||
| 133 | return $changed; | |
| 134 | } | |
| 135 | ||
| 136 | private static function enhanced(HeroInventory $inventory, ItemsEnhancedEvent $event): bool | |
| 137 | { | |
| 138 | $changed = false; | |
| 139 | foreach ($event->items as $id) { | |
| 140 | $item = $inventory->permanent->find($id)->unwrapOr(null); | |
| 141 | if (!$item instanceof Enchantable) { | |
| 142 | continue; | |
| 143 | } | |
| 144 | ||
| 145 | $item->enchant($event->enhancement); | |
| 146 | $changed = true; | |
| 147 | } | |
| 148 | ||
| 149 | return $changed; | |
| 150 | } | |
| 151 | ||
| 152 | /** | |
| 153 | * @param non-empty-list<TurnedInItem> $taken | |
| 154 | * | |
| 155 | * @throws InvalidArgumentException | |
| 156 | */ | |
| 157 | private static function taken(HeroInventory $inventory, array $taken): bool | |
| 158 | { | |
| 159 | $changed = false; | |
| 160 | foreach ($taken as $item) { | |
| 161 | if ($inventory->find($item->item)->isNone()) { | |
| 162 | continue; | |
| 163 | } | |
| 164 | ||
| 165 | $inventory->take($item->item, $item->quantity); | |
| 166 | $changed = true; | |
| 167 | } | |
| 168 | ||
| 169 | return $changed; | |
| 170 | } | |
| 171 | ||
| 172 | /** @throws InvalidArgumentException */ | |
| 173 | private static function pickedUp(HeroInventory $inventory, DropPickedUpEvent $event, Drops $drops): bool | |
| 174 | { | |
| 175 | if ($inventory->permanent->find($event->item)->isSome()) { | |
| 176 | $inventory->permanent->hold($event->item, $event->held); | |
| 177 | ||
| 178 | return true; | |
| 179 | } | |
| 180 | ||
| 181 | $dropped = $drops->find($event->item); | |
| 182 | if ($dropped === null) { | |
| 183 | return false; | |
| 184 | } | |
| 185 | ||
| 186 | $inventory->add($dropped->item, $event->held); | |
| 187 | ||
| 188 | return true; | |
| 189 | } | |
| 190 | } |