Skip to content

Commit

Permalink
feat: Import entity tags
Browse files Browse the repository at this point in the history
Fixes #124
  • Loading branch information
octfx committed May 3, 2024
1 parent c5c2736 commit 473d81a
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 43 deletions.
7 changes: 7 additions & 0 deletions app/Http/Resources/SC/Item/ItemResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@
items: new OA\Items(type: 'string'),
nullable: true,
),
new OA\Property(
property: 'entity_tags',
type: 'array',
items: new OA\Items(type: 'string'),
nullable: true,
),
new OA\Property(
property: 'interactions',
type: 'array',
Expand Down Expand Up @@ -207,6 +213,7 @@ public function toArray(Request $request): array
]),
'tags' => $this->defaultTags->pluck('name')->toArray(),
'required_tags' => $this->requiredTags->pluck('name')->toArray(),
'entity_tags' => $this->entityTags->pluck('tag')->toArray(),
'interactions' => $this->interactions->pluck('name')->toArray(),
'ports' => ItemPortResource::collection($this->whenLoaded('ports')),
$this->mergeWhen(! $this->onlySimpleData && $this->relationLoaded('heatData'), [
Expand Down
25 changes: 25 additions & 0 deletions app/Jobs/SC/Import/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Jobs\SC\Import;

use App\Models\SC\EntityTag;
use App\Models\SC\Item\Interaction;
use App\Models\SC\Item\ItemPort;
use App\Models\SC\Item\ItemPortType;
Expand Down Expand Up @@ -94,6 +95,7 @@ public function handle(): void
$this->addTags($itemModel, $this->data, 'tags');
$this->addTags($itemModel, $this->data, 'required_tags', true);
$this->addInteractions($itemModel, $this->data);
$this->addEntityTags($itemModel, $this->data);
}

private function createDimensionModel(\App\Models\SC\Item\Item $itemModel): void
Expand Down Expand Up @@ -319,4 +321,27 @@ private function addInteractions($model, $data): void

$model->interactions()->sync($interactions);
}

/**
* @param \App\Models\SC\Item\Item $model
* @param $data
*/
private function addEntityTags(\App\Models\SC\Item\Item $model, $data): void
{
if (empty($data['entity_tags'])) {
return;
}

$tags = collect($data['entity_tags'])
->map('trim')
->map(function ($tag) {
$tag = EntityTag::query()->firstOrCreate([
'tag' => $tag,
]);

return $tag->id;
});

$model->entityTags()->sync($tags);
}
}
29 changes: 29 additions & 0 deletions app/Models/SC/EntityTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models\SC;

use App\Models\SC\Item\Item;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class EntityTag extends Model
{
use HasFactory;

protected $table = 'sc_entity_tags';

protected $fillable = [
'tag',
];

public function items(): BelongsToMany
{
return $this->belongsToMany(
Item::class,
'sc_item_entity_tag',
'item_id',
'entity_tag_id'
);
}
}
11 changes: 11 additions & 0 deletions app/Models/SC/Item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Models\SC\Char\PersonalWeapon\Knife;
use App\Models\SC\Char\PersonalWeapon\PersonalWeapon;
use App\Models\SC\Char\PersonalWeapon\PersonalWeaponMagazine;
use App\Models\SC\EntityTag;
use App\Models\SC\Food\Food;
use App\Models\SC\ItemSpecification\Bomb\Bomb;
use App\Models\SC\ItemSpecification\Cooler;
Expand Down Expand Up @@ -487,4 +488,14 @@ public function variants(): HasMany
{
return $this->hasMany(self::class, 'base_id', 'id');
}

public function entityTags(): BelongsToMany
{
return $this->belongsToMany(
EntityTag::class,
'sc_item_entity_tag',
'item_id',
'entity_tag_id'
);
}
}
86 changes: 43 additions & 43 deletions app/Services/Parser/SC/ItemBaseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function getData(Collection $item): ?array
'lifetime' => Arr::get($item, 'Raw.Entity.Components.SDegradationParams.MaxLifetimeHours'),
'salvageable' => Arr::get($item, 'Raw.Entity.Components.SHealthComponentParams.IsSalvagable'),
'repairable' => Arr::get($item, 'Raw.Entity.Components.SHealthComponentParams.IsRepairable'),
], function ($entry) {
], static function ($entry) {
return $entry !== null;
});

Expand All @@ -27,105 +27,105 @@ public static function getData(Collection $item): ?array
$out['heat'] = self::addHeatData($item);
$out['distortion'] = self::addDistortionData($item);
$out['interactions'] = self::addInteractionData($item);
$out['entity_tags'] = collect($item['tags'] ?? [])->map(fn (array $tag) => $tag['value']);

return $out;
}

private static function addPowerData(Collection $item): array
{
if (!isset($item['Components']['EntityComponentPowerConnection'])) {
if (! isset($item['Components']['EntityComponentPowerConnection'])) {
return [];
}

$basePath = 'Components.EntityComponentPowerConnection.';

return array_filter([
'power_base' => Arr::get($item, $basePath . 'PowerBase'),
'power_draw' => Arr::get($item, $basePath . 'PowerDraw'),
'power_base' => Arr::get($item, $basePath.'PowerBase'),
'power_draw' => Arr::get($item, $basePath.'PowerDraw'),

'throttleable' => Arr::get($item, $basePath . 'IsThrottleable'),
'overclockable' => Arr::get($item, $basePath . 'IsOverclockable'),
'throttleable' => Arr::get($item, $basePath.'IsThrottleable'),
'overclockable' => Arr::get($item, $basePath.'IsOverclockable'),

'overclock_threshold_min' => Arr::get($item, $basePath . 'OverclockThresholdMin'),
'overclock_threshold_max' => Arr::get($item, $basePath . 'OverclockThresholdMax'),
'overclock_performance' => Arr::get($item, $basePath . 'OverclockPerformance'),
'overclock_threshold_min' => Arr::get($item, $basePath.'OverclockThresholdMin'),
'overclock_threshold_max' => Arr::get($item, $basePath.'OverclockThresholdMax'),
'overclock_performance' => Arr::get($item, $basePath.'OverclockPerformance'),

'overpower_performance' => Arr::get($item, $basePath . 'OverpowerPerformance'),
'overpower_performance' => Arr::get($item, $basePath.'OverpowerPerformance'),

'power_to_em' => Arr::get($item, $basePath . 'PowerToEM'),
'decay_rate_em' => Arr::get($item, $basePath . 'DecayRateOfEM'),
'power_to_em' => Arr::get($item, $basePath.'PowerToEM'),
'decay_rate_em' => Arr::get($item, $basePath.'DecayRateOfEM'),
], static function ($entry) {
return $entry !== null;
});
}

private static function addHeatData(Collection $item): array
{
if (!isset($item['Components']['EntityComponentHeatConnection'])) {
if (! isset($item['Components']['EntityComponentHeatConnection'])) {
return [];
}

$basePath = 'Components.EntityComponentHeatConnection.';

return array_filter([
'temperature_to_ir' => Arr::get($item, $basePath . 'TemperatureToIR'),
'ir_temperature_threshold' => Arr::get($item, $basePath . 'StartIRTemperature'),
'overpower_heat' => Arr::get($item, $basePath . 'OverpowerHeat'),
'overclock_threshold_min' => Arr::get($item, $basePath . 'OverclockThresholdMinHeat'),
'overclock_threshold_max' => Arr::get($item, $basePath . 'OverclockThresholdMaxHeat'),
'thermal_energy_base' => Arr::get($item, $basePath . 'ThermalEnergyBase'),
'thermal_energy_draw' => Arr::get($item, $basePath . 'ThermalEnergyDraw'),
'thermal_conductivity' => Arr::get($item, $basePath . 'ThermalConductivity'),
'specific_heat_capacity' => Arr::get($item, $basePath . 'SpecificHeatCapacity'),
'mass' => Arr::get($item, $basePath . 'Mass'),
'surface_area' => Arr::get($item, $basePath . 'SurfaceArea'),
'start_cooling_temperature' => Arr::get($item, $basePath . 'StartCoolingTemperature'),
'max_cooling_rate' => Arr::get($item, $basePath . 'MaxCoolingRate'),
'max_temperature' => Arr::get($item, $basePath . 'MaxTemperature'),
'min_temperature' => Arr::get($item, $basePath . 'MinTemperature'),
'overheat_temperature' => Arr::get($item, $basePath . 'OverheatTemperature'),
'recovery_temperature' => Arr::get($item, $basePath . 'RecoveryTemperature'),
'misfire_min_temperature' => Arr::get($item, $basePath . 'MisfireMinTemperature'),
'misfire_max_temperature' => Arr::get($item, $basePath . 'MisfireMaxTemperature'),
'temperature_to_ir' => Arr::get($item, $basePath.'TemperatureToIR'),
'ir_temperature_threshold' => Arr::get($item, $basePath.'StartIRTemperature'),
'overpower_heat' => Arr::get($item, $basePath.'OverpowerHeat'),
'overclock_threshold_min' => Arr::get($item, $basePath.'OverclockThresholdMinHeat'),
'overclock_threshold_max' => Arr::get($item, $basePath.'OverclockThresholdMaxHeat'),
'thermal_energy_base' => Arr::get($item, $basePath.'ThermalEnergyBase'),
'thermal_energy_draw' => Arr::get($item, $basePath.'ThermalEnergyDraw'),
'thermal_conductivity' => Arr::get($item, $basePath.'ThermalConductivity'),
'specific_heat_capacity' => Arr::get($item, $basePath.'SpecificHeatCapacity'),
'mass' => Arr::get($item, $basePath.'Mass'),
'surface_area' => Arr::get($item, $basePath.'SurfaceArea'),
'start_cooling_temperature' => Arr::get($item, $basePath.'StartCoolingTemperature'),
'max_cooling_rate' => Arr::get($item, $basePath.'MaxCoolingRate'),
'max_temperature' => Arr::get($item, $basePath.'MaxTemperature'),
'min_temperature' => Arr::get($item, $basePath.'MinTemperature'),
'overheat_temperature' => Arr::get($item, $basePath.'OverheatTemperature'),
'recovery_temperature' => Arr::get($item, $basePath.'RecoveryTemperature'),
'misfire_min_temperature' => Arr::get($item, $basePath.'MisfireMinTemperature'),
'misfire_max_temperature' => Arr::get($item, $basePath.'MisfireMaxTemperature'),
], static function ($entry) {
return $entry !== null;
});
}

private static function addDistortionData(Collection $item): array
{
if (!isset($item['Components']['SDistortionParams'])) {
if (! isset($item['Components']['SDistortionParams'])) {
return [];
}

$basePath = 'Components.SDistortionParams.';

return array_filter([
'decay_rate' => Arr::get($item, $basePath . 'DecayRate'),
'decay_delay' => Arr::get($item, $basePath . 'DecayDelay'),
'decay_rate' => Arr::get($item, $basePath.'DecayRate'),
'decay_delay' => Arr::get($item, $basePath.'DecayDelay'),

'maximum' => Arr::get($item, $basePath . 'Maximum'),
'maximum' => Arr::get($item, $basePath.'Maximum'),

'overload_ratio' => Arr::get($item, $basePath . 'OverloadRatio'),
'overload_ratio' => Arr::get($item, $basePath.'OverloadRatio'),

'warning_ratio' => Arr::get($item, $basePath . 'WarningRatio'),
'recovery_ratio' => Arr::get($item, $basePath . 'RecoveryRatio'),
'recovery_time' => Arr::get($item, $basePath . 'RecoveryTime'),
'warning_ratio' => Arr::get($item, $basePath.'WarningRatio'),
'recovery_ratio' => Arr::get($item, $basePath.'RecoveryRatio'),
'recovery_time' => Arr::get($item, $basePath.'RecoveryTime'),
], static function ($entry) {
return $entry !== null;
});
}

private static function addInteractionData(Collection $item): array
{
if (!isset($item['Components']['SEntityInteractableParams'])) {
if (! isset($item['Components']['SEntityInteractableParams'])) {
return [];
}

$basePath = 'Components.SEntityInteractableParams.Interactable.SharedInteractions';


return collect(Arr::get($item, $basePath))->map(fn(array $interaction) => $interaction['Name'])
return collect(Arr::get($item, $basePath))->map(fn (array $interaction) => $interaction['Name'])
->unique()
->map('trim')
->map('strtolower')
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_entity_tags', static function (Blueprint $table) {
$table->id();
$table->uuid('tag');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('sc_entity_tags');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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_entity_tag', static function (Blueprint $table) {
$table->unsignedBigInteger('item_id');
$table->unsignedBigInteger('entity_tag_id');

$table->foreign('item_id', 'sc_i_e_tag_item_id')
->references('id')
->on('sc_items')
->onDelete('cascade');

$table->foreign('entity_tag_id', 'sc_i_e_tag_tag_id')
->references('id')
->on('sc_entity_tags')
->onDelete('cascade');
});
}

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

0 comments on commit 473d81a

Please sign in to comment.