initial commit
This commit is contained in:
commit
1e1aa7d461
215 changed files with 35140 additions and 0 deletions
141
app/Jobs/ImportProfession.php
Normal file
141
app/Jobs/ImportProfession.php
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\Character;
|
||||
use App\Models\Profession;
|
||||
use App\Models\CharacterProfession;
|
||||
use App\Models\Recipe;
|
||||
use App\Models\RecipeCategory;
|
||||
use App\Models\Item;
|
||||
use App\ProfessionImport\Exception;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ImportProfession implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* Character profession the operation should act upon,
|
||||
*/
|
||||
protected CharacterProfession $ch_prof;
|
||||
|
||||
/**
|
||||
* Array of recipe data to the imported.
|
||||
*/
|
||||
protected array $recipes;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Character $character, $data)
|
||||
{
|
||||
// Validate character name first.
|
||||
if ($character->name !== $data->player) {
|
||||
$message = sprintf('Wrong character: %s expected %s', $data->player, $character->name);
|
||||
throw new Exception($message);
|
||||
}
|
||||
|
||||
// Validate profession
|
||||
$profession = Profession::slug($data->profession->name)->first();
|
||||
if (!$profession) {
|
||||
$message = sprintf('Invalid profession: %s', $data->profession->name);
|
||||
throw new Exception($message);
|
||||
}
|
||||
|
||||
// Create/update profession for player.
|
||||
$this->ch_prof = $character->professions()->updateOrCreate(['profession_id' => $profession->id], [
|
||||
'skill' => $data->profession->level,
|
||||
]);
|
||||
|
||||
$this->recipes = $data->profession->recipes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
DB::transaction(function () {
|
||||
|
||||
$this->processItems();
|
||||
|
||||
$profession = $this->ch_prof->profession;
|
||||
|
||||
$recipes = [];
|
||||
|
||||
// Create recipes for character
|
||||
foreach($this->recipes as $data) {
|
||||
|
||||
$item = Item::where('name', $data->name)->firstOrFail();
|
||||
|
||||
$recipes[] = $this->getRecipe($item, $profession, $data)->id;
|
||||
}
|
||||
|
||||
// Update attached recipes.
|
||||
$this->ch_prof->recipes()->sync($recipes);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
protected function processItems()
|
||||
{
|
||||
$items = collect($this->recipes);
|
||||
$nested = $items->pluck('items')->flatten()->unique('name');
|
||||
$items = $items->except('items')
|
||||
->merge($nested)
|
||||
->map(function ($item, $key) {
|
||||
return [
|
||||
'name' => $item->name,
|
||||
'slug' => Str::slug($item->name),
|
||||
'external_id' => $item->id,
|
||||
'texture' => isset($item->texture) ? $item->texture : null,
|
||||
'color' => isset($item->color) ? (string) Str::of($item->color)->replace('#', '')->limit(8) : null,
|
||||
];
|
||||
})
|
||||
->sort('name')
|
||||
->toArray();
|
||||
|
||||
Item::upsert($items, [ 'external_id', 'name', 'slug' ], [ 'external_id', 'texture', 'color' ]);
|
||||
}
|
||||
|
||||
protected function getRecipe(Item $item, Profession $profession, $data)
|
||||
{
|
||||
$recipe = $profession->recipes()
|
||||
->where('item_id', $item->id)
|
||||
->first();
|
||||
|
||||
// Create if not found.
|
||||
if ($recipe === null) {
|
||||
|
||||
$category = RecipeCategory::firstOrCreate([ 'name' => $data->categorie ]);
|
||||
|
||||
$recipe = $profession->recipes()->create([
|
||||
'item_id' => $item->id,
|
||||
'category_id' => $category->id
|
||||
]);
|
||||
|
||||
// Reagents
|
||||
foreach($data->items as $reagent) {
|
||||
$item = Item::where('name', $reagent->name)->firstOrFail();
|
||||
$recipe->reagents()->attach($item, [ 'quantity' => $reagent->num ]);
|
||||
}
|
||||
|
||||
$recipe->push();
|
||||
}
|
||||
|
||||
return $recipe;
|
||||
}
|
||||
}
|
||||
Reference in a new issue