From fc2f3af040f872bc42a856cf07c986b76151d715 Mon Sep 17 00:00:00 2001 From: Rico van Zelst Date: Wed, 1 Nov 2023 09:40:25 +0100 Subject: [PATCH] feat: skin chroma data seeder --- ...1_01_080800_skin_chromas_add_chroma_id.php | 28 ++++++++++ database/seeders/DatabaseSeeder.php | 1 + database/seeders/SkinChromaSeeder.php | 51 ++++++++++++++++++- 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2023_11_01_080800_skin_chromas_add_chroma_id.php diff --git a/database/migrations/2023_11_01_080800_skin_chromas_add_chroma_id.php b/database/migrations/2023_11_01_080800_skin_chromas_add_chroma_id.php new file mode 100644 index 0000000..7954053 --- /dev/null +++ b/database/migrations/2023_11_01_080800_skin_chromas_add_chroma_id.php @@ -0,0 +1,28 @@ +string('chroma_id')->after('id')->unique(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('skin_chromas', function (Blueprint $table) { + $table->dropColumn('chroma_id'); + }); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index e9bb29d..148952d 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -15,6 +15,7 @@ class DatabaseSeeder extends Seeder { $this->call(ChampionSeeder::class); $this->call(ChampionSkinSeeder::class); + $this->call(SkinChromaSeeder::class); Log::info('Seeding complete at ' . date('Y-m-d H:i:s')); } diff --git a/database/seeders/SkinChromaSeeder.php b/database/seeders/SkinChromaSeeder.php index 5e9112c..6842c5d 100644 --- a/database/seeders/SkinChromaSeeder.php +++ b/database/seeders/SkinChromaSeeder.php @@ -4,6 +4,8 @@ namespace Database\Seeders; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; +use App\Models\SkinChroma; +use Illuminate\Support\Facades\Log; class SkinChromaSeeder extends Seeder { @@ -12,6 +14,53 @@ class SkinChromaSeeder extends Seeder */ public function run(): void { - // + $championDataUrl = "http://cdn.merakianalytics.com/riot/lol/resources/latest/en-US/champions.json"; + $championData = json_decode(file_get_contents($championDataUrl), true); + + foreach ($championData as $champion) { + foreach ($champion['skins'] as $skin) { + if ($skin['name'] === "Original") { + continue; + } + foreach ($skin['chromas'] as $chroma) { + if ($chroma === null) continue; // To address: https://github.com/meraki-analytics/lolstaticdata/issues/71 + + $chromaId = $chroma['id']; + + $chromaExists = SkinChroma::where('chroma_id', $chromaId)->first(); + $chromaAttributes = [ + 'chroma_id' => $chromaId, + 'full_skin_id' => $skin['id'], + 'skin_name' => $skin['name'] . ' ' . $champion['name'], + 'chroma_name' => $chroma['name'], + 'chroma_colors' => $chroma['colors'], + 'chroma_image' => $chroma['chromaPath'], + ]; + + // Mundo is a special case, his skins often include his name in the skin name, so we need to remove it. + if (strpos($chromaAttributes['skin_name'], 'Mundo Dr. Mundo') !== false) { + $skinAttributes['skin_name'] = str_replace('Mundo Dr. Mundo', 'Mundo', $chromaAttributes['skin_name']); + } + + if ($chromaExists && $this->hasAttributesChanged($chromaExists, $chromaAttributes)) { + Log::info('Updating chroma: ' . $chromaId); + $chromaExists->update($chromaAttributes); + } else if (!$chromaExists) { + Log::info('Creating chroma: ' . $chromaId); + SkinChroma::create($chromaAttributes); + } + } + } + } + } + + private function hasAttributesChanged($chroma, $attributes) + { + foreach ($attributes as $key => $value) { + if ($chroma->{$key} != $value) { + return true; + } + } + return false; } }