Skip to content

Commit

Permalink
feat: Start initial loot import work
Browse files Browse the repository at this point in the history
  • Loading branch information
octfx committed Apr 28, 2024
1 parent 2f9184b commit 726187c
Show file tree
Hide file tree
Showing 28 changed files with 949 additions and 47 deletions.
115 changes: 115 additions & 0 deletions app/Console/Commands/SC/ImportLootArchetypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace App\Console\Commands\SC;

use App\Models\SC\EntityTag;
use App\Models\SC\Loot\PrimaryGroup;
use App\Models\SC\Loot\SpawnWith;
use App\Services\Parser\SC\Loot\Archetype;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class ImportLootArchetypes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sc:import-loot-archetypes';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle(): void
{

$files = File::allFiles(scdata('loot/archetypes'));
$this->withProgressBar($files, function (string $file) {

$data = (new Archetype($file))->getData();

$tagId = EntityTag::query()->firstOrCreate(['tag' => $data['uuid']])->id;

/** @var \App\Models\SC\Loot\Archetype $type */
$type = \App\Models\SC\Loot\Archetype::query()->updateOrCreate([
'uuid' => $data['uuid'],
], [
'name' => $data['class_name'],
'tag_id' => $tagId,
]);

$this->createPrimaryGroups($type, $data);
$this->createSecondaryGroups($type, $data);
});
}

private function createPrimaryGroups(\App\Models\SC\Loot\Archetype $archetype, array $data): void
{
collect($data['primary_groups'])->each(function (array $group) use ($archetype) {
$groupTagId = EntityTag::query()->firstOrCreate(['tag' => $group['tag']])->id;

/** @var PrimaryGroup $group */
$groupModel = $archetype->primaryGroups()->updateOrCreate([
'archetype_id' => $archetype->id,
'tag' => $group['tag'],
], [
'name' => $group['name'],
'weight' => $group['weight'],
'tag_id' => $groupTagId,
]);

$positiveTags = collect($group['positive_tags'])->map(fn ($tag) => EntityTag::query()->firstOrCreate(['tag' => $tag])->id);
$negativeTags = collect($group['negative_tags'])->map(fn ($tag) => EntityTag::query()->firstOrCreate(['tag' => $tag])->id);

$groupModel->tags()->syncWithPivotValues($negativeTags, ['is_positive' => false], false);
$groupModel->tags()->syncWithPivotValues($positiveTags, ['is_positive' => true], false);

if (! empty($group['stack_size'])) {
$groupModel->stackSize()->updateOrCreate([
'primary_group_id' => $groupModel->id,
], [
'min' => $group['stack_size']['min'],
'max' => $group['stack_size']['max'],
]);
}

if (! empty($group['spawn_with'])) {
/** @var SpawnWith $spawnWith */
$spawnWith = $groupModel->spawnWith()->updateOrCreate([
'primary_group_id' => $groupModel->id,
], [
'name' => $group['spawn_with']['name'],
'mode' => $group['spawn_with']['mode'],
'min' => $group['spawn_with']['min'],
'max' => $group['spawn_with']['max'],
]);

$positiveTags = collect($group['spawn_with']['positive_tags'])->map(fn ($tag) => EntityTag::query()->firstOrCreate(['tag' => $tag])->id);
$negativeTags = collect($group['spawn_with']['negative_tags'])->map(fn ($tag) => EntityTag::query()->firstOrCreate(['tag' => $tag])->id);

$spawnWith->tags()->syncWithPivotValues($positiveTags, ['is_positive' => true], false);
$spawnWith->tags()->syncWithPivotValues($negativeTags, ['is_positive' => false], false);
}
});
}

private function createSecondaryGroups(\App\Models\SC\Loot\Archetype $archetype, array $data): void
{
collect($data['secondary_groups'])->each(function (array $group) use ($archetype) {
$archetype->secondaryGroups()->updateOrCreate([
'archetype_id' => $archetype->id,
'tag' => $group['tag'],
], [
'weight' => $group['weight'],
]);
});
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use App\Console\Commands\SC\ComputeItemBaseIds;
use App\Console\Commands\SC\ImportClothing;
use App\Console\Commands\SC\ImportItems;
use App\Console\Commands\SC\ImportLootArchetypes;
use App\Console\Commands\SC\ImportPersonalWeapons;
use App\Console\Commands\SC\ImportShops;
use App\Console\Commands\SC\ImportVehicleItems;
Expand Down Expand Up @@ -115,6 +116,7 @@ class Kernel extends ConsoleKernel
ImportClothing::class,
ImportVehicleItems::class,
ImportPersonalWeapons::class,
ImportLootArchetypes::class,
ComputeItemBaseIds::class,

TranslateItems::class,
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Resources/SC/Item/ItemResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
use App\Http\Resources\SC\ItemSpecification\ShieldResource;
use App\Http\Resources\SC\ItemSpecification\ThrusterResource;
use App\Http\Resources\SC\ItemSpecification\TractorBeamResource;
use App\Http\Resources\SC\Loot\ArchetypeResource;
use App\Http\Resources\SC\Loot\PrimaryGroupResource;
use App\Http\Resources\SC\Manufacturer\ManufacturerLinkResource;
use App\Http\Resources\SC\Shop\ShopResource;
use App\Http\Resources\SC\Vehicle\Weapon\VehicleWeaponResource;
use App\Models\SC\Loot\PrimaryGroup;
use Illuminate\Http\Request;
use OpenApi\Attributes as OA;

Expand Down Expand Up @@ -161,6 +164,7 @@ public function __construct($resource, bool $isVehicleItem = false, bool $onlyBa
public static function validIncludes(): array
{
return parent::validIncludes() + [
'loot_archetypes',
'shops',
'shops.items',
];
Expand All @@ -177,9 +181,13 @@ public function toArray(Request $request): array

$vehicleItem = $this->vehicleItem;

// dd($this->lootGroups()->map(fn (PrimaryGroup $group) => $group->archetype->name));

return [
'uuid' => $this->uuid,
'name' => $this->name,
'loot_groups' => $this->lootGroups()->pluck('name')->toArray(),
'loot_archetypes' => $this->lootArchetypes()->pluck('name')->toArray(),
'class_name' => $this->class_name,
'description' => $this->getTranslation($this, $request),
'size' => $this->size,
Expand Down
22 changes: 22 additions & 0 deletions app/Http/Resources/SC/Loot/ArchetypeResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Resources\SC\Loot;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ArchetypeResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'uuid' => $this->uuid,
'name' => $this->name,
];
}
}
21 changes: 21 additions & 0 deletions app/Http/Resources/SC/Loot/PrimaryGroupResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Resources\SC\Loot;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class PrimaryGroupResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'name' => $this->name
];
}
}
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'
);
}
}
40 changes: 40 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 All @@ -32,6 +33,8 @@
use App\Models\SC\ItemSpecification\Shield;
use App\Models\SC\ItemSpecification\Thruster;
use App\Models\SC\ItemSpecification\TractorBeam;
use App\Models\SC\Loot\Archetype;
use App\Models\SC\Loot\PrimaryGroup;
use App\Models\SC\Manufacturer;
use App\Models\SC\Shop\Shop;
use App\Models\SC\Shop\ShopItem;
Expand Down Expand Up @@ -487,4 +490,41 @@ 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'
);
}


public function lootArchetypes(): BelongsToMany
{
return $this->belongsToMany(
Archetype::class,
'sc_item_entity_tag',
'item_id',
'entity_tag_id',
'id',
'tag_id',
);
}

public function lootGroups()//: BelongsToMany
{
return PrimaryGroup::query()->whereIn('tag', $this->entityTags()->pluck('tag')->toArray())->get();

return $this->belongsToMany(
PrimaryGroup::class,
'sc_item_entity_tag',
'item_id',
'entity_tag_id',
'id',
'tag_id',
);
}
}
48 changes: 48 additions & 0 deletions app/Models/SC/Loot/Archetype.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Models\SC\Loot;

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

class Archetype extends Model
{
use HasFactory;

protected $table = 'sc_loot_archetypes';

protected $fillable = [
'uuid',
'tag_id',
'name',
];

public function primaryGroups(): HasMany
{
return $this->hasMany(
PrimaryGroup::class,
'archetype_id',
'id'
);
}

public function secondaryGroups(): HasMany
{
return $this->hasMany(
SecondaryGroup::class,
'archetype_id',
'id'
);
}

public function items(): HasManyThrough
{
return $this->hasManyThrough(
Item::class,
PrimaryGroup::class,
);
}
}
Loading

0 comments on commit 726187c

Please sign in to comment.