Archived
1
0
Fork 0

app/Jobs/ImportProfession.php: Add support for recipe spell.

This commit is contained in:
Henrik Hautakoski 2021-07-08 07:43:44 +02:00
parent cc3e0584b5
commit 21ff933939

View file

@ -8,6 +8,7 @@ use App\Models\CharacterProfession;
use App\Models\Recipe;
use App\Models\RecipeCategory;
use App\Models\Item;
use App\Models\Spell;
use App\ProfessionImport\Exception;
use Illuminate\Bus\Queueable;
@ -71,6 +72,7 @@ class ImportProfession implements ShouldQueue
DB::transaction(function () {
$this->processItems();
$this->processSpells();
$profession = $this->ch_prof->profession;
@ -79,20 +81,53 @@ class ImportProfession implements ShouldQueue
// Create recipes for character
foreach($this->recipes as $data) {
$item = Item::where('name', $data->name)->first();
if (!$item) {
throw new Exception("Could not find item '{$data->name}'");
// Find spell
if (isset($data->spellId)) {
$spell = Spell::find($data->spellId);
if (!$spell) {
throw new Exception("Could not find spell '{$data->spellId}' - '{$data->name}'");
}
} else {
$spell = Spell::where('name', $data->name)->first();
if (!$spell) {
throw new Exception("Could not find spell '{$data->name}'");
}
}
$recipes[] = $this->getRecipe($item, $profession, $data)->id;
// Find crafted item (if any)
$crafted = Item::where('name', $data->name)->first();
$recipes[] = $this->getRecipe($spell, $crafted, $profession, $data)->id;
}
// Update attached recipes.
$this->ch_prof->recipes()->sync($recipes);
});
}
protected function processSpells()
{
$spells = collect($this->recipes);
$spells = $spells->except('items')
->map(function ($item, $key) {
if (!isset($item->spellId)) {
return [];
}
return [
'id' => $item->spellId,
'name' => $item->name,
'slug' => Str::slug($item->name),
];
})
->filter()
->sortBy('name')
->toArray();
Spell::insertOrIgnore($spells, [ 'id', 'name', 'slug']);
}
protected function processItems()
{
$items = collect($this->recipes);
@ -103,7 +138,9 @@ class ImportProfession implements ShouldQueue
// 2021-07-01: Profession Exporter v1.1.0 is abit buggy and dont return
// names for reagents. so we skip those.
if (!isset($item->name)) {
// 2021-07-08: Also skip items without id.
if (!isset($item->name) || !isset($items->id)) {
return [];
}
@ -122,10 +159,10 @@ class ImportProfession implements ShouldQueue
Item::upsert($items, [ 'name', 'slug' ], [ 'external_id', 'texture', 'color' ]);
}
protected function getRecipe(Item $item, Profession $profession, $data)
protected function getRecipe(Spell $spell, ?Item $crafted, Profession $profession, $data)
{
$recipe = $profession->recipes()
->where('item_id', $item->id)
->where('spell_id', $spell->id)
->first();
// Create if not found.
@ -134,10 +171,15 @@ class ImportProfession implements ShouldQueue
$category = RecipeCategory::firstOrCreate([ 'name' => $data->categorie ]);
$recipe = $profession->recipes()->create([
'item_id' => $item->id,
'item_id' => $crafted ? $crafted->id : null,
'spell_id' => $spell->id,
'category_id' => $category->id
]);
}
// Update with crafted item :)
else if ($crafted) {
$recipe->craft()->associate($crafted);
}
// Insert/Update Reagents
$reagents = [];
@ -162,6 +204,7 @@ class ImportProfession implements ShouldQueue
}
$recipe->reagents()->sync($reagents);
$recipe->push();
return $recipe;
}