Skip to content

Commit

Permalink
feat: Import compatible item (sub)types on ports
Browse files Browse the repository at this point in the history
Fixes #113
  • Loading branch information
octfx committed Mar 15, 2024
1 parent fb39cae commit 085aac0
Show file tree
Hide file tree
Showing 15 changed files with 327 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/Http/Resources/SC/Item/ItemPortResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
new OA\Property(property: 'min', type: 'integer', nullable: true),
new OA\Property(property: 'max', type: 'integer', nullable: true),
], type: 'object'),
new OA\Property(property: 'compatible_types', ref: '#/components/schemas/item_port_type_v2', nullable: true),
new OA\Property(
property: 'tags',
type: 'array',
Expand Down Expand Up @@ -53,6 +54,7 @@ public function toArray(Request $request): array
'min' => $this->min_size,
'max' => $this->max_size,
],
'compatible_types' => ItemPortTypeResource::collection($this->compatibleTypes),
'tags' => $this->defaultTags->pluck('name')->toArray(),
'required_tags' => $this->requiredTags->pluck('name')->toArray(),
'equipped_item' => new ItemLinkResource($this->item),
Expand Down
40 changes: 40 additions & 0 deletions app/Http/Resources/SC/Item/ItemPortTypeResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources\SC\Item;

use App\Http\Resources\AbstractBaseResource;
use Illuminate\Http\Request;
use OpenApi\Attributes as OA;

#[OA\Schema(
schema: 'item_port_type_v2',
title: 'Item Port Compatible Types',
properties: [
new OA\Property(property: 'type', type: 'string'),
new OA\Property(
property: 'sub_types',
type: 'array',
items: new OA\Items(type: 'string'),
nullable: true,
),
],
type: 'object'
)]
class ItemPortTypeResource extends AbstractBaseResource
{
/**
* Transform the resource collection into an array.
*
* @param Request $request
* @return array
*/
public function toArray(Request $request): array
{
return [
'type' => $this->type,
'sub_types' => $this->subTypes->pluck('sub_type')->toArray(),
];
}
}
52 changes: 50 additions & 2 deletions app/Jobs/SC/Import/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use App\Models\SC\Item\Interaction;
use App\Models\SC\Item\ItemPort;
use App\Models\SC\Item\ItemPortType;
use App\Models\SC\Item\ItemSubType;
use App\Models\SC\Item\ItemType;
use App\Models\SC\Item\Tag;
use App\Models\SC\Manufacturer;
use Illuminate\Bus\Queueable;
Expand Down Expand Up @@ -129,7 +132,7 @@ private function createContainerModel(\App\Models\SC\Item\Item $itemModel): void
private function createPorts(\App\Models\SC\Item\Item $itemModel): void
{
if (! empty($this->data['ports'])) {
collect($this->data['ports'])->each(function (array $port) use ($itemModel) {
$availablePorts = collect($this->data['ports'])->each(function (array $port) use ($itemModel) {
/** @var ItemPort $port */
$portModel = $itemModel->ports()->updateOrCreate([
'name' => $port['name'],
Expand All @@ -143,7 +146,52 @@ private function createPorts(\App\Models\SC\Item\Item $itemModel): void

$this->addTags($portModel, $port, 'tags');
$this->addTags($portModel, $port, 'required_tags', true);
});

$types = collect($port['compatible_types'])
->map(function (array $type) {
/** @var ItemType $typeModel */
$typeModel = ItemType::query()->firstOrCreate([
'type' => $type['type'],
]);

$type['id'] = $typeModel->id;

return $type;
})
->each(function (array $type) use ($portModel) {
/** @var ItemPort $portModel */
$portModelType = $portModel->compatibleTypes()->updateOrCreate([
'item_type_id' => $type['id'],
]);

$subTypes = collect($type['sub_types'])
->map(function (string $subType) {
/** @var ItemSubType $typeModel */
$typeModel = ItemSubType::query()->firstOrCreate([
'sub_type' => $subType,
]);

return $typeModel->id;
})
->each(function (int $id) use ($portModelType) {
/** @var ItemPortType $portModelType */
$portModelType->subTypes()->updateOrCreate([
'sub_type_id' => $id,
]);
});

/** @var ItemPortType $portModelType */
$portModelType->subTypes()->whereNotIn('sub_type_id', $subTypes)->delete();
})
->pluck('id');

/** @var ItemPort $portModel */
$portModel->compatibleTypes()->whereNotIn('item_type_id', $types)->delete();
})
->pluck('name');

// Remove old ports
$itemModel->ports()->whereNotIn('name', $availablePorts)->delete();
}
}

Expand Down
6 changes: 6 additions & 0 deletions app/Models/SC/Item/ItemPort.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

class ItemPort extends Model
{
Expand Down Expand Up @@ -85,4 +86,9 @@ public function requiredTags(): BelongsToMany
{
return $this->tags()->wherePivot('is_required_tag', true);
}

public function compatibleTypes(): HasMany
{
return $this->hasMany(ItemPortType::class);
}
}
35 changes: 35 additions & 0 deletions app/Models/SC/Item/ItemPortType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Models\SC\Item;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class ItemPortType extends Model
{
use HasFactory;

protected $table = 'sc_item_port_type';

protected $fillable = [
'item_port_id',
'item_type_id',
];

public function subTypes(): HasMany
{
return $this->hasMany(ItemPortTypeSubType::class);
}

public function typeName(): BelongsTo
{
return $this->belongsTo(ItemType::class, 'item_type_id', 'id');
}

public function getTypeAttribute()
{
return $this->typeName->type;
}
}
29 changes: 29 additions & 0 deletions app/Models/SC/Item/ItemPortTypeSubType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models\SC\Item;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ItemPortTypeSubType extends Model
{
use HasFactory;

protected $table = 'sc_item_port_type_sub_type';

protected $fillable = [
'item_port_type_id',
'sub_type_id',
];

public function subTypeName(): BelongsTo
{
return $this->belongsTo(ItemSubType::class, 'sub_type_id', 'id');
}

public function getSubTypeAttribute()
{
return $this->subTypeName->sub_type;
}
}
17 changes: 17 additions & 0 deletions app/Models/SC/Item/ItemSubType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Models\SC\Item;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ItemSubType extends Model
{
use HasFactory;

protected $table = 'sc_item_sub_types';

protected $fillable = [
'sub_type',
];
}
17 changes: 17 additions & 0 deletions app/Models/SC/Item/ItemType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Models\SC\Item;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ItemType extends Model
{
use HasFactory;

protected $table = 'sc_item_types';

protected $fillable = [
'type',
];
}
15 changes: 15 additions & 0 deletions app/Services/Parser/SC/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ private function mapPorts(): array
'equipped_item_uuid' => $port['EquippedItemUuid'] ?? $loadout[strtolower($port['Name'])] ?? null,
'tags' => $port['PortTags'] ?? null,
'required_tags' => $port['RequiredPortTags'] ?? null,
'compatible_types' => $this->mapPortCompatibleTypes($port),
];
}

Expand Down Expand Up @@ -296,4 +297,18 @@ private function mapPortLoadouts(): Collection
})
->filter();
}

private function mapPortCompatibleTypes(array $port): Collection
{
return collect(Arr::get($port, 'Types', []))
->map(function ($type) {
return [
'type' => $type['Type'],
'sub_types' => collect(Arr::get($type, 'SubTypes', []))
->map(fn (array $subType) => $subType['value'] ?? null)
->filter(),
];
})
->filter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public function up(): void
{
Schema::create('sc_item_ports', function (Blueprint $table) {
Schema::create('sc_item_ports', static function (Blueprint $table) {
$table->id();
$table->string('item_uuid');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('sc_item_types', static function (Blueprint $table) {
$table->id();
$table->string('type');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('sc_item_types');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('sc_item_port_type', static function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('item_port_id');
$table->unsignedBigInteger('item_type_id');

$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('sc_item_port_type');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('sc_item_sub_types', static function (Blueprint $table) {
$table->id();
$table->string('sub_type');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('sc_item_sub_types');
}
};
Loading

0 comments on commit 085aac0

Please sign in to comment.