74 lines
1.6 KiB
PHP
74 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use App\Models\Character;
|
|
use App\Models\Raid;
|
|
use App\Models\Card;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
class CardImport extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'card:import {file}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Import card data';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$file = $this->argument('file');
|
|
|
|
$data = file_get_contents($file);
|
|
|
|
try {
|
|
$json = json_decode($data, false, 8, JSON_THROW_ON_ERROR);
|
|
} catch(\JsonException $ex) {
|
|
$this->error("Json: " . $ex->getMessage());
|
|
return;
|
|
}
|
|
|
|
foreach($json as $item) {
|
|
|
|
$data = [
|
|
'body' => $item->message,
|
|
];
|
|
|
|
if (isset($item->character)) {
|
|
$character = Character::firstOrCreate(['name' => $item->character]);
|
|
$data['character_id'] = $character->id;
|
|
}
|
|
|
|
if (isset($item->raid)) {
|
|
$raid = Raid::firstOrCreate(['name' => $item->raid]);
|
|
$data['raid_id'] = $raid->id;
|
|
}
|
|
|
|
if (isset($item->class)) {
|
|
$data['class'] = Str::lower($item->class);
|
|
}
|
|
|
|
if (isset($item->role)) {
|
|
$data['role'] = $item->role;
|
|
}
|
|
|
|
Card::insert($data);
|
|
}
|
|
}
|
|
}
|