Archived
1
0
Fork 0

Initial Commit

This commit is contained in:
Henrik Hautakoski 2021-10-18 11:53:33 +02:00
commit ddf09fe00c
113 changed files with 187148 additions and 0 deletions

View file

@ -0,0 +1,52 @@
<?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 CardExport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'card:export';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Export card data';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$data = collect();
foreach(Card::with(['character', 'raid'])->get() as $card) {
$row = collect([
'message' => $card->body,
'character' => $card->character ? $card->character->name : null,
'raid' => $card->raid ? $card->raid->name : null,
'class' => $card->class,
'role' => $card->role
])->filter()->toArray();
$data->push($row);
}
$this->line($data->toJson(JSON_PRETTY_PRINT));
}
}

View file

@ -0,0 +1,74 @@
<?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);
}
}
}

44
app/Console/Kernel.php Normal file
View file

@ -0,0 +1,44 @@
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\CardImport::class,
Commands\CardExport::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('model:prune', [ '--model' => [ \App\Models\Setting::class ] ])
->description("Prune expired settings")
->everyTwoHours();
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}