|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Support\Items; |
| 6 | + |
| 7 | +use App\Models\SC\Item\Item; |
| 8 | + |
| 9 | +class RelatedItemsBuilder |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Build the related_items payload for a given Item. |
| 13 | + * |
| 14 | + * @return array{set_name:?string,base_item:?array,variant_items:array<int,array>,set_items:array<int,array>} |
| 15 | + */ |
| 16 | + public function build(Item $item): array |
| 17 | + { |
| 18 | + [$baseItem, $groupItems] = $this->gatherVariantGroup($item); |
| 19 | + |
| 20 | + $names = collect($groupItems)->pluck('name')->all(); |
| 21 | + if ($baseItem !== null) { |
| 22 | + array_unshift($names, $baseItem->name); |
| 23 | + } |
| 24 | + |
| 25 | + [$setName, $variantNames] = $this->computeSetNameAndVariantNames($names, $baseItem, $groupItems); |
| 26 | + |
| 27 | + $base = $baseItem !== null ? $this->toBaseLink($baseItem, $setName, true) : null; |
| 28 | + $variants = collect($groupItems) |
| 29 | + ->filter(fn (Item $i) => $i->uuid !== $item->uuid) // exclude current item |
| 30 | + ->map(function (Item $it) use ($variantNames) { |
| 31 | + $link = $this->toBaseLink($it, null, false); |
| 32 | + $link['variant_name'] = $variantNames[$it->uuid] ?? null; |
| 33 | + |
| 34 | + return $link; |
| 35 | + }) |
| 36 | + ->values() |
| 37 | + ->all(); |
| 38 | + |
| 39 | + $setItems = $this->findSetItems($item); |
| 40 | + |
| 41 | + return [ |
| 42 | + 'set_name' => $setName, |
| 43 | + 'base_item' => $base, |
| 44 | + 'variant_items' => $variants, |
| 45 | + 'set_items' => $setItems, |
| 46 | + ]; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Determine base item and full variant group for an item. |
| 51 | + * |
| 52 | + * @return array{0:?Item,1:array<int,Item>} |
| 53 | + */ |
| 54 | + public function gatherVariantGroup(Item $item): array |
| 55 | + { |
| 56 | + if ($item->base_id === null) { |
| 57 | + $base = $item; |
| 58 | + $siblings = $item->variants()->get()->all(); |
| 59 | + } else { |
| 60 | + $base = $item->baseVariant()->first(); |
| 61 | + $siblings = $base?->variants()->get()->all() ?? []; |
| 62 | + } |
| 63 | + |
| 64 | + return [$base, $siblings]; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Compute set name and per-item variant names. |
| 69 | + * - set name: longest common prefix among names |
| 70 | + * - variant name: item name with the set name prefix removed (trimmed); if empty, "Base". |
| 71 | + * |
| 72 | + * @param array<int,string> $names |
| 73 | + * @param array<int,Item> $group |
| 74 | + * @return array{0:?string,1:array<string,string>} [setName, map(uuid=>variantName)] |
| 75 | + */ |
| 76 | + public function computeSetNameAndVariantNames(array $names, ?Item $base, array $group): array |
| 77 | + { |
| 78 | + $rawPrefix = $this->longestCommonPrefix($names); |
| 79 | + if ($rawPrefix !== null) { |
| 80 | + $endsWithSpace = str_ends_with($rawPrefix, ' '); |
| 81 | + $setName = rtrim($rawPrefix); |
| 82 | + if (! $endsWithSpace) { |
| 83 | + $lastSpace = strrpos($setName, ' '); |
| 84 | + if ($lastSpace !== false) { |
| 85 | + $setName = substr($setName, 0, $lastSpace); |
| 86 | + } |
| 87 | + } |
| 88 | + $setName = $setName === '' ? null : $setName; |
| 89 | + } else { |
| 90 | + $setName = null; |
| 91 | + } |
| 92 | + |
| 93 | + $map = []; |
| 94 | + if ($base !== null) { |
| 95 | + $map[$base->uuid] = 'Base'; |
| 96 | + } |
| 97 | + |
| 98 | + foreach ($group as $it) { |
| 99 | + $variant = $this->stripPrefix($it->name, (string) $setName); |
| 100 | + $map[$it->uuid] = $variant === '' ? 'Base' : $variant; |
| 101 | + } |
| 102 | + |
| 103 | + return [$setName, $map]; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Find set items for armor/clothing by replacing the part token in class_name. |
| 108 | + * |
| 109 | + * @return array<int,array{uuid:string,name:string,type:?string,sub_type:?string,link:string}> |
| 110 | + */ |
| 111 | + public function findSetItems(Item $item): array |
| 112 | + { |
| 113 | + $className = $item->class_name ?? ''; |
| 114 | + if ($className === '') { |
| 115 | + return []; |
| 116 | + } |
| 117 | + |
| 118 | + $parts = config('item_sets.parts', ['helmet', 'core', 'arms', 'legs']); |
| 119 | + $currentPart = null; |
| 120 | + foreach ($parts as $part) { |
| 121 | + if (str_contains($className, '_'.$part.'_')) { |
| 122 | + $currentPart = $part; |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + if ($currentPart === null) { |
| 127 | + return []; |
| 128 | + } |
| 129 | + |
| 130 | + $set = []; |
| 131 | + foreach ($parts as $part) { |
| 132 | + if ($part === $currentPart) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + $candidate = $this->replaceFirst('_'.$currentPart.'_', '_'.$part.'_', $className); |
| 136 | + $found = Item::query()->where('class_name', $candidate)->first(); |
| 137 | + if ($found !== null && $found->uuid !== $item->uuid) { |
| 138 | + $set[] = [ |
| 139 | + 'uuid' => $found->uuid, |
| 140 | + 'name' => $found->name, |
| 141 | + 'type' => $found->type, |
| 142 | + 'sub_type' => $found->sub_type, |
| 143 | + 'link' => $this->makeLink($found->uuid), |
| 144 | + ]; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return $set; |
| 149 | + } |
| 150 | + |
| 151 | + private function toBaseLink(Item $it, ?string $setName, bool $includeVariantName): array |
| 152 | + { |
| 153 | + $link = [ |
| 154 | + 'uuid' => $it->uuid, |
| 155 | + 'name' => $it->name, |
| 156 | + 'link' => $this->makeLink($it->uuid), |
| 157 | + ]; |
| 158 | + if ($includeVariantName && $setName !== null) { |
| 159 | + $variant = $this->stripPrefix($it->name, $setName); |
| 160 | + $link['variant_name'] = $variant === '' ? 'Base' : $variant; |
| 161 | + } |
| 162 | + |
| 163 | + return $link; |
| 164 | + } |
| 165 | + |
| 166 | + private function makeLink(string $uuid): string |
| 167 | + { |
| 168 | + $baseUrl = rtrim((string) config('app.url'), '/'); |
| 169 | + |
| 170 | + return $baseUrl.'/api/v2/items/'.$uuid; |
| 171 | + } |
| 172 | + |
| 173 | + private function replaceFirst(string $search, string $replace, string $subject): string |
| 174 | + { |
| 175 | + $pos = strpos($subject, $search); |
| 176 | + if ($pos === false) { |
| 177 | + return $subject; |
| 178 | + } |
| 179 | + |
| 180 | + return substr($subject, 0, $pos).$replace.substr($subject, $pos + strlen($search)); |
| 181 | + } |
| 182 | + |
| 183 | + private function longestCommonPrefix(array $strings): ?string |
| 184 | + { |
| 185 | + if (count($strings) === 0) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + $prefix = $strings[0]; |
| 189 | + foreach ($strings as $s) { |
| 190 | + $i = 0; |
| 191 | + $max = min(strlen($prefix), strlen($s)); |
| 192 | + while ($i < $max && $prefix[$i] === $s[$i]) { |
| 193 | + $i++; |
| 194 | + } |
| 195 | + $prefix = substr($prefix, 0, $i); |
| 196 | + if ($prefix === '') { |
| 197 | + return null; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + return $prefix; |
| 202 | + } |
| 203 | + |
| 204 | + private function stripPrefix(string $name, string $prefix): string |
| 205 | + { |
| 206 | + if ($prefix === '') { |
| 207 | + return $name; |
| 208 | + } |
| 209 | + if (str_starts_with($name, $prefix)) { |
| 210 | + $rest = substr($name, strlen($prefix)); |
| 211 | + |
| 212 | + return ltrim($rest); |
| 213 | + } |
| 214 | + |
| 215 | + return $name; |
| 216 | + } |
| 217 | +} |
0 commit comments