Skip to content

Commit

Permalink
fix: Fix importing vehicle weapon hardpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
octfx committed May 19, 2024
1 parent 5b523a7 commit e74c3bf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
9 changes: 5 additions & 4 deletions app/Console/Commands/SC/ImportVehicles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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)
Expand All @@ -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'])
));

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/SC/Vehicle/HardpointResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
new OA\Property(
property: 'item',
ref: '#/components/schemas/hardpoint_item_v2',
type: 'double',
type: 'object',
nullable: true
),
],
Expand Down
43 changes: 24 additions & 19 deletions app/Jobs/SC/Import/Vehicle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion app/Models/SC/CommodityItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion app/Models/StarCitizen/Vehicle/Vehicle/Vehicle.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,9 @@ public function sc(): HasOne
return $this->hasOne(
\App\Models\SC\Vehicle\Vehicle::class,
'shipmatrix_id',
'id'
'id',
)
->withoutGlobalScopes()
->withDefault();
}

Expand Down

0 comments on commit e74c3bf

Please sign in to comment.