diff --git a/app/Models/ChampionRoles.php b/app/Models/ChampionRoles.php index 7e8f8ec..a004338 100644 --- a/app/Models/ChampionRoles.php +++ b/app/Models/ChampionRoles.php @@ -21,6 +21,8 @@ class ChampionRoles extends Model public function getRolesAttribute($value) { + $value = is_array($value) ? $value : [$value]; + $mappedRoles = []; foreach ($value as $role) { switch ($role) { diff --git a/database/seeders/ChampionRolesSeeder.php b/database/seeders/ChampionRolesSeeder.php index 087e157..0653234 100644 --- a/database/seeders/ChampionRolesSeeder.php +++ b/database/seeders/ChampionRolesSeeder.php @@ -3,6 +3,10 @@ namespace Database\Seeders; use Illuminate\Database\Seeder; +use App\Models\ChampionRoles; +use App\Models\Champion; +use Illuminate\Support\Facades\Log; + class ChampionRolesSeeder extends Seeder { @@ -11,8 +15,47 @@ class ChampionRolesSeeder extends Seeder */ public function run(): void { - // TODO: http://cdn.merakianalytics.com/riot/lol/resources/latest/en-US/championrates.json $roleDataUrl = 'http://cdn.merakianalytics.com/riot/lol/resources/latest/en-US/championrates.json'; $roleData = json_decode(file_get_contents($roleDataUrl), true); + + foreach ($roleData['data'] as $championId => $roles) { + $rolesExists = ChampionRoles::where('champion_id', $championId)->first(); + $championExists = Champion::where('champion_id', $championId)->first(); + + if ($championExists) $championName = $championExists->name; + + $playedRoles = []; + + foreach ($roles as $role => $data) { + if ($data['playRate'] > 0) { + $playedRoles[] = $role; + } + } + + $rolesAttributes = [ + 'champion_id' => $championId, + 'champion_name' => $championName, + 'roles' => $playedRoles, + ]; + + if ($rolesExists && $this->hasAttributesChanged($rolesExists, $rolesAttributes)) { + Log::info('Roles for ' . $championName . ' have changed, updating...'); + $rolesExists->update($rolesAttributes); + } elseif (!$rolesExists) { + Log::info('New roles detected for ' . $championName . '! Creating...'); + ChampionRoles::create($rolesAttributes); + } + } + } + + private function hasAttributesChanged($roleData, $attributes) + { + foreach ($attributes as $key => $value) { + if ($roleData->{$key} != $value) { + return true; + } + } + + return false; } } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 148952d..b0d3d9a 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -16,6 +16,7 @@ class DatabaseSeeder extends Seeder $this->call(ChampionSeeder::class); $this->call(ChampionSkinSeeder::class); $this->call(SkinChromaSeeder::class); + $this->call(ChampionRolesSeeder::class); Log::info('Seeding complete at ' . date('Y-m-d H:i:s')); }