-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Start initial loot import work
- Loading branch information
Showing
28 changed files
with
949 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
} |
Oops, something went wrong.