diff --git a/app/Console/Commands/SC/ImportVehicles.php b/app/Console/Commands/SC/ImportVehicles.php index fa597142..360cc626 100644 --- a/app/Console/Commands/SC/ImportVehicles.php +++ b/app/Console/Commands/SC/ImportVehicles.php @@ -36,9 +36,10 @@ class ImportVehicles extends AbstractQueueCommand public function handle() { try { - $vehicles = File::get(scdata('v2/ships.json')); + $vehicles = File::get(scdata('ships.json')); } catch (FileNotFoundException $e) { $this->error('ships.json not found. Did you clone scunpacked?'); + return Command::FAILURE; } @@ -55,7 +56,7 @@ public function handle() $this->info(sprintf( 'Importing %d vehicles in chunks of 5 (%d).', $chunks->count(), - (int)($chunks->count() / 5) + (int) ($chunks->count() / 5) )); }) ->chunk(5) @@ -69,8 +70,8 @@ public function handle() return $this->isNotIgnoredClass($vehicle['ClassName']); }) ->map(function (array $vehicle) { - $vehicle['filePathV2'] = scdata(sprintf( - 'v2/ships/%s-raw.json', + $vehicle['filePathRaw'] = scdata(sprintf( + 'ships/%s-raw.json', strtolower($vehicle['ClassName']) )); diff --git a/app/Http/Resources/SC/Vehicle/HardpointResource.php b/app/Http/Resources/SC/Vehicle/HardpointResource.php index e3461572..ca6dfd72 100644 --- a/app/Http/Resources/SC/Vehicle/HardpointResource.php +++ b/app/Http/Resources/SC/Vehicle/HardpointResource.php @@ -29,7 +29,7 @@ new OA\Property( property: 'item', ref: '#/components/schemas/hardpoint_item_v2', - type: 'double', + type: 'object', nullable: true ), ], diff --git a/app/Jobs/SC/Import/Vehicle.php b/app/Jobs/SC/Import/Vehicle.php index 8e29a5b0..45b5f82a 100644 --- a/app/Jobs/SC/Import/Vehicle.php +++ b/app/Jobs/SC/Import/Vehicle.php @@ -6,7 +6,6 @@ use App\Models\SC\Manufacturer; use App\Models\SC\Vehicle\Hardpoint; -use App\Services\Parser\SC\Labels; use App\Services\Parser\SC\Manufacturers; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Filesystem\FileNotFoundException; @@ -56,9 +55,9 @@ public function handle(): void $vehicle = $this->shipData; try { - $rawData = File::get($vehicle['filePathV2']); + $rawData = File::get($vehicle['filePathRaw']); - $vehicle['rawData'] = json_decode($rawData, true, 512, JSON_THROW_ON_ERROR); + $vehicle['rawData'] = json_decode($rawData, true, 512, JSON_THROW_ON_ERROR)['Raw']; } catch (FileNotFoundException|JsonException $e) { $this->fail($e->getMessage()); } @@ -277,12 +276,13 @@ private function numFormat($data): float|int return $negation * floor((abs((float) $num) * $coefficient)) / $coefficient; } - private function getItemUUID(string $className): ?string + private function getItem(string $search): ?\App\Models\SC\Item\Item { return \App\Models\SC\Item\Item::query() ->withoutGlobalScopes() - ->where('class_name', strtolower($className)) - ->first(['uuid'])->uuid ?? null; + ->where('uuid', $search) + ->orWhere('class_name', strtolower($search)) + ->first(); } /** @@ -308,21 +308,24 @@ private function createHardpoints(\App\Models\SC\Vehicle\Vehicle $vehicle, array ->each(function (Collection $entries) use ($hardpoints, $vehicle) { $entries ->each(function ($hardpoint) use ($hardpoints, $vehicle) { - $itemUuid = null; - if (! empty($hardpoint['entityClassName'])) { - $itemUuid = $this->getItemUUID($hardpoint['entityClassName']); + $item = null; + if (! empty($hardpoint['entityClassReference']) && $hardpoint['entityClassReference'] !== '00000000-0000-0000-0000-000000000000') { + $item = $this->getItem($hardpoint['entityClassReference']); + } elseif (! empty($hardpoint['entityClassName'])) { + $item = $this->getItem($hardpoint['entityClassName']); } $itemPortName = strtolower($hardpoint['itemPortName']); $this->hardpoints->push($hardpoint['itemPortName']); + /** @var Hardpoint $point */ $point = $vehicle->hardpoints()->updateOrCreate([ 'hardpoint_name' => $hardpoint['itemPortName'], ], [ - 'class_name' => $hardpoint['entityClassName'], - 'equipped_item_uuid' => $itemUuid, - 'min_size' => $hardpoints[$itemPortName]['ItemPort']['minsize'] ?? null, - 'max_size' => $hardpoints[$itemPortName]['ItemPort']['maxsize'] ?? null, + 'class_name' => $item?->class_name ?? $hardpoint['entityClassName'] ?? null, + 'equipped_item_uuid' => $item?->uuid ?? $hardpoint['entityClassReference'] ?? null, + 'min_size' => $hardpoints[$itemPortName]['ItemPort']['minSize'] ?? null, + 'max_size' => $hardpoints[$itemPortName]['ItemPort']['maxSize'] ?? null, ]); $this->createSubPoint( @@ -360,7 +363,7 @@ private function createHardpoints(\App\Models\SC\Vehicle\Vehicle $vehicle, array return $hardpoint['class'] === 'ItemPort'; }) ->filter(function (array $hardpoint) { - return isset($hardpoint['ItemPort']) && ! empty($hardpoint['ItemPort']['flags']) && $hardpoint['ItemPort']['minsize'] > 0; + return isset($hardpoint['ItemPort']) && ! empty($hardpoint['ItemPort']['flags']) && $hardpoint['ItemPort']['minSize'] > 0; }) ->filter(function (array $hardpoint) { // Filter out some @@ -390,8 +393,8 @@ private function createHardpoints(\App\Models\SC\Vehicle\Vehicle $vehicle, array } $vehicle->hardpoints()->updateOrCreate($where, [ - 'min_size' => Arr::get($hardpoint, 'ItemPort.minsize'), - 'max_size' => Arr::get($hardpoint, 'ItemPort.maxsize'), + 'min_size' => Arr::get($hardpoint, 'ItemPort.minSize'), + 'max_size' => Arr::get($hardpoint, 'ItemPort.maxSize'), ]); $this->hardpoints->push($hardpoint['name']); @@ -429,17 +432,19 @@ private function mapHardpoints(array $parts, array &$out, ?string $parent = null private function createSubPoint(array $entries, Hardpoint $parent, \App\Models\SC\Vehicle\Vehicle $vehicle): void { foreach ($entries as $subPoint) { - if (empty($subPoint['entityClassName'])) { + if (empty($subPoint['entityClassName']) && empty($subPoint['entityClassReference'])) { continue; } + $item = $this->getItem($subPoint['entityClassReference']) ?? $this->getItem($subPoint['entityClassName']); + $this->hardpoints->push($subPoint['itemPortName']); $point = $vehicle->hardpoints()->updateOrCreate([ 'hardpoint_name' => $subPoint['itemPortName'], 'parent_hardpoint_id' => $parent->id, ], [ - 'class_name' => $subPoint['entityClassName'], - 'equipped_item_uuid' => $this->getItemUUID($subPoint['entityClassName']), + 'class_name' => $item?->class_name ?? $subPoint['entityClassName'], + 'equipped_item_uuid' => $item?->uuid, ]); $subEntries = Arr::get($subPoint, 'loadout.SItemPortLoadoutManualParams.entries'); diff --git a/app/Models/SC/CommodityItem.php b/app/Models/SC/CommodityItem.php index fb057cd5..01beb457 100644 --- a/app/Models/SC/CommodityItem.php +++ b/app/Models/SC/CommodityItem.php @@ -44,7 +44,7 @@ static function (Builder $builder) { public function item(): BelongsTo { - return $this->belongsTo(Item::class, 'item_uuid', 'uuid'); + return $this->belongsTo(Item::class, 'item_uuid', 'uuid')->withoutGlobalScopes(); } public function getNameAttribute() diff --git a/app/Models/StarCitizen/Vehicle/Vehicle/Vehicle.php b/app/Models/StarCitizen/Vehicle/Vehicle/Vehicle.php index 7e54de00..c603de83 100644 --- a/app/Models/StarCitizen/Vehicle/Vehicle/Vehicle.php +++ b/app/Models/StarCitizen/Vehicle/Vehicle/Vehicle.php @@ -276,8 +276,9 @@ public function sc(): HasOne return $this->hasOne( \App\Models\SC\Vehicle\Vehicle::class, 'shipmatrix_id', - 'id' + 'id', ) + ->withoutGlobalScopes() ->withDefault(); }